2018-12-11 09:50:04 -05:00
|
|
|
package cliplugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/icmd"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestGlobalHelp ensures correct behaviour when running `docker help`
|
|
|
|
func TestGlobalHelp(t *testing.T) {
|
2019-02-18 06:49:41 -05:00
|
|
|
run, _, cleanup := prepare(t)
|
2018-12-11 09:50:04 -05:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
res := icmd.RunCmd(run("help"))
|
|
|
|
res.Assert(t, icmd.Expected{
|
|
|
|
ExitCode: 0,
|
|
|
|
})
|
|
|
|
assert.Assert(t, is.Equal(res.Stderr(), ""))
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(res.Stdout()))
|
|
|
|
|
|
|
|
// Instead of baking in the full current output of `docker
|
|
|
|
// help`, which can be expected to change regularly, bake in
|
|
|
|
// some checkpoints. Key things we are looking for:
|
|
|
|
//
|
|
|
|
// - The top-level description
|
|
|
|
// - Each of the main headings
|
|
|
|
// - Some builtin commands under the main headings
|
|
|
|
// - The `helloworld` plugin in the appropriate place
|
2018-12-19 06:29:01 -05:00
|
|
|
// - The `badmeta` plugin under the "Invalid Plugins" heading.
|
2018-12-11 09:50:04 -05:00
|
|
|
//
|
|
|
|
// Regexps are needed because the width depends on `unix.TIOCGWINSZ` or similar.
|
2019-02-15 11:27:22 -05:00
|
|
|
helloworldre := regexp.MustCompile(`^ helloworld\*\s+A basic Hello World plugin for tests \(Docker Inc\., testing\)$`)
|
2019-01-30 05:46:31 -05:00
|
|
|
badmetare := regexp.MustCompile(`^ badmeta\s+invalid metadata: invalid character 'i' looking for beginning of object key string$`)
|
|
|
|
var helloworldcount, badmetacount int
|
2018-12-11 09:50:04 -05:00
|
|
|
for _, expected := range []*regexp.Regexp{
|
|
|
|
regexp.MustCompile(`^A self-sufficient runtime for containers$`),
|
|
|
|
regexp.MustCompile(`^Management Commands:$`),
|
|
|
|
regexp.MustCompile(`^ container\s+Manage containers$`),
|
2019-02-21 12:27:51 -05:00
|
|
|
helloworldre,
|
|
|
|
regexp.MustCompile(`^ image\s+Manage images$`),
|
2018-12-11 09:50:04 -05:00
|
|
|
regexp.MustCompile(`^Commands:$`),
|
|
|
|
regexp.MustCompile(`^ create\s+Create a new container$`),
|
2018-12-19 06:29:01 -05:00
|
|
|
regexp.MustCompile(`^Invalid Plugins:$`),
|
2019-01-30 05:46:31 -05:00
|
|
|
badmetare,
|
|
|
|
nil, // scan to end of input rather than stopping at badmetare
|
2018-12-11 09:50:04 -05:00
|
|
|
} {
|
|
|
|
var found bool
|
|
|
|
for scanner.Scan() {
|
2019-01-30 05:46:31 -05:00
|
|
|
text := scanner.Text()
|
|
|
|
if helloworldre.MatchString(text) {
|
|
|
|
helloworldcount++
|
|
|
|
}
|
|
|
|
if badmetare.MatchString(text) {
|
|
|
|
badmetacount++
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected != nil && expected.MatchString(text) {
|
2018-12-11 09:50:04 -05:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 05:46:31 -05:00
|
|
|
assert.Assert(t, expected == nil || found, "Did not find match for %q in `docker help` output", expected)
|
2018-12-11 09:50:04 -05:00
|
|
|
}
|
2019-01-30 05:46:31 -05:00
|
|
|
// 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.
|
2019-04-26 05:37:26 -04:00
|
|
|
t.Run("help_flag", func(t *testing.T) {
|
|
|
|
res2 := icmd.RunCmd(run("--help"))
|
|
|
|
res2.Assert(t, icmd.Expected{
|
|
|
|
ExitCode: 0,
|
|
|
|
})
|
|
|
|
assert.Assert(t, is.Equal(res2.Stdout(), res.Stdout()))
|
|
|
|
assert.Assert(t, is.Equal(res2.Stderr(), ""))
|
2019-01-30 05:46:31 -05:00
|
|
|
})
|
2019-01-22 08:44:39 -05:00
|
|
|
|
2019-01-30 05:46:31 -05:00
|
|
|
// Running just `docker` (without `help` nor `--help`) should produce the same thing, except on Stderr.
|
2019-04-26 05:37:26 -04:00
|
|
|
t.Run("bare", func(t *testing.T) {
|
|
|
|
res2 := icmd.RunCmd(run())
|
|
|
|
res2.Assert(t, icmd.Expected{
|
|
|
|
ExitCode: 0,
|
|
|
|
})
|
|
|
|
assert.Assert(t, is.Equal(res2.Stdout(), ""))
|
|
|
|
assert.Assert(t, is.Equal(res2.Stderr(), res.Stdout()))
|
2019-01-22 08:44:39 -05:00
|
|
|
})
|
2019-04-26 10:08:22 -04:00
|
|
|
|
|
|
|
t.Run("badopt", func(t *testing.T) {
|
|
|
|
// Running `docker --badopt` should also produce the
|
|
|
|
// same thing, give or take the leading error message
|
|
|
|
// and a trailing carriage return (due to main() using
|
|
|
|
// Println in the error case).
|
|
|
|
res2 := icmd.RunCmd(run("--badopt"))
|
|
|
|
res2.Assert(t, icmd.Expected{
|
|
|
|
ExitCode: 125,
|
|
|
|
})
|
|
|
|
assert.Assert(t, is.Equal(res2.Stdout(), ""))
|
|
|
|
exp := "unknown flag: --badopt\nSee 'docker --help'.\n" + res.Stdout() + "\n"
|
|
|
|
assert.Assert(t, is.Equal(res2.Stderr(), exp))
|
|
|
|
})
|
2018-12-11 09:50:04 -05:00
|
|
|
}
|