From 1cace90289bfd1cbbc18ca8a1927528cfdf0bfd5 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Thu, 16 May 2024 09:21:46 -0400 Subject: [PATCH] packer_test: Add tests for valid plugin remove use cases --- packer_test/install_test.go | 43 ----------- packer_test/plugins_remove_test.go | 118 +++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 43 deletions(-) create mode 100644 packer_test/plugins_remove_test.go diff --git a/packer_test/install_test.go b/packer_test/install_test.go index a513b2c98..b72d87aee 100644 --- a/packer_test/install_test.go +++ b/packer_test/install_test.go @@ -1,9 +1,5 @@ package packer_test -import ( - "strings" -) - func (ts *PackerTestSuite) TestInstallPluginWithMetadata() { tempPluginDir, cleanup := ts.MakePluginDir("1.0.0+metadata") defer cleanup() @@ -57,45 +53,6 @@ func (ts *PackerTestSuite) TestRemoteInstallWithPluginsInstall() { }) } -func (ts *PackerTestSuite) TestRemovePluginWithLocalPath() { - pluginPath, cleanup := ts.MakePluginDir("1.0.9", "1.0.10") - defer cleanup() - - // Get installed plugins - cmd := ts.PackerCommand().UsePluginDir(pluginPath). - SetArgs("plugins", "installed") - cmd.Assert(MustSucceed()) - if ts.T().Failed() { - return - } - - plugins, _, _ := cmd.Run() - pluginList := strings.Split(strings.TrimSpace(plugins), "\n") - if len(pluginList) != 2 { - ts.T().Fatalf("Not the expected installed plugins: %v; expected 2", pluginList) - } - - ts.Run("remove one plugin version with its local path", func() { - ts.PackerCommand().UsePluginDir(pluginPath). - SetArgs("plugins", "remove", pluginList[0]). - Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.9", grepStdout)) - }) - ts.Run("ensure one plugin remaining only", func() { - ts.PackerCommand().UsePluginDir(pluginPath). - SetArgs("plugins", "installed"). - Assert( - MustSucceed(), - Grep("packer-plugin-tester_v1.0.10", grepStdout), - Grep("packer-plugin-tester_v1.0.9", grepInvert, grepStdout), - ) - }) - ts.Run("remove plugin with version", func() { - ts.PackerCommand().UsePluginDir(pluginPath). - SetArgs("plugins", "remove", "github.com/hashicorp/tester", "1.0.10"). - Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.10", grepStdout)) - }) -} - func (ts *PackerTestSuite) TestInitWithNonGithubSource() { pluginPath, cleanup := ts.MakePluginDir() defer cleanup() diff --git a/packer_test/plugins_remove_test.go b/packer_test/plugins_remove_test.go new file mode 100644 index 000000000..d8596dee5 --- /dev/null +++ b/packer_test/plugins_remove_test.go @@ -0,0 +1,118 @@ +package packer_test + +import ( + "strings" +) + +func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddress() { + pluginPath, cleanup := ts.MakePluginDir("1.0.9", "1.0.10", "2.0.0") + defer cleanup() + + // Get installed plugins + if n := InstalledPlugins(ts, pluginPath); len(n) != 3 { + ts.T().Fatalf("Expected there to be 3 installed plugins but we got %v", n) + } + + ts.Run("plugins remove with source address removes all installed plugin versions", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "remove", "github.com/hashicorp/tester"). + Assert(MustSucceed(), + Grep("packer-plugin-tester_v1.0.9", grepStdout), + Grep("packer-plugin-tester_v1.0.10", grepStdout), + Grep("packer-plugin-tester_v2.0.0", grepStdout), + ) + }) + + // Get installed plugins after removal + if n := InstalledPlugins(ts, pluginPath); len(n) != 0 { + ts.T().Fatalf("Expected there to be 0 installed plugins but we got %v", n) + } +} + +func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddressAndVersion() { + pluginPath, cleanup := ts.MakePluginDir("1.0.9", "1.0.10", "2.0.0") + defer cleanup() + + // Get installed plugins + if n := InstalledPlugins(ts, pluginPath); len(n) != 3 { + ts.T().Fatalf("Expected there to be 3 installed plugins but we got %v", n) + } + + ts.Run("plugins remove with source address and version removes only the versioned plugin", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "remove", "github.com/hashicorp/tester", ">= 2.0.0"). + Assert(MustSucceed(), Grep("packer-plugin-tester_v2.0.0", grepStdout)) + }) + + ts.Run("plugins installed after single plugins remove outputs remaining installed plugins", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "installed"). + Assert( + MustSucceed(), + Grep("packer-plugin-tester_v1.0.9", grepStdout), + Grep("packer-plugin-tester_v1.0.10", grepStdout), + Grep("packer-plugin-tester_v2.0.0", grepInvert, grepStdout), + ) + }) + + // Get installed plugins after removal + if n := InstalledPlugins(ts, pluginPath); len(n) != 2 { + ts.T().Fatalf("Expected there to be 2 installed plugins but we got %v", n) + } +} + +func (ts *PackerTestSuite) TestPluginsRemoveWithLocalPath() { + pluginPath, cleanup := ts.MakePluginDir("1.0.9", "1.0.10") + defer cleanup() + + // Get installed plugins + plugins := InstalledPlugins(ts, pluginPath) + if len(plugins) != 2 { + ts.T().Fatalf("Expected there to be 2 installed plugins but we got %v", len(plugins)) + } + + ts.Run("plugins remove with a local path removes only the specified plugin", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "remove", plugins[0]). + Assert( + MustSucceed(), + Grep("packer-plugin-tester_v1.0.9", grepStdout), + Grep("packer-plugin-tester_v1.0.10", grepInvert, grepStdout), + ) + }) + ts.Run("plugins installed after calling plugins remove outputs remaining installed plugins", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "installed"). + Assert( + MustSucceed(), + Grep("packer-plugin-tester_v1.0.10", grepStdout), + Grep("packer-plugin-tester_v1.0.9", grepInvert, grepStdout), + ) + }) + + // Get installed plugins after removal + if n := InstalledPlugins(ts, pluginPath); len(n) != 1 { + ts.T().Fatalf("Expected there to be 1 installed plugins but we got %v", n) + } +} + +func InstalledPlugins(ts *PackerTestSuite, dir string) []string { + ts.T().Helper() + + cmd := ts.PackerCommand().UsePluginDir(dir). + SetArgs("plugins", "installed") + cmd.Assert(MustSucceed()) + if ts.T().Failed() { + ts.T().Fatalf("Failed to execute plugin installed for %q", dir) + } + + out, _, _ := cmd.Run() + // Output will be split on '\n' after trimming all other white space + out = strings.TrimSpace(out) + plugins := strings.Fields(out) + n := len(plugins) + if n == 0 { + return nil + } + return plugins +}