Skip to content
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

docs: use Go testable examples in modules #1603

Merged
merged 39 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ba14798
fix: rename vault container in tests
mdelapenya Sep 6, 2023
b192a81
chore: adjust comment in template
mdelapenya Sep 6, 2023
64bd746
docs(vault): use RunContainer example in usage section
mdelapenya Sep 6, 2023
14a6022
docs: update vault docs
mdelapenya Sep 6, 2023
d5b68da
docs: add RunContainer example for redpanda
mdelapenya Sep 7, 2023
4cb9f60
docs: add RunContainer example for redis
mdelapenya Sep 7, 2023
93f2a5c
docs: document creating networks
mdelapenya Sep 7, 2023
61b37ec
docs: add RunContainer example for pulsar
mdelapenya Sep 7, 2023
e547200
docs: add RunContainer example for postgres
mdelapenya Sep 7, 2023
2273da3
docs: add RunContainer example for neo4j
mdelapenya Sep 7, 2023
a2b03fd
docs: add RunContainer example for nats
mdelapenya Sep 7, 2023
8325e51
docs: update quickstart to not use testing libraries
mdelapenya Sep 7, 2023
2886ebd
docs: add RunContainer example for mysql
mdelapenya Sep 7, 2023
72cabe1
docs: add RunContainer example for mongo
mdelapenya Sep 7, 2023
86015c5
docs: add RunContainer example for mariadb
mdelapenya Sep 7, 2023
fc9b9ce
chore: use logger to print out hostname external reason
mdelapenya Sep 7, 2023
7ad9043
docs: document new Go examples file
mdelapenya Sep 7, 2023
fc9bea3
docs: add RunContainer example for localstack
mdelapenya Sep 7, 2023
9bd0377
chore: convert k3s test into a testable example
mdelapenya Sep 7, 2023
9911613
chore: convert mongodb test into testable example
mdelapenya Sep 7, 2023
8f90e0a
fix: handle errors in example tests for elasticsearch
mdelapenya Sep 7, 2023
af18064
fix: do not deprecate used fields
mdelapenya Sep 8, 2023
286c86f
chore: convert couchbase test into testable example
mdelapenya Sep 8, 2023
7a48310
chore: convert clickhouse test into testable example
mdelapenya Sep 8, 2023
ac4c5f0
docs: adjust artemis docs
mdelapenya Sep 8, 2023
a6c4780
chore: simplify postgres example
mdelapenya Sep 8, 2023
b01f6af
fix: lint
mdelapenya Sep 8, 2023
94386b9
Merge branch 'main' into migrate-examples
mdelapenya Sep 10, 2023
d411e3a
chore(deps): bump github.com/hashicorp/vault-client-go in /modules/va…
mmorel-35 Sep 11, 2023
7c5f468
ci(lint): enable gocritic linter (#1605)
mmorel-35 Sep 11, 2023
a85ac93
ci(lint): enable errorlint linter (#1604)
mmorel-35 Sep 11, 2023
d6c4221
fix: handle errors
mdelapenya Sep 11, 2023
9235361
chore(pulsar): create/remove the network properly
mdelapenya Sep 11, 2023
b5d3749
Merge branch 'main' into migrate-examples
mdelapenya Sep 11, 2023
0e28881
chore: better container logs on errors during startup
mdelapenya Sep 11, 2023
25adc7a
Revert "chore: better container logs on errors during startup"
mdelapenya Sep 11, 2023
2d91912
chore: better container logs on errors during startup
mdelapenya Sep 11, 2023
c8a8a7b
fix: do not wrap error but print it
mdelapenya Sep 11, 2023
1f64536
fix: avoid polluting default wait strategies in pulsar
mdelapenya Sep 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: add RunContainer example for localstack
  • Loading branch information
mdelapenya committed Sep 8, 2023
commit fc9bea30a5d1251200be76ef0cbaf97d9b0be9b5
85 changes: 36 additions & 49 deletions docs/modules/localstack.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,47 @@ go get github.com/testcontainers/testcontainers-go/modules/localstack
Running LocalStack as a stand-in for multiple AWS services during a test:

<!--codeinclude-->
[Creating a LocalStack container](../../modules/localstack/v1/s3_test.go) inside_block:localStackCreateContainer
[Creating a LocalStack container](../../modules/localstack/examples_test.go) inside_block:runLocalstackContainer
<!--/codeinclude-->

Environment variables listed in [Localstack's README](https://github.com/localstack/localstack#configurations) may be used to customize Localstack's configuration.
Use the `OverrideContainerRequest` option when creating the `LocalStackContainer` to apply configuration settings.

## Creating a client using the AWS SDK for Go
## Module reference

The LocalStack module exposes one single function to create the LocalStack container, and this function receives two parameters:

```golang
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*LocalStackContainer, error)
```

- `context.Context`, the Go context.
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options.

### Container Options

When starting the Localstack container, you can pass options in a variadic way to configure it.

#### Image

### Version 1
By default, the image used is `localstack:1.4.0`. If you need to use a different image, you can use `testcontainers.WithImage` option.

#### Customize the container request

It's possible to entirely override the default LocalStack container request:

<!--codeinclude-->
[Test for a LocalStack container, usinv AWS SDK v1](../../modules/localstack/v1/s3_test.go) inside_block:awsSDKClientV1
[Customize container request](../../modules/localstack/localstack_test.go) inside_block:withCustomContainerRequest
<!--/codeinclude-->

For further reference on the SDK v1, please check out the AWS docs [here](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/setting-up.html).
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.

### Version 2
In the following example you check how it's possible to set certain environment variables that are needed by the tests, the most important of them the AWS services you want to use. Besides, the container runs in a separate Docker network with an alias:

<!--codeinclude-->
[Test for a LocalStack container, usinv AWS SDK v2](../../modules/localstack/v2/s3_test.go) inside_block:awsSDKClientV2
[Overriding the default container request](../../modules/localstack/localstack_test.go) inside_block:withCustomContainerRequest
<!--/codeinclude-->

For further reference on the SDK v2, please check out the AWS docs [here](https://aws.github.io/aws-sdk-go-v2/docs/getting-started)

## Accessing hostname-sensitive services

Some Localstack APIs, such as SQS, require the container to be aware of the hostname that it is accessible on - for example, for construction of queue URLs in responses.
Expand All @@ -56,62 +73,33 @@ Once the variable is set:

* when running the Localstack container directly without a custom network defined, it is expected that all calls to the container will be from the test host. As such, the container address will be used (typically localhost or the address where the Docker daemon is running).

<!--codeinclude-->
[Localstack container running without a custom network](../../modules/localstack/localstack_test.go) inside_block:withoutNetwork
<!--/codeinclude-->

* when running the Localstack container [with a custom network defined](/features/networking/#advanced-networking), it is expected that all calls to the container will be **from other containers on that network**. `HOSTNAME_EXTERNAL` will be set to the *last* network alias that has been configured for the Localstack container.

<!--codeinclude-->
[Localstack container running with a custom network](../../modules/localstack/localstack_test.go) inside_block:withNetwork
[Localstack container running with a custom network](../../modules/localstack/examples_test.go) inside_block:localstackWithNetwork
<!--/codeinclude-->

* Other usage scenarios, such as where the Localstack container is used from both the test host and containers on a custom network are not automatically supported. If you have this use case, you should set `HOSTNAME_EXTERNAL` manually.

## Module reference

The LocalStack module exposes one single function to create the LocalStack container, and this function receives two parameters:

```golang
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*LocalStackContainer, error)
```

- `context.Context`, the Go context.
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options.

### Container Options

When starting the Localstack container, you can pass options in a variadic way to configure it.

#### Set Image
* Other usage scenarios, such as where the Localstack container is used from both the test host and containers on a custom network, are not automatically supported. If you have this use case, you should set `HOSTNAME_EXTERNAL` manually.

By default, the image used is `localstack:1.4.0`. If you need to use a different image, you can use `testcontainers.WithImage` option.

<!--codeinclude-->
[Custom Image](../../modules/localstack/localstack_test.go) inside_block:withImage
<!--/codeinclude-->
## Obtaining a client using the AWS SDK for Go

#### Customize the container request
You can use the AWS SDK for Go to create a client for the LocalStack container. The following examples show how to create a client for the S3 service, using both the SDK v1 and v2.

It's possible to entirely override the default LocalStack container request:
### Using the AWS SDK v1

<!--codeinclude-->
[Customize container request](../../modules/localstack/localstack_test.go) inside_block:withCustomContainerRequest
[AWS SDK v1](../../modules/localstack/v1/s3_test.go) inside_block:awsSDKClientV1
<!--/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.
For further reference on the SDK v1, please check out the AWS docs [here](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/setting-up.html).

In the following example you check how it's possible to set certain environment variables that are needed by the tests, the most important of them the AWS services you want to use. Besides, the container runs in a separate Docker network with an alias:
### Using the AWS SDK v2

<!--codeinclude-->
[Overriding the default container request](../../modules/localstack/localstack_test.go) inside_block:withNetwork
[AWS SDK v2](../../modules/localstack/v2/s3_test.go) inside_block:awsSDKClientV2
<!--/codeinclude-->

If you do not need to override the container request, you can simply pass the Go context to the `RunContainer` function.

<!--codeinclude-->
[Skip overriding the default container request](../../modules/localstack/localstack_test.go) inside_block:noOverrideContainerRequest
<!--/codeinclude-->
For further reference on the SDK v2, please check out the AWS docs [here](https://aws.github.io/aws-sdk-go-v2/docs/getting-started)

## Testing the module

Expand All @@ -123,6 +111,5 @@ make test
```

!!!info

At this moment, the tests for the module include tests for the S3 service, only. They live in two different Go packages of the LocalStack module,
`v1` and `v2`, where it'll be easier to add more examples for the rest of services.
115 changes: 115 additions & 0 deletions modules/localstack/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package localstack_test

import (
"context"
"fmt"
"time"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/localstack"
"github.com/testcontainers/testcontainers-go/wait"
)

func ExampleRunContainer() {
// runLocalstackContainer {
ctx := context.Background()

localstackContainer, err := localstack.RunContainer(ctx,
testcontainers.WithImage("localstack/localstack:1.4.0"),
)
if err != nil {
panic(err)
}

// Clean up the container
defer func() {
if err := localstackContainer.Terminate(ctx); err != nil {
panic(err)
}
}()
// }

state, err := localstackContainer.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

// Output:
// true
}

func ExampleRunContainer_withNetwork() {
// localstackWithNetwork {
ctx := context.Background()

nwName := "localstack-network"

_, err := testcontainers.GenericNetwork(ctx, testcontainers.GenericNetworkRequest{
NetworkRequest: testcontainers.NetworkRequest{
Name: nwName,
},
})
if err != nil {
panic(err)
}

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"}},
},
}),
)
if err != nil {
panic(err)
}
// }

// Clean up the container
defer func() {
if err := localstackContainer.Terminate(ctx); err != nil {
panic(err)
}
}()

networks, err := localstackContainer.Networks(ctx)
if err != nil {
panic(err)
}

fmt.Println(len(networks))
fmt.Println(networks[0])

// Output:
// 1
// localstack-network
}

func ExampleRunContainer_legacyMode() {
ctx := context.Background()

_, 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),
},
}),
)
if err == nil {
panic(err)
}

fmt.Println(err)

// Output:
// version=localstack/localstack:0.10.0. Testcontainers for Go does not support running LocalStack in legacy mode. Please use a version >= 0.11.0
}
2 changes: 0 additions & 2 deletions modules/localstack/localstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func isVersion2(image string) bool {
// RunContainer creates an instance of the LocalStack container type, being possible to pass a custom request and options:
// - overrideReq: a function that can be used to override the default container request, usually used to set the image version, environment variables for localstack, etc.
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*LocalStackContainer, error) {
// defaultContainerRequest {
dockerHost := testcontainersdocker.ExtractDockerSocket(ctx)

req := testcontainers.ContainerRequest{
Expand All @@ -71,7 +70,6 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
ExposedPorts: []string{fmt.Sprintf("%d/tcp", defaultPort)},
Env: map[string]string{},
}
// }

localStackReq := LocalStackContainerRequest{
GenericContainerRequest: testcontainers.GenericContainerRequest{
Expand Down
28 changes: 0 additions & 28 deletions modules/localstack/localstack_legacy_mode_test.go

This file was deleted.

Loading