-
Notifications
You must be signed in to change notification settings - Fork 0
/
localstack_test.go
110 lines (99 loc) · 2.99 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
package sqsObserver_go
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/cenkalti/backoff/v4"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"strings"
"time"
)
const (
localstackRepository = "localstack/localstack"
localstackTag = "latest"
localstackPort = "4566/tcp"
awsID, awsSecret, awsToken = "abc", "def", "ghi"
backoffInterval = 100 * time.Millisecond
)
type (
// LocalstackConfig is a configuration for Localstack.
LocalstackConfig struct {
Region string
Services []string
ContainerExpirationSeconds int
BackoffDuration time.Duration
}
// Localstack is a Localstack docker container.
Localstack struct {
sess *session.Session
b backoff.BackOff
}
)
// NewLocalstack creates a new Localstack container and allows
// creation of streams, tables and other AWS resources.
func NewLocalstack(cfg LocalstackConfig) (*Localstack, error) {
container, err := createContainer(cfg)
if err != nil {
return nil, fmt.Errorf("failed to create container: %w", err)
}
sess, err := createSession(cfg.Region, container.GetPort(localstackPort))
if err != nil {
return nil, fmt.Errorf("failed to create session: %w", err)
}
b := backoff.NewConstantBackOff(cfg.BackoffDuration)
b.Interval = backoffInterval
return &Localstack{
sess: sess,
b: b,
}, nil
}
func createContainer(cfg LocalstackConfig) (*dockertest.Resource, error) {
pool, err := dockertest.NewPool("")
if err != nil {
return nil, fmt.Errorf("failed to create docker pool: %w", err)
}
container, err := pool.RunWithOptions(
&dockertest.RunOptions{
Repository: localstackRepository,
Tag: localstackTag,
Env: []string{
"SERVICES=" + strings.Join(cfg.Services, ","),
"DEFAULT_REGION=" + cfg.Region,
"START_WEB=0",
"DOCKER_HOST=unix:///var/run/docker.sock",
"DATA_DIR=/tmp/localstack/data",
},
Mounts: []string{
"/var/run/docker.sock:/var/run/docker.sock",
},
},
func(c *docker.HostConfig) {
c.AutoRemove = true
c.RestartPolicy = docker.RestartPolicy{
Name: "no",
}
},
)
if err != nil {
return nil, fmt.Errorf("failed to start container: %w", err)
}
if err = container.Expire(uint(cfg.ContainerExpirationSeconds)); err != nil {
return nil, fmt.Errorf("failed to expire container: %w", err)
}
return container, nil
}
func createSession(region, port string) (*session.Session, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
Endpoint: aws.String("localhost:" + port),
Credentials: credentials.NewStaticCredentials(awsID, awsSecret, awsToken),
DisableSSL: aws.Bool(true),
CredentialsChainVerboseErrors: aws.Bool(true),
})
if err != nil {
return nil, fmt.Errorf("failed to create new aws session: %w", err)
}
return sess, nil
}