-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
localstack.go
51 lines (41 loc) · 1.17 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
package testutil
import (
"context"
"fmt"
"os"
dockercontainer "github.com/docker/docker/api/types/container"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/localstack"
)
func SetupLocalStack(ctx context.Context, version string) (*localstack.LocalStackContainer, string, error) {
if err := os.Setenv("TESTCONTAINERS_RYUK_DISABLED", "true"); err != nil {
return nil, "", err
}
container, err := localstack.RunContainer(ctx, testcontainers.CustomizeRequest(
testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "localstack/localstack:" + version,
HostConfigModifier: func(hostConfig *dockercontainer.HostConfig) {
hostConfig.AutoRemove = true
},
},
},
))
if err != nil {
return nil, "", err
}
p, err := container.MappedPort(ctx, "4566/tcp")
if err != nil {
return nil, "", err
}
provider, err := testcontainers.NewDockerProvider()
if err != nil {
return nil, "", err
}
defer provider.Close()
host, err := provider.DaemonHost(ctx)
if err != nil {
return nil, "", err
}
return container, fmt.Sprintf("http://%s:%d", host, p.Int()), nil
}