Skip to content

Commit

Permalink
enhancement(5832): updated fixture install, updated assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
kaanyalti committed Dec 16, 2024
1 parent a1df208 commit 094c743
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
39 changes: 33 additions & 6 deletions pkg/testing/fixture_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions testing/integration/restrict_upgrade_deb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
})
}
4 changes: 2 additions & 2 deletions testing/integration/restrict_upgrade_rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
})
}

0 comments on commit 094c743

Please sign in to comment.