Skip to content

Commit 6b1e9c7

Browse files
committed
apis/nfd: add unit tests for match name functions
1 parent 7dd3034 commit 6b1e9c7

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

pkg/apis/nfd/nodefeaturerule/expression-api_test.go

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,202 @@ bar: { op: Lt, value: ["10"] }
223223
})
224224
}
225225
}
226+
227+
func TestMatchKeyNames(t *testing.T) {
228+
type O = []api.MatchedElement
229+
type I = map[string]nfdv1alpha1.Nil
230+
231+
type TC struct {
232+
name string
233+
me *nfdv1alpha1.MatchExpression
234+
input I
235+
result bool
236+
output O
237+
}
238+
239+
tcs := []TC{
240+
{
241+
name: "empty input",
242+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchAny},
243+
input: I{},
244+
result: false,
245+
output: O{},
246+
},
247+
{
248+
name: "MatchAny",
249+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchAny},
250+
input: I{"key1": {}, "key2": {}},
251+
result: true,
252+
output: O{{"Name": "key1"}, {"Name": "key2"}},
253+
},
254+
{
255+
name: "MatchExists",
256+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchExists},
257+
input: I{"key1": {}, "key2": {}},
258+
result: true,
259+
output: O{{"Name": "key1"}, {"Name": "key2"}},
260+
},
261+
{
262+
name: "MatchDoesNotExist",
263+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchDoesNotExist},
264+
input: I{"key1": {}, "key2": {}},
265+
result: false,
266+
output: O{},
267+
},
268+
{
269+
name: "MatchIn matches",
270+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key1"}},
271+
input: I{"key1": {}, "key2": {}},
272+
result: true,
273+
output: O{{"Name": "key1"}},
274+
},
275+
{
276+
name: "MatchIn no match",
277+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key3"}},
278+
input: I{"key1": {}, "key2": {}},
279+
result: false,
280+
output: O{},
281+
},
282+
{
283+
name: "MatchNotIn",
284+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchNotIn, Value: nfdv1alpha1.MatchValue{"key1"}},
285+
input: I{"key1": {}, "key2": {}},
286+
result: true,
287+
output: O{{"Name": "key2"}},
288+
},
289+
}
290+
291+
for _, tc := range tcs {
292+
t.Run(tc.name, func(t *testing.T) {
293+
res, ret, err := api.MatchKeyNames(tc.me, tc.input)
294+
assert.Equal(t, tc.result, res)
295+
assert.Equal(t, tc.output, ret)
296+
assert.Nil(t, err)
297+
})
298+
}
299+
}
300+
301+
func TestMatchValueNames(t *testing.T) {
302+
type O = []api.MatchedElement
303+
type I = map[string]string
304+
305+
type TC struct {
306+
name string
307+
me *nfdv1alpha1.MatchExpression
308+
input I
309+
result bool
310+
output O
311+
}
312+
313+
tcs := []TC{
314+
{
315+
name: "empty input",
316+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchAny},
317+
input: I{},
318+
result: false,
319+
output: O{},
320+
},
321+
{
322+
name: "MatchExists",
323+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchExists},
324+
input: I{"key1": "val1", "key2": "val2"},
325+
result: true,
326+
output: O{{"Name": "key1", "Value": "val1"}, {"Name": "key2", "Value": "val2"}},
327+
},
328+
{
329+
name: "MatchDoesNotExist",
330+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchDoesNotExist},
331+
input: I{"key1": "val1", "key2": "val2"},
332+
result: false,
333+
output: O{},
334+
},
335+
{
336+
name: "MatchIn matches",
337+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key1"}},
338+
input: I{"key1": "val1", "key2": "val2"},
339+
result: true,
340+
output: O{{"Name": "key1", "Value": "val1"}},
341+
},
342+
{
343+
name: "MatchIn no match",
344+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key3"}},
345+
input: I{"key1": "val1", "key2": "val2"},
346+
result: false,
347+
output: O{},
348+
},
349+
{
350+
name: "MatchNotIn",
351+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchNotIn, Value: nfdv1alpha1.MatchValue{"key1"}},
352+
input: I{"key1": "val1", "key2": "val2"},
353+
result: true,
354+
output: O{{"Name": "key2", "Value": "val2"}},
355+
},
356+
}
357+
358+
for _, tc := range tcs {
359+
t.Run(tc.name, func(t *testing.T) {
360+
res, ret, err := api.MatchValueNames(tc.me, tc.input)
361+
assert.Equal(t, tc.result, res)
362+
assert.Equal(t, tc.output, ret)
363+
assert.Nil(t, err)
364+
})
365+
}
366+
}
367+
368+
func TestMatchInstanceAttributeNames(t *testing.T) {
369+
type O = []api.MatchedElement
370+
type I = []nfdv1alpha1.InstanceFeature
371+
type A = map[string]string
372+
373+
type TC struct {
374+
name string
375+
me *nfdv1alpha1.MatchExpression
376+
input I
377+
output O
378+
}
379+
380+
tcs := []TC{
381+
{
382+
name: "empty input",
383+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchAny},
384+
input: I{},
385+
output: O{},
386+
},
387+
{
388+
name: "no match",
389+
me: &nfdv1alpha1.MatchExpression{
390+
Op: nfdv1alpha1.MatchIn,
391+
Value: nfdv1alpha1.MatchValue{"foo"},
392+
},
393+
input: I{
394+
{Attributes: A{"bar": "1"}},
395+
{Attributes: A{"baz": "2"}},
396+
},
397+
output: O{},
398+
},
399+
{
400+
name: "match",
401+
me: &nfdv1alpha1.MatchExpression{
402+
Op: nfdv1alpha1.MatchIn,
403+
Value: nfdv1alpha1.MatchValue{"foo"},
404+
},
405+
input: I{
406+
{Attributes: A{"foo": "1"}},
407+
{Attributes: A{"bar": "2"}},
408+
{Attributes: A{"foo": "3", "baz": "4"}},
409+
},
410+
output: O{
411+
{"foo": "1"},
412+
{"foo": "3", "baz": "4"},
413+
},
414+
},
415+
}
416+
417+
for _, tc := range tcs {
418+
t.Run(tc.name, func(t *testing.T) {
419+
matched, err := api.MatchInstanceAttributeNames(tc.me, tc.input)
420+
assert.Equal(t, tc.output, matched)
421+
assert.Nil(t, err)
422+
})
423+
}
424+
}

0 commit comments

Comments
 (0)