Skip to content

Commit

Permalink
Export host orchestrator logs as an e2e test artifact.
Browse files Browse the repository at this point in the history
  • Loading branch information
ser-io committed Oct 2, 2024
1 parent cf6b327 commit 0ab29e3
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions e2etests/orchestration/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,33 @@ func (h *DockerHelper) RunContainer(img string, hostPort int) (string, error) {
return createRes.ID, nil
}

func (h *DockerHelper) PullLogs(id string) error {
// `TEST_UNDECLARED_OUTPUTS_DIR` env var is defined by bazel
// https://bazel.build/reference/test-encyclopedia#initial-conditions
val, ok := os.LookupEnv("TEST_UNDECLARED_OUTPUTS_DIR")
if !ok {
return errors.New("`TEST_UNDECLARED_OUTPUTS_DIR` was not found")
}
filename := filepath.Join(val, "container.log")
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
opts := types.ContainerLogsOptions{ShowStdout: true}
reader, err := h.client.ContainerLogs(ctx, id, opts)
if err != nil {
return err
}
_, err = io.Copy(f, reader)
if err != nil && err != io.EOF {
return err
}
return nil
}

func (h *DockerHelper) RemoveContainer(id string) error {
if err := h.client.ContainerRemove(context.TODO(), id, types.ContainerRemoveOptions{Force: true}); err != nil {
return err
Expand All @@ -144,6 +171,9 @@ func Cleanup(ctx *TestContext) {
log.Printf("cleanup: failed creating docker helper: %s\n", err)
return
}
if err := dockerHelper.PullLogs(ctx.DockerContainerID); err != nil {
log.Printf("cleanup: failed pulling container logs: %s\n", err)
}
if err := dockerHelper.RemoveContainer(ctx.DockerContainerID); err != nil {
log.Printf("cleanup: failed removing container: %s\n", err)
return
Expand Down

0 comments on commit 0ab29e3

Please sign in to comment.