-
-
Notifications
You must be signed in to change notification settings - Fork 547
feat: add dynamodb-local module #2799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b4f3d89
feat: add dynamodb-local module
mdelapenya 9d6176c
docs: fix
mdelapenya 23fb181
fix: remove module from CI
mdelapenya a0a1fe6
fix: wrongly generated
mdelapenya cd4b013
Merge branch 'main' into dynamodb-module
mdelapenya 17f234d
chore: bump dockercfg
mdelapenya 4fbae32
Merge branch 'main' into dynamodb-module
mdelapenya 93a9d01
chore: wrap errors
mdelapenya 9d6ffbe
Revert "chore: wrap errors"
mdelapenya 7d8b804
fix: remove unused constant
mdelapenya c2ae596
chore: wrap errors
mdelapenya 57e8154
chore: simplify helper methods
mdelapenya 9dcb71f
chore: wrap methods
mdelapenya 7ff383e
fix: partially revert helpers as we need to check for errors in the c…
mdelapenya 3ff6cb9
chore: wrap error
mdelapenya 4da07f3
chore: simplify variable
mdelapenya af5abb0
fixlint
mdelapenya 63f1cdc
chore: pass testing.T
mdelapenya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# DynamoDB | ||
|
||
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> | ||
|
||
## Introduction | ||
|
||
The Testcontainers module for DynamoDB. | ||
|
||
## Adding this module to your project dependencies | ||
|
||
Please run the following command to add the DynamoDB module to your Go dependencies: | ||
|
||
``` | ||
go get github.com/testcontainers/testcontainers-go/modules/dynamodb | ||
``` | ||
|
||
## Usage example | ||
|
||
<!--codeinclude--> | ||
[Creating a DynamoDB container](../../modules/dynamodb/examples_test.go) inside_block:runDynamoDBContainer | ||
<!--/codeinclude--> | ||
|
||
## Module Reference | ||
|
||
### Run function | ||
|
||
- 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> | ||
|
||
The DynamoDB module exposes one entrypoint function to create the DynamoDB container, and this function receives three parameters: | ||
|
||
```golang | ||
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*DynamoDBContainer, error) | ||
``` | ||
|
||
- `context.Context`, the Go context. | ||
- `string`, the Docker image to use. | ||
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options. | ||
|
||
### Container Options | ||
|
||
When starting the DynamoDB container, you can pass options in a variadic way to configure it. | ||
|
||
#### Image | ||
|
||
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. | ||
E.g. `Run(context.Background(), "amazon/dynamodb-local:2.2.1")`. | ||
|
||
{% include "../features/common_functional_options.md" %} | ||
|
||
#### WithSharedDB | ||
|
||
- 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> | ||
|
||
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. | ||
|
||
#### WithDisableTelemetry | ||
|
||
- 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> | ||
|
||
You can turn off telemetry when starting the DynamoDB container, using the option `WithDisableTelemetry`. | ||
|
||
### Container Methods | ||
|
||
The DynamoDB container exposes the following methods: | ||
|
||
#### ConnectionString | ||
|
||
- 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> | ||
|
||
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, | ||
using the AWS SDK or any other DynamoDB client of your choice. | ||
|
||
<!--codeinclude--> | ||
[Creating a client](../../modules/dynamodb/dynamodb_test.go) inside_block:createClient | ||
<!--/codeinclude--> | ||
|
||
The above example uses `github.com/aws/aws-sdk-go-v2/service/dynamodb` to create a client and connect to the DynamoDB container. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
include ../../commons-test.mk | ||
|
||
.PHONY: test | ||
test: | ||
$(MAKE) test-dynamodb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package dynamodb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
const ( | ||
image = "amazon/dynamodb-local:2.2.1" | ||
mdelapenya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
port = "8000/tcp" | ||
containerName = "tc_dynamodb_local" | ||
) | ||
|
||
// DynamoDBContainer represents the DynamoDB container type used in the module | ||
type DynamoDBContainer struct { | ||
testcontainers.Container | ||
} | ||
|
||
// Run creates an instance of the DynamoDB container type | ||
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*DynamoDBContainer, error) { | ||
req := testcontainers.ContainerRequest{ | ||
Image: img, | ||
ExposedPorts: []string{string(port)}, | ||
Entrypoint: []string{"java", "-Djava.library.path=./DynamoDBLocal_lib"}, | ||
Cmd: []string{"-jar", "DynamoDBLocal.jar"}, | ||
WaitingFor: wait.ForListeningPort(port), | ||
} | ||
|
||
genericContainerReq := testcontainers.GenericContainerRequest{ | ||
ContainerRequest: req, | ||
Started: true, | ||
} | ||
|
||
for _, opt := range opts { | ||
if err := opt.Customize(&genericContainerReq); err != nil { | ||
return nil, fmt.Errorf("customize: %w", err) | ||
} | ||
} | ||
|
||
container, err := testcontainers.GenericContainer(ctx, genericContainerReq) | ||
var c *DynamoDBContainer | ||
if container != nil { | ||
c = &DynamoDBContainer{Container: container} | ||
} | ||
|
||
if err != nil { | ||
return c, fmt.Errorf("generic container: %w", err) | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
// ConnectionString returns DynamoDB local endpoint host and port in <host>:<port> format | ||
func (c *DynamoDBContainer) 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 | ||
mdelapenya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// WithSharedDB allows container reuse between successive runs. Data will be persisted | ||
func WithSharedDB() testcontainers.CustomizeRequestOption { | ||
return func(req *testcontainers.GenericContainerRequest) error { | ||
req.Cmd = append(req.Cmd, "-sharedDb") | ||
|
||
req.Reuse = true | ||
req.Name = containerName | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// WithDisableTelemetry - DynamoDB local will not send any telemetry | ||
func WithDisableTelemetry() testcontainers.CustomizeRequestOption { | ||
return func(req *testcontainers.GenericContainerRequest) error { | ||
// if other flags (e.g. -sharedDb) exist, append to them | ||
req.Cmd = append(req.Cmd, "-disableTelemetry") | ||
|
||
return nil | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.