Skip to content

Commit

Permalink
chore: add Kubernetes testing support
Browse files Browse the repository at this point in the history
- Add a step to get k8s info in the testing workflow
- Set environment variables for K8S_SERVER, K8S_CA_CERT, and K8S_TOKEN in the testing job
- Add a new test function for KubeConfig in plugin_test.go
- Create a new instance of the Plugin struct in the KubeConfig test function

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Jan 28, 2024
1 parent 0eda9db commit 88cdc2f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,25 @@ jobs:
run: |
kubectl version -o yaml --client
- name: get k8s info
id: k8s_info
run: |
kubectl create namespace test-namespace
kubectl apply -f example/serviceAccount.yaml
echo K8S_SERVER=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.server}') >> $GITHUB_OUTPUT
echo K8S_CA_CERT=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}') >> $GITHUB_OUTPUT
echo K8S_TOKEN=$(kubectl get secret deploy -n test-namespace -o jsonpath='{.data.token}' | base64 -d) >> $GITHUB_OUTPUT
- name: Setup go
uses: actions/setup-go@v5
with:
go-version: "^1.21"

- name: testing
env:
K8S_SERVER: ${{ steps.k8s_info.outputs.K8S_SERVER }}
K8S_CA_CERT: ${{ steps.k8s_info.outputs.K8S_CA_CERT }}
K8S_TOKEN: ${{ steps.k8s_info.outputs.K8S_TOKEN }}
run: |
make test
Expand Down
21 changes: 21 additions & 0 deletions plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"os"
"testing"

"github.com/appleboy/deploy-k8s/config"
Expand Down Expand Up @@ -33,3 +34,23 @@ func TestCheckConfig(t *testing.T) {
t.Errorf("Expected error: token is required")
}
}

func TestKubeConfig(t *testing.T) {
// Create a new instance of the Plugin struct
p := &Plugin{
Config: &config.K8S{
Server: os.Getenv("KUBE_SERVER"),
CaCert: os.Getenv("KUBE_CA_CERT"),
Output: "kubeconfig.yaml",
Debug: true,
},
AuthInfo: &config.AuthInfo{
Token: os.Getenv("KUBE_TOKEN"),
},
}

err := p.Exec()
if err != nil {
t.Errorf("Expected error: cannot create kube config: %s", err.Error())
}
}

0 comments on commit 88cdc2f

Please sign in to comment.