Merge pull request #3497 from thaJeztah/cleanup_e2e_help_test

e2e: cleanup TestGlobalHelp() to be less brittle
This commit is contained in:
Nicolas De loof 2022-03-28 15:30:18 +02:00 committed by GitHub
commit 0bcf197ae7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 43 deletions

View File

@ -1,9 +1,7 @@
package cliplugins package cliplugins
import ( import (
"bufio"
"regexp" "regexp"
"strings"
"testing" "testing"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
@ -21,7 +19,7 @@ func TestGlobalHelp(t *testing.T) {
ExitCode: 0, ExitCode: 0,
}) })
assert.Assert(t, is.Equal(res.Stderr(), "")) assert.Assert(t, is.Equal(res.Stderr(), ""))
scanner := bufio.NewScanner(strings.NewReader(res.Stdout())) output := res.Stdout()
// Instead of baking in the full current output of `docker // Instead of baking in the full current output of `docker
// help`, which can be expected to change regularly, bake in // help`, which can be expected to change regularly, bake in
@ -34,44 +32,20 @@ func TestGlobalHelp(t *testing.T) {
// - The `badmeta` plugin under the "Invalid Plugins" heading. // - The `badmeta` plugin under the "Invalid Plugins" heading.
// //
// Regexps are needed because the width depends on `unix.TIOCGWINSZ` or similar. // Regexps are needed because the width depends on `unix.TIOCGWINSZ` or similar.
helloworldre := regexp.MustCompile(`^ helloworld\*\s+A basic Hello World plugin for tests \(Docker Inc\., testing\)$`) for _, s := range []string{
badmetare := regexp.MustCompile(`^ badmeta\s+invalid metadata: invalid character 'i' looking for beginning of object key string$`) `Management Commands:`,
var helloworldcount, badmetacount int `\s+container\s+Manage containers`,
for _, expected := range []*regexp.Regexp{ `\s+helloworld\*\s+A basic Hello World plugin for tests \(Docker Inc\., testing\)`,
regexp.MustCompile(`^A self-sufficient runtime for containers$`), `\s+image\s+Manage images`,
regexp.MustCompile(`^Management Commands:$`), `Commands:`,
regexp.MustCompile(`^ container\s+Manage containers$`), `\s+create\s+Create a new container`,
helloworldre, `Invalid Plugins:`,
regexp.MustCompile(`^ image\s+Manage images$`), `\s+badmeta\s+invalid metadata: invalid character 'i' looking for beginning of object key string`,
regexp.MustCompile(`^Commands:$`),
regexp.MustCompile(`^ create\s+Create a new container$`),
regexp.MustCompile(`^Invalid Plugins:$`),
badmetare,
nil, // scan to end of input rather than stopping at badmetare
} { } {
var found bool expected := regexp.MustCompile(`(?m)^` + s + `$`)
for scanner.Scan() { matches := expected.FindAllString(output, -1)
text := scanner.Text() assert.Equal(t, len(matches), 1, "Did not find expected number of matches for %q in `docker help` output", expected)
if helloworldre.MatchString(text) {
helloworldcount++
} }
if badmetare.MatchString(text) {
badmetacount++
}
if expected != nil && expected.MatchString(text) {
found = true
break
}
}
assert.Assert(t, expected == nil || found, "Did not find match for %q in `docker help` output", expected)
}
// We successfully scanned all the input
assert.Assert(t, !scanner.Scan())
assert.NilError(t, scanner.Err())
// Plugins should only be listed once.
assert.Assert(t, is.Equal(helloworldcount, 1))
assert.Assert(t, is.Equal(badmetacount, 1))
// Running with `--help` should produce the same. // Running with `--help` should produce the same.
t.Run("help_flag", func(t *testing.T) { t.Run("help_flag", func(t *testing.T) {
@ -79,7 +53,7 @@ func TestGlobalHelp(t *testing.T) {
res2.Assert(t, icmd.Expected{ res2.Assert(t, icmd.Expected{
ExitCode: 0, ExitCode: 0,
}) })
assert.Assert(t, is.Equal(res2.Stdout(), res.Stdout())) assert.Assert(t, is.Equal(res2.Stdout(), output))
assert.Assert(t, is.Equal(res2.Stderr(), "")) assert.Assert(t, is.Equal(res2.Stderr(), ""))
}) })
@ -90,7 +64,7 @@ func TestGlobalHelp(t *testing.T) {
ExitCode: 0, ExitCode: 0,
}) })
assert.Assert(t, is.Equal(res2.Stdout(), "")) assert.Assert(t, is.Equal(res2.Stdout(), ""))
assert.Assert(t, is.Equal(res2.Stderr(), res.Stdout())) assert.Assert(t, is.Equal(res2.Stderr(), output))
}) })
t.Run("badopt", func(t *testing.T) { t.Run("badopt", func(t *testing.T) {
@ -103,7 +77,7 @@ func TestGlobalHelp(t *testing.T) {
ExitCode: 125, ExitCode: 125,
}) })
assert.Assert(t, is.Equal(res2.Stdout(), "")) assert.Assert(t, is.Equal(res2.Stdout(), ""))
exp := "unknown flag: --badopt\nSee 'docker --help'.\n" + res.Stdout() + "\n" assert.Assert(t, is.Contains(res2.Stderr(), "unknown flag: --badopt"))
assert.Assert(t, is.Equal(res2.Stderr(), exp)) assert.Assert(t, is.Contains(res2.Stderr(), "See 'docker --help"))
}) })
} }