Skip to content

Commit

Permalink
Add a core test logger to help capture the MSSQL container output (#2…
Browse files Browse the repository at this point in the history
…8472)

* Add a core test logger to help capture the MSSQL container output

 - I believe the if t.Failed prevents the logging of the container
   logging as when executed the test isn't considered failed yet.
 - Use a test core logger so that we can capture the container output
   all the time and get it from the captured log files when the test
   fails

* bump image tag to 2022-latest

---------

Co-authored-by: JM Faircloth <[email protected]>
  • Loading branch information
stevendpclark and fairclothjm authored Sep 23, 2024
1 parent 6f13aec commit 6acfc8e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions helper/testhelpers/mssql/mssqlhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"testing"

"github.com/hashicorp/vault/helper/testhelpers/corehelpers"
"github.com/hashicorp/vault/sdk/helper/docker"
)

Expand All @@ -32,34 +33,36 @@ func PrepareMSSQLTestContainer(t *testing.T) (cleanup func(), retURL string) {
return func() {}, os.Getenv("MSSQL_URL")
}

logger := corehelpers.NewTestLogger(t)

var err error
for i := 0; i < numRetries; i++ {
var svc *docker.Service
var runner *docker.Runner
runner, err = docker.NewServiceRunner(docker.RunOptions{
ContainerName: "sqlserver",
ImageRepo: "mcr.microsoft.com/mssql/server",
ImageTag: "2017-latest-ubuntu",
ImageTag: "2022-latest",
Env: []string{"ACCEPT_EULA=Y", "SA_PASSWORD=" + mssqlPassword},
Ports: []string{"1433/tcp"},
LogConsumer: func(s string) {
if t.Failed() {
t.Logf("container logs: %s", s)
}
logger.Info(s)
},
})
if err != nil {
t.Logf("Could not start docker MSSQL: %v", err)
logger.Error("failed creating new service runner", "error", err.Error())
continue
}

svc, err = runner.StartService(context.Background(), connectMSSQL)
if err == nil {
return svc.Cleanup, svc.Config.URL().String()
}

logger.Error("failed starting service", "error", err.Error())
}

t.Fatalf("Could not start docker MSSQL: %v", err)
t.Fatalf("Could not start docker MSSQL last error: %v", err)
return nil, ""
}

Expand Down

0 comments on commit 6acfc8e

Please sign in to comment.