1
- package testcontainers
1
+ package testcontainers_test
2
2
3
3
import (
4
4
"archive/tar"
@@ -16,22 +16,23 @@ import (
16
16
"github.com/stretchr/testify/assert"
17
17
"github.com/stretchr/testify/require"
18
18
19
+ "github.com/testcontainers/testcontainers-go"
19
20
"github.com/testcontainers/testcontainers-go/wait"
20
21
)
21
22
22
23
func Test_ContainerValidation (t * testing.T ) {
23
24
type ContainerValidationTestCase struct {
24
25
Name string
25
26
ExpectedError error
26
- ContainerRequest ContainerRequest
27
+ ContainerRequest testcontainers. ContainerRequest
27
28
}
28
29
29
30
testTable := []ContainerValidationTestCase {
30
31
{
31
32
Name : "cannot set both context and image" ,
32
33
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 {
35
36
Context : "." ,
36
37
},
37
38
Image : "redis:latest" ,
@@ -40,23 +41,23 @@ func Test_ContainerValidation(t *testing.T) {
40
41
{
41
42
Name : "can set image without context" ,
42
43
ExpectedError : nil ,
43
- ContainerRequest : ContainerRequest {
44
+ ContainerRequest : testcontainers. ContainerRequest {
44
45
Image : "redis:latest" ,
45
46
},
46
47
},
47
48
{
48
49
Name : "can set context without image" ,
49
50
ExpectedError : nil ,
50
- ContainerRequest : ContainerRequest {
51
- FromDockerfile : FromDockerfile {
51
+ ContainerRequest : testcontainers. ContainerRequest {
52
+ FromDockerfile : testcontainers. FromDockerfile {
52
53
Context : "." ,
53
54
},
54
55
},
55
56
},
56
57
{
57
58
Name : "Can mount same source to multiple targets" ,
58
59
ExpectedError : nil ,
59
- ContainerRequest : ContainerRequest {
60
+ ContainerRequest : testcontainers. ContainerRequest {
60
61
Image : "redis:latest" ,
61
62
HostConfigModifier : func (hc * container.HostConfig ) {
62
63
hc .Binds = []string {"/data:/srv" , "/data:/data" }
@@ -66,7 +67,7 @@ func Test_ContainerValidation(t *testing.T) {
66
67
{
67
68
Name : "Cannot mount multiple sources to same target" ,
68
69
ExpectedError : errors .New ("duplicate mount target detected: /data" ),
69
- ContainerRequest : ContainerRequest {
70
+ ContainerRequest : testcontainers. ContainerRequest {
70
71
Image : "redis:latest" ,
71
72
HostConfigModifier : func (hc * container.HostConfig ) {
72
73
hc .Binds = []string {"/data:/data" , "/data:/data" }
@@ -76,7 +77,7 @@ func Test_ContainerValidation(t *testing.T) {
76
77
{
77
78
Name : "Invalid bind mount" ,
78
79
ExpectedError : errors .New ("invalid bind mount: /data:/data:/data" ),
79
- ContainerRequest : ContainerRequest {
80
+ ContainerRequest : testcontainers. ContainerRequest {
80
81
Image : "redis:latest" ,
81
82
HostConfigModifier : func (hc * container.HostConfig ) {
82
83
hc .Binds = []string {"/data:/data:/data" }
@@ -106,27 +107,27 @@ func Test_GetDockerfile(t *testing.T) {
106
107
type TestCase struct {
107
108
name string
108
109
ExpectedDockerfileName string
109
- ContainerRequest ContainerRequest
110
+ ContainerRequest testcontainers. ContainerRequest
110
111
}
111
112
112
113
testTable := []TestCase {
113
114
{
114
115
name : "defaults to \" Dockerfile\" 1" ,
115
116
ExpectedDockerfileName : "Dockerfile" ,
116
- ContainerRequest : ContainerRequest {},
117
+ ContainerRequest : testcontainers. ContainerRequest {},
117
118
},
118
119
{
119
120
name : "defaults to \" Dockerfile\" 2" ,
120
121
ExpectedDockerfileName : "Dockerfile" ,
121
- ContainerRequest : ContainerRequest {
122
- FromDockerfile : FromDockerfile {},
122
+ ContainerRequest : testcontainers. ContainerRequest {
123
+ FromDockerfile : testcontainers. FromDockerfile {},
123
124
},
124
125
},
125
126
{
126
127
name : "will override name" ,
127
128
ExpectedDockerfileName : "CustomDockerfile" ,
128
- ContainerRequest : ContainerRequest {
129
- FromDockerfile : FromDockerfile {
129
+ ContainerRequest : testcontainers. ContainerRequest {
130
+ FromDockerfile : testcontainers. FromDockerfile {
130
131
Dockerfile : "CustomDockerfile" ,
131
132
},
132
133
},
@@ -278,16 +279,16 @@ func Test_BuildImageWithContexts(t *testing.T) {
278
279
if err != nil {
279
280
t .Fatal (err )
280
281
}
281
- req := ContainerRequest {
282
- FromDockerfile : FromDockerfile {
282
+ req := testcontainers. ContainerRequest {
283
+ FromDockerfile : testcontainers. FromDockerfile {
283
284
ContextArchive : a ,
284
285
Context : testCase .ContextPath ,
285
286
Dockerfile : testCase .Dockerfile ,
286
287
},
287
288
WaitingFor : wait .ForLog (testCase .ExpectedEchoOutput ).WithStartupTimeout (1 * time .Minute ),
288
289
}
289
290
290
- c , err := GenericContainer (ctx , GenericContainerRequest {
291
+ c , err := testcontainers . GenericContainer (ctx , testcontainers. GenericContainerRequest {
291
292
ContainerRequest : req ,
292
293
Started : true ,
293
294
})
@@ -308,14 +309,14 @@ func Test_BuildImageWithContexts(t *testing.T) {
308
309
func Test_GetLogsFromFailedContainer (t * testing.T ) {
309
310
ctx := context .Background ()
310
311
// directDockerHubReference {
311
- req := ContainerRequest {
312
+ req := testcontainers. ContainerRequest {
312
313
Image : "docker.io/alpine" ,
313
314
Cmd : []string {"echo" , "-n" , "I was not expecting this" },
314
315
WaitingFor : wait .ForLog ("I was expecting this" ).WithStartupTimeout (5 * time .Second ),
315
316
}
316
317
// }
317
318
318
- c , err := GenericContainer (ctx , GenericContainerRequest {
319
+ c , err := testcontainers . GenericContainer (ctx , testcontainers. GenericContainerRequest {
319
320
ContainerRequest : req ,
320
321
Started : true ,
321
322
})
@@ -391,7 +392,7 @@ func TestImageSubstitutors(t *testing.T) {
391
392
tests := []struct {
392
393
name string
393
394
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
395
396
expectedImage string
396
397
expectedError error
397
398
}{
@@ -403,19 +404,19 @@ func TestImageSubstitutors(t *testing.T) {
403
404
{
404
405
name : "Noop substitutor" ,
405
406
image : "alpine" ,
406
- substitutors : []ImageSubstitutor {NoopImageSubstitutor {}},
407
+ substitutors : []testcontainers. ImageSubstitutor {NoopImageSubstitutor {}},
407
408
expectedImage : "alpine" ,
408
409
},
409
410
{
410
411
name : "Prepend namespace" ,
411
412
image : "alpine" ,
412
- substitutors : []ImageSubstitutor {dockerImageSubstitutor {}},
413
+ substitutors : []testcontainers. ImageSubstitutor {dockerImageSubstitutor {}},
413
414
expectedImage : "docker.io/alpine" ,
414
415
},
415
416
{
416
417
name : "Substitution with error" ,
417
418
image : "alpine" ,
418
- substitutors : []ImageSubstitutor {errorSubstitutor {}},
419
+ substitutors : []testcontainers. ImageSubstitutor {errorSubstitutor {}},
419
420
expectedImage : "alpine" ,
420
421
expectedError : errSubstitution ,
421
422
},
@@ -424,12 +425,12 @@ func TestImageSubstitutors(t *testing.T) {
424
425
for _ , test := range tests {
425
426
t .Run (test .name , func (t * testing.T ) {
426
427
ctx := context .Background ()
427
- req := ContainerRequest {
428
+ req := testcontainers. ContainerRequest {
428
429
Image : test .image ,
429
430
ImageSubstitutors : test .substitutors ,
430
431
}
431
432
432
- container , err := GenericContainer (ctx , GenericContainerRequest {
433
+ container , err := testcontainers . GenericContainer (ctx , testcontainers. GenericContainerRequest {
433
434
ContainerRequest : req ,
434
435
Started : true ,
435
436
})
@@ -447,7 +448,7 @@ func TestImageSubstitutors(t *testing.T) {
447
448
448
449
// enforce the concrete type, as GenericContainer returns an interface,
449
450
// which will be changed in future implementations of the library
450
- dockerContainer := container .(* DockerContainer )
451
+ dockerContainer := container .(* testcontainers. DockerContainer )
451
452
assert .Equal (t , test .expectedImage , dockerContainer .Image )
452
453
})
453
454
}
@@ -462,12 +463,12 @@ func TestShouldStartContainersInParallel(t *testing.T) {
462
463
t .Run (fmt .Sprintf ("iteration_%d" , i ), func (t * testing.T ) {
463
464
t .Parallel ()
464
465
465
- req := ContainerRequest {
466
+ req := testcontainers. ContainerRequest {
466
467
Image : nginxAlpineImage ,
467
468
ExposedPorts : []string {nginxDefaultPort },
468
469
WaitingFor : wait .ForHTTP ("/" ).WithStartupTimeout (10 * time .Second ),
469
470
}
470
- container , err := GenericContainer (ctx , GenericContainerRequest {
471
+ container , err := testcontainers . GenericContainer (ctx , testcontainers. GenericContainerRequest {
471
472
ContainerRequest : req ,
472
473
Started : true ,
473
474
})
@@ -488,39 +489,14 @@ func TestShouldStartContainersInParallel(t *testing.T) {
488
489
}
489
490
}
490
491
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
-
516
492
func ExampleGenericContainer_withSubstitutors () {
517
493
ctx := context .Background ()
518
494
519
495
// applyImageSubstitutors {
520
- container , err := GenericContainer (ctx , GenericContainerRequest {
521
- ContainerRequest : ContainerRequest {
496
+ container , err := testcontainers . GenericContainer (ctx , testcontainers. GenericContainerRequest {
497
+ ContainerRequest : testcontainers. ContainerRequest {
522
498
Image : "alpine:latest" ,
523
- ImageSubstitutors : []ImageSubstitutor {dockerImageSubstitutor {}},
499
+ ImageSubstitutors : []testcontainers. ImageSubstitutor {dockerImageSubstitutor {}},
524
500
},
525
501
Started : true ,
526
502
})
@@ -538,7 +514,7 @@ func ExampleGenericContainer_withSubstitutors() {
538
514
539
515
// enforce the concrete type, as GenericContainer returns an interface,
540
516
// which will be changed in future implementations of the library
541
- dockerContainer := container .(* DockerContainer )
517
+ dockerContainer := container .(* testcontainers. DockerContainer )
542
518
543
519
fmt .Println (dockerContainer .Image )
544
520
0 commit comments