Commit Graph
18579 Commits
Author SHA1 Message Date
Lucas Bajolet a976197bbf packer_test: test non-canonical plugin loading
If a plugin is installed with a non-canonical version in its name (e.g.
01.01.01), Packer rejects it with a message to that effect in stderr, so
we add a test for this use-case.
2024-06-07 14:38:20 -04:00
Lucas Bajolet f5ca6f0c4f packer_test: add func for "manual" plugin install
Installing a plugin manually to a directory is something needed for some
tests, especially those not relying on packer commands to install
plugins as they reject/correct the path/version.

Therefore this function is introduced so we have an easy way to install
a binary as a plugin somewhere on the provided plugin directory.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 2bdf006638 packer_test: fix ExpectedName to not clean version
The ExpectedInstalledName function used to compute the expected name of
a plugin binary for a given version, based on the invoker's environment,
used to cleanup the version string passed in parameter of the function,
which could be problematic.
Besides the logic applied would produce some invalid binary names as the
prerelease plugin would not have a `-` separator, so the resulting
plugin would be ignored, and we couldn't test metadata rejection with
this logic.

This commit therefore changes how the function works: the version string
is still parsed to account for manipulation errors, but the string is
left as-is for the final binary name.
2024-06-07 14:38:20 -04:00
Lucas Bajolet c856674a3e packer_test: add init test on non-gh source URI 2024-06-07 14:38:20 -04:00
Lucas Bajolet 0f0c059631 packer_test: don't error on empty pipeline
If for some reason we only want to run a test on either stream without
doing some manipulation beforehand, we can run a PipeChecker, however
these would error if no pipe gadget was defined, preventing this
use-case.

Instead of errorring then, this commit just ignores if no pipe is
present, as none is required for the test to run.
2024-06-07 14:38:20 -04:00
Lucas Bajolet 706a59ecaf packer_test: add test for plugin remove with path 2024-06-07 14:38:20 -04:00
Lucas Bajolet 22711d176e 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-07 14:38:20 -04:00
Lucas Bajolet 99248cdcc2 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-07 14:38:20 -04:00
Lucas Bajolet a701c3a14b packer_test: migrate envvar test to pipe funcs 2024-06-07 14:38:20 -04:00
Lucas Bajolet 5ee3044402 packer_test: remove local alias to ts.T() 2024-06-07 14:38:20 -04:00
Lucas Bajolet 8724e5a422 packer_test: add functions for managing pipechecks
Instead of manually creating and populating a PipeChecker, we add
functions to do this.
2024-06-07 14:38:20 -04:00
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