Skip to content

Commit bad7d1f

Browse files
committed
apis/nfd: increase unit test coverage
Cover error cases of the "match name" functions.
1 parent 18980d8 commit bad7d1f

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ func TestMatchKeyNames(t *testing.T) {
269269
input I
270270
result bool
271271
output O
272+
err ValueAssertionFunc
272273
}
273274

274275
tcs := []TC{
@@ -278,48 +279,63 @@ func TestMatchKeyNames(t *testing.T) {
278279
input: I{},
279280
result: false,
280281
output: O{},
282+
err: assert.Nil,
281283
},
282284
{
283285
name: "MatchAny",
284286
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchAny},
285287
input: I{"key1": {}, "key2": {}},
286288
result: true,
287289
output: O{{"Name": "key1"}, {"Name": "key2"}},
290+
err: assert.Nil,
288291
},
289292
{
290293
name: "MatchExists",
291294
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchExists},
292295
input: I{"key1": {}, "key2": {}},
293296
result: true,
294297
output: O{{"Name": "key1"}, {"Name": "key2"}},
298+
err: assert.Nil,
295299
},
296300
{
297301
name: "MatchDoesNotExist",
298302
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchDoesNotExist},
299303
input: I{"key1": {}, "key2": {}},
300304
result: false,
301305
output: O{},
306+
err: assert.Nil,
302307
},
303308
{
304309
name: "MatchIn matches",
305310
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key1"}},
306311
input: I{"key1": {}, "key2": {}},
307312
result: true,
308313
output: O{{"Name": "key1"}},
314+
err: assert.Nil,
309315
},
310316
{
311317
name: "MatchIn no match",
312318
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key3"}},
313319
input: I{"key1": {}, "key2": {}},
314320
result: false,
315321
output: O{},
322+
err: assert.Nil,
316323
},
317324
{
318325
name: "MatchNotIn",
319326
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchNotIn, Value: nfdv1alpha1.MatchValue{"key1"}},
320327
input: I{"key1": {}, "key2": {}},
321328
result: true,
322329
output: O{{"Name": "key2"}},
330+
err: assert.Nil,
331+
},
332+
{
333+
name: "error",
334+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchExists, Value: nfdv1alpha1.MatchValue{"key1"}},
335+
input: I{"key1": {}, "key2": {}},
336+
result: false,
337+
output: nil,
338+
err: assert.NotNil,
323339
},
324340
}
325341

@@ -328,7 +344,7 @@ func TestMatchKeyNames(t *testing.T) {
328344
res, ret, err := api.MatchKeyNames(tc.me, tc.input)
329345
assert.Equal(t, tc.result, res)
330346
assert.Equal(t, tc.output, ret)
331-
assert.Nil(t, err)
347+
tc.err(t, err)
332348
})
333349
}
334350
}
@@ -343,6 +359,7 @@ func TestMatchValueNames(t *testing.T) {
343359
input I
344360
result bool
345361
output O
362+
err ValueAssertionFunc
346363
}
347364

348365
tcs := []TC{
@@ -352,41 +369,55 @@ func TestMatchValueNames(t *testing.T) {
352369
input: I{},
353370
result: false,
354371
output: O{},
372+
err: assert.Nil,
355373
},
356374
{
357375
name: "MatchExists",
358376
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchExists},
359377
input: I{"key1": "val1", "key2": "val2"},
360378
result: true,
361379
output: O{{"Name": "key1", "Value": "val1"}, {"Name": "key2", "Value": "val2"}},
380+
err: assert.Nil,
362381
},
363382
{
364383
name: "MatchDoesNotExist",
365384
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchDoesNotExist},
366385
input: I{"key1": "val1", "key2": "val2"},
367386
result: false,
368387
output: O{},
388+
err: assert.Nil,
369389
},
370390
{
371391
name: "MatchIn matches",
372392
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key1"}},
373393
input: I{"key1": "val1", "key2": "val2"},
374394
result: true,
375395
output: O{{"Name": "key1", "Value": "val1"}},
396+
err: assert.Nil,
376397
},
377398
{
378399
name: "MatchIn no match",
379400
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key3"}},
380401
input: I{"key1": "val1", "key2": "val2"},
381402
result: false,
382403
output: O{},
404+
err: assert.Nil,
383405
},
384406
{
385407
name: "MatchNotIn",
386408
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchNotIn, Value: nfdv1alpha1.MatchValue{"key1"}},
387409
input: I{"key1": "val1", "key2": "val2"},
388410
result: true,
389411
output: O{{"Name": "key2", "Value": "val2"}},
412+
err: assert.Nil,
413+
},
414+
{
415+
name: "error",
416+
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchNotIn},
417+
input: I{"key1": "val1", "key2": "val2"},
418+
result: false,
419+
output: nil,
420+
err: assert.NotNil,
390421
},
391422
}
392423

@@ -395,7 +426,7 @@ func TestMatchValueNames(t *testing.T) {
395426
res, ret, err := api.MatchValueNames(tc.me, tc.input)
396427
assert.Equal(t, tc.result, res)
397428
assert.Equal(t, tc.output, ret)
398-
assert.Nil(t, err)
429+
tc.err(t, err)
399430
})
400431
}
401432
}
@@ -410,6 +441,7 @@ func TestMatchInstanceAttributeNames(t *testing.T) {
410441
me *nfdv1alpha1.MatchExpression
411442
input I
412443
output O
444+
err ValueAssertionFunc
413445
}
414446

415447
tcs := []TC{
@@ -418,6 +450,7 @@ func TestMatchInstanceAttributeNames(t *testing.T) {
418450
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchAny},
419451
input: I{},
420452
output: O{},
453+
err: assert.Nil,
421454
},
422455
{
423456
name: "no match",
@@ -430,6 +463,7 @@ func TestMatchInstanceAttributeNames(t *testing.T) {
430463
{Attributes: A{"baz": "2"}},
431464
},
432465
output: O{},
466+
err: assert.Nil,
433467
},
434468
{
435469
name: "match",
@@ -446,14 +480,26 @@ func TestMatchInstanceAttributeNames(t *testing.T) {
446480
{"foo": "1"},
447481
{"foo": "3", "baz": "4"},
448482
},
483+
err: assert.Nil,
484+
},
485+
{
486+
name: "error",
487+
me: &nfdv1alpha1.MatchExpression{
488+
Op: nfdv1alpha1.MatchIn,
489+
},
490+
input: I{
491+
{Attributes: A{"foo": "1"}},
492+
},
493+
output: nil,
494+
err: assert.NotNil,
449495
},
450496
}
451497

452498
for _, tc := range tcs {
453499
t.Run(tc.name, func(t *testing.T) {
454500
matched, err := api.MatchInstanceAttributeNames(tc.me, tc.input)
455501
assert.Equal(t, tc.output, matched)
456-
assert.Nil(t, err)
502+
tc.err(t, err)
457503
})
458504
}
459505
}

0 commit comments

Comments
 (0)