Skip to content

Commit a39755d

Browse files
authored
Replace unit test assertions with Gomega matchers (#1046)
Problem: There were still some existing unit tests that weren't using the Gomega matcher library for assertions. Solution: Changed existing tests to use the Gomega matcher library, replaced deprecated NewGomegaWithT with NewWithT, and added sub-tests to existing tests.
1 parent dae5fac commit a39755d

40 files changed

+231
-306
lines changed

cmd/gateway/commands_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type flagTestCase struct {
1616
}
1717

1818
func testFlag(t *testing.T, cmd *cobra.Command, test flagTestCase) {
19-
g := NewGomegaWithT(t)
19+
g := NewWithT(t)
2020
// discard any output generated by cobra
2121
cmd.SetOut(io.Discard)
2222
cmd.SetErr(io.Discard)

cmd/gateway/validation_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestValidateGatewayControllerName(t *testing.T) {
5252

5353
for _, test := range tests {
5454
t.Run(test.name, func(t *testing.T) {
55-
g := NewGomegaWithT(t)
55+
g := NewWithT(t)
5656

5757
err := validateGatewayControllerName(test.value)
5858

@@ -115,7 +115,7 @@ func TestValidateResourceName(t *testing.T) {
115115

116116
for _, test := range tests {
117117
t.Run(test.name, func(t *testing.T) {
118-
g := NewGomegaWithT(t)
118+
g := NewWithT(t)
119119

120120
err := validateResourceName(test.value)
121121

@@ -178,7 +178,7 @@ func TestValidateNamespaceName(t *testing.T) {
178178

179179
for _, test := range tests {
180180
t.Run(test.name, func(t *testing.T) {
181-
g := NewGomegaWithT(t)
181+
g := NewWithT(t)
182182

183183
err := validateNamespaceName(test.value)
184184

@@ -240,7 +240,7 @@ func TestParseNamespacedResourceName(t *testing.T) {
240240

241241
for _, test := range tests {
242242
t.Run(test.name, func(t *testing.T) {
243-
g := NewGomegaWithT(t)
243+
g := NewWithT(t)
244244

245245
nsName, err := parseNamespacedResourceName(test.value)
246246

@@ -283,7 +283,7 @@ func TestValidateIP(t *testing.T) {
283283

284284
for _, tc := range tests {
285285
t.Run(tc.name, func(t *testing.T) {
286-
g := NewGomegaWithT(t)
286+
g := NewWithT(t)
287287

288288
err := validateIP(tc.ip)
289289
if !tc.expErr {
@@ -320,7 +320,7 @@ func TestValidatePort(t *testing.T) {
320320

321321
for _, tc := range tests {
322322
t.Run(tc.name, func(t *testing.T) {
323-
g := NewGomegaWithT(t)
323+
g := NewWithT(t)
324324

325325
err := validatePort(tc.port)
326326
if !tc.expErr {

conformance/tests/conformance_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
)
3636

3737
func TestConformance(t *testing.T) {
38-
g := NewGomegaWithT(t)
38+
g := NewWithT(t)
3939
cfg, err := config.GetConfig()
4040
g.Expect(err).To(BeNil())
4141

internal/framework/controller/filter/filter_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import (
1010
func TestCreateSingleResourceFilter(t *testing.T) {
1111
targetNsName := types.NamespacedName{Namespace: "test", Name: "resource"}
1212

13+
g := NewWithT(t)
1314
filter := CreateSingleResourceFilter(targetNsName)
14-
if filter == nil {
15-
t.Fatal("TestCreateSingleResourceFilter() returned nil filter")
16-
}
15+
g.Expect(filter).ToNot(BeNil())
1716

1817
const expectedMsg = "Resource is ignored because this controller only supports a single resource " +
1918
"test/resource of that type"
@@ -52,8 +51,7 @@ func TestCreateSingleResourceFilter(t *testing.T) {
5251

5352
for _, test := range tests {
5453
t.Run(test.name, func(t *testing.T) {
55-
g := NewGomegaWithT(t)
56-
54+
g := NewWithT(t)
5755
shouldProcess, msg := filter(test.nsname)
5856
g.Expect(shouldProcess).To(Equal(test.expectedShouldProcess))
5957
g.Expect(msg).To(Equal(test.expectedMsg))

internal/framework/controller/index/endpointslice_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package index
33
import (
44
"testing"
55

6-
"github.com/google/go-cmp/cmp"
6+
. "github.com/onsi/gomega"
77
v1 "k8s.io/api/core/v1"
88
discoveryV1 "k8s.io/api/discovery/v1"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -42,18 +42,16 @@ func TestServiceNameIndexFunc(t *testing.T) {
4242
}
4343

4444
for _, tc := range testcases {
45+
g := NewWithT(t)
4546
output := ServiceNameIndexFunc(tc.obj)
46-
if diff := cmp.Diff(tc.expOutput, output); diff != "" {
47-
t.Errorf("ServiceNameIndexFunc() mismatch on %q (-want +got):\n%s", tc.msg, diff)
48-
}
47+
g.Expect(output).To(Equal(tc.expOutput))
4948
}
5049
}
5150

5251
func TestServiceNameIndexFuncPanics(t *testing.T) {
5352
defer func() {
54-
if r := recover(); r == nil {
55-
t.Errorf("ServiceNameIndexFunc() did not panic")
56-
}
53+
g := NewWithT(t)
54+
g.Expect(recover()).ShouldNot(BeNil())
5755
}()
5856

5957
ServiceNameIndexFunc(&v1.Namespace{})

internal/framework/controller/predicate/gatewayclass_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestGatewayClassPredicate(t *testing.T) {
12-
g := NewGomegaWithT(t)
12+
g := NewWithT(t)
1313

1414
p := GatewayClassPredicate{ControllerName: "nginx-ctlr"}
1515

internal/framework/controller/predicate/service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func TestServicePortsChangedPredicate_Update(t *testing.T) {
226226

227227
for _, tc := range testcases {
228228
t.Run(tc.msg, func(t *testing.T) {
229-
g := NewGomegaWithT(t)
229+
g := NewWithT(t)
230230
update := p.Update(event.UpdateEvent{
231231
ObjectOld: tc.objectOld,
232232
ObjectNew: tc.objectNew,
@@ -238,7 +238,7 @@ func TestServicePortsChangedPredicate_Update(t *testing.T) {
238238
}
239239

240240
func TestServicePortsChangedPredicate(t *testing.T) {
241-
g := NewGomegaWithT(t)
241+
g := NewWithT(t)
242242

243243
p := ServicePortsChangedPredicate{}
244244

internal/framework/controller/register_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestRegister(t *testing.T) {
9898

9999
for _, test := range tests {
100100
t.Run(test.msg, func(t *testing.T) {
101-
g := NewGomegaWithT(t)
101+
g := NewWithT(t)
102102

103103
newReconciler := func(c controller.ReconcilerConfig) *controller.Reconciler {
104104
g.Expect(c.Getter).To(BeIdenticalTo(test.fakes.mgr.GetClient()))

internal/framework/events/events_test.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package events
33
import (
44
"testing"
55

6-
"github.com/google/go-cmp/cmp"
6+
. "github.com/onsi/gomega"
77
"sigs.k8s.io/controller-runtime/pkg/log/zap"
88
)
99

1010
func TestEventLoop_SwapBatches(t *testing.T) {
11+
g := NewWithT(t)
1112
eventLoop := NewEventLoop(nil, zap.New(), nil, nil)
1213

1314
eventLoop.currentBatch = EventBatch{
@@ -27,19 +28,8 @@ func TestEventLoop_SwapBatches(t *testing.T) {
2728

2829
eventLoop.swapBatches()
2930

30-
if l := len(eventLoop.currentBatch); l != 4 {
31-
t.Errorf("EventLoop.swapBatches() mismatch. Expected 4 events in the current batch, got %d", l)
32-
}
33-
34-
if diff := cmp.Diff(eventLoop.currentBatch, nextBatch); diff != "" {
35-
t.Errorf("EventLoop.swapBatches() mismatch on current batch events (-want +got):\n%s", diff)
36-
}
37-
38-
if l := len(eventLoop.nextBatch); l != 0 {
39-
t.Errorf("EventLoop.swapBatches() mismatch. Expected 0 events in the next batch, got %d", l)
40-
}
41-
42-
if c := cap(eventLoop.nextBatch); c != 3 {
43-
t.Errorf("EventLoop.swapBatches() mismatch. Expected capacity of 3 in the next batch, got %d", c)
44-
}
31+
g.Expect(len(eventLoop.currentBatch)).To(Equal(len(nextBatch)))
32+
g.Expect(eventLoop.currentBatch).To(Equal(nextBatch))
33+
g.Expect(len(eventLoop.nextBatch)).To(Equal(0))
34+
g.Expect(cap(eventLoop.nextBatch)).To(Equal(3))
4535
}

internal/framework/status/conditions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func CreateExpectedAPIConditions(
5454
}
5555

5656
func TestConvertRouteConditions(t *testing.T) {
57-
g := NewGomegaWithT(t)
57+
g := NewWithT(t)
5858

5959
var generation int64 = 1
6060
transitionTime := metav1.NewTime(time.Now())

internal/framework/status/gateway_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestPrepareGatewayStatus(t *testing.T) {
5252
Addresses: []v1beta1.GatewayStatusAddress{podIP},
5353
}
5454

55-
g := NewGomegaWithT(t)
55+
g := NewWithT(t)
5656

5757
result := prepareGatewayStatus(status, "1.2.3.4", transitionTime)
5858
g.Expect(helpers.Diff(expected, result)).To(BeEmpty())

internal/framework/status/gatewayclass_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestPrepareGatewayClassStatus(t *testing.T) {
2222
Conditions: CreateExpectedAPIConditions("Test", 1, transitionTime),
2323
}
2424

25-
g := NewGomegaWithT(t)
25+
g := NewWithT(t)
2626

2727
result := prepareGatewayClassStatus(status, transitionTime)
2828
g.Expect(helpers.Diff(expected, result)).To(BeEmpty())

internal/framework/status/httproute_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestPrepareHTTPRouteStatus(t *testing.T) {
6060
},
6161
}
6262

63-
g := NewGomegaWithT(t)
63+
g := NewWithT(t)
6464

6565
result := prepareHTTPRouteStatus(status, gatewayCtlrName, transitionTime)
6666
g.Expect(helpers.Diff(expected, result)).To(BeEmpty())

internal/mode/static/build_statuses_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestBuildStatuses(t *testing.T) {
193193
},
194194
}
195195

196-
g := NewGomegaWithT(t)
196+
g := NewWithT(t)
197197

198198
var nginxReloadRes nginxReloadResult
199199
result := buildStatuses(graph, nginxReloadRes)
@@ -285,7 +285,7 @@ func TestBuildStatusesNginxErr(t *testing.T) {
285285
},
286286
}
287287

288-
g := NewGomegaWithT(t)
288+
g := NewWithT(t)
289289

290290
nginxReloadRes := nginxReloadResult{error: errors.New("test error")}
291291
result := buildStatuses(graph, nginxReloadRes)
@@ -349,7 +349,7 @@ func TestBuildGatewayClassStatuses(t *testing.T) {
349349

350350
for _, test := range tests {
351351
t.Run(test.name, func(t *testing.T) {
352-
g := NewGomegaWithT(t)
352+
g := NewWithT(t)
353353

354354
result := buildGatewayClassStatuses(test.gc, test.ignoredClasses)
355355
g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty())
@@ -556,7 +556,7 @@ func TestBuildGatewayStatuses(t *testing.T) {
556556

557557
for _, test := range tests {
558558
t.Run(test.name, func(t *testing.T) {
559-
g := NewGomegaWithT(t)
559+
g := NewWithT(t)
560560

561561
result := buildGatewayStatuses(test.gateway, test.ignoredGateways, test.nginxReloadRes)
562562
g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty())

internal/mode/static/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestPrepareFirstEventBatchPreparerArgs(t *testing.T) {
6363

6464
for _, test := range tests {
6565
t.Run(test.name, func(t *testing.T) {
66-
g := NewGomegaWithT(t)
66+
g := NewWithT(t)
6767

6868
objects, objectLists := prepareFirstEventBatchPreparerArgs(gcName, test.gwNsName)
6969

@@ -112,7 +112,7 @@ func TestGetMetricsOptions(t *testing.T) {
112112

113113
for _, test := range tests {
114114
t.Run(test.name, func(t *testing.T) {
115-
g := NewGomegaWithT(t)
115+
g := NewWithT(t)
116116

117117
metricsServerOptions := getMetricsOptions(test.metricsConfig)
118118

internal/mode/static/nginx/config/execute_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,24 @@ package config
33
import (
44
"testing"
55

6+
. "github.com/onsi/gomega"
7+
68
"github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http"
79
)
810

911
func TestExecute(t *testing.T) {
12+
g := NewWithT(t)
1013
defer func() {
11-
if r := recover(); r != nil {
12-
t.Errorf("execute() panicked with %v", r)
13-
}
14+
g.Expect(recover()).Should(BeNil())
1415
}()
15-
1616
bytes := execute(serversTemplate, []http.Server{})
17-
if len(bytes) == 0 {
18-
t.Error("template.execute() did not generate anything")
19-
}
17+
g.Expect(bytes).ToNot(BeEmpty())
2018
}
2119

2220
func TestExecutePanics(t *testing.T) {
2321
defer func() {
24-
if r := recover(); r == nil {
25-
t.Error("template.execute() did not panic")
26-
}
22+
g := NewWithT(t)
23+
g.Expect(recover()).ShouldNot(BeNil())
2724
}()
2825

2926
_ = execute(serversTemplate, "not-correct-data")

internal/mode/static/nginx/config/generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestGenerate(t *testing.T) {
6060
},
6161
},
6262
}
63-
g := NewGomegaWithT(t)
63+
g := NewWithT(t)
6464

6565
generator := config.NewGeneratorImpl()
6666

internal/mode/static/nginx/config/maps_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestExecuteMaps(t *testing.T) {
14-
g := NewGomegaWithT(t)
14+
g := NewWithT(t)
1515
pathRules := []dataplane.PathRule{
1616
{
1717
MatchRules: []dataplane.MatchRule{
@@ -95,7 +95,7 @@ func TestExecuteMaps(t *testing.T) {
9595
}
9696

9797
func TestBuildAddHeaderMaps(t *testing.T) {
98-
g := NewGomegaWithT(t)
98+
g := NewWithT(t)
9999
pathRules := []dataplane.PathRule{
100100
{
101101
MatchRules: []dataplane.MatchRule{

0 commit comments

Comments
 (0)