Commit Graph
18574 Commits
Author SHA1 Message Date
Lucas Bajolet 7f9276d439 packer_test: rename mini_plugin to plugin_tester
Since the tester plugin used for blackbox testing is called
packer-plugin-tester, we rename the directory it's stored in to
plugin_tester.
2024-06-10 09:59:32 -04:00
Lucas Bajolet 9ebc961374 packer_test: remove testing.T arg on Assert
Since the command object is created using the TestSuite as argument, we
can keep a reference to the test suite's scoped T() instance for the
command's lifecycle, and reuse it later during `Assert`, instead of
needing to pass it as argument when invoking the function.
2024-06-10 09:59:32 -04:00
Lucas Bajolet 95b9a3c03e packer_test: migrate envvar test to pipe funcs 2024-06-10 09:59:32 -04:00
Lucas Bajolet ee9b4943da packer_test: remove local alias to ts.T() 2024-06-10 09:59:32 -04:00
Lucas Bajolet 2d114a044a packer_test: add functions for managing pipechecks
Instead of manually creating and populating a PipeChecker, we add
functions to do this.
2024-06-10 09:59:32 -04:00
Lucas Bajolet d175625869 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-10 09:59:32 -04:00
Lucas Bajolet 830ac98e39 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-10 09:59:32 -04:00
Lucas Bajolet ae78d78736 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-10 09:59:32 -04:00
Wilken Rivera 2fe567d3d5 Move into a packer_test pkg for black-box testing 2024-06-10 09:59:32 -04:00
Lucas Bajolet 4a028ec735 test: add more tests for Packer core 2024-06-10 09:59:32 -04:00
Lucas Bajolet c04ac1cb1c test: fix name for the Grep gadget 2024-06-10 09:59:32 -04:00
Lucas Bajolet 646f1c46a1 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-10 09:59:32 -04:00
Lucas Bajolet a31507d575 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-10 09:59:32 -04:00
Lucas Bajolet 4717955e2e 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-10 09:59:32 -04:00
Lucas Bajolet 055d46c59e 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-10 09:59:32 -04:00
Lucas Bajolet 8f7efe0d57 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-10 09:59:32 -04:00
Lucas Bajolet f48f4e786b test: add documentation on Checker interface 2024-06-10 09:59:32 -04:00
Lucas Bajolet 995b232464 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-10 09:59:32 -04:00
Lucas Bajolet afb4340539 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-10 09:59:32 -04:00
Lucas Bajolet 8e282d4100 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-10 09:59:32 -04:00
Lucas Bajolet 0f14b1d3fc 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-10 09:59:32 -04:00
Lucas Bajolet c6e38690be 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-10 09:59:32 -04:00
Lucas Bajolet 1278d50cd2 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-10 09:59:32 -04:00
Lucas Bajolet 7ab8511f1c test: add PipeGrep to grep an input 2024-06-10 09:59:32 -04:00
Lucas Bajolet b32ef74941 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-10 09:59:32 -04:00
Lucas Bajolet 75e4a47686 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-10 09:59:32 -04:00
Lucas Bajolet 829b942206 test: add loading tests with legacy/valid plugins 2024-06-10 09:59:32 -04:00
Lucas Bajolet df9012dfbe test: add WriteFile convenience function
The WriteFile function creates a new file to the specified location, and
writes some contents to it.
2024-06-10 09:59:32 -04:00
Lucas Bajolet da41272df5 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-10 09:59:32 -04:00
Lucas Bajolet 54a1d28525 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-10 09:59:32 -04:00
Lucas Bajolet ad4631fb67 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-10 09:59:32 -04:00
Lucas Bajolet fe564c579b 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-10 09:59:32 -04:00
Lucas Bajolet 3d437ede99 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-10 09:59:32 -04:00
Lucas Bajolet fc056a4594 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-10 09:59:32 -04:00
Lucas Bajolet fde79cc667 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-10 09:59:32 -04:00
Lucas Bajolet 1e7f29f7c7 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-10 09:59:32 -04:00
Lucas Bajolet 50ef9ce990 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-10 09:59:32 -04:00
Lucas Bajolet c7e11f4bf2 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-10 09:59:32 -04:00
Lucas Bajolet 99f3546e32 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-10 09:59:32 -04:00
Lucas Bajolet ba4b8b2b68 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-10 09:59:32 -04:00
Lucas Bajolet 2d7f523bd3 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-10 09:59:32 -04:00
Lucas Bajolet 6a8e888b0d 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-10 09:59:32 -04:00
Lucas Bajolet 0d3f1da016 test: call different packer cmds for loading tests 2024-06-10 09:59:32 -04:00
Lucas Bajolet 700bdd9630 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-10 09:59:32 -04:00
Lucas Bajolet 1a80508c26 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-10 09:59:32 -04:00
Lucas Bajolet 486c1d3dcb 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-10 09:59:32 -04:00
Lucas Bajolet 664a230930 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-10 09:59:32 -04:00
Lucas BajoletandWilken Rivera 280266163c docs: add plugin loading spec documentation (#13024)
* docs: add plugin loading spec documentation

The logic for discovering and loading plugins is not well documented on
the current documentation.
This causes issues for users that have to troubleshoot why a particular
plugin cannot be found or installed, so this commit adds a specification
document, detailing what are Packer's expectations when it comes to
discovering plugins.

* Apply suggestions from code review

* Update plugin loading specification navbar

---------
Co-authored-by: Wilken Rivera <[email protected]>
2024-06-10 09:56:14 -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