Skip to content

Commit e11bdd7

Browse files
author
Ian Campbell
committed
Add e2e tests for plugin vs global argument issues
These won't pass right now due to #1699 ("Plugins can't re-use the same flags as cli global flags") and the change in 935d47b ("Ignore unknown arguments on the top-level command."), but the intention is to fix them now. Signed-off-by: Ian Campbell <[email protected]>
1 parent d81aed2 commit e11bdd7

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

cli-plugins/examples/helloworld/main.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ func main() {
4444
},
4545
}
4646

47-
var who string
47+
var (
48+
who, context string
49+
debug bool
50+
)
4851
cmd := &cobra.Command{
4952
Use: "helloworld",
5053
Short: "A basic Hello World plugin for tests",
@@ -53,6 +56,18 @@ func main() {
5356
// hook.
5457
PersistentPreRunE: plugin.PersistentPreRunE,
5558
RunE: func(cmd *cobra.Command, args []string) error {
59+
if debug {
60+
fmt.Fprintf(dockerCli.Err(), "Plugin debug mode enabled")
61+
}
62+
63+
switch context {
64+
case "Christmas":
65+
fmt.Fprintf(dockerCli.Out(), "Merry Christmas!\n")
66+
return nil
67+
case "":
68+
// nothing
69+
}
70+
5671
if who == "" {
5772
who, _ = dockerCli.ConfigFile().PluginConfig("helloworld", "who")
5873
}
@@ -68,6 +83,10 @@ func main() {
6883

6984
flags := cmd.Flags()
7085
flags.StringVar(&who, "who", "", "Who are we addressing?")
86+
// These are intended to deliberately clash with the CLIs own top
87+
// level arguments.
88+
flags.BoolVarP(&debug, "debug", "D", false, "Enable debug")
89+
flags.StringVarP(&context, "context", "c", "", "Is it Christmas?")
7190

7291
cmd.AddCommand(goodbye, apiversion, exitStatus2)
7392
return cmd

e2e/cli-plugins/flags_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cliplugins
33
import (
44
"testing"
55

6+
"gotest.tools/assert"
7+
is "gotest.tools/assert/cmp"
68
"gotest.tools/icmd"
79
)
810

@@ -17,3 +19,82 @@ func TestRunGoodArgument(t *testing.T) {
1719
Out: "Hello Cleveland!",
1820
})
1921
}
22+
23+
// TestClashWithGlobalArgs ensures correct behaviour when a plugin
24+
// has an argument with the same name as one of the globals.
25+
func TestClashWithGlobalArgs(t *testing.T) {
26+
run, _, cleanup := prepare(t)
27+
defer cleanup()
28+
29+
for _, tc := range []struct {
30+
name string
31+
args []string
32+
expectedOut, expectedErr string
33+
}{
34+
{
35+
name: "short-without-val",
36+
args: []string{"-D"},
37+
expectedOut: "Hello World!",
38+
expectedErr: "Plugin debug mode enabled",
39+
},
40+
{
41+
name: "long-without-val",
42+
args: []string{"--debug"},
43+
expectedOut: "Hello World!",
44+
expectedErr: "Plugin debug mode enabled",
45+
},
46+
{
47+
name: "short-with-val",
48+
args: []string{"-c", "Christmas"},
49+
expectedOut: "Merry Christmas!",
50+
expectedErr: "",
51+
},
52+
{
53+
name: "short-with-val",
54+
args: []string{"--context", "Christmas"},
55+
expectedOut: "Merry Christmas!",
56+
expectedErr: "",
57+
},
58+
} {
59+
t.Run(tc.name, func(t *testing.T) {
60+
args := append([]string{"helloworld"}, tc.args...)
61+
res := icmd.RunCmd(run(args...))
62+
res.Assert(t, icmd.Expected{
63+
ExitCode: 0,
64+
Out: tc.expectedOut,
65+
Err: tc.expectedErr,
66+
})
67+
})
68+
}
69+
}
70+
71+
// TestUnknownGlobal checks that unknown globals report errors
72+
func TestUnknownGlobal(t *testing.T) {
73+
run, _, cleanup := prepare(t)
74+
defer cleanup()
75+
76+
t.Run("no-val", func(t *testing.T) {
77+
res := icmd.RunCmd(run("--unknown", "helloworld"))
78+
res.Assert(t, icmd.Expected{
79+
ExitCode: 125,
80+
})
81+
assert.Assert(t, is.Equal(res.Stdout(), ""))
82+
assert.Assert(t, is.Contains(res.Stderr(), "unknown flag: --unknown"))
83+
})
84+
t.Run("separate-val", func(t *testing.T) {
85+
res := icmd.RunCmd(run("--unknown", "foo", "helloworld"))
86+
res.Assert(t, icmd.Expected{
87+
ExitCode: 125,
88+
})
89+
assert.Assert(t, is.Equal(res.Stdout(), ""))
90+
assert.Assert(t, is.Contains(res.Stderr(), "unknown flag: --unknown"))
91+
})
92+
t.Run("joined-val", func(t *testing.T) {
93+
res := icmd.RunCmd(run("--unknown=foo", "helloworld"))
94+
res.Assert(t, icmd.Expected{
95+
ExitCode: 125,
96+
})
97+
assert.Assert(t, is.Equal(res.Stdout(), ""))
98+
assert.Assert(t, is.Contains(res.Stderr(), "unknown flag: --unknown"))
99+
})
100+
}

e2e/cli-plugins/testdata/docker-help-helloworld.golden

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Usage: docker helloworld [OPTIONS] COMMAND
44
A basic Hello World plugin for tests
55

66
Options:
7-
--who string Who are we addressing?
7+
-c, --context string Is it Christmas?
8+
-D, --debug Enable debug
9+
--who string Who are we addressing?
810

911
Commands:
1012
apiversion Print the API version of the server

0 commit comments

Comments
 (0)