Skip to content

Commit

Permalink
Fix fixture install/uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
pchila committed May 31, 2023
1 parent a1b4864 commit 3cb5734
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
33 changes: 26 additions & 7 deletions pkg/testing/fixture_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"

"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"

"github.com/elastic/elastic-agent/pkg/control/v2/client"
"github.com/elastic/elastic-agent/pkg/core/process"
)

Expand Down Expand Up @@ -79,15 +81,26 @@ func (f *Fixture) Install(ctx context.Context, installOpts *InstallOpts, opts ..
f.installed = true
f.installOpts = installOpts

if installOpts.BasePath == "" {
f.workDir = filepath.Join(paths.DefaultBasePath, "Elastic", "Agent")
} else {
f.workDir = filepath.Join(installOpts.BasePath, "Elastic", "Agent")
}

//we just installed agent, the control socket is at a well-known location
c := client.New(client.WithAddress(paths.ControlSocketPath))
f.setClient(c)

f.t.Cleanup(func() {
_, err := f.Uninstall(ctx, nil)
out, err := f.Uninstall(ctx, &UninstallOpts{Force: true})
f.setClient(nil)
if errors.Is(err, ErrNotInstalled) {
// Agent fixture has already been uninstalled, perhaps by
// an explicit call to fixture.Uninstall, so nothing needs
// to be done here.
return
}
require.NoError(f.t, err)
require.NoErrorf(f.t, err, "uninstalling agent failed. Output: %q", out)
})

return out, nil
Expand Down Expand Up @@ -118,7 +131,7 @@ func (f *Fixture) Uninstall(ctx context.Context, uninstallOpts *UninstallOpts, o
}
out, err := f.Exec(ctx, uninstallArgs, opts...)
if err != nil {
return nil, err
return out, err
}

// Check that Elastic Agent files are actually removed
Expand All @@ -127,12 +140,18 @@ func (f *Fixture) Uninstall(ctx context.Context, uninstallOpts *UninstallOpts, o
basePath = paths.DefaultBasePath
}
topPath := filepath.Join(basePath, "Elastic", "Agent")
_, err = os.Stat(topPath)
if os.IsExist(err) {
return out, fmt.Errorf("Elastic Agent is still installed at [%s]", topPath) //nolint:stylecheck // Elastic Agent is a proper noun
topPathStats, err := os.Stat(topPath)
if errors.Is(err, fs.ErrNotExist) {
// the path does not exist anymore, all good!
return out, nil
}

if err != nil {
return nil, err
return out, err
}

if err != nil && topPathStats != nil {
return out, fmt.Errorf("Elastic Agent is still installed at [%s]", topPath) //nolint:stylecheck // Elastic Agent is a proper noun
}

return out, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/testing/tools/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func EnrollElasticAgent(fleetUrl string, enrollmentToken string, agentFixture *a
func InstallStandaloneElasticAgent(agentFixture *atesting.Fixture) ([]byte, error) {
installOpts := atesting.InstallOpts{
NonInteractive: true,
Force: true,
}
return agentFixture.Install(context.Background(), &installOpts)
}

0 comments on commit 3cb5734

Please sign in to comment.