test: container tests wait for available networks (#16045)

This commit is contained in:
R.B. Boyer 2023-01-23 14:14:24 -06:00 committed by GitHub
parent 7b3b0f469b
commit 96389eb266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -37,6 +37,7 @@ type Cluster struct {
}
type TestingT interface {
Logf(format string, args ...any)
Cleanup(f func())
}

View File

@ -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
}