Commit Graph
18568 Commits
Author SHA1 Message Date
Lucas Bajolet 032114b3fc packer_test: add Tee pipe gadget
When troubleshooting a pipeline for a test, it can be useful to print
the input out without necessarily preventing the pipeline to work.

The Tee gadget is exactly made for this purpose, the input of the Tee is
printed out through `t.Log`, and the input is forwarded to the next step
in the pipeline.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 28bf784869 packer_test: add test for multiple plugin paths
When the PACKER_PLUGIN_PATH envvar is defined in the environment, Packer
uses it as the source of truth for the directories in which to look for
plugins to load.

Previously, we used to support multiple directories separated by the
OS-specific path separator (i.e. : on UNIX, ; on Windows).

Since this changed, and Packer returns an error to the user, we make
sure that this is well-documented and tested through this extra test.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 2d293e6504 packer_test: fix int compare checker
The IntCompare checker converts the input string into an int through
strconv.Atoi, which fails when the string isn't valid base 10. This
definition of "valid" also excludes whitespace, which occurs often
through piping gadgets, but shouldn't be a reason to fail that check, so
we trim the whitespace from the input string.
2024-06-07 14:38:20 -04:00
Wilken Rivera 124a8d8d5d Move into a packer_test pkg for black-box testing 2024-06-07 14:38:20 -04:00
Lucas Bajolet 7fbef0f5a7 test: add more tests for Packer core 2024-06-07 14:38:20 -04:00
Lucas Bajolet 9c3f45f574 test: fix name for the Grep gadget 2024-06-07 14:38:20 -04:00
Lucas Bajolet 299a5af5c5 test: allow custom Packer executable for acctests
When running the core acceptance tests we compile a temporary Packer
binary to run the tests with.

However in some cases we may want to run a test with another,
pre-compiled version of Packer.

For this use-case, we introduce an environment variable to change the
executable we're running tests with.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 1e126da916 test: fix packerCommand to use the right binary
When the Run command was introduced, despite the path to the temporary
packer binary being stored in the command, we were invoking Packer
through "packer", which would resolve whatever is in our PATH that
resolves first.

This was not the intention, so we fix this problem with this commit.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 6f93eec99b test: add test for loading plugins with wd plugin
Add one more test to ensure we don't load plugins with the old naming
convention in the workdir for a packer run.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 91eaf93375 test: add TempWorkdir function to test suite
When a test has to run in a working directory that is not the current
one we're running tests from, this may imply creating a temporary
directory, moving files into it, and then running the test from this
directory.

This can be a bit verbose to write all this code, so we abstract the
easy case through the TempWorkdir function, which works similarly to the
MakePluginDir function, by creating a temp dir, populating it with the
requested files, and returning a function to clean it up if needed.
2024-06-07 14:38:20 -04:00
Lucas Bajolet f00a49ddc9 test: add SetWD function to packerCommand
For some tests we may need Packer to run in another directory than the
one we're invoking the tests from, so we add a new function to the
packerCommand structure to change that.
2024-06-07 14:38:20 -04:00
Lucas Bajolet c69ab08f9a test: add documentation on Checker interface 2024-06-07 14:38:20 -04:00
Lucas Bajolet 7aa9bac256 test: make must succeed/fail gadgets functions
For consistency with other gadgets like Grep, we make the MustSucceed
and MustFail gadgets private with a function to return an instance of
it.
2024-06-07 14:38:20 -04:00
Lucas Bajolet d15e32cacb test: rename empty/non-empty pipe testers
The name and semantics were a bit unclear with how they were previously
named, so this commit changes the name of those functions so it's
clearer they are expecting something, and what they're expecting.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 0c2cfe3745 test: reimplement Grep with a pipe
As we've introduced pipelines, we can use those to compose a version of
grep that doesn't have specific logic.

Besides, this refactor allows us to expose grep as a function with
variadic options, so this makes it more concise and clear to assert an
input with Grep.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 0a5bdb5b78 test: add NoVerbose function on PackerCommand
By default PackerCommands are run with PACKER_LOG=1.
If for any reason we don't want that, we can remove it from the
environment so we only see the user-facing logs.
2024-06-07 14:38:20 -04:00
Lucas Bajolet b41c052c84 test: add LineCount/Compare pipe/tester
As a common use case in console-oriented pipelines, we check that a
specific command returned a certain number of lines.
With the combination of LineCount and Compare, we can do exactly this.
2024-06-07 14:38:20 -04:00
Lucas Bajolet b31b8d3067 test: add empty/non-empty input checkers
When running a pipeline on a command's output, a simple check is making
sure the pipeline returned something empty or not.

This is the goal of those two implementations, basically either the
input is empty as expected, or it errors, and the reverse.
2024-06-07 14:38:20 -04:00
Lucas Bajolet bd79b37aa7 test: add PipeGrep to grep an input 2024-06-07 14:38:20 -04:00
Lucas Bajolet c3e4c5cde4 test: add pipe checker to test dir
The pipe checker is an attempt at replicating how one would go to write
commands on a CLI and piping them together, coupled with a `test`.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 4d6d3cc81e test: add generic gadget for testing
When writing tests, one may need to write a one-off checker for a
packer command that ran, without having to completely implement the
Checker interface.

This commit introduces a generic CustomChecker implementation (i.e. a
function) that can be one-off implemented by developers if their test
doesn't fit the existing gadgets, and the need is not generic/reusable
enough to justify introducing a new gadget for other users.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 96892fbca0 test: add loading tests with legacy/valid plugins 2024-06-07 14:38:20 -04:00
Lucas Bajolet 1b643125c0 test: add WriteFile convenience function
The WriteFile function creates a new file to the specified location, and
writes some contents to it.
2024-06-07 14:38:20 -04:00
Lucas Bajolet c1a0002a10 test: add ExpectedInstalledName function
The ExpectedInstalledName function returns the expected full name of a
plugin binary after it's installed.

This is used for tests that need to copy the binary to some place before
they can run commands and ensure the logic for managing plugins conforms
to the docs/specs.
2024-06-07 14:38:20 -04:00
Lucas Bajolet e7e8ad07d4 test: add inverse mode for Grep gadget
Since sometimes we want to check for matches, and sometimes we want to
check for a lack of match, we add one more option for the Grep gadget:
inverse.

This essentially replicates `grep -v`, and will succeed only if the
regex provided did NOT match on the requested streams.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 464dbd34dd test: add installation tests to acceptance tests
This commit adds a few scenarios of plugin installations to the test
suite, in order to document and ensure we behave appropriately when
installing pre-releases/metadata.
2024-06-07 14:38:20 -04:00
Lucas Bajolet a62dab8ae7 test: error on plugin loading if not compiled
When building the temporary plugin directory for a test, we didn't check
that the LoadPluginVersion call succeeded and returned a path, which
may cause errors down the line when attempting to install the plugin.

To avoid this problem, we do the check at that time, and immediately
fail if a plugin isn't found.
2024-06-07 14:38:20 -04:00
Lucas Bajolet aa455014f5 test: compile plugins on call to MakePluginDir
MakePluginDir used to only load plugins that were precompiled at the
start of the tests, but now when invoked with any list of plugins, this
will attempt to compile plugins one-by-one, so we don't need to modify
the tests in several places when running tests.

There's still value in compiling the plugins in advance though: as they
run in parallel, they all get compiled at once, so we shave off a few
seconds from the test run.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 848188d705 test: only build plugins if not already done
Calling BuildSimplePlugin for a particular version used to mean that we
had to build it regardless of if it was previously done or not.
This commit changes this behaviour so that it checks first that the
plugin wasn't pre-compiled, and if it was, we immediately return.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 430f2d15cd test: make PanicCheck an implicit Assert gadget
When running "Assert" on a packer command, we run a series of checkers
on the command's output/error code, which provoke test failures if they
fail.

Panic checking used to be part of Run, but in the end this would make
more sense to have that as a regular checker if asserting the results of
a packer command, so that's the approach we adopt with this commit.
2024-06-07 14:38:20 -04:00
Lucas Bajolet f8749afa16 test: add method for setting plugin path for cmd
When running a PackerCommand for acceptance tests, we generally run the
test on a temporary plugin directory, populated by test plugins.

Setting that temporary directory means we need to set the environment
variable, which while it could be easier with a constant for the name
for example, isn't too straightforward.

Therefore for those tests we add a new function for PackerCommand so
that it automatically sets that envvar for the current command.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 03bb62cd99 test: simplify plugin build function
The NewPluginBuildConfig function was essentially a shortcut to
`version.Must(version.NewSemver(v))', which is superfluous at this
point, we can directly pass the version string to BuildSimplePlugin and
let that function do the creation/check.
2024-06-07 14:38:20 -04:00
Lucas Bajolet fbb8cac698 test: remove unnecessary testing.T arg
Since the MakePluginDir function takes the TestSuite as receiver, we
don't need to additionally pass in a reference to testing.T, since the
test suite already contains one instance, and offers a function to get
it from.
2024-06-07 14:38:20 -04:00
Lucas Bajolet d1b431ae27 test: remove PluginVersionConfig struct
The PluginVersionConfig structure was first introduced when building
the early versions of the test package, but it was an unnecessary
abstraction over go-version.Version.

So we remove that structure definition, and instead we directly use the
version for building those temporary plugins.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 6ab7f670ad test: add CopyFile convenience function to pkg
The CopyFile function is essentially a go recreation of the `cp'
command, which copies one file from a source path to a destination
directory or file.

This can be used for several tests in the future.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 316f890fe9 test/templates: add loading test templates
The loading_test had been added to the repository at first, but were not
versioned at that time, making those tests impossible to run on CI.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 0e2695d3e2 test: return cleanup function for temp plugin dirs
When creating a temporary plugin directory, we had to build a cleanup
function, which could be as simple as `os.Mkdirall` without any kind of
warning that a directory failed to be cleaned-up, or we could do some
more work in order to report the possible issues around this.

That code would quickly be redundant, as there's not a ton of
variability in the code that can be written for this step, so we
abstract it through a pre-defined cancellation function which can be
safely defer invoked.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 56c2b5ad8b test: call different packer cmds for loading tests 2024-06-07 14:38:20 -04:00
Lucas Bajolet 045b7b7365 test: infer gadget name from struct name
Creating the Name() function for every gadget we have is superfluous a
bit, as we're essentially parroting the name of the test itself as
implementation for the function.

So instead of requiring every checker implements `Name', we now default
to returning the type name, but if the Name function exists for the
checker, we invoke it and return the value for that function.

This allows us to only define the function where needed, and not
systematically.
2024-06-07 14:38:20 -04:00
Lucas Bajolet b62e8cef9c test/suite: compile plugins in parallel
Since compiling plugins is quick, but each invocation still takes a bit
of time, we run those compilation jobs in parallel to shave of a couple
seconds from a test run.
2024-06-07 14:38:20 -04:00
Lucas Bajolet ef50474a9f test: add base suite for core acceptance testing
Acceptance testing, i.e. running Packer core commands in a controlled
environment and ensuring the behaviour is consistent to what we
expect/document, is not something we have a robust and usable framework
for at the moment.

This commit is a proposal for a base testing framework of the sort, that
is meant to be shipped with packer core, and which will eventually host
most of the tests we currently do in command where we mock an
environment.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 1f4f2aebca test: add mini plugin for testing
The mini plugin implemented here is a minimal Packer plugin that we use
for acceptance testing Packer core.

There's a few components exposed so we can write templates using it, and
make sure Packer interprets it all as it should, and runs/errors as we
expect it.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 3d453f54f8 Makefile: bump golangci-lint to 1.54.0
The acceptance tests were failing to be run on CI as the linter reported
some typecheck mismatches.

This could be due to an older version of golangci-lint running on them,
so we bump it to v1.54.0, empirically determined as when the lint
started being adequately reported.
2024-06-07 14:36:39 -04:00
Lucas Bajolet 39856d3d58 golangci: remove deadcode/varcheck
Both linters are not supported anymore and trigger warnings every time
we try to lint our code.

To not see those warnings anymore, we remove them from the
configuration.
2024-06-07 14:36:39 -04:00
David Guo deee49de54 add missing return value when creating new XZ reader and writer (#13021) 2024-06-05 10:14:19 -04:00
hashicorp-tsccr[bot] 4bd7e951b7 Result of tsccr-helper -log-level=info gha update . 2024-06-03 14:20:36 -04:00
Alex Boutin 3fb6b76701 Fixed wrong parameter in doc 2024-06-03 10:09:35 -04:00
komisan19 9390f717ca refactor: remove deprecated function 2024-06-03 10:07:33 -04:00
IAMDAVID0920 d625694a88 refactor: remove deprecated InitializePluginVersion, replace with NewPluginVersion 2024-06-03 09:37:30 -04:00
Lucas Bajolet 361a432eac version: bump main to 1.12.0-dev
Since we released 1.11.0 today, we need to have main point to the next
minor release of Packer so CI succeeds, otherwise we get an unexpected
output for the version being out-of-date.
2024-05-31 15:17:52 -04:00