From c6388d4680b66c8ba074183f84bb8f7a9757ef9f Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Thu, 16 May 2024 09:48:23 -0400 Subject: [PATCH] packer_test: Add tests for invalid plugin remove use cases --- packer_test/plugins_remove_test.go | 62 ++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/packer_test/plugins_remove_test.go b/packer_test/plugins_remove_test.go index d8596dee5..cc10b6113 100644 --- a/packer_test/plugins_remove_test.go +++ b/packer_test/plugins_remove_test.go @@ -1,6 +1,7 @@ package packer_test import ( + "path/filepath" "strings" ) @@ -27,6 +28,24 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddress() { if n := InstalledPlugins(ts, pluginPath); len(n) != 0 { ts.T().Fatalf("Expected there to be 0 installed plugins but we got %v", n) } + + ts.Run("plugins remove with incorrect source address exits non found error", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "remove", "github.com/hashicorp/testerONE"). + Assert( + MustFail(), + Grep("No installed plugin found matching the plugin constraints github.com/hashicorp/testerONE"), + ) + }) + + ts.Run("plugins remove with invalid source address exits with non-zero code", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "remove", "github.com/hashicorp/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/tester"). + Assert( + MustFail(), + Grep("The source URL must have at most 16 components"), + ) + }) } func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddressAndVersion() { @@ -94,6 +113,49 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithLocalPath() { if n := InstalledPlugins(ts, pluginPath); len(n) != 1 { ts.T().Fatalf("Expected there to be 1 installed plugins but we got %v", n) } + + ts.Run("plugins remove with incomplete local path exits with a non-zero code", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "remove", filepath.Base(plugins[0])). + Assert( + MustFail(), + Grep("A source URL must at least contain a host and a path with 2 components", grepStdout), + ) + }) + + ts.Run("plugins remove with fake local path exits with a non-zero code", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "remove", ts.T().TempDir()). + Assert( + MustFail(), + Grep("is not under the plugin directory inferred by Packer", grepStdout), + ) + }) +} + +func (ts *PackerTestSuite) TestPluginsRemoveWithNoArguments() { + pluginPath, cleanup := ts.MakePluginDir("1.0.9") + defer cleanup() + + // Get installed plugins + if n := InstalledPlugins(ts, pluginPath); len(n) != 1 { + ts.T().Fatalf("Expected there to be 1 installed plugins but we got %v", n) + } + + ts.Run("plugins remove with no options returns non-zero with help text", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "remove"). + Assert( + MustFail(), + Grep("Usage: packer plugins remove ", grepStdout), + ) + }) + + // Get installed should remain the same + 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 {