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

[Flaky Test] TestComponentBuildHashInDiagnostics improve agent state check #5420

Merged
Changes from 1 commit
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
49 changes: 40 additions & 9 deletions testing/integration/package_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -102,23 +103,40 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
return false
}

if client.State(status.State) != client.Healthy {
stateBuff.WriteString(fmt.Sprintf(
"agent isn't healthy: %s-%s",
client.State(status.State), status.Message))
return false
}

if len(status.Components) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a comment above this to explain why this check is required being it really fixed the flaky issue in the test.

stateBuff.WriteString(fmt.Sprintf(
"healty but without components: agent status: %s-%s",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"healty but without components: agent status: %s-%s",
"healthy but without components: agent status: %s-%s",

client.State(status.State), status.Message))
return false
}

for _, c := range status.Components {
bs, err := json.MarshalIndent(status, "", " ")
if err != nil {
stateBuff.WriteString(fmt.Sprintf("%s not health, could not marshal status outptu: %v",
stateBuff.WriteString(fmt.Sprintf(
"%s not health, could not marshal status outptu: %v",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"%s not health, could not marshal status outptu: %v",
"%s not healthy, could not marshal status output: %v",

c.Name, err))
return false
}

state := client.State(c.State)
if state != client.Healthy {
stateBuff.WriteString(fmt.Sprintf("%s not health, agent status output: %s",
stateBuff.WriteString(fmt.Sprintf(
"%s not health, agent status output: %s",
c.Name, bs))
return false
}

if c.VersionInfo.Meta.Commit == "" {
stateBuff.WriteString(fmt.Sprintf("%s health, but no versionInfo. agent status output: %s",
stateBuff.WriteString(fmt.Sprintf(
"%s health, but no versionInfo. agent status output: %s",
c.Name, bs))
return false
}
Expand All @@ -130,13 +148,13 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
allHealthy,
5*time.Minute, 10*time.Second,
"agent never became healthy. Last status: %v", &stateBuff)
t.Cleanup(func() {
defer func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we switching from Cleanup to defer here? As far as I'm aware these should be interchangeable if we aren't using subtests.

Copy link
Member Author

Choose a reason for hiding this comment

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

one of them attempts to move a file from a tempDir to a persistent one and it failed. So I'm guessing it might be because the temp directory was cleaned up before my function run. So I'm attempting to avoid it by using just defer

if !t.Failed() {
return
}

t.Logf("test failed: last status output: %v", status)
})
t.Logf("test failed: last status output: %#v", status)
}()

agentbeat := "agentbeat"
if runtime.GOOS == "windows" {
Expand Down Expand Up @@ -173,8 +191,8 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {

diag := t.TempDir()
extractZipArchive(t, diagZip, diag)
// the test is flaky, so collecting some data to analyze later.
t.Cleanup(func() {
// if the test fails, the diagnostics used is useful for debugging.
defer func() {
if !t.Failed() {
return
}
Expand All @@ -194,7 +212,7 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
diagDir, err)
return
}
})
}()

stateFilePath := filepath.Join(diag, "state.yaml")
stateYAML, err := os.Open(stateFilePath)
Expand Down Expand Up @@ -228,6 +246,19 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
assert.Equalf(t, wantBuildHash, c.State.VersionInfo.Meta.Commit,
"component %s: VersionInfo.Meta.Commit mismatch", c.ID)
}

if t.Failed() {
_, seek := stateYAML.Seek(0, 0)
if seek != nil {
t.Logf("could not reset state.yaml offset to print it")
return
}
data, err := io.ReadAll(stateYAML)
if err != nil {
t.Logf("could not read state.yaml: %v", err)
}
t.Logf("test failed: state.yaml contents: %q", string(data))
}
}

func testVersionWithRunningAgent(runCtx context.Context, f *atesting.Fixture) func(*testing.T) {
Expand Down
Loading