Skip to content

Commit e332b55

Browse files
author
bstrausser
committed
Merge branch 'main' into feature/must-on-dbs
2 parents 8434cb6 + fe0d3a8 commit e332b55

File tree

6 files changed

+196
-65
lines changed

6 files changed

+196
-65
lines changed

config_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package testcontainers
1+
package testcontainers_test
22

33
import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
77

8+
"github.com/testcontainers/testcontainers-go"
89
"github.com/testcontainers/testcontainers-go/internal/config"
910
)
1011

@@ -26,9 +27,9 @@ func TestReadConfig(t *testing.T) {
2627
t.Setenv("USERPROFILE", "") // Windows support
2728
t.Setenv("TESTCONTAINERS_RYUK_DISABLED", "true")
2829

29-
cfg := ReadConfig()
30+
cfg := testcontainers.ReadConfig()
3031

31-
expected := TestcontainersConfig{
32+
expected := testcontainers.TestcontainersConfig{
3233
RyukDisabled: true,
3334
Config: config.Config{
3435
RyukDisabled: true,
@@ -38,7 +39,7 @@ func TestReadConfig(t *testing.T) {
3839
assert.Equal(t, expected, cfg)
3940

4041
t.Setenv("TESTCONTAINERS_RYUK_DISABLED", "false")
41-
cfg = ReadConfig()
42+
cfg = testcontainers.ReadConfig()
4243
assert.Equal(t, expected, cfg)
4344
})
4445
}

container_ignore_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This test is testing very internal logic that should not be exported away from this package. We'll
2+
// leave it in the main testcontainers package. Do not use for user facing examples.
3+
package testcontainers
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestParseDockerIgnore(t *testing.T) {
12+
testCases := []struct {
13+
filePath string
14+
expectedErr error
15+
expectedExcluded []string
16+
}{
17+
{
18+
filePath: "./testdata/dockerignore",
19+
expectedErr: nil,
20+
expectedExcluded: []string{"vendor", "foo", "bar"},
21+
},
22+
{
23+
filePath: "./testdata",
24+
expectedErr: nil,
25+
expectedExcluded: []string{"Dockerfile", "echo.Dockerfile"},
26+
},
27+
}
28+
29+
for _, testCase := range testCases {
30+
excluded, err := parseDockerIgnore(testCase.filePath)
31+
assert.Equal(t, testCase.expectedErr, err)
32+
assert.Equal(t, testCase.expectedExcluded, excluded)
33+
}
34+
}

container_test.go

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package testcontainers
1+
package testcontainers_test
22

33
import (
44
"archive/tar"
@@ -16,22 +16,23 @@ import (
1616
"github.com/stretchr/testify/assert"
1717
"github.com/stretchr/testify/require"
1818

19+
"github.com/testcontainers/testcontainers-go"
1920
"github.com/testcontainers/testcontainers-go/wait"
2021
)
2122

2223
func Test_ContainerValidation(t *testing.T) {
2324
type ContainerValidationTestCase struct {
2425
Name string
2526
ExpectedError error
26-
ContainerRequest ContainerRequest
27+
ContainerRequest testcontainers.ContainerRequest
2728
}
2829

2930
testTable := []ContainerValidationTestCase{
3031
{
3132
Name: "cannot set both context and image",
3233
ExpectedError: errors.New("you cannot specify both an Image and Context in a ContainerRequest"),
33-
ContainerRequest: ContainerRequest{
34-
FromDockerfile: FromDockerfile{
34+
ContainerRequest: testcontainers.ContainerRequest{
35+
FromDockerfile: testcontainers.FromDockerfile{
3536
Context: ".",
3637
},
3738
Image: "redis:latest",
@@ -40,23 +41,23 @@ func Test_ContainerValidation(t *testing.T) {
4041
{
4142
Name: "can set image without context",
4243
ExpectedError: nil,
43-
ContainerRequest: ContainerRequest{
44+
ContainerRequest: testcontainers.ContainerRequest{
4445
Image: "redis:latest",
4546
},
4647
},
4748
{
4849
Name: "can set context without image",
4950
ExpectedError: nil,
50-
ContainerRequest: ContainerRequest{
51-
FromDockerfile: FromDockerfile{
51+
ContainerRequest: testcontainers.ContainerRequest{
52+
FromDockerfile: testcontainers.FromDockerfile{
5253
Context: ".",
5354
},
5455
},
5556
},
5657
{
5758
Name: "Can mount same source to multiple targets",
5859
ExpectedError: nil,
59-
ContainerRequest: ContainerRequest{
60+
ContainerRequest: testcontainers.ContainerRequest{
6061
Image: "redis:latest",
6162
HostConfigModifier: func(hc *container.HostConfig) {
6263
hc.Binds = []string{"/data:/srv", "/data:/data"}
@@ -66,7 +67,7 @@ func Test_ContainerValidation(t *testing.T) {
6667
{
6768
Name: "Cannot mount multiple sources to same target",
6869
ExpectedError: errors.New("duplicate mount target detected: /data"),
69-
ContainerRequest: ContainerRequest{
70+
ContainerRequest: testcontainers.ContainerRequest{
7071
Image: "redis:latest",
7172
HostConfigModifier: func(hc *container.HostConfig) {
7273
hc.Binds = []string{"/data:/data", "/data:/data"}
@@ -76,7 +77,7 @@ func Test_ContainerValidation(t *testing.T) {
7677
{
7778
Name: "Invalid bind mount",
7879
ExpectedError: errors.New("invalid bind mount: /data:/data:/data"),
79-
ContainerRequest: ContainerRequest{
80+
ContainerRequest: testcontainers.ContainerRequest{
8081
Image: "redis:latest",
8182
HostConfigModifier: func(hc *container.HostConfig) {
8283
hc.Binds = []string{"/data:/data:/data"}
@@ -106,27 +107,27 @@ func Test_GetDockerfile(t *testing.T) {
106107
type TestCase struct {
107108
name string
108109
ExpectedDockerfileName string
109-
ContainerRequest ContainerRequest
110+
ContainerRequest testcontainers.ContainerRequest
110111
}
111112

112113
testTable := []TestCase{
113114
{
114115
name: "defaults to \"Dockerfile\" 1",
115116
ExpectedDockerfileName: "Dockerfile",
116-
ContainerRequest: ContainerRequest{},
117+
ContainerRequest: testcontainers.ContainerRequest{},
117118
},
118119
{
119120
name: "defaults to \"Dockerfile\" 2",
120121
ExpectedDockerfileName: "Dockerfile",
121-
ContainerRequest: ContainerRequest{
122-
FromDockerfile: FromDockerfile{},
122+
ContainerRequest: testcontainers.ContainerRequest{
123+
FromDockerfile: testcontainers.FromDockerfile{},
123124
},
124125
},
125126
{
126127
name: "will override name",
127128
ExpectedDockerfileName: "CustomDockerfile",
128-
ContainerRequest: ContainerRequest{
129-
FromDockerfile: FromDockerfile{
129+
ContainerRequest: testcontainers.ContainerRequest{
130+
FromDockerfile: testcontainers.FromDockerfile{
130131
Dockerfile: "CustomDockerfile",
131132
},
132133
},
@@ -278,16 +279,16 @@ func Test_BuildImageWithContexts(t *testing.T) {
278279
if err != nil {
279280
t.Fatal(err)
280281
}
281-
req := ContainerRequest{
282-
FromDockerfile: FromDockerfile{
282+
req := testcontainers.ContainerRequest{
283+
FromDockerfile: testcontainers.FromDockerfile{
283284
ContextArchive: a,
284285
Context: testCase.ContextPath,
285286
Dockerfile: testCase.Dockerfile,
286287
},
287288
WaitingFor: wait.ForLog(testCase.ExpectedEchoOutput).WithStartupTimeout(1 * time.Minute),
288289
}
289290

290-
c, err := GenericContainer(ctx, GenericContainerRequest{
291+
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
291292
ContainerRequest: req,
292293
Started: true,
293294
})
@@ -308,14 +309,14 @@ func Test_BuildImageWithContexts(t *testing.T) {
308309
func Test_GetLogsFromFailedContainer(t *testing.T) {
309310
ctx := context.Background()
310311
// directDockerHubReference {
311-
req := ContainerRequest{
312+
req := testcontainers.ContainerRequest{
312313
Image: "docker.io/alpine",
313314
Cmd: []string{"echo", "-n", "I was not expecting this"},
314315
WaitingFor: wait.ForLog("I was expecting this").WithStartupTimeout(5 * time.Second),
315316
}
316317
// }
317318

318-
c, err := GenericContainer(ctx, GenericContainerRequest{
319+
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
319320
ContainerRequest: req,
320321
Started: true,
321322
})
@@ -391,7 +392,7 @@ func TestImageSubstitutors(t *testing.T) {
391392
tests := []struct {
392393
name string
393394
image string // must be a valid image, as the test will try to create a container from it
394-
substitutors []ImageSubstitutor
395+
substitutors []testcontainers.ImageSubstitutor
395396
expectedImage string
396397
expectedError error
397398
}{
@@ -403,19 +404,19 @@ func TestImageSubstitutors(t *testing.T) {
403404
{
404405
name: "Noop substitutor",
405406
image: "alpine",
406-
substitutors: []ImageSubstitutor{NoopImageSubstitutor{}},
407+
substitutors: []testcontainers.ImageSubstitutor{NoopImageSubstitutor{}},
407408
expectedImage: "alpine",
408409
},
409410
{
410411
name: "Prepend namespace",
411412
image: "alpine",
412-
substitutors: []ImageSubstitutor{dockerImageSubstitutor{}},
413+
substitutors: []testcontainers.ImageSubstitutor{dockerImageSubstitutor{}},
413414
expectedImage: "docker.io/alpine",
414415
},
415416
{
416417
name: "Substitution with error",
417418
image: "alpine",
418-
substitutors: []ImageSubstitutor{errorSubstitutor{}},
419+
substitutors: []testcontainers.ImageSubstitutor{errorSubstitutor{}},
419420
expectedImage: "alpine",
420421
expectedError: errSubstitution,
421422
},
@@ -424,12 +425,12 @@ func TestImageSubstitutors(t *testing.T) {
424425
for _, test := range tests {
425426
t.Run(test.name, func(t *testing.T) {
426427
ctx := context.Background()
427-
req := ContainerRequest{
428+
req := testcontainers.ContainerRequest{
428429
Image: test.image,
429430
ImageSubstitutors: test.substitutors,
430431
}
431432

432-
container, err := GenericContainer(ctx, GenericContainerRequest{
433+
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
433434
ContainerRequest: req,
434435
Started: true,
435436
})
@@ -447,7 +448,7 @@ func TestImageSubstitutors(t *testing.T) {
447448

448449
// enforce the concrete type, as GenericContainer returns an interface,
449450
// which will be changed in future implementations of the library
450-
dockerContainer := container.(*DockerContainer)
451+
dockerContainer := container.(*testcontainers.DockerContainer)
451452
assert.Equal(t, test.expectedImage, dockerContainer.Image)
452453
})
453454
}
@@ -462,12 +463,12 @@ func TestShouldStartContainersInParallel(t *testing.T) {
462463
t.Run(fmt.Sprintf("iteration_%d", i), func(t *testing.T) {
463464
t.Parallel()
464465

465-
req := ContainerRequest{
466+
req := testcontainers.ContainerRequest{
466467
Image: nginxAlpineImage,
467468
ExposedPorts: []string{nginxDefaultPort},
468469
WaitingFor: wait.ForHTTP("/").WithStartupTimeout(10 * time.Second),
469470
}
470-
container, err := GenericContainer(ctx, GenericContainerRequest{
471+
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
471472
ContainerRequest: req,
472473
Started: true,
473474
})
@@ -488,39 +489,14 @@ func TestShouldStartContainersInParallel(t *testing.T) {
488489
}
489490
}
490491

491-
func TestParseDockerIgnore(t *testing.T) {
492-
testCases := []struct {
493-
filePath string
494-
expectedErr error
495-
expectedExcluded []string
496-
}{
497-
{
498-
filePath: "./testdata/dockerignore",
499-
expectedErr: nil,
500-
expectedExcluded: []string{"vendor", "foo", "bar"},
501-
},
502-
{
503-
filePath: "./testdata",
504-
expectedErr: nil,
505-
expectedExcluded: []string{"Dockerfile", "echo.Dockerfile"},
506-
},
507-
}
508-
509-
for _, testCase := range testCases {
510-
excluded, err := parseDockerIgnore(testCase.filePath)
511-
assert.Equal(t, testCase.expectedErr, err)
512-
assert.Equal(t, testCase.expectedExcluded, excluded)
513-
}
514-
}
515-
516492
func ExampleGenericContainer_withSubstitutors() {
517493
ctx := context.Background()
518494

519495
// applyImageSubstitutors {
520-
container, err := GenericContainer(ctx, GenericContainerRequest{
521-
ContainerRequest: ContainerRequest{
496+
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
497+
ContainerRequest: testcontainers.ContainerRequest{
522498
Image: "alpine:latest",
523-
ImageSubstitutors: []ImageSubstitutor{dockerImageSubstitutor{}},
499+
ImageSubstitutors: []testcontainers.ImageSubstitutor{dockerImageSubstitutor{}},
524500
},
525501
Started: true,
526502
})
@@ -538,7 +514,7 @@ func ExampleGenericContainer_withSubstitutors() {
538514

539515
// enforce the concrete type, as GenericContainer returns an interface,
540516
// which will be changed in future implementations of the library
541-
dockerContainer := container.(*DockerContainer)
517+
dockerContainer := container.(*testcontainers.DockerContainer)
542518

543519
fmt.Println(dockerContainer.Image)
544520

docker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ func (c *DockerContainer) startLogProduction(ctx context.Context, opts ...LogPro
728728
since = fmt.Sprintf("%d.%09d", now.Unix(), int64(now.Nanosecond()))
729729
goto BEGIN
730730
}
731-
if errors.Is(err, context.DeadlineExceeded) {
731+
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
732732
// Probably safe to continue here
733733
continue
734734
}
@@ -756,7 +756,7 @@ func (c *DockerContainer) startLogProduction(ctx context.Context, opts ...LogPro
756756
if err != nil {
757757
// TODO: add-logger: use logger to log out this error
758758
_, _ = fmt.Fprintf(os.Stderr, "error occurred reading log with known length %s", err.Error())
759-
if errors.Is(err, context.DeadlineExceeded) {
759+
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
760760
// Probably safe to continue here
761761
continue
762762
}

0 commit comments

Comments
 (0)