Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return success from RemoveAgent when the Agent MSI/Product is not installed #29577

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions pkg/fleet/installer/service/datadog_agent_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func SetupAgent(ctx context.Context, args []string) (err error) {
span.Finish(tracer.WithError(err))
}()
// Make sure there are no Agent already installed
_ = removeProduct("Datadog Agent")
_ = removeAgentIfInstalled(ctx)
err = installAgentPackage("stable", args)
return err
}
Expand Down Expand Up @@ -91,17 +91,10 @@ func PromoteAgentExperiment(_ context.Context) error {

// RemoveAgent stops and removes the agent
func RemoveAgent(ctx context.Context) (err error) {
span, _ := tracer.StartSpanFromContext(ctx, "remove_agent")
defer func() {
if err != nil {
// removal failed, this should rarely happen.
// Rollback might have restored the Agent, but we can't be sure.
log.Errorf("Failed to remove agent: %s", err)
}
span.Finish(tracer.WithError(err))
}()
err = removeProduct("Datadog Agent")
return err
// Don't return an error if the Agent is already not installed.
// returning an error here will prevent the package from being removed
// from the local repository.
return removeAgentIfInstalled(ctx)
}

// ConfigureAgent noop
Expand All @@ -118,9 +111,18 @@ func installAgentPackage(target string, args []string) error {
return nil
}

func removeAgentIfInstalled(ctx context.Context) error {
func removeAgentIfInstalled(ctx context.Context) (err error) {
if isProductInstalled("Datadog Agent") {
err := RemoveAgent(ctx)
span, _ := tracer.StartSpanFromContext(ctx, "remove_agent")
defer func() {
if err != nil {
// removal failed, this should rarely happen.
// Rollback might have restored the Agent, but we can't be sure.
log.Errorf("Failed to remove agent: %s", err)
}
span.Finish(tracer.WithError(err))
}()
err := removeProduct("Datadog Agent")
if err != nil {
return err
}
Expand Down
13 changes: 10 additions & 3 deletions test/new-e2e/tests/installer/windows/base_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type BaseInstallerSuite struct {
currentAgentVersion agentVersion.Version
stableInstallerVersion PackageVersion
stableAgentVersion PackageVersion
outputDir string
}

// Installer the Datadog Installer for testing.
Expand Down Expand Up @@ -115,10 +116,11 @@ func (s *BaseInstallerSuite) SetupSuite() {
func (s *BaseInstallerSuite) BeforeTest(suiteName, testName string) {
s.BaseSuite.BeforeTest(suiteName, testName)

outputDir, err := runner.GetTestOutputDir(runner.GetProfile(), s.T())
var err error
s.outputDir, err = runner.GetTestOutputDir(runner.GetProfile(), s.T())
s.Require().NoError(err, "should get output dir")
s.T().Logf("Output dir: %s", outputDir)
s.installer = NewDatadogInstaller(s.Env(), outputDir)
s.T().Logf("Output dir: %s", s.outputDir)
s.installer = NewDatadogInstaller(s.Env(), s.outputDir)
}

// Require instantiates a suiteAssertions for the current suite.
Expand All @@ -132,3 +134,8 @@ func (s *BaseInstallerSuite) BeforeTest(suiteName, testName string) {
func (s *BaseInstallerSuite) Require() *suiteasserts.SuiteAssertions {
return suiteasserts.New(s.BaseSuite.Require(), s)
}

// OutputDir returns the output directory for the test
func (s *BaseInstallerSuite) OutputDir() string {
return s.outputDir
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
package agenttests

import (
"path/filepath"

"github.com/DataDog/datadog-agent/test/new-e2e/pkg/e2e"
"github.com/DataDog/datadog-agent/test/new-e2e/pkg/environments/aws/host/windows"
installerwindows "github.com/DataDog/datadog-agent/test/new-e2e/tests/installer/windows"
windowsAgent "github.com/DataDog/datadog-agent/test/new-e2e/tests/windows/common/agent"

"testing"
)

Expand All @@ -30,7 +34,7 @@ func TestAgentInstalls(t *testing.T) {
func (s *testAgentInstallSuite) TestInstallAgentPackage() {
s.Run("Install", func() {
s.installAgent()
s.Run("Uninstall", s.uninstallAgent)
s.Run("Uninstall", s.removeAgentPackage)
})
}

Expand All @@ -45,7 +49,7 @@ func (s *testAgentInstallSuite) installAgent() {
s.Require().Host(s.Env().RemoteHost).HasARunningDatadogAgentService()
}

func (s *testAgentInstallSuite) uninstallAgent() {
func (s *testAgentInstallSuite) removeAgentPackage() {
// Arrange

// Act
Expand All @@ -54,4 +58,33 @@ func (s *testAgentInstallSuite) uninstallAgent() {
// Assert
s.Require().NoErrorf(err, "failed to remove the Datadog Agent package: %s", output)
s.Require().Host(s.Env().RemoteHost).HasNoDatadogAgentService()
s.Require().Host(s.Env().RemoteHost).
NoDirExists(installerwindows.GetStableDirFor(installerwindows.AgentPackage),
"the package directory should be removed")
}

func (s *testAgentInstallSuite) TestRemoveAgentAfterMSIUninstall() {
// Arrange
s.installAgent()
s.uninstallAgentWithMSI()

// Act

// Assert
s.removeAgentPackage()
}

func (s *testAgentInstallSuite) uninstallAgentWithMSI() {
// Arrange

// Act
err := windowsAgent.UninstallAgent(s.Env().RemoteHost,
filepath.Join(s.OutputDir(), "uninstall.log"),
)

// Assert
s.Require().NoErrorf(err, "failed to uninstall the Datadog Agent package")
s.Require().Host(s.Env().RemoteHost).
DirExists(installerwindows.GetStableDirFor(installerwindows.AgentPackage),
"the package directory should still exist after manually uninstalling the Agent with the MSI")
}
Loading