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

feat: add IPAM configs to NetworkRequest #394

Merged
merged 2 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,7 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
EnableIPv6: req.EnableIPv6,
Attachable: req.Attachable,
Labels: req.Labels,
IPAM: req.IPAM,
}

sessionID := uuid.New()
Expand Down
2 changes: 2 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testcontainers

import (
"context"
"github.com/docker/docker/api/types/network"

"github.com/docker/docker/api/types"
)
Expand All @@ -26,6 +27,7 @@ type NetworkRequest struct {
Name string
Labels map[string]string
Attachable bool
IPAM *network.IPAM

SkipReaper bool // indicates whether we skip setting up a reaper for this
ReaperImage string //alternative reaper registry
Expand Down
53 changes: 53 additions & 0 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package testcontainers
import (
"context"
"fmt"
"github.com/docker/docker/api/types/network"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go/wait"
"testing"
"time"
Expand Down Expand Up @@ -34,6 +36,57 @@ func ExampleNetworkProvider_CreateNetwork() {
defer nginxC.Terminate(ctx)
nginxC.GetContainerID()
}
func Test_NetworkWithIPAM(t *testing.T) {
ctx := context.Background()
networkName := "test-network-with-ipam"
ipamConfig := network.IPAM{
Driver: "default",
Config: []network.IPAMConfig{
{
Subnet: "10.1.1.0/24",
Gateway: "10.1.1.254",
IPRange: "10.1.1.1/25",
},
},
}
net, err := GenericNetwork(ctx, GenericNetworkRequest{
NetworkRequest: NetworkRequest{
Name: networkName,
CheckDuplicate: true,
IPAM: &ipamConfig,
},
})

if err != nil {
t.Fatal("cannot create network: ", err)
}

defer net.Remove(ctx)

nginxC, _ := GenericContainer(ctx, GenericContainerRequest{
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
ContainerRequest: ContainerRequest{
Image: "nginx",
ExposedPorts: []string{
"80/tcp",
},
Networks: []string{
networkName,
},
},
})
defer nginxC.Terminate(ctx)
nginxC.GetContainerID()

provider, err := ProviderDocker.GetProvider()
if err != nil {
t.Fatal("Cannot get Provider")
}
foundNetwork, err := provider.GetNetwork(ctx, NetworkRequest{Name: networkName})
if err != nil {
t.Fatal("Cannot get created network by name")
}
assert.Equal(t, ipamConfig, foundNetwork.IPAM)
}

func Test_MultipleContainersInTheNewNetwork(t *testing.T) {
ctx := context.Background()
Expand Down