-
Notifications
You must be signed in to change notification settings - Fork 388
/
selector_test.go
295 lines (274 loc) · 5.96 KB
/
selector_test.go
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2018 ko Build Authors All Rights Reserved.
//
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package resolve
import (
"testing"
"github.com/google/go-cmp/cmp"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/labels"
)
var (
webSelector = selector(`app=web`)
notWebSelector = selector(`app!=web`)
nopSelector = selector(`foo!=bark`)
hasSelector = selector(`app`)
notHasSelector = selector(`!app`)
)
const (
webPod = `apiVersion: v1
kind: Pod
metadata:
labels:
# comments should be preserved
app: web
name: rss-site
`
dbPod = `apiVersion: v1
kind: Pod
metadata:
labels:
# comments should be preserved
app: db
name: rss-db
`
podNoLabel = `apiVersion: v1
kind: Pod
metadata:
name: rss-site
`
podList = `apiVersion: v1
kind: List
metadata:
resourceVersion: ""
selfLink: ""
items:
- apiVersion: v1
kind: Pod
metadata:
labels:
app: web
name: rss-site
- apiVersion: v1
kind: Pod
metadata:
labels:
app: db
name: rss-db
`
webPodList = `apiVersion: v1
kind: List
metadata:
resourceVersion: ""
selfLink: ""
items:
- apiVersion: v1
kind: Pod
metadata:
labels:
app: web
name: rss-site
`
dbPodList = `apiVersion: v1
kind: List
metadata:
resourceVersion: ""
selfLink: ""
items:
- apiVersion: v1
kind: Pod
metadata:
labels:
app: db
name: rss-db
`
podListNoLabel = `apiVersion: v1
kind: List
metadata:
resourceVersion: ""
selfLink: ""
items:
- apiVersion: v1
kind: Pod
metadata:
name: rss-site
- apiVersion: v1
kind: Pod
metadata:
name: rss-db
`
)
func TestMatchesSelector(t *testing.T) {
tests := []struct {
desc string
input string
selector labels.Selector
output string
matches bool
}{{
desc: "single object with matching selector",
input: webPod,
selector: webSelector,
output: webPod,
matches: true,
}, {
desc: "single object with non-matching selector",
input: webPod,
selector: notWebSelector,
matches: false,
}, {
desc: "single object with noop selector",
input: dbPod,
selector: nopSelector,
output: dbPod,
matches: true,
}, {
desc: "single object with has selector",
input: webPod,
selector: hasSelector,
output: webPod,
matches: true,
}, {
desc: "single object with not-has selector",
input: webPod,
selector: notHasSelector,
matches: false,
}, {
desc: "single non-labeled object with has selector",
input: podNoLabel,
selector: hasSelector,
matches: false,
}, {
desc: "single non-labeled object with not-has selector",
input: podNoLabel,
selector: notHasSelector,
output: podNoLabel,
matches: true,
}, {
desc: "selector matching elements of list object",
input: podList,
selector: webSelector,
output: webPodList,
matches: true,
}, {
desc: "selector matching other elements of list object",
input: podList,
selector: notWebSelector,
output: dbPodList,
matches: true,
}, {
desc: "has selector matching no non-labeled element of list object",
input: podListNoLabel,
selector: hasSelector,
matches: false,
}, {
desc: "not-has selector matching all non-labeled elements of list object",
input: podListNoLabel,
selector: notHasSelector,
output: podListNoLabel,
matches: true,
}, {
desc: "selector matching all elements of list object",
input: podList,
selector: labels.Everything(),
output: podList,
matches: true,
}, {
desc: "selector matching no element of list object",
input: podList,
selector: labels.Nothing(),
matches: false,
}}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
doc := strToYAML(t, test.input)
matches, err := MatchesSelector(doc, test.selector)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if matches != test.matches {
t.Errorf("unexpected result: got %v - want %v", matches, test.matches)
}
// assert doc is mutated correctly
if test.output != "" {
// Normalize whitespace formatting
output := normalizeYAML(t, test.output)
if diff := cmp.Diff(output, yamlToStr(t, doc)); diff != "" {
t.Errorf("unexpected diff (-want, +got) %v", diff)
}
}
})
}
}
func TestSelectorFailure(t *testing.T) {
tests := []struct {
desc string
input string
}{
{
desc: "not an object",
input: "image: some.go/package",
},
{
desc: "not an object in a list",
input: `apiVersion: v1
kind: List
metadata:
resourceVersion: ""
selfLink: ""
items:
- blah: ha
`,
},
{
desc: "not a valid list",
input: `apiVersion: v1
kind: List
`,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
_, err := MatchesSelector(strToYAML(t, test.input), labels.Everything())
if err == nil {
t.Error("expected error")
}
})
}
}
func selector(s string) labels.Selector {
selector, err := labels.Parse(s)
if err != nil {
panic("unable to parse selector " + s)
}
return selector
}
func normalizeYAML(t *testing.T, yuml string) string {
t.Helper()
return yamlToStr(t, strToYAML(t, yuml))
}
func yamlToStr(t *testing.T, node *yaml.Node) string {
result, err := yaml.Marshal(node)
if err != nil {
t.Fatalf("error marshalling yaml: %v", err)
}
return string(result)
}
func strToYAML(t *testing.T, yuml string) *yaml.Node {
t.Helper()
var node yaml.Node
if err := yaml.Unmarshal([]byte(yuml), &node); err != nil {
t.Fatalf("error unmarshalling yaml: %v\n%v", err, yuml)
}
return &node
}