forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_test.go
97 lines (87 loc) · 2.24 KB
/
generic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package testcontainers
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
)
const (
reusableContainerName = "my_test_reusable_container"
)
func TestGenericReusableContainer(t *testing.T) {
ctx := context.Background()
n1, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: nginxAlpineImage,
ExposedPorts: []string{nginxDefaultPort},
WaitingFor: wait.ForListeningPort(nginxDefaultPort),
Name: reusableContainerName,
},
Started: true,
})
require.NoError(t, err)
require.True(t, n1.IsRunning())
terminateContainerOnEnd(t, ctx, n1)
copiedFileName := "hello_copy.sh"
err = n1.CopyFileToContainer(ctx, "./testdata/hello.sh", "/"+copiedFileName, 700)
require.NoError(t, err)
tests := []struct {
name string
containerName string
errorMatcher func(err error) error
reuseOption bool
}{
{
name: "reuse option with empty name",
errorMatcher: func(err error) error {
if errors.Is(err, ErrReuseEmptyName) {
return nil
}
return err
},
reuseOption: true,
},
{
name: "container already exists (reuse=false)",
containerName: reusableContainerName,
errorMatcher: func(err error) error {
if err == nil {
return errors.New("expected error but got none")
}
return nil
},
reuseOption: false,
},
{
name: "success reusing",
containerName: reusableContainerName,
reuseOption: true,
errorMatcher: func(err error) error {
return err
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
n2, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: nginxAlpineImage,
ExposedPorts: []string{nginxDefaultPort},
WaitingFor: wait.ForListeningPort(nginxDefaultPort),
Name: tc.containerName,
},
Started: true,
Reuse: tc.reuseOption,
})
require.NoError(t, tc.errorMatcher(err))
if err == nil {
c, _, err := n2.Exec(ctx, []string{"/bin/ash", copiedFileName})
require.NoError(t, err)
require.Zero(t, c)
}
})
}
}