Skip to content

Commit

Permalink
use assert instead of deepequal to stabilize tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx committed Sep 18, 2024
1 parent 30d043c commit a1d6ca2
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 136 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/go-logr/zapr v1.2.4
github.com/gogo/protobuf v1.3.2
github.com/goradd/maps v0.1.5
github.com/kinbiko/jsonassert v1.1.1
github.com/kubescape/go-logger v0.0.22
github.com/kubescape/k8s-interface v0.0.162
github.com/olvrng/ujson v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,8 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kinbiko/jsonassert v1.1.1 h1:DB12divY+YB+cVpHULLuKePSi6+ui4M/shHSzJISkSE=
github.com/kinbiko/jsonassert v1.1.1/go.mod h1:NO4lzrogohtIdNUNzx8sdzB55M4R4Q1bsrWVdqQ7C+A=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/asmfmt v1.3.2/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j0HLHbNSE=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 6 additions & 18 deletions pkg/registry/file/dynamicpathdetector/tests/analayze_opens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package dynamicpathdetectortests
import (
"encoding/json"
"fmt"
"reflect"
"testing"

"github.com/kinbiko/jsonassert"
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) {
Expand Down Expand Up @@ -124,7 +125,7 @@ func TestAnalyzeEndpoints(t *testing.T) {
{
Endpoint: ":80/x/<dynamic>/posts/<dynamic>",
Methods: []string{"GET", "POST"},
Headers: json.RawMessage([]byte{123, 34, 65, 117, 116, 104, 111, 114, 105, 122, 97, 116, 105, 111, 110, 34, 58, 91, 34, 66, 101, 97, 114, 101, 114, 32, 116, 111, 107, 101, 110, 34, 93, 44, 34, 67, 111, 110, 116, 101, 110, 116, 45, 84, 121, 112, 101, 34, 58, 91, 34, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 106, 115, 111, 110, 34, 44, 34, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 120, 109, 108, 34, 93, 44, 34, 88, 45, 65, 80, 73, 45, 75, 101, 121, 34, 58, 91, 34, 107, 101, 121, 49, 34, 93, 125}),
Headers: json.RawMessage(`{"Authorization":["Bearer token"],"Content-Type":["<<UNORDERED>>","application/json","application/xml"],"X-API-Key":["key1"]}`),
},
},
},
Expand All @@ -133,12 +134,12 @@ 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)
ja := jsonassert.New(t)
for i := range result {
assert.Equal(t, tt.expected[i].Endpoint, result[i].Endpoint)
assert.Equal(t, tt.expected[i].Methods, result[i].Methods)
ja.Assertf(string(result[i].Headers), string(tt.expected[i].Headers))
}
})
}
Expand All @@ -163,13 +164,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) {
Expand All @@ -184,15 +180,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{
Expand All @@ -201,10 +192,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{
Expand All @@ -213,10 +201,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) {
Expand All @@ -229,9 +214,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))
}
4 changes: 2 additions & 2 deletions pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down
80 changes: 20 additions & 60 deletions pkg/registry/file/dynamicpathdetector/tests/coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/kubescape/storage/pkg/registry/file/dynamicpathdetector"
"github.com/stretchr/testify/assert"
)

func TestNewPathAnalyzer(t *testing.T) {
Expand Down Expand Up @@ -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)
})
}
}
Expand All @@ -49,28 +46,20 @@ 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")
if err != nil {
t.Errorf("AnalyzePath() returned an error: %v", err)
}
expected := "/api/users/<dynamic>"
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) {
Expand All @@ -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/<dynamic>/posts/<dynamic>"
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) {
Expand All @@ -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/<dynamic>/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) {
Expand All @@ -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) {
Expand All @@ -149,9 +124,7 @@ func TestDynamicThreshold(t *testing.T) {
}

result, _ := analyzer.AnalyzePath("/api/users/991", "api")
if result != "/api/users/<dynamic>" {
t.Errorf("Path did not become dynamic after 99 different paths")
}
assert.Equal(t, "/api/users/<dynamic>", result)
}

func TestEdgeCases(t *testing.T) {
Expand All @@ -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)
})
}
}
Expand All @@ -186,22 +155,13 @@ func TestDynamicInsertion(t *testing.T) {

// Insert a new path with a different identifier
result, err := analyzer.AnalyzePath("/api/users/<dynamic>", "api")
if err != nil {
t.Errorf("AnalyzePath() returned an error: %v", err)
}
assert.NoError(t, err)
expected := "/api/users/<dynamic>"
if result != expected {
t.Errorf("AnalyzePath(\"/api/users/<dynamic>\", \"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/<dynamic>"
if result != expected {
t.Errorf("AnalyzePath(\"/api/users/<dynamic>\", \"api\") = %q, want %q", result, expected)
}

assert.Equal(t, expected, result)
}
Loading

0 comments on commit a1d6ca2

Please sign in to comment.