@@ -17,6 +17,71 @@ 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
+ panic (err )
29
+ }
30
+ defer func () {
31
+ _ = config .SetManagedMeta (nil , nil )
32
+ }()
33
+
34
+ tests := []struct {
35
+ name string
36
+ labels []api.MetaKVP
37
+ annotations []api.MetaKVP
38
+ allowed bool
39
+ }{
40
+ {name : "managed label" , labels : []api.MetaKVP {{Key : "label.com/team" }}, allowed : true },
41
+ {name : "unmanaged label" , labels : []api.MetaKVP {{Key : api .LabelIncludedNamespace }}},
42
+ {name : "managed annotation" , annotations : []api.MetaKVP {{Key : "annot.com/log-index" }}, allowed : true },
43
+ {name : "unmanaged annotation" , annotations : []api.MetaKVP {{Key : api .AnnotationManagedBy }}},
44
+
45
+ {name : "ok: prefixed label key" , labels : []api.MetaKVP {{Key : "foo.bar/team" , Value : "v" }}, allowed : true },
46
+ {name : "ok: bare label key" , labels : []api.MetaKVP {{Key : "team" , Value : "v" }}, allowed : true },
47
+ {name : "invalid: label prefix key" , labels : []api.MetaKVP {{Key : "foo;bar/team" , Value : "v" }}},
48
+ {name : "invalid: label name key" , labels : []api.MetaKVP {{Key : "foo.bar/-team" , Value : "v" }}},
49
+ {name : "invalid: empty label key" , labels : []api.MetaKVP {{Key : "" , Value : "v" }}},
50
+
51
+ {name : "ok: label value" , labels : []api.MetaKVP {{Key : "k" , Value : "foo" }}, allowed : true },
52
+ {name : "ok: empty label value" , labels : []api.MetaKVP {{Key : "k" , Value : "" }}, allowed : true },
53
+ {name : "ok: label value special char" , labels : []api.MetaKVP {{Key : "k" , Value : "f-oo" }}, allowed : true },
54
+ {name : "invalid: label value" , labels : []api.MetaKVP {{Key : "k" , Value : "-foo" }}},
55
+
56
+ {name : "ok: prefixed annotation key" , annotations : []api.MetaKVP {{Key : "foo.bar/team" , Value : "v" }}, allowed : true },
57
+ {name : "ok: bare annotation key" , annotations : []api.MetaKVP {{Key : "team" , Value : "v" }}, allowed : true },
58
+ {name : "invalid: annotation prefix key" , annotations : []api.MetaKVP {{Key : "foo;bar/team" , Value : "v" }}},
59
+ {name : "invalid: annotation name key" , annotations : []api.MetaKVP {{Key : "foo.bar/-team" , Value : "v" }}},
60
+ {name : "invalid: empty annotation key" , annotations : []api.MetaKVP {{Key : "" , Value : "v" }}},
61
+
62
+ {name : "ok: annotation value" , annotations : []api.MetaKVP {{Key : "k" , Value : "foo" }}, allowed : true },
63
+ {name : "ok: empty annotation value" , annotations : []api.MetaKVP {{Key : "k" , Value : "" }}, allowed : true },
64
+ {name : "ok: special annotation value" , annotations : []api.MetaKVP {{Key : "k" , Value : ";$+:;/*'\" " }}, allowed : true },
65
+ }
66
+ for _ , tc := range tests {
67
+ t .Run (tc .name , func (t * testing.T ) {
68
+ // Setup
69
+ g := NewWithT (t )
70
+ hc := & api.HierarchyConfiguration {Spec : api.HierarchyConfigurationSpec {}}
71
+ hc .ObjectMeta .Name = api .Singleton
72
+ hc .ObjectMeta .Namespace = "a"
73
+ hc .Spec .Labels = tc .labels
74
+ hc .Spec .Annotations = tc .annotations
75
+ req := & request {hc : hc }
76
+
77
+ got := h .handle (context .Background (), l , req )
78
+
79
+ logResult (t , got .AdmissionResponse .Result )
80
+ g .Expect (got .AdmissionResponse .Allowed ).Should (Equal (tc .allowed ))
81
+ })
82
+ }
83
+ }
84
+
20
85
func TestStructure (t * testing.T ) {
21
86
f := foresttest .Create ("-a-" ) // a <- b; c
22
87
h := & Validator {Forest : f }
0 commit comments