Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apis/nfd: increase unit test coverage #1693

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 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,26 @@ 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"}},
},
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)
})
}
}