Skip to content

Commit 17228cf

Browse files
committed
chore: use testify instead of t.Error
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
1 parent ffcb8b6 commit 17228cf

File tree

12 files changed

+85
-198
lines changed

12 files changed

+85
-198
lines changed

container_file_test.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package testcontainers
44

55
import (
6-
"errors"
76
"os"
87
"path/filepath"
98
"testing"
@@ -14,7 +13,7 @@ import (
1413
func TestContainerFileValidation(t *testing.T) {
1514
type ContainerFileValidationTestCase struct {
1615
Name string
17-
ExpectedError error
16+
ExpectedError string
1817
File ContainerFile
1918
}
2019

@@ -38,7 +37,7 @@ func TestContainerFileValidation(t *testing.T) {
3837
},
3938
{
4039
Name: "invalid container file",
41-
ExpectedError: errors.New("either HostFilePath or Reader must be specified"),
40+
ExpectedError: "either HostFilePath or Reader must be specified",
4241
File: ContainerFile{
4342
HostFilePath: "",
4443
Reader: nil,
@@ -47,7 +46,7 @@ func TestContainerFileValidation(t *testing.T) {
4746
},
4847
{
4948
Name: "invalid container file",
50-
ExpectedError: errors.New("ContainerFilePath must be specified"),
49+
ExpectedError: "ContainerFilePath must be specified",
5150
File: ContainerFile{
5251
HostFilePath: "/path/to/host",
5352
ContainerFilePath: "",
@@ -58,15 +57,10 @@ func TestContainerFileValidation(t *testing.T) {
5857
for _, testCase := range testTable {
5958
t.Run(testCase.Name, func(t *testing.T) {
6059
err := testCase.File.validate()
61-
switch {
62-
case err == nil && testCase.ExpectedError == nil:
63-
return
64-
case err == nil && testCase.ExpectedError != nil:
65-
t.Errorf("did not receive expected error: %s", testCase.ExpectedError.Error())
66-
case err != nil && testCase.ExpectedError == nil:
67-
t.Errorf("received unexpected error: %s", err.Error())
68-
case err.Error() != testCase.ExpectedError.Error():
69-
t.Errorf("errors mismatch: %s != %s", err.Error(), testCase.ExpectedError.Error())
60+
if testCase.ExpectedError != "" {
61+
require.EqualError(t, err, testCase.ExpectedError)
62+
} else {
63+
require.NoError(t, err)
7064
}
7165
})
7266
}

container_test.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import (
2323
func Test_ContainerValidation(t *testing.T) {
2424
type ContainerValidationTestCase struct {
2525
Name string
26-
ExpectedError error
26+
ExpectedError string
2727
ContainerRequest testcontainers.ContainerRequest
2828
}
2929

3030
testTable := []ContainerValidationTestCase{
3131
{
3232
Name: "cannot set both context and image",
33-
ExpectedError: errors.New("you cannot specify both an Image and Context in a ContainerRequest"),
33+
ExpectedError: "you cannot specify both an Image and Context in a ContainerRequest",
3434
ContainerRequest: testcontainers.ContainerRequest{
3535
FromDockerfile: testcontainers.FromDockerfile{
3636
Context: ".",
@@ -39,24 +39,21 @@ func Test_ContainerValidation(t *testing.T) {
3939
},
4040
},
4141
{
42-
Name: "can set image without context",
43-
ExpectedError: nil,
42+
Name: "can set image without context",
4443
ContainerRequest: testcontainers.ContainerRequest{
4544
Image: "redis:latest",
4645
},
4746
},
4847
{
49-
Name: "can set context without image",
50-
ExpectedError: nil,
48+
Name: "can set context without image",
5149
ContainerRequest: testcontainers.ContainerRequest{
5250
FromDockerfile: testcontainers.FromDockerfile{
5351
Context: ".",
5452
},
5553
},
5654
},
5755
{
58-
Name: "Can mount same source to multiple targets",
59-
ExpectedError: nil,
56+
Name: "Can mount same source to multiple targets",
6057
ContainerRequest: testcontainers.ContainerRequest{
6158
Image: "redis:latest",
6259
HostConfigModifier: func(hc *container.HostConfig) {
@@ -66,7 +63,7 @@ func Test_ContainerValidation(t *testing.T) {
6663
},
6764
{
6865
Name: "Cannot mount multiple sources to same target",
69-
ExpectedError: errors.New("duplicate mount target detected: /data"),
66+
ExpectedError: "duplicate mount target detected: /data",
7067
ContainerRequest: testcontainers.ContainerRequest{
7168
Image: "redis:latest",
7269
HostConfigModifier: func(hc *container.HostConfig) {
@@ -76,7 +73,7 @@ func Test_ContainerValidation(t *testing.T) {
7673
},
7774
{
7875
Name: "Invalid bind mount",
79-
ExpectedError: errors.New("invalid bind mount: /data:/data:/data"),
76+
ExpectedError: "invalid bind mount: /data:/data:/data",
8077
ContainerRequest: testcontainers.ContainerRequest{
8178
Image: "redis:latest",
8279
HostConfigModifier: func(hc *container.HostConfig) {
@@ -89,15 +86,10 @@ func Test_ContainerValidation(t *testing.T) {
8986
for _, testCase := range testTable {
9087
t.Run(testCase.Name, func(t *testing.T) {
9188
err := testCase.ContainerRequest.Validate()
92-
switch {
93-
case err == nil && testCase.ExpectedError == nil:
94-
return
95-
case err == nil && testCase.ExpectedError != nil:
96-
t.Errorf("did not receive expected error: %s", testCase.ExpectedError.Error())
97-
case err != nil && testCase.ExpectedError == nil:
98-
t.Errorf("received unexpected error: %s", err.Error())
99-
case err.Error() != testCase.ExpectedError.Error():
100-
t.Errorf("errors mismatch: %s != %s", err.Error(), testCase.ExpectedError.Error())
89+
if testCase.ExpectedError != "" {
90+
require.EqualError(t, err, testCase.ExpectedError)
91+
} else {
92+
require.NoError(t, err)
10193
}
10294
})
10395
}

docker_test.go

Lines changed: 27 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,11 @@ func TestContainerWithHostNetworkOptions(t *testing.T) {
8383
CleanupContainer(t, nginxC)
8484
require.NoError(t, err)
8585

86-
// host, err := nginxC.Host(ctx)
87-
// if err != nil {
88-
// t.Errorf("Expected host %s. Got '%d'.", host, err)
89-
// }
90-
//
9186
endpoint, err := nginxC.PortEndpoint(ctx, nginxHighPort, "http")
92-
if err != nil {
93-
t.Errorf("Expected server endpoint. Got '%v'.", err)
94-
}
87+
require.NoErrorf(t, err, "Expected server endpoint")
9588

9689
_, err = http.Get(endpoint)
97-
if err != nil {
98-
t.Errorf("Expected OK response. Got '%d'.", err)
99-
}
90+
require.NoErrorf(t, err, "Expected OK response")
10091
}
10192

10293
func TestContainerWithHostNetworkOptions_UseExposePortsFromImageConfigs(t *testing.T) {
@@ -115,17 +106,13 @@ func TestContainerWithHostNetworkOptions_UseExposePortsFromImageConfigs(t *testi
115106
require.NoError(t, err)
116107

117108
endpoint, err := nginxC.Endpoint(ctx, "http")
118-
if err != nil {
119-
t.Errorf("Expected server endpoint. Got '%v'.", err)
120-
}
109+
require.NoErrorf(t, err, "Expected server endpoint")
121110

122111
resp, err := http.Get(endpoint)
123112
require.NoError(t, err)
124113
defer resp.Body.Close()
125114

126-
if resp.StatusCode != http.StatusOK {
127-
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
128-
}
115+
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
129116
}
130117

131118
func TestContainerWithNetworkModeAndNetworkTogether(t *testing.T) {
@@ -192,25 +179,17 @@ func TestContainerWithHostNetwork(t *testing.T) {
192179
require.NoError(t, err)
193180

194181
portEndpoint, err := nginxC.PortEndpoint(ctx, nginxHighPort, "http")
195-
if err != nil {
196-
t.Errorf("Expected port endpoint %s. Got '%d'.", portEndpoint, err)
197-
}
182+
require.NoErrorf(t, err, "Expected port endpoint %s", portEndpoint)
198183
t.Log(portEndpoint)
199184

200185
_, err = http.Get(portEndpoint)
201-
if err != nil {
202-
t.Errorf("Expected OK response. Got '%v'.", err)
203-
}
186+
require.NoErrorf(t, err, "Expected OK response")
204187

205188
host, err := nginxC.Host(ctx)
206-
if err != nil {
207-
t.Errorf("Expected host %s. Got '%d'.", host, err)
208-
}
189+
require.NoErrorf(t, err, "Expected host %s", host)
209190

210191
_, err = http.Get("http://" + host + ":8080")
211-
if err != nil {
212-
t.Errorf("Expected OK response. Got '%v'.", err)
213-
}
192+
assert.NoErrorf(t, err, "Expected OK response")
214193
}
215194

216195
func TestContainerReturnItsContainerID(t *testing.T) {
@@ -227,9 +206,7 @@ func TestContainerReturnItsContainerID(t *testing.T) {
227206
CleanupContainer(t, nginxA)
228207
require.NoError(t, err)
229208

230-
if nginxA.GetContainerID() == "" {
231-
t.Errorf("expected a containerID but we got an empty string.")
232-
}
209+
assert.NotEmptyf(t, nginxA.GetContainerID(), "expected a containerID but we got an empty string.")
233210
}
234211

235212
// testLogConsumer is a simple implementation of LogConsumer that logs to the test output.
@@ -419,9 +396,7 @@ func TestTwoContainersExposingTheSamePort(t *testing.T) {
419396
require.NoError(t, err)
420397
defer resp.Body.Close()
421398

422-
if resp.StatusCode != http.StatusOK {
423-
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
424-
}
399+
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
425400

426401
endpointB, err := nginxB.PortEndpoint(ctx, nginxDefaultPort, "http")
427402
require.NoError(t, err)
@@ -430,9 +405,7 @@ func TestTwoContainersExposingTheSamePort(t *testing.T) {
430405
require.NoError(t, err)
431406
defer resp.Body.Close()
432407

433-
if resp.StatusCode != http.StatusOK {
434-
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
435-
}
408+
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
436409
}
437410

438411
func TestContainerCreation(t *testing.T) {
@@ -459,24 +432,15 @@ func TestContainerCreation(t *testing.T) {
459432
require.NoError(t, err)
460433
defer resp.Body.Close()
461434

462-
if resp.StatusCode != http.StatusOK {
463-
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
464-
}
435+
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
465436
networkIP, err := nginxC.ContainerIP(ctx)
466437
require.NoError(t, err)
467-
if len(networkIP) == 0 {
468-
t.Errorf("Expected an IP address, got %v", networkIP)
469-
}
438+
require.NotEmptyf(t, networkIP, "Expected an IP address, got %v", networkIP)
470439
networkAliases, err := nginxC.NetworkAliases(ctx)
471440
require.NoError(t, err)
472-
if len(networkAliases) != 1 {
473-
fmt.Printf("%v", networkAliases)
474-
t.Errorf("Expected number of connected networks %d. Got %d.", 0, len(networkAliases))
475-
}
476-
477-
if len(networkAliases["bridge"]) != 0 {
478-
t.Errorf("Expected number of aliases for 'bridge' network %d. Got %d.", 0, len(networkAliases["bridge"]))
479-
}
441+
require.Lenf(t, networkAliases, 1, "Expected number of connected networks %d. Got %d.", 0, len(networkAliases))
442+
require.Contains(t, networkAliases, "bridge")
443+
assert.Emptyf(t, networkAliases["bridge"], "Expected number of aliases for 'bridge' network %d. Got %d.", 0, len(networkAliases["bridge"]))
480444
}
481445

482446
func TestContainerCreationWithName(t *testing.T) {
@@ -505,24 +469,16 @@ func TestContainerCreationWithName(t *testing.T) {
505469
require.NoError(t, err)
506470

507471
name := inspect.Name
508-
if name != expectedName {
509-
t.Errorf("Expected container name '%s'. Got '%s'.", expectedName, name)
510-
}
472+
assert.Equalf(t, expectedName, name, "Expected container name '%s'. Got '%s'.", expectedName, name)
511473
networks, err := nginxC.Networks(ctx)
512474
require.NoError(t, err)
513-
if len(networks) != 1 {
514-
t.Errorf("Expected networks 1. Got '%d'.", len(networks))
515-
}
475+
require.Lenf(t, networks, 1, "Expected networks 1. Got '%d'.", len(networks))
516476
network := networks[0]
517477
switch providerType {
518478
case ProviderDocker:
519-
if network != Bridge {
520-
t.Errorf("Expected network name '%s'. Got '%s'.", Bridge, network)
521-
}
479+
assert.Equalf(t, Bridge, network, "Expected network name '%s'. Got '%s'.", Bridge, network)
522480
case ProviderPodman:
523-
if network != Podman {
524-
t.Errorf("Expected network name '%s'. Got '%s'.", Podman, network)
525-
}
481+
assert.Equalf(t, Podman, network, "Expected network name '%s'. Got '%s'.", Podman, network)
526482
}
527483

528484
endpoint, err := nginxC.PortEndpoint(ctx, nginxDefaultPort, "http")
@@ -532,9 +488,7 @@ func TestContainerCreationWithName(t *testing.T) {
532488
require.NoError(t, err)
533489
defer resp.Body.Close()
534490

535-
if resp.StatusCode != http.StatusOK {
536-
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
537-
}
491+
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
538492
}
539493

540494
func TestContainerCreationAndWaitForListeningPortLongEnough(t *testing.T) {
@@ -561,9 +515,7 @@ func TestContainerCreationAndWaitForListeningPortLongEnough(t *testing.T) {
561515
require.NoError(t, err)
562516
defer resp.Body.Close()
563517

564-
if resp.StatusCode != http.StatusOK {
565-
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
566-
}
518+
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
567519
}
568520

569521
func TestContainerCreationTimesOut(t *testing.T) {
@@ -582,9 +534,7 @@ func TestContainerCreationTimesOut(t *testing.T) {
582534
})
583535
CleanupContainer(t, nginxC)
584536

585-
if err == nil {
586-
t.Error("Expected timeout")
587-
}
537+
assert.Errorf(t, err, "Expected timeout")
588538
}
589539

590540
func TestContainerRespondsWithHttp200ForIndex(t *testing.T) {
@@ -610,9 +560,7 @@ func TestContainerRespondsWithHttp200ForIndex(t *testing.T) {
610560
require.NoError(t, err)
611561
defer resp.Body.Close()
612562

613-
if resp.StatusCode != http.StatusOK {
614-
t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
615-
}
563+
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
616564
}
617565

618566
func TestContainerCreationTimesOutWithHttp(t *testing.T) {
@@ -650,9 +598,7 @@ func TestContainerCreationWaitsForLogContextTimeout(t *testing.T) {
650598
Started: true,
651599
})
652600
CleanupContainer(t, c)
653-
if err == nil {
654-
t.Error("Expected timeout")
655-
}
601+
assert.Errorf(t, err, "Expected timeout")
656602
}
657603

658604
func TestContainerCreationWaitsForLog(t *testing.T) {
@@ -755,10 +701,8 @@ func Test_BuildContainerFromDockerfileWithBuildLog(t *testing.T) {
755701
require.NoError(t, err)
756702

757703
temp := strings.Split(string(out), "\n")
758-
759-
if !regexp.MustCompile(`^Step\s*1/\d+\s*:\s*FROM alpine$`).MatchString(temp[0]) {
760-
t.Errorf("Expected stdout first line to be %s. Got '%s'.", "Step 1/* : FROM alpine", temp[0])
761-
}
704+
require.NotEmpty(t, temp)
705+
assert.Regexpf(t, `^Step\s*1/\d+\s*:\s*FROM alpine$`, temp[0], "Expected stdout first line to be %s. Got '%s'.", "Step 1/* : FROM alpine", temp[0])
762706
}
763707

764708
func TestContainerCreationWaitsForLogAndPortContextTimeout(t *testing.T) {

0 commit comments

Comments
 (0)