diff --git a/pkg/trivyk8s/trivyk8s.go b/pkg/trivyk8s/trivyk8s.go index 8533d72..0255437 100644 --- a/pkg/trivyk8s/trivyk8s.go +++ b/pkg/trivyk8s/trivyk8s.go @@ -292,6 +292,9 @@ func isNodeStatusUnknown(resource unstructured.Unstructured) bool { } func ignoreNodeByLabel(resource *artifacts.Artifact, ignoreLabels map[string]string) bool { + if len(ignoreLabels) == 0 { + return false + } var matchingLabels int for key, val := range ignoreLabels { if lVal, ok := resource.Labels[key]; ok && lVal == val { diff --git a/pkg/trivyk8s/trivyk8s_test.go b/pkg/trivyk8s/trivyk8s_test.go new file mode 100644 index 0000000..0df47ff --- /dev/null +++ b/pkg/trivyk8s/trivyk8s_test.go @@ -0,0 +1,43 @@ +package trivyk8s + +import ( + "testing" + + "github.com/aquasecurity/trivy-kubernetes/pkg/artifacts" + "github.com/stretchr/testify/assert" +) + +func TestIgnoreNodeByLabel(t *testing.T) { + tests := []struct { + name string + ignoredLabels map[string]string + artifact *artifacts.Artifact + want bool + }{ + { + name: "no ignore labels", + ignoredLabels: map[string]string{}, + artifact: &artifacts.Artifact{Labels: map[string]string{"a": "b"}}, + want: false, + }, + { + name: "matching ignore labels", + ignoredLabels: map[string]string{"a": "b"}, + artifact: &artifacts.Artifact{Labels: map[string]string{"a": "b"}}, + want: true, + }, + { + name: "non matching ignore labels", + ignoredLabels: map[string]string{"a": "b", "c": "d"}, + artifact: &artifacts.Artifact{Labels: map[string]string{"a": "b"}}, + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := ignoreNodeByLabel(tt.artifact, tt.ignoredLabels) + assert.Equal(t, got, tt.want) + }) + } +}