From 88c55c2b9d9ecd44ac812261126c0c260ae199b6 Mon Sep 17 00:00:00 2001 From: Darshan Poudel Date: Thu, 12 Dec 2024 14:39:24 +0545 Subject: [PATCH] fix/duplicate-test-entries-deduplication (#11709) Signed-off-by: Darshan808 Co-authored-by: shuting --- cmd/cli/kubectl-kyverno/test/load.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/cli/kubectl-kyverno/test/load.go b/cmd/cli/kubectl-kyverno/test/load.go index 4a1b4ef1de..155d964bc7 100644 --- a/cmd/cli/kubectl-kyverno/test/load.go +++ b/cmd/cli/kubectl-kyverno/test/load.go @@ -73,9 +73,31 @@ func LoadTest(fs billy.Filesystem, path string) TestCase { Err: err, } } + cleanTest(&test) return TestCase{ Path: path, Fs: fs, Test: &test, } } + +func cleanTest(test *v1alpha1.Test) { + test.Policies = removeDuplicateStrings(test.Policies) + test.Resources = removeDuplicateStrings(test.Resources) + for index, result := range test.Results { + test.Results[index].Resources = removeDuplicateStrings(result.Resources) + } +} + +func removeDuplicateStrings(strings []string) []string { + seen := make(map[string]struct{}) + var result []string + + for _, str := range strings { + if _, exists := seen[str]; !exists { + seen[str] = struct{}{} + result = append(result, str) + } + } + return result +}