Skip to content

Commit

Permalink
Add check for deadline set in contexts passed to Fixture.Run() (#3849)
Browse files Browse the repository at this point in the history
* Add check for deadline set in contexts passed to Fixture.Run()

This change adds a check on the contexts used by integration test
fixture when calling `fixture.Run()`, failing the test if no deadline
has been specified.

This is to avoid having the test hanging on t.Run() when the state
specified in the FSM are never reached.

* Add timeouts to contexts in integration tests

* fixup! Add timeouts to contexts in integration tests
  • Loading branch information
pchila authored Nov 30, 2023
1 parent 93f159a commit 55b01c6
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 46 deletions.
8 changes: 8 additions & 0 deletions pkg/testing/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ func (f *Fixture) RunBeat(ctx context.Context) error {
return errors.New("RunBeat() can't be run against elastic-agent")
}

if _, deadlineSet := ctx.Deadline(); !deadlineSet {
f.t.Fatal("Context passed to Fixture.RunBeat() has no deadline set.")
}

var err error
err = f.EnsurePrepared(ctx)
if err != nil {
Expand Down Expand Up @@ -366,6 +370,10 @@ func (f *Fixture) RunBeat(ctx context.Context) error {
// The `elastic-agent.yml` generated by `Fixture.Configure` is ignored
// when `Run` is called.
func (f *Fixture) Run(ctx context.Context, states ...State) error {
if _, deadlineSet := ctx.Deadline(); !deadlineSet {
f.t.Fatal("Context passed to Fixture.Run() has no deadline set.")
}

if f.binaryName != "elastic-agent" {
return errors.New("Run() can only be used with elastic-agent, use RunBeat()")
}
Expand Down
9 changes: 4 additions & 5 deletions testing/integration/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/elastic/elastic-agent/pkg/core/process"
integrationtest "github.com/elastic/elastic-agent/pkg/testing"
"github.com/elastic/elastic-agent/pkg/testing/define"
"github.com/elastic/elastic-agent/pkg/testing/tools/testcontext"
)

const diagnosticsArchiveGlobPattern = "elastic-agent-diagnostics-*.zip"
Expand Down Expand Up @@ -95,7 +97,7 @@ func TestDiagnosticsOptionalValues(t *testing.T) {
fixture, err := define.NewFixture(t, define.Version())
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()
err = fixture.Prepare(ctx, fakeComponent, fakeShipper)
require.NoError(t, err)
Expand All @@ -121,14 +123,11 @@ func TestDiagnosticsCommand(t *testing.T) {
f, err := define.NewFixture(t, define.Version())
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()
err = f.Prepare(ctx, fakeComponent, fakeShipper)
require.NoError(t, err)

ctx, cancel = context.WithCancel(context.Background())
defer cancel()

err = f.Run(ctx, integrationtest.State{
Configure: simpleConfig2,
AgentState: integrationtest.NewClientState(client.Healthy),
Expand Down
8 changes: 4 additions & 4 deletions testing/integration/endpoint_security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func testInstallAndUnenrollWithEndpointSecurity(t *testing.T, info *define.Info,
Force: true,
}

ctx, cn := context.WithCancel(context.Background())
ctx, cn := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cn()

policy, err := tools.InstallAgentWithPolicy(ctx, t, installOpts, fixture, info.KibanaClient, createPolicyReq)
Expand Down Expand Up @@ -353,7 +353,7 @@ func testInstallWithEndpointSecurityAndRemoveEndpointIntegration(t *testing.T, i
Force: true,
}

ctx, cn := context.WithCancel(context.Background())
ctx, cn := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cn()

policy, err := tools.InstallAgentWithPolicy(ctx, t, installOpts, fixture, info.KibanaClient, createPolicyReq)
Expand Down Expand Up @@ -497,7 +497,7 @@ func TestEndpointSecurityNonDefaultBasePath(t *testing.T) {
Sudo: true, // requires Agent installation
})

ctx, cn := context.WithCancel(context.Background())
ctx, cn := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cn()

// Get path to agent executable.
Expand Down Expand Up @@ -527,7 +527,7 @@ func TestEndpointSecurityNonDefaultBasePath(t *testing.T) {
pkgPolicyResp, err := installElasticDefendPackage(t, info, policyResp.ID)
require.NoErrorf(t, err, "Policy Response was: %v", pkgPolicyResp)

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()

c := fixture.Client()
Expand Down
3 changes: 2 additions & 1 deletion testing/integration/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/elastic/elastic-agent/pkg/control/v2/client"
atesting "github.com/elastic/elastic-agent/pkg/testing"
"github.com/elastic/elastic-agent/pkg/testing/define"
"github.com/elastic/elastic-agent/pkg/testing/tools/testcontext"
)

var simpleConfig1 = `
Expand Down Expand Up @@ -51,7 +52,7 @@ func TestFakeComponent(t *testing.T) {
f, err := define.NewFixture(t, define.Version())
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()
err = f.Prepare(ctx, fakeComponent, fakeShipper)
require.NoError(t, err)
Expand Down
9 changes: 6 additions & 3 deletions testing/integration/fqdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/elastic/elastic-agent/pkg/testing/define"
"github.com/elastic/elastic-agent/pkg/testing/tools"
"github.com/elastic/elastic-agent/pkg/testing/tools/fleettools"
"github.com/elastic/elastic-agent/pkg/testing/tools/testcontext"
"github.com/elastic/go-elasticsearch/v8"
)

Expand All @@ -52,11 +53,13 @@ func TestFQDN(t *testing.T) {
origEtcHosts, err := getEtcHosts()
require.NoError(t, err)

ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()

// Save original hostname so we can restore it at the end of each test
origHostname, err := getHostname(context.Background())
origHostname, err := getHostname(ctx)
require.NoError(t, err)

ctx := context.Background()
kibClient := info.KibanaClient

shortName := strings.ToLower(randStr(6))
Expand Down Expand Up @@ -93,7 +96,7 @@ func TestFQDN(t *testing.T) {
assert.NoError(t, fleettools.UnEnrollAgent(info.KibanaClient, policy.ID))

t.Log("Restoring hostname...")
err := setHostname(context.Background(), origHostname, t.Log)
err := setHostname(ctx, origHostname, t.Log)
require.NoError(t, err)

t.Log("Restoring original /etc/hosts...")
Expand Down
19 changes: 13 additions & 6 deletions testing/integration/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

atesting "github.com/elastic/elastic-agent/pkg/testing"
"github.com/elastic/elastic-agent/pkg/testing/define"
"github.com/elastic/elastic-agent/pkg/testing/tools/testcontext"

"github.com/stretchr/testify/require"
)
Expand All @@ -38,8 +39,11 @@ func TestInstallWithoutBasePath(t *testing.T) {
fixture, err := define.NewFixture(t, define.Version())
require.NoError(t, err)

ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()

// Prepare the Elastic Agent so the binary is extracted and ready to use.
err = fixture.Prepare(context.Background())
err = fixture.Prepare(ctx)
require.NoError(t, err)

// Check that default base path is clean
Expand All @@ -59,15 +63,15 @@ func TestInstallWithoutBasePath(t *testing.T) {

// Run `elastic-agent install`. We use `--force` to prevent interactive
// execution.
out, err := fixture.Install(context.Background(), &atesting.InstallOpts{Force: true})
out, err := fixture.Install(ctx, &atesting.InstallOpts{Force: true})
if err != nil {
t.Logf("install output: %s", out)
require.NoError(t, err)
}

// Check that Agent was installed in default base path
checkInstallSuccess(t, topPath)
t.Run("check agent package version", testAgentPackageVersion(context.Background(), fixture, true))
t.Run("check agent package version", testAgentPackageVersion(ctx, fixture, true))
}

func TestInstallWithBasePath(t *testing.T) {
Expand All @@ -86,8 +90,11 @@ func TestInstallWithBasePath(t *testing.T) {
fixture, err := define.NewFixture(t, define.Version())
require.NoError(t, err)

ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()

// Prepare the Elastic Agent so the binary is extracted and ready to use.
err = fixture.Prepare(context.Background())
err = fixture.Prepare(ctx)
require.NoError(t, err)

// Set up random temporary directory to serve as base path for Elastic Agent
Expand All @@ -97,7 +104,7 @@ func TestInstallWithBasePath(t *testing.T) {

// Run `elastic-agent install`. We use `--force` to prevent interactive
// execution.
out, err := fixture.Install(context.Background(), &atesting.InstallOpts{
out, err := fixture.Install(ctx, &atesting.InstallOpts{
BasePath: randomBasePath,
Force: true,
})
Expand All @@ -109,7 +116,7 @@ func TestInstallWithBasePath(t *testing.T) {
// Check that Agent was installed in the custom base path
topPath := filepath.Join(randomBasePath, "Elastic", "Agent")
checkInstallSuccess(t, topPath)
t.Run("check agent package version", testAgentPackageVersion(context.Background(), fixture, true))
t.Run("check agent package version", testAgentPackageVersion(ctx, fixture, true))
}

func checkInstallSuccess(t *testing.T, topPath string) {
Expand Down
15 changes: 11 additions & 4 deletions testing/integration/install_unprivileged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/elastic/elastic-agent/internal/pkg/agent/install"
atesting "github.com/elastic/elastic-agent/pkg/testing"
"github.com/elastic/elastic-agent/pkg/testing/define"
"github.com/elastic/elastic-agent/pkg/testing/tools/testcontext"
)

func TestInstallUnprivilegedWithoutBasePath(t *testing.T) {
Expand All @@ -49,8 +50,11 @@ func TestInstallUnprivilegedWithoutBasePath(t *testing.T) {
fixture, err := define.NewFixture(t, define.Version())
require.NoError(t, err)

ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()

// Prepare the Elastic Agent so the binary is extracted and ready to use.
err = fixture.Prepare(context.Background())
err = fixture.Prepare(ctx)
require.NoError(t, err)

// Check that default base path is clean
Expand All @@ -70,7 +74,7 @@ func TestInstallUnprivilegedWithoutBasePath(t *testing.T) {

// Run `elastic-agent install`. We use `--force` to prevent interactive
// execution.
out, err := fixture.Install(context.Background(), &atesting.InstallOpts{Force: true, Unprivileged: true})
out, err := fixture.Install(ctx, &atesting.InstallOpts{Force: true, Unprivileged: true})
if err != nil {
t.Logf("install output: %s", out)
require.NoError(t, err)
Expand Down Expand Up @@ -103,8 +107,11 @@ func TestInstallUnprivilegedWithBasePath(t *testing.T) {
fixture, err := define.NewFixture(t, define.Version())
require.NoError(t, err)

ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()

// Prepare the Elastic Agent so the binary is extracted and ready to use.
err = fixture.Prepare(context.Background())
err = fixture.Prepare(ctx)
require.NoError(t, err)

// Other test `TestInstallWithBasePath` uses a random directory for the base
Expand All @@ -125,7 +132,7 @@ func TestInstallUnprivilegedWithBasePath(t *testing.T) {

// Run `elastic-agent install`. We use `--force` to prevent interactive
// execution.
out, err := fixture.Install(context.Background(), &atesting.InstallOpts{
out, err := fixture.Install(ctx, &atesting.InstallOpts{
BasePath: basePath,
Force: true,
Unprivileged: true,
Expand Down
5 changes: 4 additions & 1 deletion testing/integration/logs_ingestion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/elastic/elastic-agent/pkg/testing/tools/check"
"github.com/elastic/elastic-agent/pkg/testing/tools/estools"
"github.com/elastic/elastic-agent/pkg/testing/tools/fleettools"
"github.com/elastic/elastic-agent/pkg/testing/tools/testcontext"
"github.com/elastic/elastic-transport-go/v8/elastictransport"
)

Expand All @@ -43,7 +44,9 @@ func TestLogIngestionFleetManaged(t *testing.T) {
Local: false,
Sudo: true,
})
ctx := context.Background()

ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()

agentFixture, err := define.NewFixture(t, define.Version())
require.NoError(t, err)
Expand Down
6 changes: 4 additions & 2 deletions testing/integration/package_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"context"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent/pkg/control/v2/client"
atesting "github.com/elastic/elastic-agent/pkg/testing"
"github.com/elastic/elastic-agent/pkg/testing/define"
"github.com/elastic/elastic-agent/pkg/testing/tools/testcontext"
"github.com/elastic/elastic-agent/version"
)

Expand All @@ -29,7 +31,7 @@ func TestPackageVersion(t *testing.T) {
f, err := define.NewFixture(t, define.Version())
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()
err = f.Prepare(ctx, fakeComponent, fakeShipper)
require.NoError(t, err)
Expand Down Expand Up @@ -93,7 +95,7 @@ func testAfterRemovingPkgVersionFiles(ctx context.Context, f *atesting.Fixture)
}
testf := func() error {
// check the version returned by the running agent
stdout, stderr, processState := getAgentVersionOutput(t, f, context.Background(), false)
stdout, stderr, processState := getAgentVersionOutput(t, f, ctx, false)

binaryActualVersion := unmarshalVersionOutput(t, stdout, "binary")
assert.Equal(t, version.GetDefaultVersion(), binaryActualVersion, "binary version does not return default beat version when the package version file is missing")
Expand Down
2 changes: 1 addition & 1 deletion testing/integration/pkgversion_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func testAgentPackageVersion(ctx context.Context, f *integrationtest.Fixture, bi
require.NotEmpty(t, pkgVersion, "elastic agent has been packaged with an empty package version")

// check the version returned by the running agent
actualVersionBytes := getAgentVersion(t, f, context.Background(), binaryOnly)
actualVersionBytes := getAgentVersion(t, f, ctx, binaryOnly)

actualVersion := unmarshalVersionOutput(t, actualVersionBytes, "binary")
assert.Equal(t, pkgVersion, actualVersion, "binary version does not match package version")
Expand Down
Loading

0 comments on commit 55b01c6

Please sign in to comment.