From b871fe727270deec2eb7eb1a67f74e7d2ded19ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 29 Mar 2023 19:35:57 +0200 Subject: [PATCH] Revert "chore: use new container request customisation in localstack" This reverts commit 376dc26dd40687d1b340bfbf62a2c30669847b23. --- docs/modules/localstack.md | 18 ++++++++++----- modules/localstack/localstack.go | 22 +++++++----------- modules/localstack/localstack_test.go | 25 ++++++++++++-------- modules/localstack/types.go | 33 ++++++++++++--------------- modules/localstack/v1/s3_test.go | 4 ++-- modules/localstack/v2/s3_test.go | 4 ++-- 6 files changed, 54 insertions(+), 52 deletions(-) diff --git a/docs/modules/localstack.md b/docs/modules/localstack.md index bfb4b0ee92..7aada1f93f 100644 --- a/docs/modules/localstack.md +++ b/docs/modules/localstack.md @@ -19,7 +19,7 @@ Running LocalStack as a stand-in for multiple AWS services during a test: Environment variables listed in [Localstack's README](https://github.com/localstack/localstack#configurations) may be used to customize Localstack's configuration. -Use the `testcontainers.CustomizeContainerRequest` option when creating the LocalStack `Container` to apply configuration settings. +Use the `OverrideContainerRequest` option when creating the `LocalStackContainer` to apply configuration settings. ## Creating a client using the AWS SDK for Go @@ -64,21 +64,21 @@ Testcontainers will inform Localstack of the best hostname automatically, using 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.CustomizeContainerRequestOption) (*Container, error) { +func StartContainer(ctx context.Context, overrideReq OverrideContainerRequestOption) (*LocalStackContainer, error) ``` - `context.Context` -- `testcontainers.CustomizeContainerRequestOption` +- `OverrideContainerRequestOption` -### CustomizeContainerRequestOption +### OverrideContainerRequestOption -The variadic `testcontainers.CustomizeContainerRequestOption` functional option represents a way to override the default LocalStack container request: +The `OverrideContainerRequestOption` functional option represents a way to override the default LocalStack container request: [Default container request](../../modules/localstack/localstack.go) inside_block:defaultContainerRequest -With simply passing your own instance of an `testcontainers.CustomizeContainerRequest` type 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. +With simply passing your own instance of an `OverrideContainerRequestOption` type to the `StartContainer` 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 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: @@ -86,6 +86,12 @@ In the following example you check how it's possible to set certain environment [Overriding the default container request](../../modules/localstack/localstack_test.go) inside_block:withNetwork +If you do not need to override the container request, you can pass `nil` or the `NoopOverrideContainerRequest` instance, which is exposed as a helper for this reason. + + +[Skip overriding the default container request](../../modules/localstack/localstack_test.go) inside_block:noopOverrideContainerRequest + + ## Testing the module The module includes unit and integration tests that can be run from its source code. To run the tests please execute the following command: diff --git a/modules/localstack/localstack.go b/modules/localstack/localstack.go index 686b87437d..4c36a3f71b 100644 --- a/modules/localstack/localstack.go +++ b/modules/localstack/localstack.go @@ -40,9 +40,9 @@ func isLegacyMode(image string) bool { return true } -// RunContainer creates an instance of the LocalStack container type, being possible to pass a custom request and options: +// StartContainer 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.CustomizeContainerRequestOption) (*Container, error) { +func StartContainer(ctx context.Context, overrideReq OverrideContainerRequestOption) (*LocalStackContainer, error) { // defaultContainerRequest { req := testcontainers.ContainerRequest{ Image: fmt.Sprintf("localstack/localstack:%s", defaultVersion), @@ -53,13 +53,14 @@ func RunContainer(ctx context.Context, opts ...testcontainers.CustomizeContainer } // } - localStackReq := ContainerRequest{ + localStackReq := LocalStackContainerRequest{ ContainerRequest: req, } // first, when needed, we merge the user request with the default one - for _, opt := range opts { - localStackReq.ContainerRequest = opt(localStackReq.ContainerRequest) + if overrideReq != nil { + merged := overrideReq(localStackReq.ContainerRequest) + localStackReq.ContainerRequest = merged } if isLegacyMode(localStackReq.Image) { @@ -80,20 +81,13 @@ func RunContainer(ctx context.Context, opts ...testcontainers.CustomizeContainer return nil, err } - c := &Container{ + c := &LocalStackContainer{ Container: container, } return c, nil } -// StartContainer 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. -// Deprecated use RunContainer instead -func StartContainer(ctx context.Context, overrideReq OverrideContainerRequestOption) (*Container, error) { - return RunContainer(ctx, testcontainers.CustomizeContainerRequestOption(overrideReq)) -} - -func configureDockerHost(req *ContainerRequest) (reason string, err error) { +func configureDockerHost(req *LocalStackContainerRequest) (reason string, err error) { err = nil reason = "" diff --git a/modules/localstack/localstack_test.go b/modules/localstack/localstack_test.go index 456f5b4974..fa1c7ca410 100644 --- a/modules/localstack/localstack_test.go +++ b/modules/localstack/localstack_test.go @@ -10,8 +10,8 @@ import ( "github.com/testcontainers/testcontainers-go" ) -func generateContainerRequest() *ContainerRequest { - return &ContainerRequest{ +func generateContainerRequest() *LocalStackContainerRequest { + return &LocalStackContainerRequest{ ContainerRequest: testcontainers.ContainerRequest{ Env: map[string]string{}, ExposedPorts: []string{}, @@ -91,13 +91,13 @@ func TestIsLegacyMode(t *testing.T) { } } -func TestRunContainer(t *testing.T) { +func TestStart(t *testing.T) { ctx := context.Background() // withoutNetwork { - container, err := RunContainer( + container, err := StartContainer( ctx, - testcontainers.CustomizeContainerRequest(testcontainers.ContainerRequest{ + OverrideContainerRequest(testcontainers.ContainerRequest{ Image: fmt.Sprintf("localstack/localstack:%s", defaultVersion), }), ) @@ -122,15 +122,20 @@ func TestRunContainer(t *testing.T) { }) } -func TestRunContainerWithoutOverride(t *testing.T) { +func TestStartWithoutOverride(t *testing.T) { + // noopOverrideContainerRequest { ctx := context.Background() - container, err := RunContainer(ctx) + container, err := StartContainer( + ctx, + NoopOverrideContainerRequest, + ) require.Nil(t, err) assert.NotNil(t, container) + // } } -func TestRunContainerWithNetwork(t *testing.T) { +func TestStartWithNetwork(t *testing.T) { // withNetwork { ctx := context.Background() @@ -142,9 +147,9 @@ func TestRunContainerWithNetwork(t *testing.T) { require.Nil(t, err) assert.NotNil(t, nw) - container, err := RunContainer( + container, err := StartContainer( ctx, - testcontainers.CustomizeContainerRequest(testcontainers.ContainerRequest{ + OverrideContainerRequest(testcontainers.ContainerRequest{ Image: "localstack/localstack:0.13.0", Env: map[string]string{"SERVICES": "s3,sqs"}, Networks: []string{"localstack-network"}, diff --git a/modules/localstack/types.go b/modules/localstack/types.go index 5eca3981c9..9f08095d69 100644 --- a/modules/localstack/types.go +++ b/modules/localstack/types.go @@ -1,43 +1,40 @@ package localstack import ( + "fmt" + + "github.com/imdario/mergo" "github.com/testcontainers/testcontainers-go" ) -// Container represents the LocalStack container type used in the module -type Container struct { - testcontainers.Container -} - -// Deprecated: use Container instead +// LocalStackContainer represents the LocalStack container type used in the module type LocalStackContainer struct { - Container + testcontainers.Container } -// ContainerRequest represents the LocalStack container request type used in the module +// LocalStackContainerRequest represents the LocalStack container request type used in the module // to configure the container -type ContainerRequest struct { - testcontainers.ContainerRequest -} - -// Deprecated: use ContainerRequest instead type LocalStackContainerRequest struct { - ContainerRequest + testcontainers.ContainerRequest } // OverrideContainerRequestOption is a type that can be used to configure the Testcontainers container request. // The passed request will be merged with the default one. -// Deprecated: use testcontainers.CustomizeContainerRequestOption instead type OverrideContainerRequestOption func(req testcontainers.ContainerRequest) testcontainers.ContainerRequest // NoopOverrideContainerRequest returns a helper function that does not override the container request -// Deprecated var NoopOverrideContainerRequest = func(req testcontainers.ContainerRequest) testcontainers.ContainerRequest { return req } // OverrideContainerRequest returns a function that can be used to merge the passed container request with one that is created by the LocalStack container -// Deprecated: use testcontainers.CustomizeContainerRequestOption instead func OverrideContainerRequest(r testcontainers.ContainerRequest) func(req testcontainers.ContainerRequest) testcontainers.ContainerRequest { - return testcontainers.CustomizeContainerRequest(r) + return func(req testcontainers.ContainerRequest) testcontainers.ContainerRequest { + if err := mergo.Merge(&req, r, mergo.WithOverride); err != nil { + fmt.Printf("error merging container request %v. Keeping the default one: %v", err, req) + return req + } + + return req + } } diff --git a/modules/localstack/v1/s3_test.go b/modules/localstack/v1/s3_test.go index 8895f64123..3d795b5e1f 100644 --- a/modules/localstack/v1/s3_test.go +++ b/modules/localstack/v1/s3_test.go @@ -28,7 +28,7 @@ const ( // awsSDKClientV1 { // awsSession returns a new AWS session for the given service. To retrieve the specific AWS service client, use the // session's client method, e.g. s3manager.NewUploader(session). -func awsSession(ctx context.Context, l *localstack.Container) (*session.Session, error) { +func awsSession(ctx context.Context, l *localstack.LocalStackContainer) (*session.Session, error) { mappedPort, err := l.MappedPort(ctx, nat.Port("4566/tcp")) if err != nil { return &session.Session{}, err @@ -62,7 +62,7 @@ func TestS3(t *testing.T) { ctx := context.Background() // localStackCreateContainer { - container, err := localstack.RunContainer(ctx) + container, err := localstack.StartContainer(ctx, localstack.NoopOverrideContainerRequest) require.Nil(t, err) // } diff --git a/modules/localstack/v2/s3_test.go b/modules/localstack/v2/s3_test.go index fcb0eada8e..c80d534b39 100644 --- a/modules/localstack/v2/s3_test.go +++ b/modules/localstack/v2/s3_test.go @@ -25,7 +25,7 @@ const ( ) // awsSDKClientV2 { -func s3Client(ctx context.Context, l *localstack.Container) (*s3.Client, error) { +func s3Client(ctx context.Context, l *localstack.LocalStackContainer) (*s3.Client, error) { mappedPort, err := l.MappedPort(ctx, nat.Port("4566/tcp")) if err != nil { return nil, err @@ -72,7 +72,7 @@ func s3Client(ctx context.Context, l *localstack.Container) (*s3.Client, error) func TestS3(t *testing.T) { ctx := context.Background() - container, err := localstack.RunContainer(ctx) + container, err := localstack.StartContainer(ctx, localstack.NoopOverrideContainerRequest) require.Nil(t, err) s3Client, err := s3Client(ctx, container)