From 094c743ec99be90730cf71fe9b867fa96644e41e Mon Sep 17 00:00:00 2001 From: kaanyalti Date: Tue, 3 Dec 2024 22:23:14 -0500 Subject: [PATCH] enhancement(5832): updated fixture install, updated assertions --- pkg/testing/fixture_install.go | 39 ++++++++++++++++--- .../integration/restrict_upgrade_deb_test.go | 4 +- .../integration/restrict_upgrade_rpm_test.go | 4 +- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/pkg/testing/fixture_install.go b/pkg/testing/fixture_install.go index cf33122a93b..77b7d98a283 100644 --- a/pkg/testing/fixture_install.go +++ b/pkg/testing/fixture_install.go @@ -175,6 +175,14 @@ func (i *InstallOpts) ToCmdArgs() []string { // - the combined output of Install command stdout and stderr // - an error if any. func (f *Fixture) Install(ctx context.Context, installOpts *InstallOpts, opts ...process.CmdOption) ([]byte, error) { + return f.installFunc(ctx, installOpts, true, opts...) +} + +func (f *Fixture) InstallWithoutEnroll(ctx context.Context, installOpts *InstallOpts, opts ...process.CmdOption) ([]byte, error) { + return f.installFunc(ctx, installOpts, false, opts...) +} + +func (f *Fixture) installFunc(ctx context.Context, installOpts *InstallOpts, shouldEnroll bool, opts ...process.CmdOption) ([]byte, error) { f.t.Logf("[test %s] Inside fixture install function", f.t.Name()) // check for running agents before installing, but only if not installed into a namespace whose point is allowing two agents at once. @@ -184,11 +192,11 @@ func (f *Fixture) Install(ctx context.Context, installOpts *InstallOpts, opts .. switch f.packageFormat { case "targz", "zip": - return f.installNoPkgManager(ctx, installOpts, opts) + return f.installNoPkgManager(ctx, installOpts, shouldEnroll, opts) case "deb": - return f.installDeb(ctx, installOpts, opts) + return f.installDeb(ctx, installOpts, shouldEnroll, opts) case "rpm": - return f.installRpm(ctx, installOpts, opts) + return f.installRpm(ctx, installOpts, shouldEnroll, opts) default: return nil, fmt.Errorf("package format %s isn't supported yet", f.packageFormat) } @@ -202,14 +210,25 @@ func (f *Fixture) Install(ctx context.Context, installOpts *InstallOpts, opts .. // It returns: // - the combined output of Install command stdout and stderr // - an error if any. -func (f *Fixture) installNoPkgManager(ctx context.Context, installOpts *InstallOpts, opts []process.CmdOption) ([]byte, error) { +func (f *Fixture) installNoPkgManager(ctx context.Context, installOpts *InstallOpts, shouldEnroll bool, opts []process.CmdOption) ([]byte, error) { f.t.Logf("[test %s] Inside fixture installNoPkgManager function", f.t.Name()) if installOpts == nil { // default options when not provided installOpts = &InstallOpts{} } + // Removes install params to prevent enrollment + removeEnrollParams := func(installOpts *InstallOpts) { + installOpts.URL = "" + installOpts.EnrollmentToken = "" + installOpts.ESHost = "" + } + installArgs := []string{"install"} + if !shouldEnroll { + removeEnrollParams(installOpts) + } + installArgs = append(installArgs, installOpts.ToCmdArgs()...) out, err := f.Exec(ctx, installArgs, opts...) if err != nil { @@ -410,7 +429,7 @@ func getProcesses(t *gotesting.T, regex string) []runningProcess { // It returns: // - the combined output of Install command stdout and stderr // - an error if any. -func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, opts []process.CmdOption) ([]byte, error) { +func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, shouldEnroll bool, opts []process.CmdOption) ([]byte, error) { f.t.Logf("[test %s] Inside fixture installDeb function", f.t.Name()) // Prepare so that the f.srcPackage string is populated err := f.EnsurePrepared(ctx) @@ -456,6 +475,10 @@ func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, opts return out, fmt.Errorf("systemctl start elastic-agent failed: %w", err) } + if !shouldEnroll { + return nil, nil + } + // apt install doesn't enroll, so need to do that enrollArgs := []string{"elastic-agent", "enroll"} if installOpts.Force { @@ -491,7 +514,7 @@ func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, opts // It returns: // - the combined output of Install command stdout and stderr // - an error if any. -func (f *Fixture) installRpm(ctx context.Context, installOpts *InstallOpts, opts []process.CmdOption) ([]byte, error) { +func (f *Fixture) installRpm(ctx context.Context, installOpts *InstallOpts, shouldEnroll bool, opts []process.CmdOption) ([]byte, error) { f.t.Logf("[test %s] Inside fixture installRpm function", f.t.Name()) // Prepare so that the f.srcPackage string is populated err := f.EnsurePrepared(ctx) @@ -530,6 +553,10 @@ func (f *Fixture) installRpm(ctx context.Context, installOpts *InstallOpts, opts return out, fmt.Errorf("systemctl start elastic-agent failed: %w", err) } + if !shouldEnroll { + return nil, nil + } + // rpm install doesn't enroll, so need to do that enrollArgs := []string{"elastic-agent", "enroll"} if installOpts.Force { diff --git a/testing/integration/restrict_upgrade_deb_test.go b/testing/integration/restrict_upgrade_deb_test.go index d4589227b0b..8290a4d1563 100644 --- a/testing/integration/restrict_upgrade_deb_test.go +++ b/testing/integration/restrict_upgrade_deb_test.go @@ -7,7 +7,7 @@ import ( "testing" "time" - "github.com/elastic/elastic-agent/internal/pkg/agent/cmd" + "github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator" atesting "github.com/elastic/elastic-agent/pkg/testing" "github.com/elastic/elastic-agent/pkg/testing/define" "github.com/stretchr/testify/require" @@ -49,6 +49,6 @@ func TestRestrictUpgradeDeb(t *testing.T) { out, err := fixture.Exec(ctx, []string{"upgrade", "1.0.0"}) require.Error(t, err) - require.Contains(t, string(out), cmd.UpgradeDisabledError.Error()) + require.Contains(t, string(out), coordinator.ErrNotUpgradable.Error()) }) } diff --git a/testing/integration/restrict_upgrade_rpm_test.go b/testing/integration/restrict_upgrade_rpm_test.go index f99d52f9e07..293acb08efe 100644 --- a/testing/integration/restrict_upgrade_rpm_test.go +++ b/testing/integration/restrict_upgrade_rpm_test.go @@ -7,7 +7,7 @@ import ( "testing" "time" - "github.com/elastic/elastic-agent/internal/pkg/agent/cmd" + "github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator" atesting "github.com/elastic/elastic-agent/pkg/testing" "github.com/elastic/elastic-agent/pkg/testing/define" "github.com/stretchr/testify/require" @@ -49,6 +49,6 @@ func TestRestrictUpgradeRPM(t *testing.T) { out, err := fixture.Exec(ctx, []string{"upgrade", "1.0.0"}) require.Error(t, err) - require.Contains(t, string(out), cmd.UpgradeDisabledError.Error()) + require.Contains(t, string(out), coordinator.ErrNotUpgradable.Error()) }) }