2018-12-11 09:50:04 -05:00
|
|
|
package cliplugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/icmd"
|
2018-12-11 09:50:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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(), ""))
|
2022-03-26 12:54:40 -04:00
|
|
|
output := res.Stdout()
|
2018-12-11 09:50:04 -05:00
|
|
|
|
|
|
|
// 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.
|
2022-03-26 12:54:40 -04:00
|
|
|
for _, s := range []string{
|
|
|
|
`Management Commands:`,
|
|
|
|
`\s+container\s+Manage containers`,
|
2024-02-26 09:31:25 -05:00
|
|
|
`\s+helloworld\*\s+A basic Hello World plugin for tests`,
|
2022-03-26 12:54:40 -04:00
|
|
|
`\s+image\s+Manage images`,
|
|
|
|
`Commands:`,
|
|
|
|
`\s+create\s+Create a new container`,
|
|
|
|
`Invalid Plugins:`,
|
|
|
|
`\s+badmeta\s+invalid metadata: invalid character 'i' looking for beginning of object key string`,
|
2018-12-11 09:50:04 -05:00
|
|
|
} {
|
2022-03-26 12:54:40 -04:00
|
|
|
expected := regexp.MustCompile(`(?m)^` + s + `$`)
|
|
|
|
matches := expected.FindAllString(output, -1)
|
|
|
|
assert.Equal(t, len(matches), 1, "Did not find expected number of matches for %q in `docker help` output", expected)
|
2018-12-11 09:50:04 -05:00
|
|
|
}
|
2019-01-30 05:46:31 -05:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
})
|
2022-03-26 12:54:40 -04:00
|
|
|
assert.Assert(t, is.Equal(res2.Stdout(), output))
|
2019-04-26 05:37:26 -04:00
|
|
|
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(), ""))
|
2022-03-26 12:54:40 -04:00
|
|
|
assert.Assert(t, is.Equal(res2.Stderr(), output))
|
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(), ""))
|
2022-03-26 12:54:40 -04:00
|
|
|
assert.Assert(t, is.Contains(res2.Stderr(), "unknown flag: --badopt"))
|
|
|
|
assert.Assert(t, is.Contains(res2.Stderr(), "See 'docker --help"))
|
2019-04-26 10:08:22 -04:00
|
|
|
})
|
2018-12-11 09:50:04 -05:00
|
|
|
}
|