Files
Packer-Cn/packer_test/plugin_tests/init_test.go
T
Lucas Bajolet 7d176daef0 packer_test: split BuildPackerPlugin in build/get
The interface for building a plugin through the test suite was
confusing, as it would build the plugin and return its path, cache the
path for the version built, and return the path regardless if something
was built or not.

While in the current state this is harmless as builds are idempotent,
since the state of the plugin package/module does not change, this will
in the future as we introduce customisation techniques on the plugin's
directory and files, making this double-use potentially dangerous.

Furthermore, the current behaviour is unclear, as the function hides
that caching mechanism, which could come as a surprise for users
attempting to build a plugin for the duration of a test, while the built
plugin is linked to the test suite being run, and not the unit test
being evaluated.

Therefore this commit changes the sequence in which plugins are built
and used. Now the `CompilePlugin` function builds a plugin, and does not
return its path anymore, instead terminating the tests immediately if
they fail.
In normal test usage, a new `GetPluginPath` function is introduced,
which looks-up the path in the suite's cache, failing immediately if
invoked before the plugin is built.

With this change, it is heavily advised to build plugins when
initialising the suite, then in the tests, the GetPluginPath function
should be used to get a plugin's path for interacting with packer
commands.
2024-08-14 16:12:53 -04:00

83 lines
3.1 KiB
Go

package plugin_tests
import "github.com/hashicorp/packer/packer_test/lib"
func (ts *PackerPluginTestSuite) TestPackerInitForce() {
ts.SkipNoAcc()
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(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", lib.GrepStdout))
})
ts.Run("reinstalls plugins matching version constraints", func() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("init", "--force", "./templates/init/hashicups.pkr.hcl").
Assert(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", lib.GrepStdout))
})
}
func (ts *PackerPluginTestSuite) TestPackerInitUpgrade() {
ts.SkipNoAcc()
pluginPath, cleanup := ts.MakePluginDir()
defer cleanup()
cmd := ts.PackerCommand().UsePluginDir(pluginPath)
cmd.SetArgs("plugins", "install", "github.com/hashicorp/hashicups", "1.0.1")
cmd.Assert(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.1", lib.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(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", lib.GrepStdout))
})
}
func (ts *PackerPluginTestSuite) 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(lib.MustFail(), lib.Grep(`doesn't appear to be a valid "github.com" source address`, lib.GrepStdout))
})
ts.Run("manually install plugin to the expected source", func() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.10"), "hubgit.com/hashicorp/tester").
Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1.0.10", lib.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(lib.MustSucceed(),
lib.MkPipeCheck("no output in stdout").SetTester(lib.ExpectEmptyInput()).SetStream(lib.OnlyStdout))
})
}
func (ts *PackerPluginTestSuite) TestPackerInitWithMixedVersions() {
ts.SkipNoAcc()
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(lib.MustFail(),
lib.Grep("binary reported a pre-release version of 10.7.3-dev", lib.GrepStdout))
})
}