Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write docker inspect output to diagnostics #3291

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions e2e/testsuite/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package diagnostics

import (
"context"
"encoding/json"
"fmt"
"os"
ospath "path"
Expand All @@ -17,7 +18,8 @@ import (
)

const (
e2eDir = "e2e"
e2eDir = "e2e"
defaultFilePerm = 0750
)

// Collect can be used in `t.Cleanup` and will copy all the of the container logs and relevant files
Expand All @@ -41,7 +43,7 @@ func Collect(t *testing.T, dc *dockerclient.Client, cfg testconfig.ChainOptions)

logsDir := fmt.Sprintf("%s/diagnostics", e2eDir)

if err := os.MkdirAll(fmt.Sprintf("%s/%s", logsDir, t.Name()), 0750); err != nil {
if err := os.MkdirAll(fmt.Sprintf("%s/%s", logsDir, t.Name()), defaultFilePerm); err != nil {
t.Logf("failed creating logs directory in test cleanup: %s", err)
return
}
Expand All @@ -55,7 +57,7 @@ func Collect(t *testing.T, dc *dockerclient.Client, cfg testconfig.ChainOptions)
for _, container := range testContainers {
containerName := getContainerName(t, container)
containerDir := fmt.Sprintf("%s/%s/%s", logsDir, t.Name(), containerName)
if err := os.MkdirAll(containerDir, 0750); err != nil {
if err := os.MkdirAll(containerDir, defaultFilePerm); err != nil {
t.Logf("failed creating logs directory for container %s: %s", containerDir, err)
continue
}
Expand Down Expand Up @@ -84,6 +86,13 @@ func Collect(t *testing.T, dc *dockerclient.Client, cfg testconfig.ChainOptions)
}
t.Logf("successfully wrote diagnostics file %s", absoluteFilePathInContainer)
}

localFilePath := ospath.Join(containerDir, "docker-inspect.json")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"docker-inspect.json" could be a const maybe, but all g!

if err := fetchAndWriteDockerInspectOutput(ctx, dc, container.ID, localFilePath); err != nil {
t.Logf("failed to fetch docker inspect output: %s", err)
continue
}
t.Logf("successfully wrote docker inspect output")
}
}

Expand Down Expand Up @@ -113,12 +122,22 @@ func fetchAndWriteDiagnosticsFile(ctx context.Context, dc *dockerclient.Client,
return err
}

filePath := ospath.Join(localPath)
if err := os.WriteFile(filePath, fileBz, 0750); err != nil {
return os.WriteFile(localPath, fileBz, defaultFilePerm)
}

// fetchAndWriteDockerInspectOutput writes the contents of docker inspect to the specified file.
func fetchAndWriteDockerInspectOutput(ctx context.Context, dc *dockerclient.Client, containerID, localPath string) error {
containerJSON, err := dc.ContainerInspect(ctx, containerID)
if err != nil {
return err
}

fileBz, err := json.MarshalIndent(containerJSON, "", "\t")
if err != nil {
return err
}

return nil
return os.WriteFile(localPath, fileBz, defaultFilePerm)
}

// chainDiagnosticAbsoluteFilePaths returns a slice of absolute file paths (in the containers) which are the files that should be
Expand Down