Skip to content

Commit

Permalink
[Flaky Test] TestComponentBuildHashInDiagnostics improve agent state …
Browse files Browse the repository at this point in the history
…check (#5420)

ensure the agent status has components, all components are healthy and the version info is up-to-date

(cherry picked from commit 116e73f)

# Conflicts:
#	testing/integration/package_version_test.go
  • Loading branch information
AndersonQ authored and mergify[bot] committed Sep 5, 2024
1 parent fd477ec commit 3245b41
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 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 @@ -101,7 +102,26 @@ 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 {
stateBuff.WriteString(fmt.Sprintf(
"healthy but without components: agent status: %s-%s",
client.State(status.State), status.Message))
return false
}

// the agent might be healthy but waiting its first configuration,
// in that case, there would be no components yet. Therefore, ensure
// the agent received the policy with components before proceeding with
// the test.
for _, c := range status.Components {
<<<<<<< HEAD
state := client.State(c.State)
if state != client.Healthy {
bs, err := json.MarshalIndent(status, "", " ")
Expand All @@ -112,6 +132,32 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
}

stateBuff.WriteString(fmt.Sprintf("%s not health, agent status output: %s",
=======
bs, err := json.MarshalIndent(status, "", " ")
if err != nil {
stateBuff.WriteString(fmt.Sprintf(
"%s not healthy, could not marshal status outptu: %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",
c.Name, bs))
return false
}

// there is a rare a race condition unlike to happen on a
// production scenario where the component is healthy but the
// version info delays to update. As the Status command and the
// diagnostics fetch this information in the same way, it guarantees
// the version info is up-to-date before proceeding with the test.
if c.VersionInfo.Meta.Commit == "" {
stateBuff.WriteString(fmt.Sprintf(
"%s health, but no versionInfo. agent status output: %s",
>>>>>>> 116e73f952 ([Flaky Test] TestComponentBuildHashInDiagnostics improve agent state check (#5420))
c.Name, bs))
return false
}
Expand All @@ -123,6 +169,16 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
allHealthy,
5*time.Minute, 10*time.Second,
"agent never became healthy. Last status: %v", &stateBuff)
<<<<<<< HEAD
=======
defer func() {
if !t.Failed() {
return
}

t.Logf("test failed: last status output: %#v", status)
}()
>>>>>>> 116e73f952 ([Flaky Test] TestComponentBuildHashInDiagnostics improve agent state check (#5420))

agentbeat := "agentbeat"
if runtime.GOOS == "windows" {
Expand Down Expand Up @@ -159,6 +215,31 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {

diag := t.TempDir()
extractZipArchive(t, diagZip, diag)
<<<<<<< HEAD
=======
// if the test fails, the diagnostics used is useful for debugging.
defer func() {
if !t.Failed() {
return
}

t.Logf("the test failed: trying to save the diagnostics used on the test")
diagDir, err := f.DiagDir()
if err != nil {
t.Logf("could not get diagnostics directory to save the diagnostics used on the test")
return
}

err = os.Rename(diagZip, filepath.Join(diagDir,
fmt.Sprintf("TestComponentBuildHashInDiagnostics-used-diag-%d.zip",
time.Now().Unix())))
if err != nil {
t.Logf("could not move diagnostics used in the test to %s: %v",
diagDir, err)
return
}
}()
>>>>>>> 116e73f952 ([Flaky Test] TestComponentBuildHashInDiagnostics improve agent state check (#5420))

stateFilePath := filepath.Join(diag, "state.yaml")
stateYAML, err := os.Open(stateFilePath)
Expand Down Expand Up @@ -192,6 +273,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

0 comments on commit 3245b41

Please sign in to comment.