From 1d65ad676fdcbac3145566ba1a0334a2b3e274e7 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Thu, 23 May 2024 15:28:08 -0400 Subject: [PATCH] Add test cases for init command These changes include a series of test cases for validating packer init using the force and upgrade flag. Include in this test is a test case for validating the plugin installation error when init encounters a plugin whose reported version does not match the version within the plugin name. ``` --- PASS: Test_PackerCoreSuite (18.66s) --- PASS: Test_PackerCoreSuite/TestPackerInitForce (4.91s) --- PASS: Test_PackerCoreSuite/TestPackerInitForce/installs_any_missing_plugins (2.76s) --- PASS: Test_PackerCoreSuite/TestPackerInitForce/reinstalls_plugins_matching_version_constraints (2.14s) --- PASS: Test_PackerCoreSuite/TestPackerInitUpgrade (3.70s) --- PASS: Test_PackerCoreSuite/TestPackerInitUpgrade/upgrades_a_plugin_to_the_latest_matching_version_constraints (2.02s) --- PASS: Test_PackerCoreSuite/TestPackerInitWithMixedVersions (1.96s) --- PASS: Test_PackerCoreSuite/TestPackerInitWithMixedVersions/skips_the_plugin_installation_with_mixed_versions_before_exiting_with_an_error (1.96s) --- PASS: Test_PackerCoreSuite/TestPackerInitWithNonGithubSource (1.22s) --- PASS: Test_PackerCoreSuite/TestPackerInitWithNonGithubSource/try_installing_from_a_non-github_source,_should_fail (0.07s) --- PASS: Test_PackerCoreSuite/TestPackerInitWithNonGithubSource/manually_install_plugin_to_the_expected_source (0.59s) --- PASS: Test_PackerCoreSuite/TestPackerInitWithNonGithubSource/re-run_packer_init_on_same_template,_should_succeed_silently (0.55s) ``` --- packer_test/init_test.go | 74 +++++++++++++++++++ packer_test/install_test.go | 24 ------ packer_test/templates/init/hashicups.pkr.hcl | 9 +++ .../templates/init/mixed_versions.pkr.hcl | 9 +++ .../templates/{ => init}/non_gh.pkr.hcl | 0 5 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 packer_test/init_test.go create mode 100644 packer_test/templates/init/hashicups.pkr.hcl create mode 100644 packer_test/templates/init/mixed_versions.pkr.hcl rename packer_test/templates/{ => init}/non_gh.pkr.hcl (100%) diff --git a/packer_test/init_test.go b/packer_test/init_test.go new file mode 100644 index 000000000..6a9196a94 --- /dev/null +++ b/packer_test/init_test.go @@ -0,0 +1,74 @@ +package packer_test + +func (ts *PackerTestSuite) TestPackerInitForce() { + pluginPath, cleanup := ts.MakePluginDir() + defer cleanup() + + ts.Run("installs any missing plugins", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("init", "--force", "./templates/init/hashicups.pkr.hcl"). + Assert(MustSucceed(), Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", grepStdout)) + }) + + ts.Run("reinstalls plugins matching version constraints", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("init", "--force", "./templates/init/hashicups.pkr.hcl"). + Assert(MustSucceed(), Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", grepStdout)) + }) +} + +func (ts *PackerTestSuite) TestPackerInitUpgrade() { + pluginPath, cleanup := ts.MakePluginDir() + defer cleanup() + + cmd := ts.PackerCommand().UsePluginDir(pluginPath) + cmd.SetArgs("plugins", "install", "github.com/hashicorp/hashicups", "1.0.1") + cmd.Assert(MustSucceed(), Grep("Installed plugin github.com/hashicorp/hashicups v1.0.1", grepStdout)) + + _, _, err := cmd.Run() + if err != nil { + ts.T().Fatalf("packer plugins install failed to install previous version of hashicups: %q", err) + } + + ts.Run("upgrades a plugin to the latest matching version constraints", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("init", "--upgrade", "./templates/init/hashicups.pkr.hcl"). + Assert(MustSucceed(), Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", grepStdout)) + }) +} + +func (ts *PackerTestSuite) TestPackerInitWithNonGithubSource() { + pluginPath, cleanup := ts.MakePluginDir() + defer cleanup() + + ts.Run("try installing from a non-github source, should fail", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("init", "./templates/init/non_gh.pkr.hcl"). + Assert(MustFail(), Grep(`doesn't appear to be a valid "github.com" source address`, grepStdout)) + }) + + ts.Run("manually install plugin to the expected source", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("plugins", "install", "--path", BuildSimplePlugin("1.0.10", ts.T()), "hubgit.com/hashicorp/tester"). + Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.10", grepStdout)) + }) + + ts.Run("re-run packer init on same template, should succeed silently", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("init", "./templates/init/non_gh.pkr.hcl"). + Assert(MustSucceed(), + MkPipeCheck("no output in stdout").SetTester(ExpectEmptyInput()).SetStream(OnlyStdout)) + }) +} + +func (ts *PackerTestSuite) TestPackerInitWithMixedVersions() { + pluginPath, cleanup := ts.MakePluginDir() + defer cleanup() + + ts.Run("skips the plugin installation with mixed versions before exiting with an error", func() { + ts.PackerCommand().UsePluginDir(pluginPath). + SetArgs("init", "./templates/init/mixed_versions.pkr.hcl"). + Assert(MustFail(), + Grep("binary reported a pre-release version of 10.7.3-dev", grepStdout)) + }) +} diff --git a/packer_test/install_test.go b/packer_test/install_test.go index a16c3e575..abc017dd3 100644 --- a/packer_test/install_test.go +++ b/packer_test/install_test.go @@ -84,27 +84,3 @@ func (ts *PackerTestSuite) TestRemoteInstallWithPluginsInstall() { Assert(MustFail(), Grep("Remote installation of pre-release plugin versions is unsupported.", grepStdout)) }) } - -func (ts *PackerTestSuite) TestInitWithNonGithubSource() { - pluginPath, cleanup := ts.MakePluginDir() - defer cleanup() - - ts.Run("try installing from a non-github source, should fail", func() { - ts.PackerCommand().UsePluginDir(pluginPath). - SetArgs("init", "./templates/non_gh.pkr.hcl"). - Assert(MustFail(), Grep(`doesn't appear to be a valid "github.com" source address`, grepStdout)) - }) - - ts.Run("manually install plugin to the expected source", func() { - ts.PackerCommand().UsePluginDir(pluginPath). - SetArgs("plugins", "install", "--path", BuildSimplePlugin("1.0.10", ts.T()), "hubgit.com/hashicorp/tester"). - Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.10", grepStdout)) - }) - - ts.Run("re-run packer init on same template, should succeed silently", func() { - ts.PackerCommand().UsePluginDir(pluginPath). - SetArgs("init", "./templates/non_gh.pkr.hcl"). - Assert(MustSucceed(), - MkPipeCheck("no output in stdout").SetTester(ExpectEmptyInput()).SetStream(OnlyStdout)) - }) -} diff --git a/packer_test/templates/init/hashicups.pkr.hcl b/packer_test/templates/init/hashicups.pkr.hcl new file mode 100644 index 000000000..d6d735bf9 --- /dev/null +++ b/packer_test/templates/init/hashicups.pkr.hcl @@ -0,0 +1,9 @@ +packer { + required_plugins { + tester = { + source = "github.com/hashicorp/hashicups" + version = ">= 1.0.2" + } + } +} + diff --git a/packer_test/templates/init/mixed_versions.pkr.hcl b/packer_test/templates/init/mixed_versions.pkr.hcl new file mode 100644 index 000000000..d228643ee --- /dev/null +++ b/packer_test/templates/init/mixed_versions.pkr.hcl @@ -0,0 +1,9 @@ +packer { + required_plugins { + tester = { + source = "github.com/mondoohq/cnspec" + version = "= 10.7.3" # plugin describe reports 10.7.x-dev so init must fail + } + } +} + diff --git a/packer_test/templates/non_gh.pkr.hcl b/packer_test/templates/init/non_gh.pkr.hcl similarity index 100% rename from packer_test/templates/non_gh.pkr.hcl rename to packer_test/templates/init/non_gh.pkr.hcl