github.com/hashicorp/packer@v1.14.3/packer_test/plugin_tests/plugins_remove_test.go (about)

     1  package plugin_tests
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/packer/packer_test/common/check"
     8  )
     9  
    10  func (ts *PackerPluginTestSuite) TestPluginsRemoveWithSourceAddress() {
    11  	pluginPath := ts.MakePluginDir().InstallPluginVersions("1.0.9", "1.0.10", "2.0.0")
    12  	defer pluginPath.Cleanup()
    13  
    14  	// Get installed plugins
    15  	if n := InstalledPlugins(ts, pluginPath.Dir()); len(n) != 3 {
    16  		ts.T().Fatalf("Expected there to be 3 installed plugins but we got  %v", n)
    17  	}
    18  
    19  	ts.Run("plugins remove with source address removes all installed plugin versions", func() {
    20  		ts.PackerCommand().UsePluginDir(pluginPath).
    21  			SetArgs("plugins", "remove", "github.com/hashicorp/tester").
    22  			Assert(check.MustSucceed(),
    23  				check.Grep("packer-plugin-tester_v1.0.9", check.GrepStdout),
    24  				check.Grep("packer-plugin-tester_v1.0.10", check.GrepStdout),
    25  				check.Grep("packer-plugin-tester_v2.0.0", check.GrepStdout),
    26  			)
    27  	})
    28  
    29  	// Get installed plugins after removal
    30  	if n := InstalledPlugins(ts, pluginPath.Dir()); len(n) != 0 {
    31  		ts.T().Fatalf("Expected there to be 0 installed plugins but we got  %v", n)
    32  	}
    33  
    34  	ts.Run("plugins remove with incorrect source address exits non found error", func() {
    35  		ts.PackerCommand().UsePluginDir(pluginPath).
    36  			SetArgs("plugins", "remove", "github.com/hashicorp/testerONE").
    37  			Assert(
    38  				check.MustFail(),
    39  				check.Grep("No installed plugin found matching the plugin constraints github.com/hashicorp/testerONE"),
    40  			)
    41  	})
    42  
    43  	ts.Run("plugins remove with invalid source address exits with non-zero code", func() {
    44  		ts.PackerCommand().UsePluginDir(pluginPath).
    45  			SetArgs("plugins", "remove", "github.com/hashicorp/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/tester").
    46  			Assert(
    47  				check.MustFail(),
    48  				check.Grep("The source URL must have at most 16 components"),
    49  			)
    50  	})
    51  }
    52  
    53  func (ts *PackerPluginTestSuite) TestPluginsRemoveWithSourceAddressAndVersion() {
    54  	pluginPath := ts.MakePluginDir().InstallPluginVersions("1.0.9", "1.0.10", "2.0.0")
    55  	defer pluginPath.Cleanup()
    56  
    57  	// Get installed plugins
    58  	if n := InstalledPlugins(ts, pluginPath.Dir()); len(n) != 3 {
    59  		ts.T().Fatalf("Expected there to be 3 installed plugins but we got  %v", n)
    60  	}
    61  
    62  	ts.Run("plugins remove with source address and version removes only the versioned plugin", func() {
    63  		ts.PackerCommand().UsePluginDir(pluginPath).
    64  			SetArgs("plugins", "remove", "github.com/hashicorp/tester", ">= 2.0.0").
    65  			Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v2.0.0", check.GrepStdout))
    66  	})
    67  
    68  	ts.Run("plugins installed after single plugins remove outputs remaining installed plugins", func() {
    69  		ts.PackerCommand().UsePluginDir(pluginPath).
    70  			SetArgs("plugins", "installed").
    71  			Assert(
    72  				check.MustSucceed(),
    73  				check.Grep("packer-plugin-tester_v1.0.9", check.GrepStdout),
    74  				check.Grep("packer-plugin-tester_v1.0.10", check.GrepStdout),
    75  				check.GrepInverted("packer-plugin-tester_v2.0.0", check.GrepStdout),
    76  			)
    77  	})
    78  
    79  	// Get installed plugins after removal
    80  	if n := InstalledPlugins(ts, pluginPath.Dir()); len(n) != 2 {
    81  		ts.T().Fatalf("Expected there to be 2 installed plugins but we got  %v", n)
    82  	}
    83  }
    84  
    85  func (ts *PackerPluginTestSuite) TestPluginsRemoveWithLocalPath() {
    86  	pluginPath := ts.MakePluginDir().InstallPluginVersions("1.0.9", "1.0.10")
    87  	defer pluginPath.Cleanup()
    88  
    89  	// Get installed plugins
    90  	plugins := InstalledPlugins(ts, pluginPath.Dir())
    91  	if len(plugins) != 2 {
    92  		ts.T().Fatalf("Expected there to be 2 installed plugins but we got  %v", len(plugins))
    93  	}
    94  
    95  	ts.Run("plugins remove with a local path removes only the specified plugin", func() {
    96  		ts.PackerCommand().UsePluginDir(pluginPath).
    97  			SetArgs("plugins", "remove", plugins[0]).
    98  			Assert(
    99  				check.MustSucceed(),
   100  				check.Grep("packer-plugin-tester_v1.0.9", check.GrepStdout),
   101  				check.GrepInverted("packer-plugin-tester_v1.0.10", check.GrepStdout),
   102  			)
   103  	})
   104  	ts.Run("plugins installed after calling plugins remove outputs remaining installed plugins", func() {
   105  		ts.PackerCommand().UsePluginDir(pluginPath).
   106  			SetArgs("plugins", "installed").
   107  			Assert(
   108  				check.MustSucceed(),
   109  				check.Grep("packer-plugin-tester_v1.0.10", check.GrepStdout),
   110  				check.GrepInverted("packer-plugin-tester_v1.0.9", check.GrepStdout),
   111  			)
   112  	})
   113  
   114  	// Get installed plugins after removal
   115  	if n := InstalledPlugins(ts, pluginPath.Dir()); len(n) != 1 {
   116  		ts.T().Fatalf("Expected there to be 1 installed plugins but we got  %v", n)
   117  	}
   118  
   119  	ts.Run("plugins remove with incomplete local path exits with a non-zero code", func() {
   120  		ts.PackerCommand().UsePluginDir(pluginPath).
   121  			SetArgs("plugins", "remove", filepath.Base(plugins[0])).
   122  			Assert(
   123  				check.MustFail(),
   124  				check.Grep("A source URL must at least contain a host and a path with 2 components", check.GrepStdout),
   125  			)
   126  	})
   127  
   128  	ts.Run("plugins remove with fake local path exits with a non-zero code", func() {
   129  		ts.PackerCommand().UsePluginDir(pluginPath).
   130  			SetArgs("plugins", "remove", ts.T().TempDir()).
   131  			Assert(
   132  				check.MustFail(),
   133  				check.Grep("is not under the plugin directory inferred by Packer", check.GrepStdout),
   134  			)
   135  	})
   136  }
   137  
   138  func (ts *PackerPluginTestSuite) TestPluginsRemoveWithNoArguments() {
   139  	pluginPath := ts.MakePluginDir().InstallPluginVersions("1.0.9")
   140  	defer pluginPath.Cleanup()
   141  
   142  	// Get installed plugins
   143  	if n := InstalledPlugins(ts, pluginPath.Dir()); len(n) != 1 {
   144  		ts.T().Fatalf("Expected there to be 1 installed plugins but we got  %v", n)
   145  	}
   146  
   147  	ts.Run("plugins remove with no options returns non-zero with help text", func() {
   148  		ts.PackerCommand().UsePluginDir(pluginPath).
   149  			SetArgs("plugins", "remove").
   150  			Assert(
   151  				check.MustFail(),
   152  				check.Grep("Usage: packer plugins remove <plugin>", check.GrepStdout),
   153  			)
   154  	})
   155  
   156  	// Get installed should remain the same
   157  	if n := InstalledPlugins(ts, pluginPath.Dir()); len(n) != 1 {
   158  		ts.T().Fatalf("Expected there to be 1 installed plugins but we got  %v", n)
   159  	}
   160  
   161  }
   162  
   163  func InstalledPlugins(ts *PackerPluginTestSuite, dir string) []string {
   164  	ts.T().Helper()
   165  
   166  	cmd := ts.PackerCommand().UseRawPluginDir(dir).
   167  		SetArgs("plugins", "installed").
   168  		SetAssertFatal()
   169  	cmd.Assert(check.MustSucceed())
   170  
   171  	out, _, _ := cmd.Output()
   172  	// Output will be split on '\n' after trimming all other white space
   173  	out = strings.TrimSpace(out)
   174  	plugins := strings.Fields(out)
   175  	n := len(plugins)
   176  	if n == 0 {
   177  		return nil
   178  	}
   179  	return plugins
   180  }