Add ability to capture container logs, and have mssql test helper use it (#13272)

This commit is contained in:
Nick Cabatoff 2021-11-24 12:01:38 -05:00 committed by GitHub
parent ea2728a229
commit c4764433a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -40,6 +40,7 @@ type RunOptions struct {
DoNotAutoRemove bool
AuthUsername string
AuthPassword string
LogConsumer func(string)
}
func NewServiceRunner(opts RunOptions) (*Runner, error) {
@ -135,6 +136,23 @@ func (d *Runner) StartService(ctx context.Context, connect ServiceAdapter) (*Ser
}
cleanup := func() {
if d.RunOptions.LogConsumer != nil {
rc, err := d.DockerAPI.ContainerLogs(ctx, container.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Timestamps: true,
Details: true,
})
if err == nil {
b, err := ioutil.ReadAll(rc)
if err != nil {
d.RunOptions.LogConsumer(fmt.Sprintf("error reading container logs, err=%v, read: %s", err, string(b)))
} else {
d.RunOptions.LogConsumer(string(b))
}
}
}
for i := 0; i < 10; i++ {
err := d.DockerAPI.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{Force: true})
if err == nil {

View File

@ -32,6 +32,11 @@ func PrepareMSSQLTestContainer(t *testing.T) (cleanup func(), retURL string) {
ImageTag: "2017-latest-ubuntu",
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)
}
},
})
if err != nil {
t.Fatalf("Could not start docker MSSQL: %s", err)