From 806c97b5f5b674543986ca09cf579de277ba29c4 Mon Sep 17 00:00:00 2001 From: Branden Clark Date: Wed, 25 Sep 2024 15:22:50 -0400 Subject: [PATCH 1/5] succeed remove if Agent Product is not installed --- pkg/fleet/installer/service/datadog_agent_windows.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/fleet/installer/service/datadog_agent_windows.go b/pkg/fleet/installer/service/datadog_agent_windows.go index 5b5b9097cb56f..d978663d1964d 100644 --- a/pkg/fleet/installer/service/datadog_agent_windows.go +++ b/pkg/fleet/installer/service/datadog_agent_windows.go @@ -100,7 +100,10 @@ func RemoveAgent(ctx context.Context) (err error) { } span.Finish(tracer.WithError(err)) }() - err = removeProduct("Datadog Agent") + // 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. + err = removeAgentIfInstalled(ctx) return err } From 8a6c4e9f3ad5dd2d9e8499301c068fda0527f6d7 Mon Sep 17 00:00:00 2001 From: Branden Clark Date: Wed, 25 Sep 2024 15:41:17 -0400 Subject: [PATCH 2/5] save outputdir on suite --- test/new-e2e/tests/installer/windows/base_suite.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/new-e2e/tests/installer/windows/base_suite.go b/test/new-e2e/tests/installer/windows/base_suite.go index 5a3fd22833af5..02c2c8aa2389d 100644 --- a/test/new-e2e/tests/installer/windows/base_suite.go +++ b/test/new-e2e/tests/installer/windows/base_suite.go @@ -61,6 +61,7 @@ type BaseInstallerSuite struct { currentAgentVersion agentVersion.Version stableInstallerVersion PackageVersion stableAgentVersion PackageVersion + outputDir string } // Installer the Datadog Installer for testing. @@ -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. @@ -132,3 +134,7 @@ func (s *BaseInstallerSuite) BeforeTest(suiteName, testName string) { func (s *BaseInstallerSuite) Require() *suiteasserts.SuiteAssertions { return suiteasserts.New(s.BaseSuite.Require(), s) } + +func (s *BaseInstallerSuite) OutputDir() string { + return s.outputDir +} From 2ca990d98bae67fdb42b3aa04a48a4fc30817d6e Mon Sep 17 00:00:00 2001 From: Branden Clark Date: Wed, 25 Sep 2024 15:41:35 -0400 Subject: [PATCH 3/5] add e2e test --- .../suites/agent-package/install_test.go | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/test/new-e2e/tests/installer/windows/suites/agent-package/install_test.go b/test/new-e2e/tests/installer/windows/suites/agent-package/install_test.go index b318186b53f72..38fd858783940 100644 --- a/test/new-e2e/tests/installer/windows/suites/agent-package/install_test.go +++ b/test/new-e2e/tests/installer/windows/suites/agent-package/install_test.go @@ -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" ) @@ -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) }) } @@ -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 @@ -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") } From beb09dde5e3fbc09c636ffd2d50d3b0fd31a9c78 Mon Sep 17 00:00:00 2001 From: Branden Clark Date: Wed, 25 Sep 2024 19:19:20 -0400 Subject: [PATCH 4/5] revive --- test/new-e2e/tests/installer/windows/base_suite.go | 1 + 1 file changed, 1 insertion(+) diff --git a/test/new-e2e/tests/installer/windows/base_suite.go b/test/new-e2e/tests/installer/windows/base_suite.go index 02c2c8aa2389d..cde5d2be78610 100644 --- a/test/new-e2e/tests/installer/windows/base_suite.go +++ b/test/new-e2e/tests/installer/windows/base_suite.go @@ -135,6 +135,7 @@ 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 } From 123eaf2315bf41ba77da01f3b6cf28e332884f0a Mon Sep 17 00:00:00 2001 From: Branden Clark Date: Thu, 26 Sep 2024 10:45:20 -0400 Subject: [PATCH 5/5] fix recursion, oops --- .../service/datadog_agent_windows.go | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/pkg/fleet/installer/service/datadog_agent_windows.go b/pkg/fleet/installer/service/datadog_agent_windows.go index d978663d1964d..35284e0bb2351 100644 --- a/pkg/fleet/installer/service/datadog_agent_windows.go +++ b/pkg/fleet/installer/service/datadog_agent_windows.go @@ -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 } @@ -91,20 +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)) - }() // 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. - err = removeAgentIfInstalled(ctx) - return err + return removeAgentIfInstalled(ctx) } // ConfigureAgent noop @@ -121,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 }