diff --git a/helper/testhelpers/mssql/mssqlhelper.go b/helper/testhelpers/mssql/mssqlhelper.go index 018eeabbc..c6a0e0757 100644 --- a/helper/testhelpers/mssql/mssqlhelper.go +++ b/helper/testhelpers/mssql/mssqlhelper.go @@ -13,28 +13,38 @@ import ( const mssqlPassword = "yourStrong(!)Password" +// This constant is used in retrying the mssql container restart, since +// intermittently the container starts but mssql within the container +// is unreachable. +const numRetries = 3 + func PrepareMSSQLTestContainer(t *testing.T) (cleanup func(), retURL string) { if os.Getenv("MSSQL_URL") != "" { return func() {}, os.Getenv("MSSQL_URL") } - runner, err := docker.NewServiceRunner(docker.RunOptions{ - ContainerName: "sqlserver", - ImageRepo: "mcr.microsoft.com/mssql/server", - ImageTag: "2017-latest-ubuntu", - Env: []string{"ACCEPT_EULA=Y", "SA_PASSWORD=" + mssqlPassword}, - Ports: []string{"1433/tcp"}, - }) - if err != nil { - t.Fatalf("Could not start docker MSSQL: %s", err) + var err error + for i := 0; i < numRetries; i++ { + var svc *docker.Service + runner, err := docker.NewServiceRunner(docker.RunOptions{ + ContainerName: "sqlserver", + ImageRepo: "mcr.microsoft.com/mssql/server", + ImageTag: "2017-latest-ubuntu", + Env: []string{"ACCEPT_EULA=Y", "SA_PASSWORD=" + mssqlPassword}, + Ports: []string{"1433/tcp"}, + }) + if err != nil { + t.Fatalf("Could not start docker MSSQL: %s", err) + } + + svc, err = runner.StartService(context.Background(), connectMSSQL) + if err == nil { + return svc.Cleanup, svc.Config.URL().String() + } } - svc, err := runner.StartService(context.Background(), connectMSSQL) - if err != nil { - t.Fatalf("Could not start docker MSSQL: %s", err) - } - - return svc.Cleanup, svc.Config.URL().String() + t.Fatalf("Could not start docker MSSQL: %s", err) + return nil, "" } func connectMSSQL(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {