2022-11-01 19:03:23 +00:00
package cluster
import (
"context"
2023-01-23 20:14:24 +00:00
"strings"
"time"
2022-11-01 19:03:23 +00:00
"github.com/pkg/errors"
"github.com/testcontainers/testcontainers-go"
)
2023-01-23 20:14:24 +00:00
const tooManyNetworksError = "could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network: failed to create network"
2023-01-11 21:34:27 +00:00
func createNetwork ( t TestingT , name string ) ( testcontainers . Network , error ) {
2022-11-01 19:03:23 +00:00
req := testcontainers . GenericNetworkRequest {
NetworkRequest : testcontainers . NetworkRequest {
Name : name ,
Attachable : true ,
CheckDuplicate : true ,
} ,
}
2023-01-23 20:14:24 +00:00
first := true
RETRY :
2022-11-01 19:03:23 +00:00
network , err := testcontainers . GenericNetwork ( context . Background ( ) , req )
if err != nil {
2023-01-23 20:14:24 +00:00
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
}
2022-11-01 19:03:23 +00:00
return nil , errors . Wrap ( err , "could not create network" )
}
2023-01-11 21:34:27 +00:00
t . Cleanup ( func ( ) {
_ = network . Remove ( context . Background ( ) )
} )
2023-01-23 20:14:24 +00:00
2022-11-01 19:03:23 +00:00
return network , nil
}