Add ability to capture container logs, and have mssql test helper use it (#13272)
This commit is contained in:
parent
ea2728a229
commit
c4764433a8
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue