Skip to content

Commit

Permalink
apis/nfd: increase unit test coverage
Browse files Browse the repository at this point in the history
Cover error cases of the "match name" functions.
  • Loading branch information
marquiz committed Apr 30, 2024
1 parent 18980d8 commit c200302
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions pkg/apis/nfd/nodefeaturerule/expression-api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func TestMatchKeyNames(t *testing.T) {
input I
result bool
output O
err ValueAssertionFunc
}

tcs := []TC{
Expand All @@ -278,48 +279,63 @@ func TestMatchKeyNames(t *testing.T) {
input: I{},
result: false,
output: O{},
err: assert.Nil,
},
{
name: "MatchAny",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchAny},
input: I{"key1": {}, "key2": {}},
result: true,
output: O{{"Name": "key1"}, {"Name": "key2"}},
err: assert.Nil,
},
{
name: "MatchExists",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchExists},
input: I{"key1": {}, "key2": {}},
result: true,
output: O{{"Name": "key1"}, {"Name": "key2"}},
err: assert.Nil,
},
{
name: "MatchDoesNotExist",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchDoesNotExist},
input: I{"key1": {}, "key2": {}},
result: false,
output: O{},
err: assert.Nil,
},
{
name: "MatchIn matches",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key1"}},
input: I{"key1": {}, "key2": {}},
result: true,
output: O{{"Name": "key1"}},
err: assert.Nil,
},
{
name: "MatchIn no match",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key3"}},
input: I{"key1": {}, "key2": {}},
result: false,
output: O{},
err: assert.Nil,
},
{
name: "MatchNotIn",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchNotIn, Value: nfdv1alpha1.MatchValue{"key1"}},
input: I{"key1": {}, "key2": {}},
result: true,
output: O{{"Name": "key2"}},
err: assert.Nil,
},
{
name: "error",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchExists, Value: nfdv1alpha1.MatchValue{"key1"}},
input: I{"key1": {}, "key2": {}},
result: false,
output: nil,
err: assert.NotNil,
},
}

Expand All @@ -328,7 +344,7 @@ func TestMatchKeyNames(t *testing.T) {
res, ret, err := api.MatchKeyNames(tc.me, tc.input)
assert.Equal(t, tc.result, res)
assert.Equal(t, tc.output, ret)
assert.Nil(t, err)
tc.err(t, err)
})
}
}
Expand All @@ -343,6 +359,7 @@ func TestMatchValueNames(t *testing.T) {
input I
result bool
output O
err ValueAssertionFunc
}

tcs := []TC{
Expand All @@ -352,41 +369,55 @@ func TestMatchValueNames(t *testing.T) {
input: I{},
result: false,
output: O{},
err: assert.Nil,
},
{
name: "MatchExists",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchExists},
input: I{"key1": "val1", "key2": "val2"},
result: true,
output: O{{"Name": "key1", "Value": "val1"}, {"Name": "key2", "Value": "val2"}},
err: assert.Nil,
},
{
name: "MatchDoesNotExist",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchDoesNotExist},
input: I{"key1": "val1", "key2": "val2"},
result: false,
output: O{},
err: assert.Nil,
},
{
name: "MatchIn matches",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key1"}},
input: I{"key1": "val1", "key2": "val2"},
result: true,
output: O{{"Name": "key1", "Value": "val1"}},
err: assert.Nil,
},
{
name: "MatchIn no match",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchIn, Value: nfdv1alpha1.MatchValue{"key3"}},
input: I{"key1": "val1", "key2": "val2"},
result: false,
output: O{},
err: assert.Nil,
},
{
name: "MatchNotIn",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchNotIn, Value: nfdv1alpha1.MatchValue{"key1"}},
input: I{"key1": "val1", "key2": "val2"},
result: true,
output: O{{"Name": "key2", "Value": "val2"}},
err: assert.Nil,
},
{
name: "error",
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchNotIn},
input: I{"key1": "val1", "key2": "val2"},
result: false,
output: nil,
err: assert.NotNil,
},
}

Expand All @@ -395,7 +426,7 @@ func TestMatchValueNames(t *testing.T) {
res, ret, err := api.MatchValueNames(tc.me, tc.input)
assert.Equal(t, tc.result, res)
assert.Equal(t, tc.output, ret)
assert.Nil(t, err)
tc.err(t, err)
})
}
}
Expand All @@ -410,6 +441,7 @@ func TestMatchInstanceAttributeNames(t *testing.T) {
me *nfdv1alpha1.MatchExpression
input I
output O
err ValueAssertionFunc
}

tcs := []TC{
Expand All @@ -418,6 +450,7 @@ func TestMatchInstanceAttributeNames(t *testing.T) {
me: &nfdv1alpha1.MatchExpression{Op: nfdv1alpha1.MatchAny},
input: I{},
output: O{},
err: assert.Nil,
},
{
name: "no match",
Expand All @@ -430,6 +463,7 @@ func TestMatchInstanceAttributeNames(t *testing.T) {
{Attributes: A{"baz": "2"}},
},
output: O{},
err: assert.Nil,
},
{
name: "match",
Expand All @@ -446,14 +480,27 @@ func TestMatchInstanceAttributeNames(t *testing.T) {
{"foo": "1"},
{"foo": "3", "baz": "4"},
},
err: assert.Nil,
},
{
name: "error",
me: &nfdv1alpha1.MatchExpression{
Op: nfdv1alpha1.MatchIn,
},
input: I{
{Attributes: A{"foo": "1"}},
},
result: false,
output: nil,
err: assert.NotNil,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
matched, err := api.MatchInstanceAttributeNames(tc.me, tc.input)
assert.Equal(t, tc.output, matched)
assert.Nil(t, err)
tc.err(t, err)
})
}
}

0 comments on commit c200302

Please sign in to comment.