diff --git a/cmd/gateway/commands_test.go b/cmd/gateway/commands_test.go index 0c352fda7..a8fe46144 100644 --- a/cmd/gateway/commands_test.go +++ b/cmd/gateway/commands_test.go @@ -16,7 +16,7 @@ type flagTestCase struct { } func testFlag(t *testing.T, cmd *cobra.Command, test flagTestCase) { - g := NewGomegaWithT(t) + g := NewWithT(t) // discard any output generated by cobra cmd.SetOut(io.Discard) cmd.SetErr(io.Discard) diff --git a/cmd/gateway/validation_test.go b/cmd/gateway/validation_test.go index 66a983a3d..4bf678147 100644 --- a/cmd/gateway/validation_test.go +++ b/cmd/gateway/validation_test.go @@ -52,7 +52,7 @@ func TestValidateGatewayControllerName(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) err := validateGatewayControllerName(test.value) @@ -115,7 +115,7 @@ func TestValidateResourceName(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) err := validateResourceName(test.value) @@ -178,7 +178,7 @@ func TestValidateNamespaceName(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) err := validateNamespaceName(test.value) @@ -240,7 +240,7 @@ func TestParseNamespacedResourceName(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) nsName, err := parseNamespacedResourceName(test.value) @@ -283,7 +283,7 @@ func TestValidateIP(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) err := validateIP(tc.ip) if !tc.expErr { @@ -320,7 +320,7 @@ func TestValidatePort(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) err := validatePort(tc.port) if !tc.expErr { diff --git a/conformance/tests/conformance_test.go b/conformance/tests/conformance_test.go index dc3aae7ba..b7ccbbabb 100644 --- a/conformance/tests/conformance_test.go +++ b/conformance/tests/conformance_test.go @@ -35,7 +35,7 @@ import ( ) func TestConformance(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) cfg, err := config.GetConfig() g.Expect(err).To(BeNil()) diff --git a/internal/framework/controller/filter/filter_test.go b/internal/framework/controller/filter/filter_test.go index fadfd2b9e..d9638aeb8 100644 --- a/internal/framework/controller/filter/filter_test.go +++ b/internal/framework/controller/filter/filter_test.go @@ -10,10 +10,9 @@ import ( func TestCreateSingleResourceFilter(t *testing.T) { targetNsName := types.NamespacedName{Namespace: "test", Name: "resource"} + g := NewWithT(t) filter := CreateSingleResourceFilter(targetNsName) - if filter == nil { - t.Fatal("TestCreateSingleResourceFilter() returned nil filter") - } + g.Expect(filter).ToNot(BeNil()) const expectedMsg = "Resource is ignored because this controller only supports a single resource " + "test/resource of that type" @@ -52,8 +51,7 @@ func TestCreateSingleResourceFilter(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) - + g := NewWithT(t) shouldProcess, msg := filter(test.nsname) g.Expect(shouldProcess).To(Equal(test.expectedShouldProcess)) g.Expect(msg).To(Equal(test.expectedMsg)) diff --git a/internal/framework/controller/index/endpointslice_test.go b/internal/framework/controller/index/endpointslice_test.go index c972ff776..749f53828 100644 --- a/internal/framework/controller/index/endpointslice_test.go +++ b/internal/framework/controller/index/endpointslice_test.go @@ -3,7 +3,7 @@ package index import ( "testing" - "github.com/google/go-cmp/cmp" + . "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" discoveryV1 "k8s.io/api/discovery/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -42,18 +42,16 @@ func TestServiceNameIndexFunc(t *testing.T) { } for _, tc := range testcases { + g := NewWithT(t) output := ServiceNameIndexFunc(tc.obj) - if diff := cmp.Diff(tc.expOutput, output); diff != "" { - t.Errorf("ServiceNameIndexFunc() mismatch on %q (-want +got):\n%s", tc.msg, diff) - } + g.Expect(output).To(Equal(tc.expOutput)) } } func TestServiceNameIndexFuncPanics(t *testing.T) { defer func() { - if r := recover(); r == nil { - t.Errorf("ServiceNameIndexFunc() did not panic") - } + g := NewWithT(t) + g.Expect(recover()).ShouldNot(BeNil()) }() ServiceNameIndexFunc(&v1.Namespace{}) diff --git a/internal/framework/controller/predicate/gatewayclass_test.go b/internal/framework/controller/predicate/gatewayclass_test.go index b5d6ada80..b2814c6fc 100644 --- a/internal/framework/controller/predicate/gatewayclass_test.go +++ b/internal/framework/controller/predicate/gatewayclass_test.go @@ -9,7 +9,7 @@ import ( ) func TestGatewayClassPredicate(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) p := GatewayClassPredicate{ControllerName: "nginx-ctlr"} diff --git a/internal/framework/controller/predicate/service_test.go b/internal/framework/controller/predicate/service_test.go index 09526ad40..85051c94f 100644 --- a/internal/framework/controller/predicate/service_test.go +++ b/internal/framework/controller/predicate/service_test.go @@ -226,7 +226,7 @@ func TestServicePortsChangedPredicate_Update(t *testing.T) { for _, tc := range testcases { t.Run(tc.msg, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) update := p.Update(event.UpdateEvent{ ObjectOld: tc.objectOld, ObjectNew: tc.objectNew, @@ -238,7 +238,7 @@ func TestServicePortsChangedPredicate_Update(t *testing.T) { } func TestServicePortsChangedPredicate(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) p := ServicePortsChangedPredicate{} diff --git a/internal/framework/controller/register_test.go b/internal/framework/controller/register_test.go index 942c8dfe3..97faa26ae 100644 --- a/internal/framework/controller/register_test.go +++ b/internal/framework/controller/register_test.go @@ -98,7 +98,7 @@ func TestRegister(t *testing.T) { for _, test := range tests { t.Run(test.msg, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) newReconciler := func(c controller.ReconcilerConfig) *controller.Reconciler { g.Expect(c.Getter).To(BeIdenticalTo(test.fakes.mgr.GetClient())) diff --git a/internal/framework/events/events_test.go b/internal/framework/events/events_test.go index a32d6dc97..17f1383aa 100644 --- a/internal/framework/events/events_test.go +++ b/internal/framework/events/events_test.go @@ -3,11 +3,12 @@ package events import ( "testing" - "github.com/google/go-cmp/cmp" + . "github.com/onsi/gomega" "sigs.k8s.io/controller-runtime/pkg/log/zap" ) func TestEventLoop_SwapBatches(t *testing.T) { + g := NewWithT(t) eventLoop := NewEventLoop(nil, zap.New(), nil, nil) eventLoop.currentBatch = EventBatch{ @@ -27,19 +28,8 @@ func TestEventLoop_SwapBatches(t *testing.T) { eventLoop.swapBatches() - if l := len(eventLoop.currentBatch); l != 4 { - t.Errorf("EventLoop.swapBatches() mismatch. Expected 4 events in the current batch, got %d", l) - } - - if diff := cmp.Diff(eventLoop.currentBatch, nextBatch); diff != "" { - t.Errorf("EventLoop.swapBatches() mismatch on current batch events (-want +got):\n%s", diff) - } - - if l := len(eventLoop.nextBatch); l != 0 { - t.Errorf("EventLoop.swapBatches() mismatch. Expected 0 events in the next batch, got %d", l) - } - - if c := cap(eventLoop.nextBatch); c != 3 { - t.Errorf("EventLoop.swapBatches() mismatch. Expected capacity of 3 in the next batch, got %d", c) - } + g.Expect(len(eventLoop.currentBatch)).To(Equal(len(nextBatch))) + g.Expect(eventLoop.currentBatch).To(Equal(nextBatch)) + g.Expect(len(eventLoop.nextBatch)).To(Equal(0)) + g.Expect(cap(eventLoop.nextBatch)).To(Equal(3)) } diff --git a/internal/framework/status/conditions_test.go b/internal/framework/status/conditions_test.go index de4fd02c2..eb247a947 100644 --- a/internal/framework/status/conditions_test.go +++ b/internal/framework/status/conditions_test.go @@ -54,7 +54,7 @@ func CreateExpectedAPIConditions( } func TestConvertRouteConditions(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) var generation int64 = 1 transitionTime := metav1.NewTime(time.Now()) diff --git a/internal/framework/status/gateway_test.go b/internal/framework/status/gateway_test.go index 4d33ee9a4..4af1ad465 100644 --- a/internal/framework/status/gateway_test.go +++ b/internal/framework/status/gateway_test.go @@ -52,7 +52,7 @@ func TestPrepareGatewayStatus(t *testing.T) { Addresses: []v1beta1.GatewayStatusAddress{podIP}, } - g := NewGomegaWithT(t) + g := NewWithT(t) result := prepareGatewayStatus(status, "1.2.3.4", transitionTime) g.Expect(helpers.Diff(expected, result)).To(BeEmpty()) diff --git a/internal/framework/status/gatewayclass_test.go b/internal/framework/status/gatewayclass_test.go index 530d0566d..65b23f85f 100644 --- a/internal/framework/status/gatewayclass_test.go +++ b/internal/framework/status/gatewayclass_test.go @@ -22,7 +22,7 @@ func TestPrepareGatewayClassStatus(t *testing.T) { Conditions: CreateExpectedAPIConditions("Test", 1, transitionTime), } - g := NewGomegaWithT(t) + g := NewWithT(t) result := prepareGatewayClassStatus(status, transitionTime) g.Expect(helpers.Diff(expected, result)).To(BeEmpty()) diff --git a/internal/framework/status/httproute_test.go b/internal/framework/status/httproute_test.go index 92a276d94..b0363e6ce 100644 --- a/internal/framework/status/httproute_test.go +++ b/internal/framework/status/httproute_test.go @@ -60,7 +60,7 @@ func TestPrepareHTTPRouteStatus(t *testing.T) { }, } - g := NewGomegaWithT(t) + g := NewWithT(t) result := prepareHTTPRouteStatus(status, gatewayCtlrName, transitionTime) g.Expect(helpers.Diff(expected, result)).To(BeEmpty()) diff --git a/internal/mode/static/build_statuses_test.go b/internal/mode/static/build_statuses_test.go index f2feb13a0..f0283ded2 100644 --- a/internal/mode/static/build_statuses_test.go +++ b/internal/mode/static/build_statuses_test.go @@ -193,7 +193,7 @@ func TestBuildStatuses(t *testing.T) { }, } - g := NewGomegaWithT(t) + g := NewWithT(t) var nginxReloadRes nginxReloadResult result := buildStatuses(graph, nginxReloadRes) @@ -285,7 +285,7 @@ func TestBuildStatusesNginxErr(t *testing.T) { }, } - g := NewGomegaWithT(t) + g := NewWithT(t) nginxReloadRes := nginxReloadResult{error: errors.New("test error")} result := buildStatuses(graph, nginxReloadRes) @@ -349,7 +349,7 @@ func TestBuildGatewayClassStatuses(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := buildGatewayClassStatuses(test.gc, test.ignoredClasses) g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty()) @@ -556,7 +556,7 @@ func TestBuildGatewayStatuses(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := buildGatewayStatuses(test.gateway, test.ignoredGateways, test.nginxReloadRes) g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty()) diff --git a/internal/mode/static/manager_test.go b/internal/mode/static/manager_test.go index 07b73f1c4..43f2d9561 100644 --- a/internal/mode/static/manager_test.go +++ b/internal/mode/static/manager_test.go @@ -63,7 +63,7 @@ func TestPrepareFirstEventBatchPreparerArgs(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) objects, objectLists := prepareFirstEventBatchPreparerArgs(gcName, test.gwNsName) @@ -112,7 +112,7 @@ func TestGetMetricsOptions(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) metricsServerOptions := getMetricsOptions(test.metricsConfig) diff --git a/internal/mode/static/nginx/config/execute_test.go b/internal/mode/static/nginx/config/execute_test.go index 9b3a4b5f2..0e452f7b6 100644 --- a/internal/mode/static/nginx/config/execute_test.go +++ b/internal/mode/static/nginx/config/execute_test.go @@ -3,27 +3,24 @@ package config import ( "testing" + . "github.com/onsi/gomega" + "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" ) func TestExecute(t *testing.T) { + g := NewWithT(t) defer func() { - if r := recover(); r != nil { - t.Errorf("execute() panicked with %v", r) - } + g.Expect(recover()).Should(BeNil()) }() - bytes := execute(serversTemplate, []http.Server{}) - if len(bytes) == 0 { - t.Error("template.execute() did not generate anything") - } + g.Expect(bytes).ToNot(BeEmpty()) } func TestExecutePanics(t *testing.T) { defer func() { - if r := recover(); r == nil { - t.Error("template.execute() did not panic") - } + g := NewWithT(t) + g.Expect(recover()).ShouldNot(BeNil()) }() _ = execute(serversTemplate, "not-correct-data") diff --git a/internal/mode/static/nginx/config/generator_test.go b/internal/mode/static/nginx/config/generator_test.go index 983185d2c..465a1d5fe 100644 --- a/internal/mode/static/nginx/config/generator_test.go +++ b/internal/mode/static/nginx/config/generator_test.go @@ -60,7 +60,7 @@ func TestGenerate(t *testing.T) { }, }, } - g := NewGomegaWithT(t) + g := NewWithT(t) generator := config.NewGeneratorImpl() diff --git a/internal/mode/static/nginx/config/maps_test.go b/internal/mode/static/nginx/config/maps_test.go index 96a4dae9f..b6ac952a8 100644 --- a/internal/mode/static/nginx/config/maps_test.go +++ b/internal/mode/static/nginx/config/maps_test.go @@ -11,7 +11,7 @@ import ( ) func TestExecuteMaps(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) pathRules := []dataplane.PathRule{ { MatchRules: []dataplane.MatchRule{ @@ -95,7 +95,7 @@ func TestExecuteMaps(t *testing.T) { } func TestBuildAddHeaderMaps(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) pathRules := []dataplane.PathRule{ { MatchRules: []dataplane.MatchRule{ diff --git a/internal/mode/static/nginx/config/servers_test.go b/internal/mode/static/nginx/config/servers_test.go index 84d10ebe6..1741be30f 100644 --- a/internal/mode/static/nginx/config/servers_test.go +++ b/internal/mode/static/nginx/config/servers_test.go @@ -62,17 +62,10 @@ func TestExecuteServers(t *testing.T) { "ssl_certificate /etc/nginx/secrets/test-keypair.pem;": 2, "ssl_certificate_key /etc/nginx/secrets/test-keypair.pem;": 2, } - + g := NewWithT(t) servers := string(executeServers(conf)) for expSubStr, expCount := range expSubStrings { - if expCount != strings.Count(servers, expSubStr) { - t.Errorf( - "executeServers() did not generate servers with substring %q %d times. Servers: %v", - expSubStr, - expCount, - servers, - ) - } + g.Expect(strings.Count(servers, expSubStr)).To(Equal(expCount)) } } @@ -145,7 +138,7 @@ func TestExecuteForDefaultServers(t *testing.T) { for _, tc := range testcases { t.Run(tc.msg, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) cfg := string(executeServers(tc.conf)) @@ -445,10 +438,9 @@ func TestCreateServers(t *testing.T) { } expectedMatchString := func(m []httpMatch) string { + g := NewWithT(t) b, err := json.Marshal(m) - if err != nil { - t.Errorf("error marshaling test match: %v", err) - } + g.Expect(err).ShouldNot(HaveOccurred()) return string(b) } @@ -661,7 +653,7 @@ func TestCreateServers(t *testing.T) { }, } - g := NewGomegaWithT(t) + g := NewWithT(t) result := createServers(httpServers, sslServers) g.Expect(helpers.Diff(expectedServers, result)).To(BeEmpty()) @@ -852,7 +844,7 @@ func TestCreateServersConflicts(t *testing.T) { }, } - g := NewGomegaWithT(t) + g := NewWithT(t) result := createServers(httpServers, []dataplane.VirtualServer{}) g.Expect(helpers.Diff(expectedServers, result)).To(BeEmpty()) @@ -861,7 +853,7 @@ func TestCreateServersConflicts(t *testing.T) { } func TestCreateLocationsRootPath(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) hrNsName := types.NamespacedName{Namespace: "test", Name: "route1"} @@ -972,8 +964,10 @@ func TestCreateLocationsRootPath(t *testing.T) { } for _, test := range tests { - locs := createLocations(test.pathRules, 80) - g.Expect(locs).To(Equal(test.expLocations), fmt.Sprintf("test case: %s", test.name)) + t.Run(test.name, func(t *testing.T) { + locs := createLocations(test.pathRules, 80) + g.Expect(locs).To(Equal(test.expLocations)) + }) } } @@ -1100,7 +1094,7 @@ func TestCreateReturnValForRedirectFilter(t *testing.T) { for _, test := range tests { t.Run(test.msg, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := createReturnValForRedirectFilter(test.filter, test.listenerPort) g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty()) @@ -1259,7 +1253,7 @@ func TestCreateHTTPMatch(t *testing.T) { } for _, tc := range tests { t.Run(tc.msg, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := createHTTPMatch(tc.match, testPath) g.Expect(helpers.Diff(result, tc.expected)).To(BeEmpty()) @@ -1268,7 +1262,7 @@ func TestCreateHTTPMatch(t *testing.T) { } func TestCreateQueryParamKeyValString(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) expected := "key=value" @@ -1294,7 +1288,7 @@ func TestCreateQueryParamKeyValString(t *testing.T) { } func TestCreateHeaderKeyValString(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) expected := "kEy:vALUe" @@ -1354,7 +1348,7 @@ func TestIsPathOnlyMatch(t *testing.T) { for _, tc := range tests { t.Run(tc.msg, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := isPathOnlyMatch(tc.match) g.Expect(result).To(Equal(tc.expected)) @@ -1363,7 +1357,7 @@ func TestIsPathOnlyMatch(t *testing.T) { } func TestCreateProxyPass(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) tests := []struct { expected string @@ -1408,7 +1402,7 @@ func TestCreateProxyPass(t *testing.T) { } func TestCreateMatchLocation(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) expected := http.Location{ Path: "/path", @@ -1420,7 +1414,7 @@ func TestCreateMatchLocation(t *testing.T) { } func TestCreatePathForMatch(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) tests := []struct { expected string @@ -1444,7 +1438,7 @@ func TestCreatePathForMatch(t *testing.T) { } func TestGenerateProxySetHeaders(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) filters := dataplane.HTTPHeaderFilter{ Add: []dataplane.HTTPHeader{ diff --git a/internal/mode/static/nginx/config/split_clients_test.go b/internal/mode/static/nginx/config/split_clients_test.go index 0396c5932..685de1a28 100644 --- a/internal/mode/static/nginx/config/split_clients_test.go +++ b/internal/mode/static/nginx/config/split_clients_test.go @@ -1,10 +1,9 @@ package config import ( - "strings" "testing" - "github.com/google/go-cmp/cmp" + . "github.com/onsi/gomega" "k8s.io/apimachinery/pkg/types" "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" @@ -97,29 +96,18 @@ func TestExecuteSplitClients(t *testing.T) { } for _, test := range tests { - sc := string(executeSplitClients(dataplane.Configuration{BackendGroups: test.backendGroups})) + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + sc := string(executeSplitClients(dataplane.Configuration{BackendGroups: test.backendGroups})) - for _, expSubString := range test.expStrings { - if !strings.Contains(sc, expSubString) { - t.Errorf( - "executeSplitClients() did not generate split clients with substring %q for test %q. Got: %v", - expSubString, - test.msg, - sc, - ) + for _, expSubString := range test.expStrings { + g.Expect(sc).To(ContainSubstring(expSubString)) } - } - for _, notExpString := range test.notExpStrings { - if strings.Contains(sc, notExpString) { - t.Errorf( - "executeSplitClients() generated split clients with unexpected substring %q for test %q. Got: %v", - notExpString, - test.msg, - sc, - ) + for _, notExpString := range test.notExpStrings { + g.Expect(sc).ToNot(ContainSubstring(notExpString)) } - } + }) } } @@ -249,10 +237,11 @@ func TestCreateSplitClients(t *testing.T) { } for _, test := range tests { - result := createSplitClients(test.backendGroups) - if diff := cmp.Diff(test.expSplitClients, result); diff != "" { - t.Errorf("createSplitClients() mismatch for %q (-want +got):\n%s", test.msg, diff) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + result := createSplitClients(test.backendGroups) + g.Expect(result).To(Equal(test.expSplitClients)) + }) } } @@ -395,10 +384,11 @@ func TestCreateSplitClientDistributions(t *testing.T) { } for _, test := range tests { - result := createSplitClientDistributions(dataplane.BackendGroup{Backends: test.backends}) - if diff := cmp.Diff(test.expDistributions, result); diff != "" { - t.Errorf("createSplitClientDistributions() mismatch for %q (-want +got):\n%s", test.msg, diff) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + result := createSplitClientDistributions(dataplane.BackendGroup{Backends: test.backends}) + g.Expect(result).To(Equal(test.expDistributions)) + }) } } @@ -427,13 +417,11 @@ func TestGetSplitClientValue(t *testing.T) { } for _, test := range tests { - result := getSplitClientValue(test.backend) - if result != test.expValue { - t.Errorf( - "getSplitClientValue() mismatch for %q; expected %s, got %s", - test.msg, test.expValue, result, - ) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + result := getSplitClientValue(test.backend) + g.Expect(result).To(Equal(test.expValue)) + }) } } @@ -501,13 +489,11 @@ func TestPercentOf(t *testing.T) { } for _, test := range tests { - percent := percentOf(test.weight, test.totalWeight) - if percent != test.expPercent { - t.Errorf( - "percentOf() mismatch for test %q; expected %f, got %f", - test.msg, test.expPercent, percent, - ) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + percent := percentOf(test.weight, test.totalWeight) + g.Expect(percent).To(Equal(test.expPercent)) + }) } } @@ -584,14 +570,15 @@ func TestBackendGroupNeedsSplit(t *testing.T) { } for _, test := range tests { - bg := dataplane.BackendGroup{ - Source: types.NamespacedName{Namespace: "test", Name: "hr"}, - Backends: test.backends, - } - result := backendGroupNeedsSplit(bg) - if result != test.expSplit { - t.Errorf("backendGroupNeedsSplit() mismatch for %q; expected %t", test.msg, result) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + bg := dataplane.BackendGroup{ + Source: types.NamespacedName{Namespace: "test", Name: "hr"}, + Backends: test.backends, + } + result := backendGroupNeedsSplit(bg) + g.Expect(result).To(Equal(test.expSplit)) + }) } } @@ -679,14 +666,15 @@ func TestBackendGroupName(t *testing.T) { } for _, test := range tests { - bg := dataplane.BackendGroup{ - Source: types.NamespacedName{Namespace: "test", Name: "hr"}, - RuleIdx: 0, - Backends: test.backends, - } - result := backendGroupName(bg) - if result != test.expName { - t.Errorf("backendGroupName() mismatch for %q; expected %s, got %s", test.msg, test.expName, result) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + bg := dataplane.BackendGroup{ + Source: types.NamespacedName{Namespace: "test", Name: "hr"}, + RuleIdx: 0, + Backends: test.backends, + } + result := backendGroupName(bg) + g.Expect(result).To(Equal(test.expName)) + }) } } diff --git a/internal/mode/static/nginx/config/upstreams_test.go b/internal/mode/static/nginx/config/upstreams_test.go index 79cc381f8..c24a5820f 100644 --- a/internal/mode/static/nginx/config/upstreams_test.go +++ b/internal/mode/static/nginx/config/upstreams_test.go @@ -1,10 +1,9 @@ package config import ( - "strings" "testing" - "github.com/google/go-cmp/cmp" + . "github.com/onsi/gomega" "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http" "github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane" @@ -48,14 +47,9 @@ func TestExecuteUpstreams(t *testing.T) { } upstreams := string(executeUpstreams(dataplane.Configuration{Upstreams: stateUpstreams})) + g := NewWithT(t) for _, expSubString := range expectedSubStrings { - if !strings.Contains(upstreams, expSubString) { - t.Errorf( - "executeUpstreams() did not generate upstreams with expected substring %q, got %q", - expSubString, - upstreams, - ) - } + g.Expect(upstreams).To(ContainSubstring(expSubString)) } } @@ -134,10 +128,9 @@ func TestCreateUpstreams(t *testing.T) { }, } + g := NewWithT(t) result := createUpstreams(stateUpstreams) - if diff := cmp.Diff(expUpstreams, result); diff != "" { - t.Errorf("createUpstreams() mismatch (-want +got):\n%s", diff) - } + g.Expect(result).To(Equal(expUpstreams)) } func TestCreateUpstream(t *testing.T) { @@ -213,9 +206,10 @@ func TestCreateUpstream(t *testing.T) { } for _, test := range tests { - result := createUpstream(test.stateUpstream) - if diff := cmp.Diff(test.expectedUpstream, result); diff != "" { - t.Errorf("createUpstream() %q mismatch (-want +got):\n%s", test.msg, diff) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + result := createUpstream(test.stateUpstream) + g.Expect(result).To(Equal(test.expectedUpstream)) + }) } } diff --git a/internal/mode/static/nginx/config/validation/framework_test.go b/internal/mode/static/nginx/config/validation/framework_test.go index d23cbe0f7..6b5c64117 100644 --- a/internal/mode/static/nginx/config/validation/framework_test.go +++ b/internal/mode/static/nginx/config/validation/framework_test.go @@ -14,7 +14,7 @@ type supportedValuesValidatorFunc[T configValue] func(v T) (bool, []string) func runValidatorTests[T configValue](t *testing.T, run func(g *WithT, v T), caseNamePrefix string, values ...T) { for i, v := range values { t.Run(fmt.Sprintf("%s_case_#%d", caseNamePrefix, i), func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) run(g, v) }) } @@ -136,7 +136,7 @@ func TestGetSortedKeysAsString(t *testing.T) { expected := []string{"value1", "value2", "value3"} - g := NewGomegaWithT(t) + g := NewWithT(t) result := getSortedKeysAsString(values) g.Expect(result).To(Equal(expected)) diff --git a/internal/mode/static/nginx/config/variable_names_test.go b/internal/mode/static/nginx/config/variable_names_test.go index 1e1843fc2..acaebe9aa 100644 --- a/internal/mode/static/nginx/config/variable_names_test.go +++ b/internal/mode/static/nginx/config/variable_names_test.go @@ -24,19 +24,14 @@ func TestConvertStringToSafeVariableName(t *testing.T) { }, } for _, test := range tests { - if result := convertStringToSafeVariableName(test.s); result != test.expected { - t.Errorf( - "convertStringToSafeVariableName() mismatch for test %q; expected %s, got %s", - test.msg, - test.expected, - result, - ) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + g.Expect(convertStringToSafeVariableName(test.s)).To(Equal(test.expected)) + }) } } func TestGenerateAddHeaderMapVariableName(t *testing.T) { - g := NewGomegaWithT(t) tests := []struct { msg string headerName string @@ -53,8 +48,11 @@ func TestGenerateAddHeaderMapVariableName(t *testing.T) { expected: "my_cool_header_header_var", }, } - for _, tc := range tests { - actual := generateAddHeaderMapVariableName(tc.headerName) - g.Expect(actual).To(Equal(tc.expected)) + for _, test := range tests { + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + actual := generateAddHeaderMapVariableName(test.headerName) + g.Expect(actual).To(Equal(test.expected)) + }) } } diff --git a/internal/mode/static/nginx/file/folders_test.go b/internal/mode/static/nginx/file/folders_test.go index a04beba58..67fa0d12f 100644 --- a/internal/mode/static/nginx/file/folders_test.go +++ b/internal/mode/static/nginx/file/folders_test.go @@ -14,15 +14,14 @@ import ( func writeFile(t *testing.T, name string, data []byte) { t.Helper() + g := NewWithT(t) //nolint:gosec // the file permission is ok for unit testing - if err := os.WriteFile(name, data, 0o644); err != nil { - t.Fatal(err) - } + g.Expect(os.WriteFile(name, data, 0o644)).To(Succeed()) } func TestClearFoldersRemoves(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) tempDir := t.TempDir() @@ -79,7 +78,7 @@ func TestClearFoldersFails(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) removedFiles, err := file.ClearFolders(test.fileMgr, files) diff --git a/internal/mode/static/sort/sort_test.go b/internal/mode/static/sort/sort_test.go index c5fbb5975..6379309f6 100644 --- a/internal/mode/static/sort/sort_test.go +++ b/internal/mode/static/sort/sort_test.go @@ -79,7 +79,7 @@ func TestLessObjectMeta(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := LessObjectMeta(test.meta1, test.meta2) invertedResult := LessObjectMeta(test.meta2, test.meta1) diff --git a/internal/mode/static/state/conditions/conditions_test.go b/internal/mode/static/state/conditions/conditions_test.go index f1067899c..2f7aea48b 100644 --- a/internal/mode/static/state/conditions/conditions_test.go +++ b/internal/mode/static/state/conditions/conditions_test.go @@ -10,7 +10,7 @@ import ( ) func TestDeduplicateConditions(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) conds := []conditions.Condition{ { diff --git a/internal/mode/static/state/dataplane/configuration_test.go b/internal/mode/static/state/dataplane/configuration_test.go index cff5bc859..4e8ecc3c6 100644 --- a/internal/mode/static/state/dataplane/configuration_test.go +++ b/internal/mode/static/state/dataplane/configuration_test.go @@ -1486,7 +1486,7 @@ func TestBuildConfiguration(t *testing.T) { for _, test := range tests { t.Run(test.msg, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := BuildConfiguration(context.TODO(), test.graph, fakeResolver, 1) @@ -1529,10 +1529,11 @@ func TestGetPath(t *testing.T) { } for _, test := range tests { - result := getPath(test.path) - if result != test.expected { - t.Errorf("getPath() returned %q but expected %q for the case of %q", result, test.expected, test.msg) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + result := getPath(test.path) + g.Expect(result).To(Equal(test.expected)) + }) } } @@ -1642,7 +1643,7 @@ func TestCreateFilters(t *testing.T) { for _, test := range tests { t.Run(test.msg, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := createHTTPFilters(test.filters) g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty()) @@ -1677,15 +1678,11 @@ func TestGetListenerHostname(t *testing.T) { } for _, test := range tests { - result := getListenerHostname(test.hostname) - if result != test.expected { - t.Errorf( - "getListenerHostname() returned %q but expected %q for the case of %q", - result, - test.expected, - test.msg, - ) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + result := getListenerHostname(test.hostname) + g.Expect(result).To(Equal(test.expected)) + }) } } @@ -1871,7 +1868,7 @@ func TestBuildUpstreams(t *testing.T) { } }) - g := NewGomegaWithT(t) + g := NewWithT(t) upstreams := buildUpstreams(context.TODO(), listeners, fakeResolver) g.Expect(upstreams).To(ConsistOf(expUpstreams)) @@ -1942,7 +1939,7 @@ func TestBuildBackendGroups(t *testing.T) { hrNoBackends, } - g := NewGomegaWithT(t) + g := NewWithT(t) result := buildBackendGroups(servers) @@ -2002,7 +1999,7 @@ func TestHostnameMoreSpecific(t *testing.T) { for _, tc := range tests { t.Run(tc.msg, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) g.Expect(listenerHostnameMoreSpecific(tc.host1, tc.host2)).To(Equal(tc.host1Wins)) }) diff --git a/internal/mode/static/state/dataplane/convert_test.go b/internal/mode/static/state/dataplane/convert_test.go index 1d167b978..19ea4bf23 100644 --- a/internal/mode/static/state/dataplane/convert_test.go +++ b/internal/mode/static/state/dataplane/convert_test.go @@ -115,7 +115,7 @@ func TestConvertMatch(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := convertMatch(test.match) g.Expect(helpers.Diff(result, test.expected)).To(BeEmpty()) @@ -153,7 +153,7 @@ func TestConvertHTTPRequestRedirectFilter(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := convertHTTPRequestRedirectFilter(test.filter) g.Expect(result).To(Equal(test.expected)) @@ -201,7 +201,7 @@ func TestConvertHTTPHeaderFilter(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := convertHTTPHeaderFilter(test.filter) g.Expect(result).To(Equal(test.expected)) @@ -210,7 +210,7 @@ func TestConvertHTTPHeaderFilter(t *testing.T) { } func TestConvertPathType(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) tests := []struct { pathType v1beta1.PathMatchType diff --git a/internal/mode/static/state/dataplane/sort_test.go b/internal/mode/static/state/dataplane/sort_test.go index ab8ad5ce5..edaca77da 100644 --- a/internal/mode/static/state/dataplane/sort_test.go +++ b/internal/mode/static/state/dataplane/sort_test.go @@ -158,6 +158,6 @@ func TestSort(t *testing.T) { sortMatchRules(rules) - g := NewGomegaWithT(t) + g := NewWithT(t) g.Expect(cmp.Diff(sortedRules, rules)).To(BeEmpty()) } diff --git a/internal/mode/static/state/graph/backend_refs_test.go b/internal/mode/static/state/graph/backend_refs_test.go index d4fa16b4c..2cd03de6d 100644 --- a/internal/mode/static/state/graph/backend_refs_test.go +++ b/internal/mode/static/state/graph/backend_refs_test.go @@ -79,7 +79,7 @@ func TestValidateHTTPBackendRef(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) resolver := newReferenceGrantResolver(nil) valid, cond := validateHTTPBackendRef(test.ref, "test", resolver, field.NewPath("test")) @@ -210,7 +210,7 @@ func TestValidateBackendRef(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) resolver := newReferenceGrantResolver(test.refGrants) valid, cond := validateBackendRef(test.ref, "test", resolver, field.NewPath("test")) @@ -225,7 +225,7 @@ func TestValidateWeight(t *testing.T) { validWeights := []int32{0, 1, 1000000} invalidWeights := []int32{-1, 1000001} - g := NewGomegaWithT(t) + g := NewWithT(t) for _, w := range validWeights { err := validateWeight(w) @@ -284,7 +284,7 @@ func TestGetServiceAndPortFromRef(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) svc, port, err := getServiceAndPortFromRef(test.ref, "test", services, refPath) @@ -488,7 +488,7 @@ func TestAddBackendRefsToRulesTest(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) resolver := newReferenceGrantResolver(nil) addBackendRefsToRules(test.route, resolver, services) @@ -616,7 +616,7 @@ func TestCreateBackend(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) resolver := newReferenceGrantResolver(nil) backend, cond := createBackendRef(test.ref, sourceNamespace, resolver, services, refPath) diff --git a/internal/mode/static/state/graph/gateway_listener_test.go b/internal/mode/static/state/graph/gateway_listener_test.go index f2c1e3bba..ed4689e1a 100644 --- a/internal/mode/static/state/graph/gateway_listener_test.go +++ b/internal/mode/static/state/graph/gateway_listener_test.go @@ -48,7 +48,7 @@ func TestValidateHTTPListener(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) v := createHTTPListenerValidator(protectedPorts) @@ -191,7 +191,7 @@ func TestValidateHTTPSListener(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) v := createHTTPSListenerValidator(protectedPorts) @@ -236,7 +236,7 @@ func TestValidateListenerHostname(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) conds := validateListenerHostname(v1beta1.Listener{Hostname: test.hostname}) @@ -339,7 +339,7 @@ func TestGetAndValidateListenerSupportedKinds(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) listener := v1beta1.Listener{ Protocol: test.protocol, @@ -391,7 +391,7 @@ func TestValidateListenerLabelSelector(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) // create iteration variable inside the loop to fix implicit memory aliasing from := test.from diff --git a/internal/mode/static/state/graph/gateway_test.go b/internal/mode/static/state/graph/gateway_test.go index 8cfebea0e..f500c74cf 100644 --- a/internal/mode/static/state/graph/gateway_test.go +++ b/internal/mode/static/state/graph/gateway_test.go @@ -57,7 +57,7 @@ func TestProcessedGatewaysGetAllNsNames(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := test.gws.GetAllNsNames() g.Expect(result).To(Equal(test.expected)) }) @@ -132,7 +132,7 @@ func TestProcessGateways(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := processGateways(test.gws, gcName) g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty()) }) @@ -861,7 +861,7 @@ func TestBuildGateway(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) resolver := newReferenceGrantResolver(test.refGrants) result := buildGateway(test.gateway, secretResolver, test.gatewayClass, resolver, protectedPorts) g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty()) diff --git a/internal/mode/static/state/graph/gatewayclass_test.go b/internal/mode/static/state/graph/gatewayclass_test.go index 789cbe100..0ec15001d 100644 --- a/internal/mode/static/state/graph/gatewayclass_test.go +++ b/internal/mode/static/state/graph/gatewayclass_test.go @@ -110,7 +110,7 @@ func TestProcessGatewayClasses(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result, exists := processGatewayClasses(test.gcs, gcName, ctlrName) g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty()) g.Expect(exists).To(Equal(test.exists)) @@ -162,7 +162,7 @@ func TestBuildGatewayClass(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := buildGatewayClass(test.gc) g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty()) diff --git a/internal/mode/static/state/graph/graph_test.go b/internal/mode/static/state/graph/graph_test.go index efe1bc5c2..d45445d5d 100644 --- a/internal/mode/static/state/graph/graph_test.go +++ b/internal/mode/static/state/graph/graph_test.go @@ -340,7 +340,7 @@ func TestBuildGraph(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) result := BuildGraph( test.store, diff --git a/internal/mode/static/state/graph/httproute_test.go b/internal/mode/static/state/graph/httproute_test.go index b65476593..557ac1b9e 100644 --- a/internal/mode/static/state/graph/httproute_test.go +++ b/internal/mode/static/state/graph/httproute_test.go @@ -4,7 +4,6 @@ import ( "errors" "testing" - "github.com/google/go-cmp/cmp" . "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -128,7 +127,7 @@ func TestBuildRoutes(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) routes := buildRoutesForGateways(validator, hrRoutes, test.gwNsNames) g.Expect(helpers.Diff(test.expected, routes)).To(BeEmpty()) }) @@ -189,7 +188,7 @@ func TestBuildSectionNameRefs(t *testing.T) { }, } - g := NewGomegaWithT(t) + g := NewWithT(t) result := buildSectionNameRefs(parentRefs, routeNamespace, gwNsNames) g.Expect(result).To(Equal(expected)) @@ -234,7 +233,7 @@ func TestBuildSectionNameRefsPanicsForDuplicateParentRefs(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) run := func() { buildSectionNameRefs(test.parentRefs, gwNsName.Namespace, gwNsNames) } g.Expect(run).To(Panic()) }) @@ -317,7 +316,7 @@ func TestFindGatewayForParentRef(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) gw, found := findGatewayForParentRef(test.ref, routeNamespace, gwNsNames) g.Expect(found).To(Equal(test.expectedFound)) @@ -526,7 +525,7 @@ func TestBuildRoute(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) route := buildRoute(test.validator, test.hr, gatewayNsNames) g.Expect(helpers.Diff(test.expected, route)).To(BeEmpty()) @@ -1208,7 +1207,7 @@ func TestBindRouteToListeners(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) bindRouteToListeners(test.route, test.gateway, namespaces) @@ -1293,10 +1292,11 @@ func TestFindAcceptedHostnames(t *testing.T) { } for _, test := range tests { - result := findAcceptedHostnames(test.listenerHostname, test.routeHostnames) - if diff := cmp.Diff(test.expected, result); diff != "" { - t.Errorf("findAcceptedHostnames() %q mismatch (-want +got):\n%s", test.msg, diff) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + result := findAcceptedHostnames(test.listenerHostname, test.routeHostnames) + g.Expect(result).To(Equal(test.expected)) + }) } } @@ -1327,10 +1327,11 @@ func TestGetHostname(t *testing.T) { } for _, test := range tests { - result := getHostname(test.h) - if result != test.expected { - t.Errorf("getHostname() returned %q but expected %q for the case of %q", result, test.expected, test.msg) - } + t.Run(test.msg, func(t *testing.T) { + g := NewWithT(t) + result := getHostname(test.h) + g.Expect(result).To(Equal(test.expected)) + }) } } @@ -1365,7 +1366,7 @@ func TestValidateHostnames(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) err := validateHostnames(test.hostnames, path) @@ -1623,7 +1624,7 @@ func TestValidateMatch(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) allErrs := validateMatch(test.validator, test.match, field.NewPath("test")) g.Expect(allErrs).To(HaveLen(test.expectErrCount)) }) @@ -1665,7 +1666,7 @@ func TestValidateFilter(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) allErrs := validateFilter(&validationfakes.FakeHTTPFieldsValidator{}, test.filter, filterPath) g.Expect(allErrs).To(HaveLen(test.expectErrCount)) }) @@ -1811,7 +1812,7 @@ func TestValidateFilterRedirect(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) allErrs := validateFilterRedirect(test.validator, test.filter, filterPath) g.Expect(allErrs).To(HaveLen(test.expectErrCount)) }) @@ -1925,7 +1926,7 @@ func TestValidateFilterRequestHeaderModifier(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) allErrs := validateFilterHeaderModifier(test.validator, test.filter, filterPath) g.Expect(allErrs).To(HaveLen(test.expectErrCount)) }) diff --git a/internal/mode/static/state/graph/reference_grant_test.go b/internal/mode/static/state/graph/reference_grant_test.go index 7e65ae7b1..eba07e3de 100644 --- a/internal/mode/static/state/graph/reference_grant_test.go +++ b/internal/mode/static/state/graph/reference_grant_test.go @@ -160,7 +160,7 @@ func TestReferenceGrantResolver(t *testing.T) { for _, test := range tests { t.Run(test.msg, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) g.Expect(resolver.refAllowed(test.to, test.from)).To(Equal(test.allowed)) }) @@ -176,7 +176,7 @@ func TestToSecret(t *testing.T) { name: "secret", } - g := NewGomegaWithT(t) + g := NewWithT(t) g.Expect(ref).To(Equal(exp)) } @@ -189,7 +189,7 @@ func TestToService(t *testing.T) { name: "service", } - g := NewGomegaWithT(t) + g := NewWithT(t) g.Expect(ref).To(Equal(exp)) } @@ -202,7 +202,7 @@ func TestFromGateway(t *testing.T) { namespace: "ns", } - g := NewGomegaWithT(t) + g := NewWithT(t) g.Expect(ref).To(Equal(exp)) } @@ -215,6 +215,6 @@ func TestFromHTTPRoute(t *testing.T) { namespace: "ns", } - g := NewGomegaWithT(t) + g := NewWithT(t) g.Expect(ref).To(Equal(exp)) } diff --git a/internal/mode/static/state/graph/secret_test.go b/internal/mode/static/state/graph/secret_test.go index 72e4a5f7b..81aa59503 100644 --- a/internal/mode/static/state/graph/secret_test.go +++ b/internal/mode/static/state/graph/secret_test.go @@ -186,7 +186,7 @@ func TestSecretResolver(t *testing.T) { // Not running tests with t.Run(...) because the last one (getResolvedSecrets) depends on the execution of // all cases. - g := NewGomegaWithT(t) + g := NewWithT(t) for _, test := range tests { err := resolver.resolve(test.nsname) diff --git a/internal/mode/static/state/graph/validation_test.go b/internal/mode/static/state/graph/validation_test.go index c680aa2e4..d364adcf8 100644 --- a/internal/mode/static/state/graph/validation_test.go +++ b/internal/mode/static/state/graph/validation_test.go @@ -41,7 +41,7 @@ func TestValidateHostname(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) err := validateHostname(test.hostname) diff --git a/internal/mode/static/state/relationship/relationships_test.go b/internal/mode/static/state/relationship/relationships_test.go index 9d797cf3f..74d6f3f88 100644 --- a/internal/mode/static/state/relationship/relationships_test.go +++ b/internal/mode/static/state/relationship/relationships_test.go @@ -3,7 +3,7 @@ package relationship import ( "testing" - "github.com/google/go-cmp/cmp" + . "github.com/onsi/gomega" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/gateway-api/apis/v1beta1" @@ -113,10 +113,10 @@ func TestGetBackendServiceNamesFromRoute(t *testing.T) { {Namespace: "test", Name: "multiple-refs"}: {}, {Namespace: "test", Name: "multiple-refs2"}: {}, } + + g := NewWithT(t) names := getBackendServiceNamesFromRoute(hr) - if diff := cmp.Diff(expNames, names); diff != "" { - t.Errorf("getBackendServiceNamesFromRoute() mismatch (-want +got):\n%s", diff) - } + g.Expect(names).To(Equal(expNames)) } func TestCapturerImpl_DecrementRouteCount(t *testing.T) { @@ -150,6 +150,7 @@ func TestCapturerImpl_DecrementRouteCount(t *testing.T) { svc := types.NamespacedName{Namespace: "test", Name: "svc"} for _, tc := range testcases { + g := NewWithT(t) if tc.startingRefCount > 0 { capturer.serviceRefCount[svc] = tc.startingRefCount } @@ -157,17 +158,7 @@ func TestCapturerImpl_DecrementRouteCount(t *testing.T) { capturer.decrementRefCount(svc) count, exists := capturer.serviceRefCount[svc] - if tc.exists != exists { - t.Errorf("decrementRefCount() test case %q expected exists to be %t", tc.msg, tc.exists) - } - - if tc.expectedRefCount != count { - t.Errorf( - "decrementRefCount() test case %q expected ref count to be %d, got %d", - tc.msg, - tc.expectedRefCount, - count, - ) - } + g.Expect(exists).To(Equal(tc.exists)) + g.Expect(count).To(Equal(tc.expectedRefCount)) } } diff --git a/internal/mode/static/state/resolver/resolver_test.go b/internal/mode/static/state/resolver/resolver_test.go index f40254ce0..bf20e37d5 100644 --- a/internal/mode/static/state/resolver/resolver_test.go +++ b/internal/mode/static/state/resolver/resolver_test.go @@ -4,7 +4,6 @@ import ( "fmt" "testing" - "github.com/google/go-cmp/cmp" . "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" discoveryV1 "k8s.io/api/discovery/v1" @@ -102,9 +101,8 @@ func TestFilterEndpointSliceList(t *testing.T) { expFilteredList := []discoveryV1.EndpointSlice{validEndpointSlice, mixedValidityEndpointSlice} filteredSliceList := filterEndpointSliceList(sliceList, svcPort) - if diff := cmp.Diff(expFilteredList, filteredSliceList); diff != "" { - t.Errorf("filterEndpointSliceList() mismatch (-want +got):\n%s", diff) - } + g := NewWithT(t) + g.Expect(filteredSliceList).To(Equal(expFilteredList)) } func TestGetServicePort(t *testing.T) { @@ -124,25 +122,18 @@ func TestGetServicePort(t *testing.T) { }, } + g := NewWithT(t) // ports exist for _, p := range []int32{80, 81, 82} { port, err := getServicePort(svc, p) - if err != nil { - t.Errorf("getServicePort() returned an error for port %d: %v", p, err) - } - if port.Port != p { - t.Errorf("getServicePort() returned the wrong port for port %d; expected %d, got %d", p, p, port.Port) - } + g.Expect(err).ShouldNot(HaveOccurred()) + g.Expect(port.Port).To(Equal(p)) } // port doesn't exist port, err := getServicePort(svc, 83) - if err == nil { - t.Errorf("getServicePort() didn't return an error for port 83") - } - if port.Port != 0 { - t.Errorf("getServicePort() returned the wrong port for port 83; expected 0, got %d", port.Port) - } + g.Expect(err).Should(HaveOccurred()) + g.Expect(port.Port).To(Equal(int32(0))) } func TestGetDefaultPort(t *testing.T) { @@ -176,11 +167,9 @@ func TestGetDefaultPort(t *testing.T) { }, } for _, tc := range testcases { + g := NewWithT(t) port := getDefaultPort(tc.svcPort) - - if tc.expPort != port { - t.Errorf("getTargetPort() mismatch on port for %q; expected %d, got %d", tc.msg, tc.expPort, port) - } + g.Expect(port).To(Equal(tc.expPort)) } } @@ -287,9 +276,8 @@ func TestIgnoreEndpointSlice(t *testing.T) { }, } for _, tc := range testcases { - if ignoreEndpointSlice(tc.slice, tc.servicePort) != tc.ignore { - t.Errorf("ignoreEndpointSlice() mismatch for %q; expected %t", tc.msg, tc.ignore) - } + g := NewWithT(t) + g.Expect(ignoreEndpointSlice(tc.slice, tc.servicePort)).To(Equal(tc.ignore)) } } @@ -328,9 +316,8 @@ func TestEndpointReady(t *testing.T) { }, } for _, tc := range testcases { - if endpointReady(tc.endpoint) != tc.ready { - t.Errorf("endpointReady() mismatch for %q; expected %t", tc.msg, tc.ready) - } + g := NewWithT(t) + g.Expect(endpointReady(tc.endpoint)).To(Equal(tc.ready)) } } @@ -460,21 +447,14 @@ func TestFindPort(t *testing.T) { }, } for _, tc := range testcases { + g := NewWithT(t) port := findPort(tc.ports, tc.svcPort) - - if port != tc.expPort { - t.Errorf( - "findPort() mismatch on %q; expected port %d; got port %d", - tc.msg, - tc.expPort, - port, - ) - } + g.Expect(port).To(Equal(tc.expPort)) } } func TestCalculateReadyEndpoints(t *testing.T) { - g := NewGomegaWithT(t) + g := NewWithT(t) slices := []discoveryV1.EndpointSlice{ {