Unverified Commit 0e9db8be authored by Nick Cabatoff's avatar Nick Cabatoff Committed by GitHub
Browse files

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

parent 0514a8e2
Showing with 23 additions and 0 deletions
+23 -0
...@@ -40,6 +40,7 @@ type RunOptions struct { ...@@ -40,6 +40,7 @@ type RunOptions struct {
DoNotAutoRemove bool DoNotAutoRemove bool
AuthUsername string AuthUsername string
AuthPassword string AuthPassword string
LogConsumer func(string)
} }
func NewServiceRunner(opts RunOptions) (*Runner, error) { func NewServiceRunner(opts RunOptions) (*Runner, error) {
...@@ -135,6 +136,23 @@ func (d *Runner) StartService(ctx context.Context, connect ServiceAdapter) (*Ser ...@@ -135,6 +136,23 @@ func (d *Runner) StartService(ctx context.Context, connect ServiceAdapter) (*Ser
} }
cleanup := func() { 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++ { for i := 0; i < 10; i++ {
err := d.DockerAPI.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{Force: true}) err := d.DockerAPI.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{Force: true})
if err == nil { if err == nil {
......
...@@ -32,6 +32,11 @@ func PrepareMSSQLTestContainer(t *testing.T) (cleanup func(), retURL string) { ...@@ -32,6 +32,11 @@ func PrepareMSSQLTestContainer(t *testing.T) (cleanup func(), retURL string) {
ImageTag: "2017-latest-ubuntu", ImageTag: "2017-latest-ubuntu",
Env: []string{"ACCEPT_EULA=Y", "SA_PASSWORD=" + mssqlPassword}, Env: []string{"ACCEPT_EULA=Y", "SA_PASSWORD=" + mssqlPassword},
Ports: []string{"1433/tcp"}, Ports: []string{"1433/tcp"},
LogConsumer: func(s string) {
if t.Failed() {
t.Logf("container logs: %s", s)
}
},
}) })
if err != nil { if err != nil {
t.Fatalf("Could not start docker MSSQL: %s", err) t.Fatalf("Could not start docker MSSQL: %s", err)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment