diff --git a/test/gadgets_test.go b/test/gadgets_test.go index 03ee4c35e..16ea28019 100644 --- a/test/gadgets_test.go +++ b/test/gadgets_test.go @@ -1,10 +1,8 @@ package test import ( - "errors" "fmt" "reflect" - "regexp" "strings" "testing" ) @@ -71,46 +69,40 @@ func (_ MustFail) Check(stdout, stderr string, err error) error { return nil } -// Grep is essentially the equivalent to a normal grep -E on the command line. -// -// The `expect` string is meant to be a regexp, which will be compiled on-demand, -// and will panic if it isn't a valid POSIX extended regexp. -type Grep struct { - streams Stream - expect string - inverse bool -} - -func (g Grep) Check(stdout, stderr string, err error) error { - re := regexp.MustCompilePOSIX(g.expect) - - streams := []string{} - - switch g.streams { - case BothStreams: - streams = append(streams, stdout, stderr) - case OnlyStdout: - streams = append(streams, stdout) - case OnlyStderr: - streams = append(streams, stderr) - } +type grepOpts int - var found bool - for _, stream := range streams { - found = found || re.MatchString(stream) - } +const ( + // Invert the check, i.e. by default an empty grep fails, if this is set, a non-empty grep fails + grepInvert grepOpts = iota + // Only grep stderr + grepStderr + // Only grep stdout + grepStdout +) - if g.inverse && found { - return errors.New("unexpectedly matched the regexp") +// Grep returns a checker that performs a regexp match on the command's output and returns an error if it failed +// +// Note: by default both streams will be checked by the grep +func Grep(expression string, opts ...grepOpts) Checker { + pc := PipeChecker{ + name: "command | grep -E %q", + stream: BothStreams, + pipers: []Pipe{ + PipeGrep(expression), + }, + check: EmptyInput(), } - if !g.inverse && !found { - return errors.New("did not match the regexp") + for _, opt := range opts { + switch opt { + case grepInvert: + pc.check = NonEmptyInput() + case grepStderr: + pc.stream = OnlyStderr + case grepStdout: + pc.stream = OnlyStdout + } } - return nil -} - -func (g Grep) Name() string { - return fmt.Sprintf("command (%s) | grep -E %q", g.streams, g.expect) + return pc } type Dump struct { diff --git a/test/install_test.go b/test/install_test.go index c3b2d5830..65810a606 100644 --- a/test/install_test.go +++ b/test/install_test.go @@ -7,28 +7,19 @@ func (ts *PackerTestSuite) TestInstallPluginWithMetadata() { ts.Run("metadata plugin installed must not have metadata in its path", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). SetArgs("plugins", "installed"). - Assert(ts.T(), MustSucceed{}, Grep{ - streams: OnlyStdout, - expect: "packer-plugin-tester_v1.0.0[^+]", - }) + Assert(ts.T(), MustSucceed{}, Grep("packer-plugin-tester_v1.0.0[^+]", grepStdout)) }) ts.Run("plugin with metadata should work with validate", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). SetArgs("validate", "./templates/simple.pkr.hcl"). - Assert(ts.T(), MustSucceed{}, Grep{ - streams: OnlyStderr, - expect: "packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", - }) + Assert(ts.T(), MustSucceed{}, Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", grepStderr)) }) ts.Run("plugin with metadata should work with build", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). SetArgs("build", "./templates/simple.pkr.hcl"). - Assert(ts.T(), MustSucceed{}, Grep{ - streams: OnlyStderr, - expect: "packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", - }) + Assert(ts.T(), MustSucceed{}, Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", grepStderr)) }) } @@ -41,9 +32,6 @@ func (ts *PackerTestSuite) TestInstallPluginPrerelease() { ts.Run("try install plugin with alpha1 prerelease - should fail", func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("plugins", "install", "--path", pluginPath, "github.com/hashicorp/tester"). - Assert(ts.T(), MustFail{}, Grep{ - streams: OnlyStdout, - expect: "Packer can only install plugin releases with this command", - }) + Assert(ts.T(), MustFail{}, Grep("Packer can only install plugin releases with this command", grepStdout)) }) } diff --git a/test/loading_test.go b/test/loading_test.go index e1523cf85..69682e31b 100644 --- a/test/loading_test.go +++ b/test/loading_test.go @@ -43,10 +43,7 @@ func (ts *PackerTestSuite) TestLoadingOrder() { ts.PackerCommand(). SetArgs(command, tt.templatePath). UsePluginDir(pluginDir). - Assert(t, MustSucceed{}, Grep{ - streams: BothStreams, - expect: tt.grepStr, - }) + Assert(t, MustSucceed{}, Grep(tt.grepStr)) }) } } @@ -63,10 +60,7 @@ func (ts *PackerTestSuite) TestLoadWithLegacyPluginName() { ts.Run("multiple plugins installed: one with no version in path, one with qualified name. Should pick-up the qualified one only.", func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("build", "templates/simple.pkr.hcl"). - Assert(ts.T(), MustSucceed{}, Grep{ - streams: OnlyStderr, - expect: "packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", - }) + Assert(ts.T(), MustSucceed{}, Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", grepStderr)) }) } @@ -83,17 +77,10 @@ func (ts *PackerTestSuite) TestLoadWithSHAMismatches() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("plugins", "installed"). - Assert(ts.T(), MustSucceed{}, Grep{ - streams: OnlyStdout, - expect: "packer-plugin-tester_v1\\.0\\.9[^\\n]+", - }, Grep{ - streams: OnlyStdout, - expect: "packer-plugin-tester_v1.0.10", - inverse: true, - }, Grep{ - streams: OnlyStderr, - expect: "v1.0.10[^\\n]+ignoring possibly unsafe binary", - }) + Assert(ts.T(), MustSucceed{}, + Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", grepStdout), + Grep("packer-plugin-tester_v1.0.10", grepStdout, grepInvert), + Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", grepStderr)) }) ts.Run("move plugin with right name, invalid SHA256SUM, should reject", func() { @@ -113,19 +100,10 @@ func (ts *PackerTestSuite) TestLoadWithSHAMismatches() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("plugins", "installed"). - Assert(ts.T(), MustSucceed{}, Grep{ - streams: OnlyStdout, - expect: "packer-plugin-tester_v1\\.0\\.9[^\\n]+", - }, Grep{ - streams: OnlyStdout, - expect: "packer-plugin-tester_v1.0.10", - inverse: true, - }, Grep{ - streams: OnlyStderr, - expect: "v1.0.10[^\\n]+ignoring possibly unsafe binary", - }, Grep{ - streams: OnlyStderr, - expect: `Checksums \(\*sha256\.digest\) did not match.`, - }) + Assert(ts.T(), MustSucceed{}, + Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", grepStdout), + Grep("packer-plugin-tester_v1.0.10", grepInvert, grepStdout), + Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", grepStderr), + Grep(`Checksums \(\*sha256\.digest\) did not match.`, grepStderr)) }) }