forked from abhirockzz/dynamodb-local-testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamodb.go
116 lines (96 loc) · 3.28 KB
/
dynamodb.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
package dynamodblocal
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
// DynamodbLocalContainer represents the a DynamoDB Local container - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html
type DynamodbLocalContainer struct {
testcontainers.Container
}
const (
image = "amazon/dynamodb-local:2.4.0"
port = nat.Port("8000/tcp")
containerName = "dynamodb_local"
)
// RunContainer creates an instance of the dynamodb container type
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*DynamodbLocalContainer, error) {
req := testcontainers.ContainerRequest{
Image: image,
ExposedPorts: []string{string(port)},
WaitingFor: wait.ForListeningPort(port),
}
genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}
for _, opt := range opts {
opt.Customize(&genericContainerReq)
}
//log.Println("CMD", genericContainerReq.Cmd)
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
if err != nil {
return nil, err
}
return &DynamodbLocalContainer{Container: container}, nil
}
// ConnectionString returns DynamoDB local endpoint host and port in <host>:<port> format
func (c *DynamodbLocalContainer) ConnectionString(ctx context.Context) (string, error) {
mappedPort, err := c.MappedPort(ctx, port)
if err != nil {
return "", err
}
hostIP, err := c.Host(ctx)
if err != nil {
return "", err
}
uri := fmt.Sprintf("%s:%s", hostIP, mappedPort.Port())
return uri, nil
}
func (c *DynamodbLocalContainer) GetDynamoDBClient(ctx context.Context) (*dynamodb.Client, error) {
hostAndPort, err := c.ConnectionString(context.Background())
if err != nil {
return nil, err
}
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: "DUMMYIDEXAMPLE",
SecretAccessKey: "DUMMYEXAMPLEKEY",
},
}))
if err != nil {
return nil, err
}
return dynamodb.NewFromConfig(cfg, dynamodb.WithEndpointResolverV2(&DynamoDBLocalResolver{hostAndPort: hostAndPort})), nil
}
// WithSharedDB allows container reuse between successive runs. Data will be persisted
func WithSharedDB() testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
if len(req.Cmd) > 0 {
req.Cmd = append(req.Cmd, "-sharedDb")
} else {
req.Cmd = append(req.Cmd, "-jar", "DynamoDBLocal.jar", "-sharedDb")
}
req.Name = containerName
req.Reuse = true
return nil
}
}
// WithTelemetryDisabled - DynamoDB local will not send any telemetry
func WithTelemetryDisabled() testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
// if other flags (e.g. -sharedDb) exist, append to them
if len(req.Cmd) > 0 {
req.Cmd = append(req.Cmd, "-disableTelemetry")
} else {
req.Cmd = append(req.Cmd, "-jar", "DynamoDBLocal.jar", "-disableTelemetry")
}
return nil
}
}