Skip to content

Commit

Permalink
packer_test: hide run and introduce Output
Browse files Browse the repository at this point in the history
When using a PackerCommand, the Run function was made public as a way to
access the contents of an execution.

This was clumsy as it had too many responsabilities, and was not needed
strictly as Assert was performing the executions, as many times as
required.

This could introduce cases in which one run as spent by the caller, then
the remainder were executed through Assert.

Therefore, we change this convention.

Now, run is private to the type, and only through Assert can a command
be executed.
If a test needs access to a command's output, stderr, or error, it can
do so through the Output function, which requires Assert to be called
first.
  • Loading branch information
lbajolet-hashicorp committed Sep 11, 2024
1 parent 7116941 commit 31b6109
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
19 changes: 17 additions & 2 deletions packer_test/common/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (pc *packerCommand) SetAssertFatal() *packerCommand {
// Note: while originally "Run" was designed to be idempotent, with the
// introduction of multiple runs for a command, this is not the case anymore
// and the function should not be considered thread-safe anymore.
func (pc *packerCommand) Run() (string, string, error) {
func (pc *packerCommand) run() (string, string, error) {
if pc.runs <= 0 {
return pc.stdout.String(), pc.stderr.String(), pc.err
}
Expand Down Expand Up @@ -160,11 +160,26 @@ func (pc *packerCommand) Run() (string, string, error) {
return pc.stdout.String(), pc.stderr.String(), pc.err
}

// Output returns the results of the latest Run that was executed.
//
// In general there is only one run of the command, but as it can be changed
// through the Runs function, only the latest run will be returned.
//
// If the command was not run (through Assert), this will make the test fail
// immediately.
func (pc *packerCommand) Output() (string, string, error) {
if pc.runs > 0 {
pc.t.Fatalf("command was not run, invoke Assert first, then Output.")
}

return pc.stdout.String(), pc.stderr.String(), pc.err
}

func (pc *packerCommand) Assert(checks ...check.Checker) {
attempt := 0
for pc.runs > 0 {
attempt++
stdout, stderr, err := pc.Run()
stdout, stderr, err := pc.run()

for _, checker := range checks {
checkErr := checker.Check(stdout, stderr, err)
Expand Down
5 changes: 4 additions & 1 deletion packer_test/common/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func (ts *PackerTestSuite) CompilePlugin(t *testing.T, versionString string) {
// packer will be able to use that directory for running its functions.
//
// Deletion of the directory is the caller's responsibility.
//
// Note: all of the plugin versions specified to be installed in this plugin directory
// must have been compiled beforehand.
func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDir string, cleanup func()) {
t := ts.T()

Expand Down Expand Up @@ -158,7 +161,7 @@ func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDi
path := ts.GetPluginPath(t, pluginVersion)
cmd := ts.PackerCommand().SetArgs("plugins", "install", "--path", path, "github.com/hashicorp/tester").AddEnv("PACKER_PLUGIN_PATH", pluginTempDir)
cmd.Assert(check.MustSucceed())
out, stderr, cmdErr := cmd.Run()
out, stderr, cmdErr := cmd.run()
if cmdErr != nil {
err = fmt.Errorf("failed to install tester plugin version %q: %s\nCommand stdout: %s\nCommand stderr: %s", pluginVersion, err, out, stderr)
return
Expand Down
2 changes: 1 addition & 1 deletion packer_test/plugin_tests/plugins_remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func InstalledPlugins(ts *PackerPluginTestSuite, dir string) []string {
SetAssertFatal()
cmd.Assert(check.MustSucceed())

out, _, _ := cmd.Run()
out, _, _ := cmd.Output()
// Output will be split on '\n' after trimming all other white space
out = strings.TrimSpace(out)
plugins := strings.Fields(out)
Expand Down

0 comments on commit 31b6109

Please sign in to comment.