Skip to content

Commit de3dec4

Browse files
authored
save diagnostic and agent status on failed test (#5408)
1 parent d8bdd71 commit de3dec4

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

pkg/testing/fixture_install.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func getProcesses(t *gotesting.T, regex string) []runningProcess {
402402
// - an error if any.
403403
func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, opts []process.CmdOption) ([]byte, error) {
404404
f.t.Logf("[test %s] Inside fixture installDeb function", f.t.Name())
405-
//Prepare so that the f.srcPackage string is populated
405+
// Prepare so that the f.srcPackage string is populated
406406
err := f.EnsurePrepared(ctx)
407407
if err != nil {
408408
return nil, fmt.Errorf("failed to prepare: %w", err)
@@ -483,7 +483,7 @@ func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, opts
483483
// - an error if any.
484484
func (f *Fixture) installRpm(ctx context.Context, installOpts *InstallOpts, opts []process.CmdOption) ([]byte, error) {
485485
f.t.Logf("[test %s] Inside fixture installRpm function", f.t.Name())
486-
//Prepare so that the f.srcPackage string is populated
486+
// Prepare so that the f.srcPackage string is populated
487487
err := f.EnsurePrepared(ctx)
488488
if err != nil {
489489
return nil, fmt.Errorf("failed to prepare: %w", err)
@@ -649,12 +649,12 @@ func (f *Fixture) collectDiagnostics() {
649649
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
650650
defer cancel()
651651

652-
dir, err := findProjectRoot(f.caller)
652+
diagPath, err := f.DiagDir()
653653
if err != nil {
654-
f.t.Logf("failed to collect diagnostics; failed to find project root: %s", err)
654+
f.t.Logf("failed to collect diagnostics: %v", err)
655655
return
656656
}
657-
diagPath := filepath.Join(dir, "build", "diagnostics")
657+
658658
err = os.MkdirAll(diagPath, 0755)
659659
if err != nil {
660660
f.t.Logf("failed to collect diagnostics; failed to create %s: %s", diagPath, err)
@@ -699,6 +699,18 @@ func (f *Fixture) collectDiagnostics() {
699699
}
700700
}
701701

702+
// DiagDir returned {projectRoot}/build/diagnostics path. Files on this path
703+
// are saved if any test fails. Use it to save files for further investigation.
704+
func (f *Fixture) DiagDir() (string, error) {
705+
dir, err := findProjectRoot(f.caller)
706+
if err != nil {
707+
return "", fmt.Errorf("failed to find project root: %w", err)
708+
}
709+
710+
diagPath := filepath.Join(dir, "build", "diagnostics")
711+
return diagPath, nil
712+
}
713+
702714
func (f *Fixture) archiveInstallDirectory(installPath string, outputPath string) error {
703715
file, err := os.Create(outputPath)
704716
if err != nil {

testing/integration/package_version_test.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
9191
"failed to install start agent [output: %s]", string(output))
9292

9393
stateBuff := bytes.Buffer{}
94+
var status atesting.AgentStatusOutput
9495
allHealthy := func() bool {
9596
stateBuff.Reset()
9697

97-
status, err := f.ExecStatus(ctx)
98+
status, err = f.ExecStatus(ctx)
9899
if err != nil {
99100
stateBuff.WriteString(fmt.Sprintf("failed to get agent status: %v",
100101
err))
@@ -111,7 +112,6 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
111112

112113
state := client.State(c.State)
113114
if state != client.Healthy {
114-
115115
stateBuff.WriteString(fmt.Sprintf("%s not health, agent status output: %s",
116116
c.Name, bs))
117117
return false
@@ -130,6 +130,13 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
130130
allHealthy,
131131
5*time.Minute, 10*time.Second,
132132
"agent never became healthy. Last status: %v", &stateBuff)
133+
t.Cleanup(func() {
134+
if !t.Failed() {
135+
return
136+
}
137+
138+
t.Logf("test failed: last status output: %v", status)
139+
})
133140

134141
agentbeat := "agentbeat"
135142
if runtime.GOOS == "windows" {
@@ -166,6 +173,28 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
166173

167174
diag := t.TempDir()
168175
extractZipArchive(t, diagZip, diag)
176+
// the test is flaky, so collecting some data to analyze later.
177+
t.Cleanup(func() {
178+
if !t.Failed() {
179+
return
180+
}
181+
182+
t.Logf("the test failed: trying to save the diagnostics used on the test")
183+
diagDir, err := f.DiagDir()
184+
if err != nil {
185+
t.Logf("could not get diagnostics directory to save the diagnostics used on the test")
186+
return
187+
}
188+
189+
err = os.Rename(diagZip, filepath.Join(diagDir,
190+
fmt.Sprintf("TestComponentBuildHashInDiagnostics-used-diag-%d.zip",
191+
time.Now().Unix())))
192+
if err != nil {
193+
t.Logf("could not move diagnostics used in the test to %s: %v",
194+
diagDir, err)
195+
return
196+
}
197+
})
169198

170199
stateFilePath := filepath.Join(diag, "state.yaml")
171200
stateYAML, err := os.Open(stateFilePath)

0 commit comments

Comments
 (0)