This repository was archived by the owner on Apr 17, 2025. It is now read-only.
generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathmutator_test.go
49 lines (41 loc) · 1.58 KB
/
mutator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package namespaces
import (
"testing"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
api "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2"
"sigs.k8s.io/hierarchical-namespaces/internal/config"
)
func TestMutateNamespaceIncludedLabel(t *testing.T) {
m := &Mutator{}
l := zap.New()
config.SetNamespaces("", "excluded")
tests := []struct {
name string
nsn string
lval string
expectlval string
}{
{name: "Set label on non-excluded namespace without label", nsn: "a", expectlval: "true"},
{name: "No operation on non-excluded namespace with label (e.g. from a yaml file)", nsn: "a", lval: "true", expectlval: "true"},
{name: "No operation on non-excluded namespace with label with wrong value(e.g. from a yaml file)", nsn: "a", lval: "wrong", expectlval: "wrong"},
{name: "No operation on excluded namespace without label", nsn: "excluded"},
{name: "No operation on excluded namespace with label (e.g. from a yaml file)", nsn: "excluded", lval: "true", expectlval: "true"},
{name: "No operation on excluded namespace with label with wrong value (e.g. from a yaml file)", nsn: "excluded", lval: "wrong", expectlval: "wrong"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
nsInst := &corev1.Namespace{}
nsInst.Name = tc.nsn
if tc.lval != "" {
nsInst.SetLabels(map[string]string{api.LabelIncludedNamespace: tc.lval})
}
// Test
m.handle(l, nsInst)
// Report
g.Expect(nsInst.Labels[api.LabelIncludedNamespace]).Should(Equal(tc.expectlval))
})
}
}