From 0b794e0620b93a304cb2ad48112582489467812d Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 13 Mar 2019 17:50:51 +0000 Subject: [PATCH] e2e/cli-plugins: explicitly check that PersistentPreRunE works I regressed this in d4ced2ef77fc ("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 --- cli-plugins/examples/helloworld/main.go | 11 +++++++++-- e2e/cli-plugins/run_test.go | 4 ++-- .../testdata/docker-help-helloworld.golden | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cli-plugins/examples/helloworld/main.go b/cli-plugins/examples/helloworld/main.go index eca6acbef9..a99c59122e 100644 --- a/cli-plugins/examples/helloworld/main.go +++ b/cli-plugins/examples/helloworld/main.go @@ -45,12 +45,18 @@ func main() { } var ( - who, context string - debug bool + who, context string + preRun, debug bool ) cmd := &cobra.Command{ Use: "helloworld", 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 { if debug { fmt.Fprintf(dockerCli.Err(), "Plugin debug mode enabled") @@ -79,6 +85,7 @@ func main() { flags := cmd.Flags() 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 // level arguments. flags.BoolVarP(&debug, "debug", "D", false, "Enable debug") diff --git a/e2e/cli-plugins/run_test.go b/e2e/cli-plugins/run_test.go index 171ed81c83..96cbead4a0 100644 --- a/e2e/cli-plugins/run_test.go +++ b/e2e/cli-plugins/run_test.go @@ -177,10 +177,10 @@ func TestCliInitialized(t *testing.T) { run, _, cleanup := prepare(t) defer cleanup() - res := icmd.RunCmd(run("helloworld", "apiversion")) + res := icmd.RunCmd(run("helloworld", "--pre-run", "apiversion")) res.Assert(t, icmd.Success) 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. diff --git a/e2e/cli-plugins/testdata/docker-help-helloworld.golden b/e2e/cli-plugins/testdata/docker-help-helloworld.golden index 24bc424bd7..9d2f6673ee 100644 --- a/e2e/cli-plugins/testdata/docker-help-helloworld.golden +++ b/e2e/cli-plugins/testdata/docker-help-helloworld.golden @@ -6,6 +6,7 @@ A basic Hello World plugin for tests Options: -c, --context string Is it Christmas? -D, --debug Enable debug + --pre-run Log from prerun hook --who string Who are we addressing? Commands: