@@ -17,6 +17,69 @@ import (
17
17
"sigs.k8s.io/hierarchical-namespaces/internal/objects"
18
18
)
19
19
20
+ func TestManagedMeta(t *testing.T) {
21
+ f := foresttest.Create("-") // a
22
+ h := &Validator{Forest: f}
23
+ l := zap.New()
24
+ // For this test we accept any label or annotation not starting with 'h',
25
+ // to allow almost any meta - except the hnc.x-k8s.io labels/annotations,
26
+ // which cannot be managed anyway. And allows us to use that for testing.
27
+ if err := config.SetManagedMeta([]string{"[^h].*"}, []string{"[^h].*"}); err != nil {
28
+ t.Fatal(err)
29
+ }
30
+ defer config.SetManagedMeta(nil, nil)
31
+
32
+ tests := []struct {
33
+ name string
34
+ labels []api.MetaKVP
35
+ annotations []api.MetaKVP
36
+ allowed bool
37
+ }{
38
+ {name: "ok: managed label", labels: []api.MetaKVP{{Key: "label.com/team"}}, allowed: true},
39
+ {name: "invalid: unmanaged label", labels: []api.MetaKVP{{Key: api.LabelIncludedNamespace}}},
40
+ {name: "ok: managed annotation", annotations: []api.MetaKVP{{Key: "annot.com/log-index"}}, allowed: true},
41
+ {name: "invalid: unmanaged annotation", annotations: []api.MetaKVP{{Key: api.AnnotationManagedBy}}},
42
+
43
+ {name: "ok: prefixed label key", labels: []api.MetaKVP{{Key: "foo.bar/team", Value: "v"}}, allowed: true},
44
+ {name: "ok: bare label key", labels: []api.MetaKVP{{Key: "team", Value: "v"}}, allowed: true},
45
+ {name: "invalid: label prefix key", labels: []api.MetaKVP{{Key: "foo;bar/team", Value: "v"}}},
46
+ {name: "invalid: label name key", labels: []api.MetaKVP{{Key: "foo.bar/-team", Value: "v"}}},
47
+ {name: "invalid: empty label key", labels: []api.MetaKVP{{Key: "", Value: "v"}}},
48
+
49
+ {name: "ok: label value", labels: []api.MetaKVP{{Key: "k", Value: "foo"}}, allowed: true},
50
+ {name: "ok: empty label value", labels: []api.MetaKVP{{Key: "k", Value: ""}}, allowed: true},
51
+ {name: "ok: label value special char", labels: []api.MetaKVP{{Key: "k", Value: "f-oo"}}, allowed: true},
52
+ {name: "invalid: label value", labels: []api.MetaKVP{{Key: "k", Value: "-foo"}}},
53
+
54
+ {name: "ok: prefixed annotation key", annotations: []api.MetaKVP{{Key: "foo.bar/team", Value: "v"}}, allowed: true},
55
+ {name: "ok: bare annotation key", annotations: []api.MetaKVP{{Key: "team", Value: "v"}}, allowed: true},
56
+ {name: "invalid: annotation prefix key", annotations: []api.MetaKVP{{Key: "foo;bar/team", Value: "v"}}},
57
+ {name: "invalid: annotation name key", annotations: []api.MetaKVP{{Key: "foo.bar/-team", Value: "v"}}},
58
+ {name: "invalid: empty annotation key", annotations: []api.MetaKVP{{Key: "", Value: "v"}}},
59
+
60
+ {name: "ok: annotation value", annotations: []api.MetaKVP{{Key: "k", Value: "foo"}}, allowed: true},
61
+ {name: "ok: empty annotation value", annotations: []api.MetaKVP{{Key: "k", Value: ""}}, allowed: true},
62
+ {name: "ok: special annotation value", annotations: []api.MetaKVP{{Key: "k", Value: ";$+:;/*'\""}}, allowed: true},
63
+ }
64
+ for _, tc := range tests {
65
+ t.Run(tc.name, func(t *testing.T) {
66
+ // Setup
67
+ g := NewWithT(t)
68
+ hc := &api.HierarchyConfiguration{Spec: api.HierarchyConfigurationSpec{}}
69
+ hc.ObjectMeta.Name = api.Singleton
70
+ hc.ObjectMeta.Namespace = "a"
71
+ hc.Spec.Labels = tc.labels
72
+ hc.Spec.Annotations = tc.annotations
73
+ req := &request{hc: hc}
74
+
75
+ got := h.handle(context.Background(), l, req)
76
+
77
+ logResult(t, got.AdmissionResponse.Result)
78
+ g.Expect(got.AdmissionResponse.Allowed).Should(Equal(tc.allowed))
79
+ })
80
+ }
81
+ }
82
+
20
83
func TestStructure(t *testing.T) {
21
84
f := foresttest.Create("-a-") // a <- b; c
22
85
h := &Validator{Forest: f}
0 commit comments