Skip to content

Commit

Permalink
randomized non-overlapping networks on 10.A.B.0/20
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Oct 4, 2024
1 parent c9fa8f0 commit 86688e2
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion test/utils/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ package utils
import (
"context"
"fmt"
"strconv"
"strings"

dockerNet "github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
"github.com/kwilteam/kwil-db/core/utils/random"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/network"
)

// EnsureNetworkExist creates a new docker network with a random UUID name.
func EnsureNetworkExist(ctx context.Context, testName string) (
*testcontainers.DockerNetwork, error) {
// random subnet 10.A.B.0/20 with 4096 addresses each, not overlapping
rng := random.New2()
randA := strconv.Itoa(rng.IntN(255))
randB := strconv.Itoa(rng.IntN(16) * 16)
subnet := "10." + randA + "." + randB + ".0/20"
net, err := network.New(ctx,
network.WithCheckDuplicate(),
network.WithAttachable(),
Expand All @@ -22,7 +29,7 @@ func EnsureNetworkExist(ctx context.Context, testName string) (
Options: map[string]string{},
Config: []dockerNet.IPAMConfig{
{
Subnet: "10.9.0.0/16",
Subnet: subnet,
},
},
}),
Expand All @@ -31,6 +38,8 @@ func EnsureNetworkExist(ctx context.Context, testName string) (
network.WithDriver("bridge"),
)

fmt.Printf("subnet: %s\n", subnet)

if err != nil {
return nil, fmt.Errorf("failed to create docker network for %s: %w", testName, err)
} else {
Expand Down

0 comments on commit 86688e2

Please sign in to comment.