/
localstack.go
182 lines (147 loc) · 5.52 KB
/
localstack.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
package localstack
import (
"context"
"fmt"
"strings"
"time"
"golang.org/x/mod/semver"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
)
const (
defaultPort = 4566
defaultVersion = "1.4.0"
hostnameExternalEnvVar = "HOSTNAME_EXTERNAL"
localstackHostEnvVar = "LOCALSTACK_HOST"
)
func isLegacyMode(image string) bool {
parts := strings.Split(image, ":")
version := parts[len(parts)-1]
if version == "latest" {
return false
}
if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
}
if semver.IsValid(version) {
return semver.Compare(version, "v0.11") < 0 // version < v0.11
}
return true
}
func isVersion2(image string) bool {
parts := strings.Split(image, ":")
version := parts[len(parts)-1]
if version == "latest" {
return true
}
if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
}
if semver.IsValid(version) {
return semver.Compare(version, "v2.0") > 0 // version >= v2.0
}
return true
}
// WithNetwork creates a network with the given name and attaches the container to it, setting the network alias
// on that network to the given alias.
func WithNetwork(networkName string, alias string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
_, err := testcontainers.GenericNetwork(context.Background(), testcontainers.GenericNetworkRequest{
NetworkRequest: testcontainers.NetworkRequest{
Name: networkName,
},
})
if err != nil {
logger := req.Logger
if logger == nil {
logger = testcontainers.Logger
}
logger.Printf("Failed to create network '%s'. Container won't be attached to this network: %v", networkName, err)
return
}
req.Networks = append(req.Networks, networkName)
if req.NetworkAliases == nil {
req.NetworkAliases = make(map[string][]string)
}
req.NetworkAliases[networkName] = []string{alias}
}
}
// RunContainer creates an instance of the LocalStack container type, being possible to pass a custom request and options:
// - overrideReq: a function that can be used to override the default container request, usually used to set the image version, environment variables for localstack, etc.
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*LocalStackContainer, error) {
dockerHost := testcontainersdocker.ExtractDockerSocket(ctx)
req := testcontainers.ContainerRequest{
Image: fmt.Sprintf("localstack/localstack:%s", defaultVersion),
Binds: []string{fmt.Sprintf("%s:/var/run/docker.sock", dockerHost)},
WaitingFor: wait.ForHTTP("/_localstack/health").WithPort("4566/tcp").WithStartupTimeout(120 * time.Second),
ExposedPorts: []string{fmt.Sprintf("%d/tcp", defaultPort)},
Env: map[string]string{},
}
localStackReq := LocalStackContainerRequest{
GenericContainerRequest: testcontainers.GenericContainerRequest{
ContainerRequest: req,
Logger: testcontainers.Logger,
Started: true,
},
}
for _, opt := range opts {
opt.Customize(&localStackReq.GenericContainerRequest)
}
if isLegacyMode(localStackReq.Image) {
return nil, fmt.Errorf("version=%s. Testcontainers for Go does not support running LocalStack in legacy mode. Please use a version >= 0.11.0", localStackReq.Image)
}
envVar := hostnameExternalEnvVar
if isVersion2(localStackReq.Image) {
envVar = localstackHostEnvVar
}
hostnameExternalReason, err := configureDockerHost(&localStackReq, envVar)
if err != nil {
return nil, err
}
localStackReq.GenericContainerRequest.Logger.Printf("Setting %s to %s (%s)\n", envVar, req.Env[envVar], hostnameExternalReason)
container, err := testcontainers.GenericContainer(ctx, localStackReq.GenericContainerRequest)
if err != nil {
return nil, err
}
c := &LocalStackContainer{
Container: container,
}
return c, nil
}
// StartContainer creates an instance of the LocalStack container type, being possible to pass a custom request and options:
// - overrideReq: a function that can be used to override the default container request, usually used to set the image version, environment variables for localstack, etc.
// Deprecated: use RunContainer instead
func StartContainer(ctx context.Context, overrideReq OverrideContainerRequestOption) (*LocalStackContainer, error) {
return RunContainer(ctx, overrideReq)
}
func configureDockerHost(req *LocalStackContainerRequest, envVar string) (reason string, err error) {
err = nil
reason = ""
if _, ok := req.Env[envVar]; ok {
reason = "explicitly as environment variable"
return
}
// if the container is not connected to the default network, use the last network alias in the first network
// for that we need to check if the container is connected to a network and if it has network aliases
if len(req.Networks) > 0 && len(req.NetworkAliases) > 0 && len(req.NetworkAliases[req.Networks[0]]) > 0 {
alias := req.NetworkAliases[req.Networks[0]][len(req.NetworkAliases[req.Networks[0]])-1]
req.Env[envVar] = alias
reason = "to match last network alias on container with non-default network"
return
}
var dockerProvider *testcontainers.DockerProvider
dockerProvider, err = testcontainers.NewDockerProvider()
if err != nil {
return
}
defer dockerProvider.Close()
var daemonHost string
daemonHost, err = dockerProvider.DaemonHost(context.Background())
if err != nil {
return
}
req.Env[envVar] = daemonHost
reason = "to match host-routable address for container"
return
}