diff --git a/go.mod b/go.mod index cd612e4505c5..f07829224dab 100644 --- a/go.mod +++ b/go.mod @@ -49,7 +49,7 @@ require ( github.com/gobwas/glob v0.2.3 github.com/google/osv-scanner v1.3.2 github.com/mcuadros/go-jsonschema-generator v0.0.0-20200330054847-ba7a369d4303 - github.com/onsi/ginkgo/v2 v2.9.2 + github.com/onsi/ginkgo/v2 v2.9.3 github.com/otiai10/copy v1.11.0 sigs.k8s.io/release-utils v0.6.0 ) diff --git a/go.sum b/go.sum index d537ff8870b3..e31f9bb7ed6a 100644 --- a/go.sum +++ b/go.sum @@ -1658,8 +1658,8 @@ github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47 github.com/onsi/ginkgo/v2 v2.1.6/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= github.com/onsi/ginkgo/v2 v2.3.0/go.mod h1:Eew0uilEqZmIEZr8JrvYlvOM7Rr6xzTmMV8AyFNU9d0= github.com/onsi/ginkgo/v2 v2.4.0/go.mod h1:iHkDK1fKGcBoEHT5W7YBq4RFWaQulw+caOMkAt4OrFo= -github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU= -github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts= +github.com/onsi/ginkgo/v2 v2.9.3 h1:5X2vl/isiKqkrOYjiaGgp3JQOcLV59g5o5SuTMqCcxU= +github.com/onsi/ginkgo/v2 v2.9.3/go.mod h1:gCQYp2Q+kSoIj7ykSVb9nskRSsR6PUj4AiLywzIhbKM= github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= diff --git a/rule/rule_test.go b/rule/rule_test.go index e6484a23f2ee..e84de19d47f7 100644 --- a/rule/rule_test.go +++ b/rule/rule_test.go @@ -17,10 +17,12 @@ package rule import ( "embed" "errors" + "fmt" "testing" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "gopkg.in/yaml.v3" ) func errCmp(e1, e2 error) bool { @@ -85,3 +87,283 @@ func Test_New(t *testing.T) { }) } } + +func TestRisk_GreaterThan(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + r Risk + rr Risk + want bool + }{ + { + name: "greater than", + r: RiskHigh, + rr: RiskLow, + want: true, + }, + { + name: "less than", + r: RiskLow, + rr: RiskHigh, + want: false, + }, + { + name: "equal", + r: RiskMedium, + rr: RiskMedium, + want: false, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + if got := tt.r.GreaterThan(tt.rr); got != tt.want { + t.Errorf("Risk.GreaterThan() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRisk_String(t *testing.T) { + t.Parallel() + + tests := []struct { //nolint:govet + name string + r Risk + want string + }{ + { + name: "RiskNone", + r: RiskNone, + want: "None", + }, + { + name: "RiskLow", + r: RiskLow, + want: "Low", + }, + { + name: "RiskMedium", + r: RiskMedium, + want: "Medium", + }, + { + name: "RiskHigh", + r: RiskHigh, + want: "High", + }, + { + name: "RiskCritical", + r: RiskCritical, + want: "Critical", + }, + { + name: "invalid", + r: Risk(100), + want: "", + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + if got := tt.r.String(); got != tt.want { + t.Errorf("Risk.String() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRemediationEffort_String(t *testing.T) { + t.Parallel() + + tests := []struct { //nolint:govet + name string + effort RemediationEffort + want string + }{ + { + name: "RemediationEffortNone", + effort: RemediationEffortNone, + want: "", + }, + { + name: "RemediationEffortLow", + effort: RemediationEffortLow, + want: "Low", + }, + { + name: "RemediationEffortMedium", + effort: RemediationEffortMedium, + want: "Medium", + }, + { + name: "RemediationEffortHigh", + effort: RemediationEffortHigh, + want: "High", + }, + { + name: "invalid", + effort: RemediationEffort(100), + want: "", + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + if got := tt.effort.String(); got != tt.want { + t.Errorf("RemediationEffort.String() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRisk_UnmarshalYAML(t *testing.T) { + t.Parallel() + + tests := []struct { //nolint:govet + name string + input string + wantErr error + want Risk + }{ + { + name: "RiskNone", + input: "None", + want: RiskNone, + }, + { + name: "RiskLow", + input: "Low", + want: RiskLow, + }, + { + name: "RiskMedium", + input: "Medium", + want: RiskMedium, + }, + { + name: "RiskHigh", + input: "High", + want: RiskHigh, + }, + { + name: "RiskCritical", + input: "Critical", + want: RiskCritical, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + var r Risk + err := yaml.Unmarshal([]byte(tt.input), &r) + if err != nil { + if tt.wantErr == nil || !errors.Is(err, tt.wantErr) { + t.Errorf("Risk.UnmarshalYAML() error = %v, wantErr %v", err, tt.wantErr) + } + return + } + if r != tt.want { + t.Errorf("Risk.UnmarshalYAML() got = %v, want %v", r, tt.want) + } + }) + } +} + +func TestRemediationEffort_UnmarshalYAML(t *testing.T) { + t.Parallel() + + tests := []struct { //nolint:govet + name string + input string + wantErr error + want RemediationEffort + }{ + { + name: "RemediationEffortLow", + input: "Low", + want: RemediationEffortLow, + }, + { + name: "RemediationEffortMedium", + input: "Medium", + want: RemediationEffortMedium, + }, + { + name: "RemediationEffortHigh", + input: "High", + want: RemediationEffortHigh, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + var r RemediationEffort + err := yaml.Unmarshal([]byte(tt.input), &r) + if err != nil { + if tt.wantErr == nil || !errors.Is(err, tt.wantErr) { + t.Errorf("RemediationEffort.UnmarshalYAML() error = %v, wantErr %v", err, tt.wantErr) + } + return + } + if r != tt.want { + t.Errorf("RemediationEffort.UnmarshalYAML() got = %v, want %v", r, tt.want) + } + }) + } +} + +func Test_validate(t *testing.T) { + t.Parallel() + + tests := []struct { //nolint:govet + name string + rule *jsonRule + wantErr error + }{ + { + name: "valid", + rule: &jsonRule{ + Risk: RiskLow, + Remediation: jsonRemediation{ + Effort: RemediationEffortHigh, + }, + }, + wantErr: nil, + }, + { + name: "invalid risk", + rule: &jsonRule{ + Risk: Risk(100), + Remediation: jsonRemediation{ + Effort: RemediationEffortHigh, + }, + }, + wantErr: fmt.Errorf("%w: invalid: risk '100'", errInvalid), + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + err := validate(tt.rule) + if err != nil { + if tt.wantErr == nil || !cmp.Equal(tt.wantErr.Error(), err.Error()) { + t.Logf("got: %s", err.Error()) + t.Errorf("validate() error = %v, wantErr %v", err, cmp.Diff(tt.wantErr.Error(), err.Error())) + } + return + } + if tt.wantErr != nil { + t.Errorf("validate() error = %v, wantErr %v", err, cmp.Diff(tt.wantErr, err)) + } + }) + } +} diff --git a/tools/go.mod b/tools/go.mod index 986497eccf60..a4657fdb9c56 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -9,7 +9,7 @@ require ( github.com/google/ko v0.13.0 github.com/goreleaser/goreleaser v1.17.2 github.com/naveensrinivasan/stunning-tribble v0.4.2 - github.com/onsi/ginkgo/v2 v2.9.2 + github.com/onsi/ginkgo/v2 v2.9.3 google.golang.org/protobuf v1.30.0 ) @@ -143,7 +143,7 @@ require ( github.com/go-git/gcfg v1.5.0 // indirect github.com/go-git/go-billy/v5 v5.3.1 // indirect github.com/go-git/go-git/v5 v5.4.2 // indirect - github.com/go-logr/logr v1.2.3 // indirect + github.com/go-logr/logr v1.2.4 // indirect github.com/go-openapi/analysis v0.21.4 // indirect github.com/go-openapi/errors v0.20.3 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect diff --git a/tools/go.sum b/tools/go.sum index fe5d78a51ba5..458b77c559ce 100644 --- a/tools/go.sum +++ b/tools/go.sum @@ -1140,8 +1140,9 @@ github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTg github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.1/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= +github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-openapi/analysis v0.21.2/go.mod h1:HZwRk4RRisyG8vx2Oe6aqeSQcoxRp47Xkp3+K6q+LdY= @@ -1966,8 +1967,8 @@ github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47 github.com/onsi/ginkgo/v2 v2.1.6/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= github.com/onsi/ginkgo/v2 v2.3.0/go.mod h1:Eew0uilEqZmIEZr8JrvYlvOM7Rr6xzTmMV8AyFNU9d0= github.com/onsi/ginkgo/v2 v2.4.0/go.mod h1:iHkDK1fKGcBoEHT5W7YBq4RFWaQulw+caOMkAt4OrFo= -github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU= -github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts= +github.com/onsi/ginkgo/v2 v2.9.3 h1:5X2vl/isiKqkrOYjiaGgp3JQOcLV59g5o5SuTMqCcxU= +github.com/onsi/ginkgo/v2 v2.9.3/go.mod h1:gCQYp2Q+kSoIj7ykSVb9nskRSsR6PUj4AiLywzIhbKM= github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= @@ -1986,7 +1987,7 @@ github.com/onsi/gomega v1.20.1/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeR github.com/onsi/gomega v1.21.1/go.mod h1:iYAIXgPSaDHak0LCMA+AWBpIKBr8WZicMxnE8luStNc= github.com/onsi/gomega v1.22.1/go.mod h1:x6n7VNe4hw0vkyYUM4mjIXx3JbLiPaBPNgB7PRQ1tuM= github.com/onsi/gomega v1.23.0/go.mod h1:Z/NWtiqwBrwUt4/2loMmHL63EDLnYHmVbuBpDr2vQAg= -github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E= +github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= github.com/opencontainers/go-digest v0.0.0-20170106003457-a6d0ee40d420/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= github.com/opencontainers/go-digest v0.0.0-20180430190053-c9281466c8b2/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=