-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmain_test.go
More file actions
177 lines (165 loc) · 4.34 KB
/
Copy pathmain_test.go
File metadata and controls
177 lines (165 loc) · 4.34 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Copyright 2023 Timofey Larkin.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
*/
package main
import (
"os"
"path/filepath"
"reflect"
"testing"
"sigs.k8s.io/controller-runtime/pkg/cache"
)
func TestDiscoverClusterDomain(t *testing.T) {
cases := []struct {
name string
contents string
want string
}{
{
name: "standard kubelet-injected resolv.conf",
contents: `search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5
`,
want: "cluster.local",
},
{
name: "cozystack",
contents: `search myns.svc.cozy.local svc.cozy.local cozy.local
nameserver 10.96.0.10
options ndots:5
`,
want: "cozy.local",
},
{
name: "multi-segment cluster domain",
contents: `search myns.svc.example.internal svc.example.internal example.internal
nameserver 10.96.0.10
`,
want: "example.internal",
},
{
name: "comments and blank lines",
contents: `# This is the cluster resolv.conf injected by kubelet.
search myns.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
`,
want: "cluster.local",
},
{
name: "host-style resolv.conf — no .svc suffix",
contents: `search example.com
nameserver 8.8.8.8
`,
want: "",
},
{
name: "no search line at all",
contents: `nameserver 8.8.8.8
`,
want: "",
},
{
name: "empty file",
contents: "",
want: "",
},
{
name: "svc-only entry (degenerate, should not match)",
contents: `search svc.
nameserver 10.96.0.10
`,
want: "",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "resolv.conf")
if err := os.WriteFile(path, []byte(tc.contents), 0o644); err != nil {
t.Fatalf("write fixture: %v", err)
}
got := discoverClusterDomain(path)
if got != tc.want {
t.Fatalf("discoverClusterDomain = %q; want %q", got, tc.want)
}
})
}
}
// TestDiscoverClusterDomain_MissingFile covers the running-outside-a-pod
// case (no /etc/resolv.conf at all, or the operator binary running on a
// developer laptop): the function returns "" so callers fall back to
// the explicit --cluster-domain flag or the default.
func TestDiscoverClusterDomain_MissingFile(t *testing.T) {
if got := discoverClusterDomain("/no/such/file/anywhere"); got != "" {
t.Fatalf("missing file: got %q; want empty", got)
}
}
func TestWatchNamespaceCacheOptions(t *testing.T) {
cases := []struct {
name string
raw string
want map[string]cache.Config
}{
{
name: "empty means all namespaces",
raw: "",
want: nil,
},
{
name: "whitespace only means all namespaces",
raw: " ",
want: nil,
},
{
name: "separators only means all namespaces",
raw: " , ,, ",
want: nil,
},
{
name: "single namespace",
raw: "tenant-a",
want: map[string]cache.Config{"tenant-a": {}},
},
{
name: "multiple namespaces",
raw: "tenant-a,tenant-b",
want: map[string]cache.Config{"tenant-a": {}, "tenant-b": {}},
},
{
name: "whitespace and empty entries are dropped",
raw: " tenant-a , ,tenant-b, ",
want: map[string]cache.Config{"tenant-a": {}, "tenant-b": {}},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := watchNamespaceCacheOptions(tc.raw)
if tc.want == nil {
// Zero value keeps the unset path identical to the old config.
if !reflect.DeepEqual(got, cache.Options{}) {
t.Fatalf("watchNamespaceCacheOptions(%q) = %+v; want zero cache.Options", tc.raw, got)
}
return
}
if !reflect.DeepEqual(got.DefaultNamespaces, tc.want) {
t.Fatalf("watchNamespaceCacheOptions(%q).DefaultNamespaces = %+v; want %+v", tc.raw, got.DefaultNamespaces, tc.want)
}
})
}
}
func TestOperatorImageError(t *testing.T) {
if err := operatorImageError(placeholderOperatorImage); err == nil {
t.Errorf("operatorImageError(%q) = nil, want error (placeholder must be rejected)", placeholderOperatorImage)
}
// A real image ref and empty (snapshots simply unavailable) are both allowed.
for _, img := range []string{"registry.example.com/etcd-operator:v1.2.3", ""} {
if err := operatorImageError(img); err != nil {
t.Errorf("operatorImageError(%q) = %v, want nil", img, err)
}
}
}