forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_test.go
133 lines (111 loc) · 3.78 KB
/
options_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package testcontainers_test
import (
"context"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/wait"
)
func TestOverrideContainerRequest(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"BAR": "BAR",
},
Image: "foo",
ExposedPorts: []string{"12345/tcp"},
WaitingFor: wait.ForNop(
func(ctx context.Context, target wait.StrategyTarget) error {
return nil
},
),
Networks: []string{"foo", "bar", "baaz"},
NetworkAliases: map[string][]string{
"foo": {"foo0", "foo1", "foo2", "foo3"},
},
},
}
toBeMergedRequest := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"FOO": "FOO",
},
Image: "bar",
ExposedPorts: []string{"67890/tcp"},
Networks: []string{"foo1", "bar1"},
NetworkAliases: map[string][]string{
"foo1": {"bar"},
},
WaitingFor: wait.ForLog("foo"),
},
}
// the toBeMergedRequest should be merged into the req
testcontainers.CustomizeRequest(toBeMergedRequest)(&req)
// toBeMergedRequest should not be changed
assert.Equal(t, "", toBeMergedRequest.Env["BAR"])
assert.Len(t, toBeMergedRequest.ExposedPorts, 1)
assert.Equal(t, "67890/tcp", toBeMergedRequest.ExposedPorts[0])
// req should be merged with toBeMergedRequest
assert.Equal(t, "FOO", req.Env["FOO"])
assert.Equal(t, "BAR", req.Env["BAR"])
assert.Equal(t, "bar", req.Image)
assert.Equal(t, []string{"12345/tcp", "67890/tcp"}, req.ExposedPorts)
assert.Equal(t, []string{"foo", "bar", "baaz", "foo1", "bar1"}, req.Networks)
assert.Equal(t, []string{"foo0", "foo1", "foo2", "foo3"}, req.NetworkAliases["foo"])
assert.Equal(t, []string{"bar"}, req.NetworkAliases["foo1"])
assert.Equal(t, wait.ForLog("foo"), req.WaitingFor)
}
type msgsLogConsumer struct {
msgs []string
}
// Accept prints the log to stdout
func (lc *msgsLogConsumer) Accept(l testcontainers.Log) {
lc.msgs = append(lc.msgs, string(l.Content))
}
func TestWithLogConsumers(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "mysql:8.0.36",
WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"),
},
Started: true,
}
lc := &msgsLogConsumer{}
testcontainers.WithLogConsumers(lc)(&req)
c, err := testcontainers.GenericContainer(context.Background(), req)
// we expect an error because the MySQL environment variables are not set
// but this is expected because we just want to test the log consumer
require.Error(t, err)
defer func() {
err = c.Terminate(context.Background())
require.NoError(t, err)
}()
assert.NotEmpty(t, lc.msgs)
}
func TestWithStartupCommand(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "alpine",
Entrypoint: []string{"tail", "-f", "/dev/null"},
},
Started: true,
}
testExec := testcontainers.NewRawCommand([]string{"touch", "/tmp/.testcontainers"})
testcontainers.WithStartupCommand(testExec)(&req)
assert.Len(t, req.LifecycleHooks, 1)
assert.Len(t, req.LifecycleHooks[0].PostStarts, 1)
c, err := testcontainers.GenericContainer(context.Background(), req)
require.NoError(t, err)
defer func() {
err = c.Terminate(context.Background())
require.NoError(t, err)
}()
_, reader, err := c.Exec(context.Background(), []string{"ls", "/tmp/.testcontainers"}, exec.Multiplexed())
require.NoError(t, err)
content, err := io.ReadAll(reader)
require.NoError(t, err)
assert.Equal(t, "/tmp/.testcontainers\n", string(content))
}