|
| 1 | +package kubernetes_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/antonmedv/expr" |
| 9 | + "github.com/h2non/gock" |
| 10 | + "github.com/linuxsuren/api-testing/pkg/runner/kubernetes" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestKubernetesValidatorFunc(t *testing.T) { |
| 15 | + os.Setenv("KUBERNETES_SERVER", "http://foo") |
| 16 | + os.Setenv("KUBERNETES_TOKEN", "token") |
| 17 | + gock.InterceptClient(kubernetes.GetClient()) |
| 18 | + defer gock.RestoreClient(http.DefaultClient) |
| 19 | + defer gock.Off() |
| 20 | + |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + prepare func() |
| 24 | + expression string |
| 25 | + expectBool bool |
| 26 | + expectErr bool |
| 27 | + }{{ |
| 28 | + name: "pod exist expr", |
| 29 | + prepare: preparePod, |
| 30 | + expression: `pod('ns', 'foo').Exist()`, |
| 31 | + expectBool: true, |
| 32 | + }, { |
| 33 | + name: "pod expectField expr", |
| 34 | + prepare: preparePod, |
| 35 | + expression: `pod('ns', 'foo').ExpectField('pod', 'kind')`, |
| 36 | + expectBool: true, |
| 37 | + }, { |
| 38 | + name: "pod expectField expr, not match", |
| 39 | + prepare: preparePod, |
| 40 | + expression: `pod('ns', 'foo').ExpectField('pods', 'kind')`, |
| 41 | + expectBool: false, |
| 42 | + }, { |
| 43 | + name: "pod expectField expr, not find field", |
| 44 | + prepare: preparePod, |
| 45 | + expression: `pod('ns', 'foo').ExpectField('pods', 'kinds')`, |
| 46 | + expectBool: false, |
| 47 | + }, { |
| 48 | + name: "no enough params", |
| 49 | + expression: `k8s('crd')`, |
| 50 | + prepare: emptyPrepare, |
| 51 | + expectBool: false, |
| 52 | + expectErr: true, |
| 53 | + }, { |
| 54 | + name: "crd", |
| 55 | + expression: `k8s({"kind":"vms","group":"bar","version":"v2"}, "ns", "foo").Exist()`, |
| 56 | + prepare: prepareCRDVM, |
| 57 | + expectBool: true, |
| 58 | + }, { |
| 59 | + name: "deploy", |
| 60 | + expression: `k8s("deployments", "ns", "foo").Exist()`, |
| 61 | + prepare: prepareDeploy, |
| 62 | + expectBool: true, |
| 63 | + }, { |
| 64 | + name: "statefulset", |
| 65 | + expression: `k8s("statefulsets", "ns", "foo").Exist()`, |
| 66 | + prepare: prepareStatefulset, |
| 67 | + expectBool: true, |
| 68 | + }, { |
| 69 | + name: "daemonset", |
| 70 | + expression: `k8s("daemonsets", "ns", "foo").Exist()`, |
| 71 | + prepare: prepareDaemonset, |
| 72 | + expectBool: true, |
| 73 | + }, { |
| 74 | + name: "no kind", |
| 75 | + expression: `k8s({"foo": "bar"}, "ns", "foo").Exist()`, |
| 76 | + prepare: emptyPrepare, |
| 77 | + expectErr: true, |
| 78 | + }} |
| 79 | + for _, tt := range tests { |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + tt.prepare() |
| 82 | + vm, err := expr.Compile(tt.expression, kubernetes.KubernetesValidatorFunc(), |
| 83 | + kubernetes.PodValidatorFunc()) |
| 84 | + assert.Nil(t, err) |
| 85 | + |
| 86 | + result, err := expr.Run(vm, expr.Env(tt)) |
| 87 | + assert.Equal(t, tt.expectErr, err != nil) |
| 88 | + if err == nil { |
| 89 | + assert.Equal(t, tt.expectBool, result) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func emptyPrepare() {} |
| 96 | + |
| 97 | +func preparePod() { |
| 98 | + gock.New("http://foo"). |
| 99 | + Get("/api/v1/namespaces/ns/pods/foo"). |
| 100 | + MatchHeader("Authorization", "Bearer token"). |
| 101 | + Reply(http.StatusOK). |
| 102 | + JSON(`{"kind":"pod"}`) |
| 103 | +} |
| 104 | + |
| 105 | +func prepareDeploy() { |
| 106 | + gock.New("http://foo"). |
| 107 | + Get("/apis/apps/v1/namespaces/ns/deployments/foo"). |
| 108 | + MatchHeader("Authorization", "Bearer token"). |
| 109 | + Reply(http.StatusOK). |
| 110 | + JSON(`{"kind":"deploy"}`) |
| 111 | +} |
| 112 | + |
| 113 | +func prepareStatefulset() { |
| 114 | + gock.New("http://foo"). |
| 115 | + Get("/apis/apps/v1/namespaces/ns/statefulsets/foo"). |
| 116 | + MatchHeader("Authorization", "Bearer token"). |
| 117 | + Reply(http.StatusOK). |
| 118 | + JSON(`{"kind":"statefulset"}`) |
| 119 | +} |
| 120 | + |
| 121 | +func prepareDaemonset() { |
| 122 | + gock.New("http://foo"). |
| 123 | + Get("/apis/apps/v1/namespaces/ns/daemonsets/foo"). |
| 124 | + MatchHeader("Authorization", "Bearer token"). |
| 125 | + Reply(http.StatusOK). |
| 126 | + JSON(`{"kind":"daemonset"}`) |
| 127 | +} |
| 128 | + |
| 129 | +func prepareCRDVM() { |
| 130 | + gock.New("http://foo"). |
| 131 | + Get("/apis/bar/v2/namespaces/ns/vms/foo"). |
| 132 | + MatchHeader("Authorization", "Bearer token"). |
| 133 | + Reply(http.StatusOK). |
| 134 | + JSON(`{"kind":"vm"}`) |
| 135 | +} |
0 commit comments