From 96389eb266a6dcfaf412b27783105f976cb9fddc Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" <4903+rboyer@users.noreply.github.com> Date: Mon, 23 Jan 2023 14:14:24 -0600 Subject: [PATCH] test: container tests wait for available networks (#16045) --- .../consul-container/libs/cluster/cluster.go | 1 + .../consul-container/libs/cluster/network.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/test/integration/consul-container/libs/cluster/cluster.go b/test/integration/consul-container/libs/cluster/cluster.go index 73bd6d4040..7eb1b81dd1 100644 --- a/test/integration/consul-container/libs/cluster/cluster.go +++ b/test/integration/consul-container/libs/cluster/cluster.go @@ -37,6 +37,7 @@ type Cluster struct { } type TestingT interface { + Logf(format string, args ...any) Cleanup(f func()) } diff --git a/test/integration/consul-container/libs/cluster/network.go b/test/integration/consul-container/libs/cluster/network.go index edab30deeb..c7db86c189 100644 --- a/test/integration/consul-container/libs/cluster/network.go +++ b/test/integration/consul-container/libs/cluster/network.go @@ -2,11 +2,15 @@ package cluster import ( "context" + "strings" + "time" "github.com/pkg/errors" "github.com/testcontainers/testcontainers-go" ) +const tooManyNetworksError = "could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network: failed to create network" + func createNetwork(t TestingT, name string) (testcontainers.Network, error) { req := testcontainers.GenericNetworkRequest{ NetworkRequest: testcontainers.NetworkRequest{ @@ -15,12 +19,23 @@ func createNetwork(t TestingT, name string) (testcontainers.Network, error) { CheckDuplicate: true, }, } + first := true +RETRY: network, err := testcontainers.GenericNetwork(context.Background(), req) if err != nil { + if strings.Contains(err.Error(), tooManyNetworksError) { + if first { + t.Logf("waiting until possible to get a network") + first = false + } + time.Sleep(1 * time.Second) + goto RETRY + } return nil, errors.Wrap(err, "could not create network") } t.Cleanup(func() { _ = network.Remove(context.Background()) }) + return network, nil }