Commit Graph
18838 Commits
Author SHA1 Message Date
Wilken Rivera c6388d4680 packer_test: Add tests for invalid plugin remove use cases 2024-06-10 09:59:32 -04:00
Wilken Rivera 1cace90289 packer_test: Add tests for valid plugin remove use cases 2024-06-10 09:59:32 -04:00
Lucas Bajolet 9f7098b230 packer_test: fix typo in error message for cleanup 2024-06-10 09:59:32 -04:00
Lucas Bajolet a2e08329a3 packer_test: fix shasum file name for tests
When manually installing a plugin to the plugin directory, we compute a
SHA256SUM file from the plugin binary, and install it alongside it so we
can test the loading process for Packer.

In the introduction of the function, we added a check that if we were
running on Windows, we'd remove the extension of the sumfile's name
before writing it.

This is actually not necessary (and breaks the loading logic) as Packer
looks for the name of the plugin with extension, followed by
_SHA256SUM in order to compare the effective digest of the file to the
one written to this file.

Since this prevents the tests that use this function from succeeding in
a Windows environment, we remove this extra step.
2024-06-10 09:59:32 -04:00
Lucas Bajolet 2a414af35f packer_test: set TMP envvar for commands
Windows relies on the `TMP` (or alternatives) being set in the
environment in order to be able to create temporary directories and
files.

If this is not set, the `os.TempDir` function defaults on the windows
installation root directory (typically C:\Windows), leading to
permission errors when running Packer in the context of a test, as we're
installing plugins in a temporary directory.

To avoid this problem, we get the current setting from the test's
invocation environment, and forward it to the subcommand we execute for
our tests.
2024-06-10 09:59:32 -04:00
Lucas Bajolet f77da46b3f packer_test: compile packer with .exe for Windows
Since on Windows extensions are mandatory in order to have something
executable, we compile Packer with a `.exe` suffix during tests, so we
can use the executable afterwards to run tests with.
2024-06-10 09:59:32 -04:00
Lucas Bajolet 7124cf81fa packer_test: test build/validate with ignore flag
As we're introducing a --ignore-prerelease-plugins flag to both the
validate and build subcommands, we need to make sure they work as we
expect it to, so we add a test case for that.
2024-06-10 09:59:32 -04:00
Lucas Bajolet 66e70a863d packer_test: add test for plugin with meta in name
Plugins with metadata information in their file name (i.e.
v1.0.0+metadata) should be ignored by Packer as they could introduce
ambiguity since the metadata is free-form, so we add that test to make
sure Packer behaves coherently.
2024-06-10 09:59:32 -04:00
Lucas Bajolet a49da98350 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-10 09:59:32 -04:00
Lucas Bajolet 707e40e2e6 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-10 09:59:32 -04:00
Lucas Bajolet 901f31ad82 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-10 09:59:32 -04:00
Lucas Bajolet 7e0b27adaa packer_test: add init test on non-gh source URI 2024-06-10 09:59:32 -04:00
Lucas Bajolet 4e20956bae 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-10 09:59:32 -04:00
Lucas Bajolet 7943b8ce4e packer_test: add test for plugin remove with path 2024-06-10 09:59:32 -04:00
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