-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathmain_test.go
More file actions
97 lines (92 loc) · 2.2 KB
/
main_test.go
File metadata and controls
97 lines (92 loc) · 2.2 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"os"
"reflect"
"testing"
"sigs.k8s.io/controller-runtime/pkg/cache"
)
func TestGetWatchNamespaces(t *testing.T) {
tests := []struct {
name string
envValue string
setEnv bool
expectError bool
expectKeys []string
}{
{
name: "unset env returns error",
setEnv: false,
expectError: true,
expectKeys: nil,
},
{
name: "empty env allows cluster scope",
envValue: "",
setEnv: true,
expectError: false,
expectKeys: nil,
},
{
name: "single namespace",
envValue: "ns1",
setEnv: true,
expectError: false,
expectKeys: []string{"ns1"},
},
{
name: "multiple namespaces comma separated",
envValue: "ns1,ns2,ns3",
setEnv: true,
expectError: false,
expectKeys: []string{"ns1", "ns2", "ns3"},
},
{
name: "trims whitespace around namespaces",
envValue: " ns1 , ns2 ",
setEnv: true,
expectError: false,
expectKeys: []string{"ns1", "ns2"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setEnv {
os.Setenv(watchNamespaceEnvVar, tt.envValue)
defer os.Unsetenv(watchNamespaceEnvVar)
} else {
os.Unsetenv(watchNamespaceEnvVar)
}
nsMap, err := getWatchNamespaces()
if tt.expectError && err == nil {
t.Fatalf("expected error, got none")
}
if !tt.expectError && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !tt.expectError {
if tt.expectKeys == nil {
// Cluster-wide scope should return nil map
if nsMap != nil {
t.Errorf("expected nil map for cluster scope, got %v", nsMap)
}
} else {
for _, key := range tt.expectKeys {
if _, ok := nsMap[key]; !ok {
t.Errorf("expected key %s in map, but not found", key)
}
}
// Check size matches
if len(nsMap) != len(tt.expectKeys) {
t.Errorf("expected %d namespaces, got %d", len(tt.expectKeys), len(nsMap))
}
// Ensure all values are of type cache.Config
for k, v := range nsMap {
if !reflect.DeepEqual(v, cache.Config{}) {
t.Errorf("expected empty cache.Config for key %s, got %#v", k, v)
}
}
}
}
})
}
}