Skip to content

Commit ad9dbce

Browse files
committed
test: Verify that empty namespace label key will not return a match
1 parent 4078346 commit ad9dbce

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

pkg/controllers/namespacesync/controller.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ type Reconciler struct {
3232
TargetNamespaceFilter func(ns *corev1.Namespace) bool
3333
}
3434

35-
var NamespaceHasLabelKey = func(key string) func(ns *corev1.Namespace) bool {
36-
return func(ns *corev1.Namespace) bool {
37-
_, ok := ns.GetLabels()[key]
38-
return ok
39-
}
40-
}
41-
4235
func (r *Reconciler) SetupWithManager(
4336
ctx context.Context,
4437
mgr ctrl.Manager,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package namespacesync
2+
3+
import corev1 "k8s.io/api/core/v1"
4+
5+
var NamespaceHasLabelKey = func(key string) func(ns *corev1.Namespace) bool {
6+
return func(ns *corev1.Namespace) bool {
7+
_, ok := ns.GetLabels()[key]
8+
return ok
9+
}
10+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package namespacesync
2+
3+
import (
4+
"testing"
5+
6+
corev1 "k8s.io/api/core/v1"
7+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
)
9+
10+
func TestNamespaceHasLabelKey(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
key string
14+
ns *corev1.Namespace
15+
want bool
16+
}{
17+
{
18+
name: "match a labeled namespace",
19+
key: "testkey",
20+
ns: &corev1.Namespace{
21+
ObjectMeta: v1.ObjectMeta{
22+
Name: "test",
23+
Labels: map[string]string{
24+
"testkey": "",
25+
},
26+
},
27+
},
28+
want: true,
29+
},
30+
{
31+
name: "do not match if label key is not found",
32+
key: "testkey",
33+
ns: &corev1.Namespace{
34+
ObjectMeta: v1.ObjectMeta{
35+
Name: "test",
36+
},
37+
},
38+
want: false,
39+
},
40+
{
41+
name: "do not match if label key is empty string",
42+
key: "",
43+
ns: &corev1.Namespace{
44+
ObjectMeta: v1.ObjectMeta{
45+
Name: "test",
46+
},
47+
},
48+
want: false,
49+
},
50+
}
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
fn := NamespaceHasLabelKey(tt.key)
54+
if got := fn(tt.ns); got != tt.want {
55+
t.Fatalf("got %t, want %t", got, tt.want)
56+
}
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)