Skip to content

Commit

Permalink
Merge pull request #394 from aivinog1/299-network-ipam
Browse files Browse the repository at this point in the history
feat: add IPAM configs to `NetworkRequest`
  • Loading branch information
mdelapenya committed Jun 6, 2022
2 parents b0e8ee3 + c77d94f commit c6d9b74
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
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{
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

0 comments on commit c6d9b74

Please sign in to comment.