Skip to content

Commit

Permalink
Fix TestEnvWithDefault and TestContainerCMDEventToStderr (elastic#5361)
Browse files Browse the repository at this point in the history
Now TestEnvWithDefault unsets all environment variables it sets,
allowing it to be run multiple times using `-count`.

TestContainerCMDEventToStderr did not call agentFixture.Prepare early
enough leading to an empty STATE_PATH env var, so all state
information was in /usr/share/elastic-agent, which could make
subsequent tests to fail because they could read
/usr/share/elastic-agent/state/container-paths.yml and use a state
path different than the one set in the test.

---------

Co-authored-by: Paolo Chilà <[email protected]>
  • Loading branch information
belimawr and pchila authored Sep 6, 2024
1 parent c113a26 commit 632eec6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions internal/pkg/agent/cmd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ func TestEnvWithDefault(t *testing.T) {
require.Equal(t, def, res)

err := os.Setenv(key1, "key1")
defer os.Unsetenv(key1)
if err != nil {
t.Skipf("could not export env var: %s", err)
}

err = os.Setenv(key2, "key2")
defer os.Unsetenv(key2)
if err != nil {
t.Skipf("could not export env var: %s", err)
}
Expand Down
5 changes: 4 additions & 1 deletion testing/integration/container_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ func TestContainerCMDEventToStderr(t *testing.T) {
agentFixture, err := define.NewFixtureFromLocalBuild(t, define.Version())
require.NoError(t, err)

// We call agentFixture.Prepare to set the workdir
require.NoError(t, agentFixture.Prepare(ctx), "failed preparing agent fixture")

_, outputID := createMockESOutput(t, info)
policyID, enrollmentAPIKey := createPolicy(
t,
Expand Down Expand Up @@ -366,7 +369,7 @@ func TestContainerCMDEventToStderr(t *testing.T) {
// the agent logs will be present in the error message
// which should help to explain why the agent was not
// healthy.
err := agentFixture.IsHealthy(ctx)
err := agentFixture.IsHealthy(ctx, withEnv(env))
return err == nil
},
2*time.Minute, time.Second,
Expand Down

0 comments on commit 632eec6

Please sign in to comment.