diff --git a/pkg/apis/softwarecomposition/networkpolicy/v2/networkpolicy_test.go b/pkg/apis/softwarecomposition/networkpolicy/v2/networkpolicy_test.go index fcfe69453..16bcc1050 100644 --- a/pkg/apis/softwarecomposition/networkpolicy/v2/networkpolicy_test.go +++ b/pkg/apis/softwarecomposition/networkpolicy/v2/networkpolicy_test.go @@ -1387,9 +1387,7 @@ func TestRemoveLabels(t *testing.T) { removeLabels(labels) - if !reflect.DeepEqual(labels, expected) { - t.Errorf("removeLabels() = %v, want %v", labels, expected) - } + assert.Equal(t, expected, labels) } func TestMergeIngressRulesByPorts(t *testing.T) { diff --git a/pkg/registry/file/dynamicpathdetector/tests/analayze_opens_test.go b/pkg/registry/file/dynamicpathdetector/tests/analayze_opens_test.go index b9ec89480..5873939ac 100644 --- a/pkg/registry/file/dynamicpathdetector/tests/analayze_opens_test.go +++ b/pkg/registry/file/dynamicpathdetector/tests/analayze_opens_test.go @@ -2,11 +2,11 @@ package dynamicpathdetectortests import ( "fmt" - "reflect" "testing" types "github.com/kubescape/storage/pkg/apis/softwarecomposition" "github.com/kubescape/storage/pkg/registry/file/dynamicpathdetector" + "github.com/stretchr/testify/assert" ) func TestAnalyzeOpensWithThreshold(t *testing.T) { @@ -26,13 +26,8 @@ func TestAnalyzeOpensWithThreshold(t *testing.T) { } result, err := dynamicpathdetector.AnalyzeOpens(input, analyzer) - if err != nil { - t.Errorf("AnalyzeOpens() error = %v", err) - return - } - if !reflect.DeepEqual(result, expected) { - t.Errorf("AnalyzeOpens() = %v, want %v", result, expected) - } + assert.NoError(t, err) + assert.Equal(t, expected, result) } func TestAnalyzeOpensWithFlagMergingAndThreshold(t *testing.T) { @@ -103,20 +98,13 @@ func TestAnalyzeOpensWithFlagMergingAndThreshold(t *testing.T) { t.Run(tt.name, func(t *testing.T) { analyzer := dynamicpathdetector.NewPathAnalyzer(3) result, err := dynamicpathdetector.AnalyzeOpens(tt.input, analyzer) - if err != nil { - t.Errorf("AnalyzeOpens() error = %v", err) - return - } + assert.NoError(t, err) - if !reflect.DeepEqual(result, tt.expected) { - t.Errorf("AnalyzeOpens() = %v, want %v", result, tt.expected) - } + assert.Equal(t, tt.expected, result) // Additional check for flag uniqueness for _, openCall := range result { - if !areStringSlicesUnique(openCall.Flags) { - t.Errorf("Flags are not unique for path %s: %v", openCall.Path, openCall.Flags) - } + assert.True(t, areStringSlicesUnique(openCall.Flags), "Flags are not unique for path %s: %v", openCall.Path, openCall.Flags) } }) } diff --git a/pkg/registry/file/dynamicpathdetector/tests/analyze_endpoints_test.go b/pkg/registry/file/dynamicpathdetector/tests/analyze_endpoints_test.go index 65c23a25c..50ee84857 100644 --- a/pkg/registry/file/dynamicpathdetector/tests/analyze_endpoints_test.go +++ b/pkg/registry/file/dynamicpathdetector/tests/analyze_endpoints_test.go @@ -3,11 +3,11 @@ package dynamicpathdetectortests import ( "encoding/json" "fmt" - "reflect" "testing" types "github.com/kubescape/storage/pkg/apis/softwarecomposition" "github.com/kubescape/storage/pkg/registry/file/dynamicpathdetector" + "github.com/stretchr/testify/assert" ) func TestAnalyzeEndpoints(t *testing.T) { @@ -133,13 +133,8 @@ func TestAnalyzeEndpoints(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result, err := dynamicpathdetector.AnalyzeEndpoints(&tt.input, analyzer) - if err != nil { - t.Errorf("AnalyzeEndpoints() error = %v", err) - return - } - if !reflect.DeepEqual(result, tt.expected) { - t.Errorf("AnalyzeEndpoints() = %v, want %v", result, tt.expected) - } + assert.NoError(t, err) + assert.Equal(t, tt.expected, result) }) } } @@ -163,13 +158,8 @@ func TestAnalyzeEndpointsWithThreshold(t *testing.T) { } result, err := dynamicpathdetector.AnalyzeEndpoints(&input, analyzer) - if err != nil { - t.Errorf("AnalyzeEndpoints() error = %v", err) - return - } - if !reflect.DeepEqual(result, expected) { - t.Errorf("AnalyzeEndpoints() = %v, want %v", result, expected) - } + assert.NoError(t, err) + assert.Equal(t, expected, result) } func TestAnalyzeEndpointsWithExactThreshold(t *testing.T) { @@ -184,15 +174,10 @@ func TestAnalyzeEndpointsWithExactThreshold(t *testing.T) { } result, err := dynamicpathdetector.AnalyzeEndpoints(&input, analyzer) - if err != nil { - t.Errorf("AnalyzeEndpoints() error = %v", err) - return - } + assert.NoError(t, err) // Check that all 100 endpoints are still individual - if len(result) != 100 { - t.Errorf("Expected 100 individual endpoints, got %d", len(result)) - } + assert.Equal(t, 100, len(result)) // Now add one more endpoint to trigger the dynamic behavior input = append(input, types.HTTPEndpoint{ @@ -201,10 +186,7 @@ func TestAnalyzeEndpointsWithExactThreshold(t *testing.T) { }) result, err = dynamicpathdetector.AnalyzeEndpoints(&input, analyzer) - if err != nil { - t.Errorf("AnalyzeEndpoints() error = %v", err) - return - } + assert.NoError(t, err) // Check that all endpoints are now merged into one dynamic endpoint expected := []types.HTTPEndpoint{ @@ -213,10 +195,7 @@ func TestAnalyzeEndpointsWithExactThreshold(t *testing.T) { Methods: []string{"GET"}, }, } - - if !reflect.DeepEqual(result, expected) { - t.Errorf("AnalyzeEndpoints() = %v, want %v", result, expected) - } + assert.Equal(t, expected, result) } func TestAnalyzeEndpointsWithInvalidURL(t *testing.T) { @@ -229,9 +208,7 @@ func TestAnalyzeEndpointsWithInvalidURL(t *testing.T) { }, } - result, _ := dynamicpathdetector.AnalyzeEndpoints(&input, analyzer) - - if len(result) != 0 { - t.Errorf("Expected empty result, got %v", result) - } + result, err := dynamicpathdetector.AnalyzeEndpoints(&input, analyzer) + assert.NoError(t, err) + assert.Equal(t, 0, len(result)) } diff --git a/pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go b/pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go index 73b80c669..affafefd9 100644 --- a/pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go +++ b/pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go @@ -50,7 +50,7 @@ func BenchmarkAnalyzePathWithDifferentLengths(b *testing.B) { } func BenchmarkAnalyzeOpensVsDeflateStringer(b *testing.B) { - paths := PathsToOpens(generateMixedPaths(10000, 0)) + paths := pathsToOpens(generateMixedPaths(10000, 0)) analyzer := dynamicpathdetector.NewPathAnalyzer(100) b.Run("AnalyzeOpens", func(b *testing.B) { @@ -125,7 +125,7 @@ func generateRandomString(length int) string { return string(result) } -func PathsToOpens(paths []string) []types.OpenCalls { +func pathsToOpens(paths []string) []types.OpenCalls { opens := make([]types.OpenCalls, len(paths)) for i, path := range paths { opens[i] = types.OpenCalls{ diff --git a/pkg/registry/file/dynamicpathdetector/tests/coverage_test.go b/pkg/registry/file/dynamicpathdetector/tests/coverage_test.go index d957d0d63..fd6f74836 100644 --- a/pkg/registry/file/dynamicpathdetector/tests/coverage_test.go +++ b/pkg/registry/file/dynamicpathdetector/tests/coverage_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/kubescape/storage/pkg/registry/file/dynamicpathdetector" + "github.com/stretchr/testify/assert" ) func TestNewPathAnalyzer(t *testing.T) { @@ -32,12 +33,8 @@ func TestAnalyzePath(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { result, err := analyzer.AnalyzePath(tc.path, tc.identifier) - if err != nil { - t.Errorf("AnalyzePath(%q, %q) returned an error: %v", tc.path, tc.identifier, err) - } - if result != tc.expected { - t.Errorf("AnalyzePath(%q, %q) = %q, want %q", tc.path, tc.identifier, result, tc.expected) - } + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) }) } } @@ -49,9 +46,7 @@ func TestDynamicSegments(t *testing.T) { for i := 0; i < 101; i++ { path := fmt.Sprintf("/api/users/%d", i) _, err := analyzer.AnalyzePath(path, "api") - if err != nil { - t.Errorf("AnalyzePath() returned an error: %v", err) - } + assert.NoError(t, err) } result, err := analyzer.AnalyzePath("/api/users/101", "api") @@ -59,18 +54,12 @@ func TestDynamicSegments(t *testing.T) { t.Errorf("AnalyzePath() returned an error: %v", err) } expected := "/api/users/" - if result != expected { - t.Errorf("AnalyzePath(\"/users/101\", \"api\") = %q, want %q", result, expected) - } + assert.Equal(t, expected, result) // Test with one of the original IDs to ensure it's also marked as dynamic result, err = analyzer.AnalyzePath("/api/users/50", "api") - if err != nil { - t.Errorf("AnalyzePath() returned an error: %v", err) - } - if result != expected { - t.Errorf("AnalyzePath(\"/users/50\", \"api\") = %q, want %q", result, expected) - } + assert.NoError(t, err) + assert.Equal(t, expected, result) } func TestMultipleDynamicSegments(t *testing.T) { @@ -87,14 +76,9 @@ func TestMultipleDynamicSegments(t *testing.T) { // Test with the 100th unique user and post IDs (should trigger dynamic segments) result, err := analyzer.AnalyzePath("/api/users/101/posts/1031", "api") - if err != nil { - t.Errorf("AnalyzePath() returned an error: %v", err) - } + assert.NoError(t, err) expected := "/api/users//posts/" - if result != expected { - t.Errorf("AnalyzePath(\"/users/99/posts/99\", \"api\") = %q, want %q", result, expected) - } - + assert.Equal(t, expected, result) } func TestMixedStaticAndDynamicSegments(t *testing.T) { @@ -111,14 +95,9 @@ func TestMixedStaticAndDynamicSegments(t *testing.T) { // Test with the 100th unique user ID but same 'posts' segment (should trigger dynamic segment for users) result, err := analyzer.AnalyzePath("/api/users/99/posts", "api") - if err != nil { - t.Errorf("AnalyzePath() returned an error: %v", err) - } + assert.NoError(t, err) expected := "/api/users//posts" - if result != expected { - t.Errorf("AnalyzePath(\"/users/99/posts\", \"api\") = %q, want %q", result, expected) - } - + assert.Equal(t, expected, result) } func TestDifferentRootIdentifiers(t *testing.T) { @@ -128,13 +107,9 @@ func TestDifferentRootIdentifiers(t *testing.T) { result1, _ := analyzer.AnalyzePath("/api/users/123", "api") result2, _ := analyzer.AnalyzePath("/api/products/456", "store") - if result1 != "/api/users/123" { - t.Errorf("AnalyzePath(\"/users/123\", \"api\") = %q, want \"/api/users/123\"", result1) - } + assert.Equal(t, "/api/users/123", result1) - if result2 != "/api/products/456" { - t.Errorf("AnalyzePath(\"/products/456\", \"store\") = %q, want \"/store/products/456\"", result2) - } + assert.Equal(t, "/api/products/456", result2) } func TestDynamicThreshold(t *testing.T) { @@ -149,9 +124,7 @@ func TestDynamicThreshold(t *testing.T) { } result, _ := analyzer.AnalyzePath("/api/users/991", "api") - if result != "/api/users/" { - t.Errorf("Path did not become dynamic after 99 different paths") - } + assert.Equal(t, "/api/users/", result) } func TestEdgeCases(t *testing.T) { @@ -171,12 +144,8 @@ func TestEdgeCases(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { result, err := analyzer.AnalyzePath(tc.path, tc.identifier) - if err != nil { - t.Errorf("AnalyzePath(%q, %q) returned an error: %v", tc.path, tc.identifier, err) - } - if result != tc.expected { - t.Errorf("AnalyzePath(%q, %q) = %q, want %q", tc.path, tc.identifier, result, tc.expected) - } + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) }) } } @@ -186,22 +155,13 @@ func TestDynamicInsertion(t *testing.T) { // Insert a new path with a different identifier result, err := analyzer.AnalyzePath("/api/users/", "api") - if err != nil { - t.Errorf("AnalyzePath() returned an error: %v", err) - } + assert.NoError(t, err) expected := "/api/users/" - if result != expected { - t.Errorf("AnalyzePath(\"/api/users/\", \"api\") = %q, want %q", result, expected) - } + assert.Equal(t, expected, result) // Insert a new path with the same identifier result, err = analyzer.AnalyzePath("/api/users/102", "api") - if err != nil { - t.Errorf("AnalyzePath() returned an error: %v", err) - } + assert.NoError(t, err) expected = "/api/users/" - if result != expected { - t.Errorf("AnalyzePath(\"/api/users/\", \"api\") = %q, want %q", result, expected) - } - + assert.Equal(t, expected, result) } diff --git a/pkg/registry/softwarecomposition/applicationprofile/strategy_test.go b/pkg/registry/softwarecomposition/applicationprofile/strategy_test.go index 7cd5237f5..6c304ac1c 100644 --- a/pkg/registry/softwarecomposition/applicationprofile/strategy_test.go +++ b/pkg/registry/softwarecomposition/applicationprofile/strategy_test.go @@ -2,11 +2,11 @@ package applicationprofile import ( "context" - "reflect" "testing" "github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers" "github.com/kubescape/storage/pkg/apis/softwarecomposition" + "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -99,9 +99,7 @@ func TestPrepareForUpdateAnnotations(t *testing.T) { old := &softwarecomposition.ApplicationProfile{ObjectMeta: metav1.ObjectMeta{Annotations: tt.oldAnnotations}} s.PrepareForUpdate(context.Background(), obj, old) - if !reflect.DeepEqual(obj.Annotations, tt.expected) { - t.Errorf("PrepareForUpdate() = %v, want %v", obj.Annotations, tt.expected) - } + assert.Equal(t, tt.expected, obj.Annotations) }) } } @@ -260,9 +258,7 @@ func TestPrepareForUpdateFullObj(t *testing.T) { t.Run(tt.name, func(t *testing.T) { s := applicationProfileStrategy{} s.PrepareForUpdate(context.Background(), tt.new, tt.old) - if !reflect.DeepEqual(tt.new, tt.expected) { - t.Errorf("PrepareForUpdate() = %v, want %v", tt.new, tt.expected) - } + assert.Equal(t, tt.expected, tt.new) }) } } diff --git a/pkg/registry/softwarecomposition/networkneighborhood/strategy_test.go b/pkg/registry/softwarecomposition/networkneighborhood/strategy_test.go index f2abfbb79..491810a91 100644 --- a/pkg/registry/softwarecomposition/networkneighborhood/strategy_test.go +++ b/pkg/registry/softwarecomposition/networkneighborhood/strategy_test.go @@ -2,11 +2,11 @@ package networkneighborhood import ( "context" - "reflect" "testing" "github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers" "github.com/kubescape/storage/pkg/apis/softwarecomposition" + "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/ptr" ) @@ -100,9 +100,7 @@ func TestPrepareForUpdate(t *testing.T) { old := &softwarecomposition.NetworkNeighborhood{ObjectMeta: metav1.ObjectMeta{Annotations: tt.oldAnnotations}} s.PrepareForUpdate(context.Background(), obj, old) - if !reflect.DeepEqual(obj.Annotations, tt.expected) { - t.Errorf("PrepareForUpdate() = %v, want %v", obj.Annotations, tt.expected) - } + assert.Equal(t, tt.expected, obj.Annotations) }) } } @@ -342,9 +340,7 @@ func TestPrepareForUpdateFullObj(t *testing.T) { t.Run(tt.name, func(t *testing.T) { s := NetworkNeighborhoodStrategy{} s.PrepareForUpdate(context.Background(), tt.new, tt.old) - if !reflect.DeepEqual(tt.new, tt.expected) { - t.Errorf("PrepareForUpdate() = %v, want %v", tt.new, tt.expected) - } + assert.Equal(t, tt.expected, tt.new) }) } } diff --git a/pkg/registry/softwarecomposition/networkneighbors/strategy_test.go b/pkg/registry/softwarecomposition/networkneighbors/strategy_test.go index acdcd3019..c0b22fbf5 100644 --- a/pkg/registry/softwarecomposition/networkneighbors/strategy_test.go +++ b/pkg/registry/softwarecomposition/networkneighbors/strategy_test.go @@ -2,11 +2,11 @@ package networkneighbors import ( "context" - "reflect" "testing" "github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers" "github.com/kubescape/storage/pkg/apis/softwarecomposition" + "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -84,9 +84,7 @@ func TestPrepareForUpdate(t *testing.T) { old := &softwarecomposition.NetworkNeighbors{ObjectMeta: metav1.ObjectMeta{Annotations: tt.oldAnnotations}} s.PrepareForUpdate(context.Background(), obj, old) - if !reflect.DeepEqual(obj.Annotations, tt.expected) { - t.Errorf("PrepareForUpdate() = %v, want %v", obj.Annotations, tt.expected) - } + assert.Equal(t, tt.expected, obj.Annotations) }) } }