2024-06-12 15:38:21 -04:00
|
|
|
package plugin_tests
|
2024-05-23 15:28:08 -04:00
|
|
|
|
packer_test: add func to change assert behaviour
When a command asserts its output with checkers, by default it will
register errors through a t.Errorf.
While this works, in some cases we would want to stop execution
immediately if a function's Assert fails, as the rest of the test may
depend on the assertion being valid.
In the current state, this means either getting the result of the run to
check if an error was returned (not fully reliable as if the command was
run multiple times, and the last run succeeded, we won't get an error),
or relying on t.IsFailed() (completely reliable).
Instead, we introduce a new function on packerCommand, that lets users
change how Assert behaves, so that if an error was reported, instead of
logging the error and flagging the test as failed, we can use t.Fatalf,
so that the test immedately fails and stops execution.
2024-08-15 10:21:35 -04:00
|
|
|
import (
|
|
|
|
|
"github.com/hashicorp/packer/packer_test/common/check"
|
|
|
|
|
)
|
2024-06-12 15:38:21 -04:00
|
|
|
|
|
|
|
|
func (ts *PackerPluginTestSuite) TestPackerInitForce() {
|
2024-06-12 15:54:03 -04:00
|
|
|
ts.SkipNoAcc()
|
|
|
|
|
|
2024-08-15 14:09:33 -04:00
|
|
|
pluginPath := ts.MakePluginDir()
|
|
|
|
|
defer pluginPath.Cleanup()
|
2024-05-23 15:28:08 -04:00
|
|
|
|
|
|
|
|
ts.Run("installs any missing plugins", func() {
|
|
|
|
|
ts.PackerCommand().UsePluginDir(pluginPath).
|
|
|
|
|
SetArgs("init", "--force", "./templates/init/hashicups.pkr.hcl").
|
2024-08-15 10:05:27 -04:00
|
|
|
Assert(check.MustSucceed(), check.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", check.GrepStdout))
|
2024-05-23 15:28:08 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
ts.Run("reinstalls plugins matching version constraints", func() {
|
|
|
|
|
ts.PackerCommand().UsePluginDir(pluginPath).
|
|
|
|
|
SetArgs("init", "--force", "./templates/init/hashicups.pkr.hcl").
|
2024-08-15 10:05:27 -04:00
|
|
|
Assert(check.MustSucceed(), check.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", check.GrepStdout))
|
2024-05-23 15:28:08 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 15:38:21 -04:00
|
|
|
func (ts *PackerPluginTestSuite) TestPackerInitUpgrade() {
|
2024-06-12 15:54:03 -04:00
|
|
|
ts.SkipNoAcc()
|
|
|
|
|
|
2024-08-15 14:09:33 -04:00
|
|
|
pluginPath := ts.MakePluginDir()
|
|
|
|
|
defer pluginPath.Cleanup()
|
2024-05-23 15:28:08 -04:00
|
|
|
|
|
|
|
|
cmd := ts.PackerCommand().UsePluginDir(pluginPath)
|
|
|
|
|
cmd.SetArgs("plugins", "install", "github.com/hashicorp/hashicups", "1.0.1")
|
packer_test: add func to change assert behaviour
When a command asserts its output with checkers, by default it will
register errors through a t.Errorf.
While this works, in some cases we would want to stop execution
immediately if a function's Assert fails, as the rest of the test may
depend on the assertion being valid.
In the current state, this means either getting the result of the run to
check if an error was returned (not fully reliable as if the command was
run multiple times, and the last run succeeded, we won't get an error),
or relying on t.IsFailed() (completely reliable).
Instead, we introduce a new function on packerCommand, that lets users
change how Assert behaves, so that if an error was reported, instead of
logging the error and flagging the test as failed, we can use t.Fatalf,
so that the test immedately fails and stops execution.
2024-08-15 10:21:35 -04:00
|
|
|
cmd.SetAssertFatal()
|
2024-08-15 10:05:27 -04:00
|
|
|
cmd.Assert(check.MustSucceed(), check.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.1", check.GrepStdout))
|
2024-05-23 15:28:08 -04:00
|
|
|
|
|
|
|
|
ts.Run("upgrades a plugin to the latest matching version constraints", func() {
|
|
|
|
|
ts.PackerCommand().UsePluginDir(pluginPath).
|
|
|
|
|
SetArgs("init", "--upgrade", "./templates/init/hashicups.pkr.hcl").
|
2024-08-15 10:05:27 -04:00
|
|
|
Assert(check.MustSucceed(), check.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", check.GrepStdout))
|
2024-05-23 15:28:08 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 15:38:21 -04:00
|
|
|
func (ts *PackerPluginTestSuite) TestPackerInitWithNonGithubSource() {
|
2024-08-15 14:09:33 -04:00
|
|
|
pluginPath := ts.MakePluginDir()
|
|
|
|
|
defer pluginPath.Cleanup()
|
2024-05-23 15:28:08 -04:00
|
|
|
|
|
|
|
|
ts.Run("try installing from a non-github source, should fail", func() {
|
|
|
|
|
ts.PackerCommand().UsePluginDir(pluginPath).
|
|
|
|
|
SetArgs("init", "./templates/init/non_gh.pkr.hcl").
|
2024-08-15 10:05:27 -04:00
|
|
|
Assert(check.MustFail(), check.Grep(`doesn't appear to be a valid "github.com" source address`, check.GrepStdout))
|
2024-05-23 15:28:08 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
ts.Run("manually install plugin to the expected source", func() {
|
|
|
|
|
ts.PackerCommand().UsePluginDir(pluginPath).
|
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
|
|
|
SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.10"), "hubgit.com/hashicorp/tester").
|
2024-08-15 10:05:27 -04:00
|
|
|
Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v1.0.10", check.GrepStdout))
|
2024-05-23 15:28:08 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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").
|
2024-08-15 10:05:27 -04:00
|
|
|
Assert(check.MustSucceed(),
|
|
|
|
|
check.MkPipeCheck("no output in stdout").SetTester(check.ExpectEmptyInput()).SetStream(check.OnlyStdout))
|
2024-05-23 15:28:08 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 15:38:21 -04:00
|
|
|
func (ts *PackerPluginTestSuite) TestPackerInitWithMixedVersions() {
|
2024-06-12 15:54:03 -04:00
|
|
|
ts.SkipNoAcc()
|
|
|
|
|
|
2024-08-15 14:09:33 -04:00
|
|
|
pluginPath := ts.MakePluginDir()
|
|
|
|
|
defer pluginPath.Cleanup()
|
2024-05-23 15:28:08 -04:00
|
|
|
|
|
|
|
|
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").
|
2024-08-15 10:05:27 -04:00
|
|
|
Assert(check.MustFail(),
|
|
|
|
|
check.Grep("binary reported a pre-release version of 10.7.3-dev", check.GrepStdout))
|
2024-05-23 15:28:08 -04:00
|
|
|
})
|
|
|
|
|
}
|