e2e/cli-plugins: explicitly check that PersistentPreRunE works

I regressed this in d4ced2ef77 ("allow plugins to have argument which match a
top-level flag.") by unconditionally overwriting any `PersistentRunE` that the
user may have supplied.

We need to ensure two things:

1. That the user can use `PersistentRunE` (or `PersistentRun`) for their own
   purposes.
2. That our initialisation always runs, even if the user has used
   `PersistentRun*`, since that will shadow the root.

To do this add a `PersistentRunE` to the helloworld plugin which logs (covers 1
above) and then use it when calling the `apiversion` subcommand (which covers 2
since that uses the client)

Signed-off-by: Ian Campbell <ijc@docker.com>
This commit is contained in:
Ian Campbell 2019-03-13 17:50:51 +00:00
parent 237bdbf5f6
commit 0b794e0620
3 changed files with 12 additions and 4 deletions

View File

@ -46,11 +46,17 @@ func main() {
var ( var (
who, context string who, context string
debug bool preRun, debug bool
) )
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "helloworld", Use: "helloworld",
Short: "A basic Hello World plugin for tests", Short: "A basic Hello World plugin for tests",
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
if preRun {
fmt.Fprintf(dockerCli.Err(), "Plugin PersistentPreRunE called")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if debug { if debug {
fmt.Fprintf(dockerCli.Err(), "Plugin debug mode enabled") fmt.Fprintf(dockerCli.Err(), "Plugin debug mode enabled")
@ -79,6 +85,7 @@ func main() {
flags := cmd.Flags() flags := cmd.Flags()
flags.StringVar(&who, "who", "", "Who are we addressing?") flags.StringVar(&who, "who", "", "Who are we addressing?")
flags.BoolVar(&preRun, "pre-run", false, "Log from prerun hook")
// These are intended to deliberately clash with the CLIs own top // These are intended to deliberately clash with the CLIs own top
// level arguments. // level arguments.
flags.BoolVarP(&debug, "debug", "D", false, "Enable debug") flags.BoolVarP(&debug, "debug", "D", false, "Enable debug")

View File

@ -177,10 +177,10 @@ func TestCliInitialized(t *testing.T) {
run, _, cleanup := prepare(t) run, _, cleanup := prepare(t)
defer cleanup() defer cleanup()
res := icmd.RunCmd(run("helloworld", "apiversion")) res := icmd.RunCmd(run("helloworld", "--pre-run", "apiversion"))
res.Assert(t, icmd.Success) res.Assert(t, icmd.Success)
assert.Assert(t, res.Stdout() != "") assert.Assert(t, res.Stdout() != "")
assert.Assert(t, is.Equal(res.Stderr(), "")) assert.Assert(t, is.Equal(res.Stderr(), "Plugin PersistentPreRunE called"))
} }
// TestPluginErrorCode tests when the plugin return with a given exit status. // TestPluginErrorCode tests when the plugin return with a given exit status.

View File

@ -6,6 +6,7 @@ A basic Hello World plugin for tests
Options: Options:
-c, --context string Is it Christmas? -c, --context string Is it Christmas?
-D, --debug Enable debug -D, --debug Enable debug
--pre-run Log from prerun hook
--who string Who are we addressing? --who string Who are we addressing?
Commands: Commands: