forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalstack_test.go
202 lines (169 loc) · 5.34 KB
/
localstack_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package localstack
import (
"context"
"fmt"
"io"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/network"
"github.com/testcontainers/testcontainers-go/wait"
)
func generateContainerRequest() *LocalStackContainerRequest {
return &LocalStackContainerRequest{
GenericContainerRequest: testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{},
ExposedPorts: []string{},
},
},
}
}
func TestConfigureDockerHost(t *testing.T) {
tests := []struct {
envVar string
}{
{hostnameExternalEnvVar},
{localstackHostEnvVar},
}
for _, tt := range tests {
t.Run("HOSTNAME_EXTERNAL variable is passed as part of the request", func(t *testing.T) {
req := generateContainerRequest()
req.Env[tt.envVar] = "foo"
reason, err := configureDockerHost(req, tt.envVar)
require.NoError(t, err)
assert.Equal(t, "explicitly as environment variable", reason)
})
t.Run("HOSTNAME_EXTERNAL matches the last network alias on a container with non-default network", func(t *testing.T) {
req := generateContainerRequest()
req.Networks = []string{"foo", "bar", "baaz"}
req.NetworkAliases = map[string][]string{
"foo": {"foo0", "foo1", "foo2", "foo3"},
"bar": {"bar0", "bar1", "bar2", "bar3"},
"baaz": {"baaz0", "baaz1", "baaz2", "baaz3"},
}
reason, err := configureDockerHost(req, tt.envVar)
require.NoError(t, err)
assert.Equal(t, "to match last network alias on container with non-default network", reason)
assert.Equal(t, "foo3", req.Env[tt.envVar])
})
t.Run("HOSTNAME_EXTERNAL matches the daemon host because there are no aliases", func(t *testing.T) {
dockerProvider, err := testcontainers.NewDockerProvider()
require.NoError(t, err)
defer dockerProvider.Close()
// because the daemon host could be a remote one, we need to get it from the provider
expectedDaemonHost, err := dockerProvider.DaemonHost(context.Background())
require.NoError(t, err)
req := generateContainerRequest()
req.Networks = []string{"foo", "bar", "baaz"}
req.NetworkAliases = map[string][]string{}
reason, err := configureDockerHost(req, tt.envVar)
require.NoError(t, err)
assert.Equal(t, "to match host-routable address for container", reason)
assert.Equal(t, expectedDaemonHost, req.Env[tt.envVar])
})
}
}
func TestIsLegacyMode(t *testing.T) {
tests := []struct {
version string
want bool
}{
{"foo", true},
{"latest", false},
{"0.10.0", true},
{"0.10.999", true},
{"0.11", false},
{"0.11.2", false},
{"0.12", false},
{"1.0", false},
}
for _, tt := range tests {
t.Run(tt.version, func(t *testing.T) {
got := isLegacyMode(fmt.Sprintf("localstack/localstack:%s", tt.version))
assert.Equal(t, tt.want, got, "runInLegacyMode() = %v, want %v", got, tt.want)
})
}
}
func TestRunContainer(t *testing.T) {
tests := []struct {
version string
}{
{defaultVersion},
{"2.0.0"},
}
for _, tt := range tests {
ctx := context.Background()
container, err := RunContainer(
ctx,
testcontainers.WithImage(fmt.Sprintf("localstack/localstack:%s", tt.version)),
)
t.Run("Localstack:"+tt.version+" - multiple services exposed on same port", func(t *testing.T) {
require.NoError(t, err)
assert.NotNil(t, container)
inspect, err := container.Inspect(ctx)
require.NoError(t, err)
rawPorts := inspect.NetworkSettings.Ports
ports := 0
// only one port is exposed among all the ports in the container
for _, v := range rawPorts {
if len(v) > 0 {
ports++
}
}
assert.Equal(t, 1, ports) // a single port is exposed
})
}
}
func TestStartWithoutOverride(t *testing.T) {
ctx := context.Background()
container, err := RunContainer(ctx)
require.NoError(t, err)
assert.NotNil(t, container)
}
func TestStartV2WithNetwork(t *testing.T) {
ctx := context.Background()
nw, err := network.New(ctx)
require.NoError(t, err)
localstack, err := RunContainer(
ctx,
network.WithNetwork([]string{"localstack"}, nw),
testcontainers.WithImage("localstack/localstack:2.0.0"),
testcontainers.WithEnv(map[string]string{"SERVICES": "s3,sqs"}),
)
require.NoError(t, err)
assert.NotNil(t, localstack)
networkName := nw.Name
cli, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "amazon/aws-cli:2.7.27",
Networks: []string{networkName},
Entrypoint: []string{"tail"},
Cmd: []string{"-f", "/dev/null"},
Env: map[string]string{
"AWS_ACCESS_KEY_ID": "accesskey",
"AWS_SECRET_ACCESS_KEY": "secretkey",
"AWS_REGION": "eu-west-1",
},
WaitingFor: wait.ForExec([]string{
"/usr/local/bin/aws", "sqs", "create-queue", "--queue-name", "baz", "--region", "eu-west-1",
"--endpoint-url", "http://localstack:4566", "--no-verify-ssl",
}).
WithStartupTimeout(time.Second * 10).
WithExitCodeMatcher(func(exitCode int) bool {
return exitCode == 0
}).
WithResponseMatcher(func(r io.Reader) bool {
respBytes, _ := io.ReadAll(r)
resp := string(respBytes)
return strings.Contains(resp, "http://localstack:4566")
}),
},
Started: true,
})
require.NoError(t, err)
assert.NotNil(t, cli)
}