Skip to content

Commit 7a1419d

Browse files
mdelapenyastevenh
andauthored
feat: add dynamodb-local module (testcontainers#2799)
* feat: add dynamodb-local module * docs: fix * fix: remove module from CI * fix: wrongly generated * chore: bump dockercfg * chore: wrap errors * Revert "chore: wrap errors" This reverts commit 93a9d01. * fix: remove unused constant * chore: wrap errors * chore: simplify helper methods * chore: wrap methods * fix: partially revert helpers as we need to check for errors in the caller * chore: wrap error * chore: simplify variable Co-authored-by: Steven Hartland <[email protected]> * fixlint * chore: pass testing.T --------- Co-authored-by: Steven Hartland <[email protected]>
1 parent 9562594 commit 7a1419d

File tree

11 files changed

+781
-2
lines changed

11 files changed

+781
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
matrix:
9595
go-version: [1.22.x, 1.x]
9696
platform: [ubuntu-latest]
97-
module: [artemis, azurite, cassandra, chroma, clickhouse, cockroachdb, compose, consul, couchbase, databend, dolt, elasticsearch, gcloud, grafana-lgtm, inbucket, influxdb, k3s, k6, kafka, localstack, mariadb, milvus, minio, mockserver, mongodb, mssql, mysql, nats, neo4j, ollama, openfga, openldap, opensearch, postgres, pulsar, qdrant, rabbitmq, redis, redpanda, registry, surrealdb, valkey, vault, vearch, weaviate]
97+
module: [artemis, azurite, cassandra, chroma, clickhouse, cockroachdb, compose, consul, couchbase, databend, dolt, dynamodb, elasticsearch, gcloud, grafana-lgtm, inbucket, influxdb, k3s, k6, kafka, localstack, mariadb, milvus, minio, mockserver, mongodb, mssql, mysql, nats, neo4j, ollama, openfga, openldap, opensearch, postgres, pulsar, qdrant, rabbitmq, redis, redpanda, registry, surrealdb, valkey, vault, vearch, weaviate]
9898
uses: ./.github/workflows/ci-test-go.yml
9999
with:
100100
go-version: ${{ matrix.go-version }}

.vscode/.testcontainers-go.code-workspace

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
"name": "module / dolt",
5858
"path": "../modules/dolt"
5959
},
60+
{
61+
"name": "module / dynamodb",
62+
"path": "../modules/dynamodb"
63+
},
6064
{
6165
"name": "module / elasticsearch",
6266
"path": "../modules/elasticsearch"

docs/modules/dynamodb.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# DynamoDB
2+
3+
Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>
4+
5+
## Introduction
6+
7+
The Testcontainers module for DynamoDB.
8+
9+
## Adding this module to your project dependencies
10+
11+
Please run the following command to add the DynamoDB module to your Go dependencies:
12+
13+
```
14+
go get github.com/testcontainers/testcontainers-go/modules/dynamodb
15+
```
16+
17+
## Usage example
18+
19+
<!--codeinclude-->
20+
[Creating a DynamoDB container](../../modules/dynamodb/examples_test.go) inside_block:runDynamoDBContainer
21+
<!--/codeinclude-->
22+
23+
## Module Reference
24+
25+
### Run function
26+
27+
- Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>
28+
29+
The DynamoDB module exposes one entrypoint function to create the DynamoDB container, and this function receives three parameters:
30+
31+
```golang
32+
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*DynamoDBContainer, error)
33+
```
34+
35+
- `context.Context`, the Go context.
36+
- `string`, the Docker image to use.
37+
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options.
38+
39+
### Container Options
40+
41+
When starting the DynamoDB container, you can pass options in a variadic way to configure it.
42+
43+
#### Image
44+
45+
If you need to set a different DynamoDB Docker image, you can set a valid Docker image as the second argument in the `Run` function.
46+
E.g. `Run(context.Background(), "amazon/dynamodb-local:2.2.1")`.
47+
48+
{% include "../features/common_functional_options.md" %}
49+
50+
#### WithSharedDB
51+
52+
- Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>
53+
54+
The `WithSharedDB` option tells the DynamoDB container to use a single database file. At the same time, it marks the container as reusable, which causes that successive calls to the `Run` function will return the same container instance, and therefore, the same database file.
55+
56+
#### WithDisableTelemetry
57+
58+
- Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>
59+
60+
You can turn off telemetry when starting the DynamoDB container, using the option `WithDisableTelemetry`.
61+
62+
### Container Methods
63+
64+
The DynamoDB container exposes the following methods:
65+
66+
#### ConnectionString
67+
68+
- Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>
69+
70+
The `ConnectionString` method returns the connection string to the DynamoDB container. This connection string can be used to connect to the DynamoDB container from your application,
71+
using the AWS SDK or any other DynamoDB client of your choice.
72+
73+
<!--codeinclude-->
74+
[Creating a client](../../modules/dynamodb/dynamodb_test.go) inside_block:createClient
75+
<!--/codeinclude-->
76+
77+
The above example uses `github.com/aws/aws-sdk-go-v2/service/dynamodb` to create a client and connect to the DynamoDB container.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ nav:
7676
- modules/couchbase.md
7777
- modules/databend.md
7878
- modules/dolt.md
79+
- modules/dynamodb.md
7980
- modules/elasticsearch.md
8081
- modules/gcloud.md
8182
- modules/grafana-lgtm.md

modules/dynamodb/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include ../../commons-test.mk
2+
3+
.PHONY: test
4+
test:
5+
$(MAKE) test-dynamodb

modules/dynamodb/dynamodb.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package dynamodb
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/testcontainers/testcontainers-go"
8+
"github.com/testcontainers/testcontainers-go/wait"
9+
)
10+
11+
const (
12+
port = "8000/tcp"
13+
containerName = "tc_dynamodb_local"
14+
)
15+
16+
// DynamoDBContainer represents the DynamoDB container type used in the module
17+
type DynamoDBContainer struct {
18+
testcontainers.Container
19+
}
20+
21+
// Run creates an instance of the DynamoDB container type
22+
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*DynamoDBContainer, error) {
23+
req := testcontainers.ContainerRequest{
24+
Image: img,
25+
ExposedPorts: []string{string(port)},
26+
Entrypoint: []string{"java", "-Djava.library.path=./DynamoDBLocal_lib"},
27+
Cmd: []string{"-jar", "DynamoDBLocal.jar"},
28+
WaitingFor: wait.ForListeningPort(port),
29+
}
30+
31+
genericContainerReq := testcontainers.GenericContainerRequest{
32+
ContainerRequest: req,
33+
Started: true,
34+
}
35+
36+
for _, opt := range opts {
37+
if err := opt.Customize(&genericContainerReq); err != nil {
38+
return nil, fmt.Errorf("customize: %w", err)
39+
}
40+
}
41+
42+
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
43+
var c *DynamoDBContainer
44+
if container != nil {
45+
c = &DynamoDBContainer{Container: container}
46+
}
47+
48+
if err != nil {
49+
return c, fmt.Errorf("generic container: %w", err)
50+
}
51+
52+
return c, nil
53+
}
54+
55+
// ConnectionString returns DynamoDB local endpoint host and port in <host>:<port> format
56+
func (c *DynamoDBContainer) ConnectionString(ctx context.Context) (string, error) {
57+
mappedPort, err := c.MappedPort(ctx, port)
58+
if err != nil {
59+
return "", err
60+
}
61+
62+
hostIP, err := c.Host(ctx)
63+
if err != nil {
64+
return "", err
65+
}
66+
67+
return hostIP + ":" + mappedPort.Port(), nil
68+
}
69+
70+
// WithSharedDB allows container reuse between successive runs. Data will be persisted
71+
func WithSharedDB() testcontainers.CustomizeRequestOption {
72+
return func(req *testcontainers.GenericContainerRequest) error {
73+
req.Cmd = append(req.Cmd, "-sharedDb")
74+
75+
req.Reuse = true
76+
req.Name = containerName
77+
78+
return nil
79+
}
80+
}
81+
82+
// WithDisableTelemetry - DynamoDB local will not send any telemetry
83+
func WithDisableTelemetry() testcontainers.CustomizeRequestOption {
84+
return func(req *testcontainers.GenericContainerRequest) error {
85+
// if other flags (e.g. -sharedDb) exist, append to them
86+
req.Cmd = append(req.Cmd, "-disableTelemetry")
87+
88+
return nil
89+
}
90+
}

0 commit comments

Comments
 (0)