Skip to content

Commit

Permalink
chore: use withEnv in localstack module (#2337)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya authored Mar 8, 2024
1 parent c3992ed commit afeac2c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/modules/localstack.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ By default, the image used is `localstack:1.4.0`. If you need to use a differen
It's possible to entirely override the default LocalStack container request:
<!--codeinclude-->
[Customize container request](../../modules/localstack/localstack_test.go) inside_block:withCustomContainerRequest
[Customize container request](../../modules/localstack/examples_test.go) inside_block:withCustomContainerRequest
<!--/codeinclude-->
With simply passing the `testcontainers.CustomizeRequest` functional option to the `RunContainer` function, you'll be able to configure the LocalStack container with your own needs, as this new container request will be merged with the original one.

In the above example you can check how it's possible to set certain environment variables that are needed by the tests, the most important ones are the AWS services you want to use. Besides, the container runs in a separate Docker network with an alias.
In the above example you can check how it's possible to copy files that are needed by the tests. The `flagsFn` function is a helper function that converts Docker labels used by Ryuk to a string with the format requested by LocalStack.
## Accessing hostname-sensitive services
Expand Down
31 changes: 12 additions & 19 deletions modules/localstack/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,9 @@ func ExampleRunContainer_withNetwork() {

localstackContainer, err := localstack.RunContainer(
ctx,
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "localstack/localstack:0.13.0",
Env: map[string]string{"SERVICES": "s3,sqs"},
Networks: []string{nwName},
NetworkAliases: map[string][]string{nwName: {"localstack"}},
},
}),
testcontainers.WithImage("localstack/localstack:0.13.0"),
testcontainers.WithEnv(map[string]string{"SERVICES": "s3,sqs"}),
network.WithNetwork([]string{nwName}, newNetwork),
)
if err != nil {
log.Fatalf("failed to start container: %s", err)
Expand Down Expand Up @@ -99,13 +94,9 @@ func ExampleRunContainer_legacyMode() {

_, err := localstack.RunContainer(
ctx,
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "localstack/localstack:0.10.0",
Env: map[string]string{"SERVICES": "s3,sqs"},
WaitingFor: wait.ForLog("Ready.").WithStartupTimeout(5 * time.Minute).WithOccurrence(1),
},
}),
testcontainers.WithImage("localstack/localstack:0.10.0"),
testcontainers.WithEnv(map[string]string{"SERVICES": "s3,sqs"}),
testcontainers.WithWaitStrategy(wait.ForLog("Ready.").WithStartupTimeout(5*time.Minute).WithOccurrence(1)),
)
if err == nil {
log.Fatalf("expected an error, got nil")
Expand Down Expand Up @@ -133,14 +124,15 @@ func ExampleRunContainer_usingLambdas() {

lambdaName := "localstack-lambda-url-example"

// withCustomContainerRequest {
container, err := localstack.RunContainer(ctx,
testcontainers.WithImage("localstack/localstack:2.3.0"),
testcontainers.WithEnv(map[string]string{
"SERVICES": "lambda",
"LAMBDA_DOCKER_FLAGS": flagsFn(),
}),
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"SERVICES": "lambda",
"LAMBDA_DOCKER_FLAGS": flagsFn(),
},
Files: []testcontainers.ContainerFile{
{
HostFilePath: filepath.Join("testdata", "function.zip"),
Expand All @@ -149,6 +141,7 @@ func ExampleRunContainer_usingLambdas() {
},
},
}),
// }
)
if err != nil {
log.Fatalf("failed to start container: %s", err)
Expand Down
10 changes: 2 additions & 8 deletions modules/localstack/localstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,15 @@ func TestStartWithoutOverride(t *testing.T) {
func TestStartV2WithNetwork(t *testing.T) {
ctx := context.Background()

// withCustomContainerRequest {
nw, err := network.New(ctx)
require.NoError(t, err)

localstack, err := RunContainer(
ctx,
network.WithNetwork([]string{"localstack"}, nw),
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "localstack/localstack:2.0.0",
Env: map[string]string{"SERVICES": "s3,sqs"},
},
}),
testcontainers.WithImage("localstack/localstack:2.0.0"),
testcontainers.WithEnv(map[string]string{"SERVICES": "s3,sqs"}),
)
// }
require.NoError(t, err)
assert.NotNil(t, localstack)

Expand Down

0 comments on commit afeac2c

Please sign in to comment.