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 Sep 27, 2024
1 parent cb6b0a4 commit d8bffa4
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 d8bffa4

Please # to comment.