Skip to content

Commit

Permalink
chore: support multiple aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Dec 18, 2023
1 parent 99c09c6 commit b02232c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/features/common_functional_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ You could use this feature to run a custom script, or to run a command that is n

- 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>

By default, the container is started in the default Docker network. If you want to use an already existing Docker network you created in your code, you can use the `network.WithNetwork(alias string, nw *testcontainers.DockerNetwork)` option, which receives an alias as parameter and your network, attaching the container to it, and setting the network alias for that network.
By default, the container is started in the default Docker network. If you want to use an already existing Docker network you created in your code, you can use the `network.WithNetwork(aliases []string, nw *testcontainers.DockerNetwork)` option, which receives an alias as parameter and your network, attaching the container to it, and setting the network alias for that network.

In the case you need to retrieve the network name, you can simply read it from the struct's `Name` field. E.g. `nw.Name`.

Expand All @@ -55,7 +55,7 @@ In the case you need to retrieve the network name, you can simply read it from t

- 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>

If you want to attach your containers to a throw-away network, you can use the `network.WithNewNetwork(ctx context.Context, alias string, opts ...network.NetworkCustomizer)` option, which receives an alias as parameter, creating the new network with a random name, attaching the container to it, and setting the network alias for that network.
If you want to attach your containers to a throw-away network, you can use the `network.WithNewNetwork(ctx context.Context, aliases []string, opts ...network.NetworkCustomizer)` option, which receives an alias as parameter, creating the new network with a random name, attaching the container to it, and setting the network alias for that network.

In the case you need to retrieve the network name, you can use the `Networks(ctx)` method of the `Container` interface, right after it's running, which returns a slice of strings with the names of the networks where the container is attached.

Expand Down
4 changes: 2 additions & 2 deletions modules/localstack/localstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func isVersion2(image string) bool {

// WithNetwork creates a network with the given name and attaches the container to it, setting the network alias
// on that network to the given alias.
// Deprecated: use network.WithNewNetwork instead
// Deprecated: use network.WithNetwork or network.WithNewNetwork instead
func WithNetwork(networkName string, alias string) testcontainers.CustomizeRequestOption {
return network.WithNewNetwork(context.Background(), alias, network.WithCheckDuplicate())
return network.WithNewNetwork(context.Background(), []string{alias}, network.WithCheckDuplicate())
}

// RunContainer creates an instance of the LocalStack container type, being possible to pass a custom request and options:
Expand Down
2 changes: 1 addition & 1 deletion modules/localstack/localstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestStartV2WithNetwork(t *testing.T) {

localstack, err := RunContainer(
ctx,
network.WithNetwork("localstack", nw),
network.WithNetwork([]string{"localstack"}, nw),
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "localstack/localstack:2.0.0",
Expand Down
8 changes: 4 additions & 4 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func WithIPAM(ipam *network.IPAM) CustomizeNetworkOption {

// WithNetwork reuses an already existing network, attaching the container to it.
// Finally it sets the network alias on that network to the given alias.
func WithNetwork(alias string, nw *testcontainers.DockerNetwork) testcontainers.CustomizeRequestOption {
func WithNetwork(aliases []string, nw *testcontainers.DockerNetwork) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
networkName := nw.Name

Expand All @@ -131,13 +131,13 @@ func WithNetwork(alias string, nw *testcontainers.DockerNetwork) testcontainers.
if req.NetworkAliases == nil {
req.NetworkAliases = make(map[string][]string)
}
req.NetworkAliases[networkName] = []string{alias}
req.NetworkAliases[networkName] = aliases
}
}

// WithNewNetwork creates a new network with random name and customizers, and attaches the container to it.
// Finally it sets the network alias on that network to the given alias.
func WithNewNetwork(ctx context.Context, alias string, opts ...NetworkCustomizer) testcontainers.CustomizeRequestOption {
func WithNewNetwork(ctx context.Context, aliases []string, opts ...NetworkCustomizer) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
newNetwork, err := New(ctx, opts...)
if err != nil {
Expand All @@ -157,6 +157,6 @@ func WithNewNetwork(ctx context.Context, alias string, opts ...NetworkCustomizer
if req.NetworkAliases == nil {
req.NetworkAliases = make(map[string][]string)
}
req.NetworkAliases[networkName] = []string{alias}
req.NetworkAliases[networkName] = aliases
}
}
8 changes: 4 additions & 4 deletions network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func TestWithNetwork(t *testing.T) {
ContainerRequest: testcontainers.ContainerRequest{},
}

network.WithNetwork("alias", nw)(&req)
network.WithNetwork([]string{"alias"}, nw)(&req)

assert.Equal(t, 1, len(req.Networks))
assert.Equal(t, networkName, req.Networks[0])
Expand Down Expand Up @@ -466,7 +466,7 @@ func TestWithSyntheticNetwork(t *testing.T) {
},
}

network.WithNetwork("alias", nw)(&req)
network.WithNetwork([]string{"alias"}, nw)(&req)

assert.Equal(t, 1, len(req.Networks))
assert.Equal(t, networkName, req.Networks[0])
Expand Down Expand Up @@ -500,7 +500,7 @@ func TestWithNewNetwork(t *testing.T) {
ContainerRequest: testcontainers.ContainerRequest{},
}

network.WithNewNetwork(context.Background(), "alias",
network.WithNewNetwork(context.Background(), []string{"alias"},
network.WithAttachable(),
network.WithInternal(),
network.WithLabels(map[string]string{"this-is-a-test": "value"}),
Expand Down Expand Up @@ -547,7 +547,7 @@ func TestWithNewNetworkContextTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()

network.WithNewNetwork(ctx, "alias",
network.WithNewNetwork(ctx, []string{"alias"},
network.WithAttachable(),
network.WithInternal(),
network.WithLabels(map[string]string{"this-is-a-test": "value"}),
Expand Down

0 comments on commit b02232c

Please sign in to comment.