Skip to content

Commit ad4b492

Browse files
committed
test: use gomega for asserts instead of testify to avoid extra dependency
Signed-off-by: Chris Privitere <23177737+cprivitere@users.noreply.github.com>
1 parent 34e472b commit ad4b492

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/pkg/errors v0.9.1
1111
github.com/spf13/cobra v1.8.0
1212
github.com/spf13/pflag v1.0.5
13+
github.com/stretchr/testify v1.9.0
1314
golang.org/x/oauth2 v0.18.0
1415
k8s.io/api v0.29.3
1516
k8s.io/apimachinery v0.29.3
@@ -58,6 +59,7 @@ require (
5859
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5960
github.com/modern-go/reflect2 v1.0.2 // indirect
6061
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
62+
github.com/pmezard/go-difflib v1.0.0 // indirect
6163
github.com/prometheus/client_golang v1.18.0 // indirect
6264
github.com/prometheus/client_model v0.5.0 // indirect
6365
github.com/prometheus/common v0.45.0 // indirect

internal/emlb/emlb_test.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,27 @@ package emlb
1919

2020
import (
2121
"os"
22-
"reflect"
2322
"testing"
2423

24+
. "github.com/onsi/gomega"
25+
2526
corev1 "k8s.io/api/core/v1"
2627
)
2728

2829
func Test_getResourceName(t *testing.T) {
30+
g := NewWithT(t)
2931
loadBalancerName := "my-loadbalancer"
3032
resourceType := "pool"
3133
want := "my-loadbalancer-pool"
3234

3335
got := getResourceName(loadBalancerName, resourceType)
3436

35-
if got != want {
36-
t.Errorf("getResourceName() = %v, want %v", got, want)
37-
}
37+
// assert name is correct
38+
g.Expect(got).To(Equal(want))
3839
}
3940

4041
func Test_checkDebugEnabled(t *testing.T) {
42+
g := NewWithT(t)
4143
// Set the PACKNGO_DEBUG environment variable to enable debug mode
4244
if err := os.Setenv("PACKNGO_DEBUG", "true"); err != nil {
4345
t.Errorf("Error testing checkDebugEnabled: %v", err)
@@ -47,9 +49,7 @@ func Test_checkDebugEnabled(t *testing.T) {
4749
debugEnabled := checkDebugEnabled()
4850

4951
// Check if debugEnabled is true
50-
if !debugEnabled {
51-
t.Errorf("checkDebugEnabled() = %v, want %v", debugEnabled, true)
52-
}
52+
g.Expect(debugEnabled).To(BeTrue())
5353

5454
// Unset the PACKNGO_DEBUG environment variable
5555
os.Unsetenv("PACKNGO_DEBUG")
@@ -58,9 +58,7 @@ func Test_checkDebugEnabled(t *testing.T) {
5858
debugEnabled = checkDebugEnabled()
5959

6060
// Check if debugEnabled is false
61-
if debugEnabled {
62-
t.Errorf("checkDebugEnabled() = %v, want %v", debugEnabled, false)
63-
}
61+
g.Expect(debugEnabled).To(BeFalse())
6462
}
6563

6664
func Test_convertToTarget(t *testing.T) {
@@ -114,9 +112,9 @@ func Test_convertToTarget(t *testing.T) {
114112
}
115113
for _, tt := range tests {
116114
t.Run(tt.name, func(t *testing.T) {
117-
if got := convertToTarget(tt.args.devaddr); !reflect.DeepEqual(got, tt.want) {
118-
t.Errorf("convertToTarget() = %v, want %v", got, tt.want)
119-
}
115+
g := NewWithT(t)
116+
got := convertToTarget(tt.args.devaddr)
117+
g.Expect(got).To(Equal(tt.want))
120118
})
121119
}
122120
}
@@ -167,34 +165,33 @@ func Test_getExternalIPv4Target(t *testing.T) {
167165
}
168166
for _, tt := range tests {
169167
t.Run(tt.name, func(t *testing.T) {
168+
g := NewWithT(t)
170169
got, err := getExternalIPv4Target(tt.args.deviceAddr)
171170
if (err != nil) != tt.wantErr {
172-
t.Errorf("getExternalIPv4Target() error = %v, wantErr %v", err, tt.wantErr)
171+
g.Expect(err).To(Equal(tt.wantErr))
173172
return
174173
}
175-
if !reflect.DeepEqual(got, tt.want) {
176-
t.Errorf("getExternalIPv4Target() = %v, want %v", got, tt.want)
177-
}
174+
g.Expect(got).To(Equal(tt.want))
178175
})
179176
}
180177
}
181178
func TestNewEMLB(t *testing.T) {
179+
g := NewWithT(t)
182180
metalAPIKey := "metal-api-key" //nolint:gosec
183181
projectID := "project-id"
184182
metro := "am"
185183

186184
emlb := NewEMLB(metalAPIKey, projectID, metro)
187185

188-
if emlb.client == nil {
189-
t.Error("NewEMLB() client is nil")
190-
}
191-
if emlb.tokenExchanger == nil {
192-
t.Error("NewEMLB() tokenExchanger is nil")
193-
}
194-
if emlb.projectID != projectID {
195-
t.Errorf("NewEMLB() projectID = %s, want %s", emlb.projectID, projectID)
196-
}
197-
if emlb.metro != metro {
198-
t.Errorf("NewEMLB() metro = %s, want %s", emlb.metro, metro)
199-
}
186+
// assert client is not nil
187+
g.Expect(emlb.client).To(Not(BeNil()))
188+
189+
// assert tokenExchanger is not nil
190+
g.Expect(emlb.tokenExchanger).To(Not(BeNil()))
191+
192+
// assert project ID is correct
193+
g.Expect(emlb.projectID).To(Equal(projectID))
194+
195+
// assert metro is correct
196+
g.Expect(emlb.metro).To(Equal(metro))
200197
}

0 commit comments

Comments
 (0)