cli-plugins: add test names for easier debugging

Signed-off-by: Tibor Vass <tibor@docker.com>
(cherry picked from commit bb8e89bb2e)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Tibor Vass 2019-05-21 22:24:00 +00:00 committed by Sebastiaan van Stijn
parent c5431132d7
commit 57f1de13b3
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 36 additions and 32 deletions

View File

@ -36,9 +36,10 @@ func TestValidateCandidate(t *testing.T) {
builtinName = NamePrefix + "builtin" builtinName = NamePrefix + "builtin"
builtinAlias = NamePrefix + "alias" builtinAlias = NamePrefix + "alias"
badPrefixPath = "/usr/local/libexec/cli-plugins/wobble" badPrefixPath = "/usr/local/libexec/cli-plugins/wobble"
badNamePath = "/usr/local/libexec/cli-plugins/docker-123456" badNamePath = "/usr/local/libexec/cli-plugins/docker-123456"
goodPluginPath = "/usr/local/libexec/cli-plugins/" + goodPluginName goodPluginPath = "/usr/local/libexec/cli-plugins/" + goodPluginName
metaExperimental = `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing", "Experimental": true}`
) )
fakeroot := &cobra.Command{Use: "docker"} fakeroot := &cobra.Command{Use: "docker"}
@ -50,43 +51,46 @@ func TestValidateCandidate(t *testing.T) {
}) })
for _, tc := range []struct { for _, tc := range []struct {
c *fakeCandidate name string
c *fakeCandidate
// Either err or invalid may be non-empty, but not both (both can be empty for a good plugin). // Either err or invalid may be non-empty, but not both (both can be empty for a good plugin).
err string err string
invalid string invalid string
}{ }{
/* Each failing one of the tests */ /* Each failing one of the tests */
{c: &fakeCandidate{path: ""}, err: "plugin candidate path cannot be empty"}, {name: "empty path", c: &fakeCandidate{path: ""}, err: "plugin candidate path cannot be empty"},
{c: &fakeCandidate{path: badPrefixPath}, err: fmt.Sprintf("does not have %q prefix", NamePrefix)}, {name: "bad prefix", c: &fakeCandidate{path: badPrefixPath}, err: fmt.Sprintf("does not have %q prefix", NamePrefix)},
{c: &fakeCandidate{path: badNamePath}, invalid: "did not match"}, {name: "bad path", c: &fakeCandidate{path: badNamePath}, invalid: "did not match"},
{c: &fakeCandidate{path: builtinName}, invalid: `plugin "builtin" duplicates builtin command`}, {name: "builtin command", c: &fakeCandidate{path: builtinName}, invalid: `plugin "builtin" duplicates builtin command`},
{c: &fakeCandidate{path: builtinAlias}, invalid: `plugin "alias" duplicates an alias of builtin command "builtin"`}, {name: "builtin alias", c: &fakeCandidate{path: builtinAlias}, invalid: `plugin "alias" duplicates an alias of builtin command "builtin"`},
{c: &fakeCandidate{path: goodPluginPath, exec: false}, invalid: fmt.Sprintf("failed to fetch metadata: faked a failure to exec %q", goodPluginPath)}, {name: "fetch failure", c: &fakeCandidate{path: goodPluginPath, exec: false}, invalid: fmt.Sprintf("failed to fetch metadata: faked a failure to exec %q", goodPluginPath)},
{c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `xyzzy`}, invalid: "invalid character"}, {name: "metadata not json", c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `xyzzy`}, invalid: "invalid character"},
{c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{}`}, invalid: `plugin SchemaVersion "" is not valid`}, {name: "empty schemaversion", c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{}`}, invalid: `plugin SchemaVersion "" is not valid`},
{c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "xyzzy"}`}, invalid: `plugin SchemaVersion "xyzzy" is not valid`}, {name: "invalid schemaversion", c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "xyzzy"}`}, invalid: `plugin SchemaVersion "xyzzy" is not valid`},
{c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0"}`}, invalid: "plugin metadata does not define a vendor"}, {name: "no vendor", c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0"}`}, invalid: "plugin metadata does not define a vendor"},
{c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": ""}`}, invalid: "plugin metadata does not define a vendor"}, {name: "empty vendor", c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": ""}`}, invalid: "plugin metadata does not define a vendor"},
{c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing", "Experimental": true}`}, invalid: "requires experimental CLI"}, {name: "experimental required", c: &fakeCandidate{path: goodPluginPath, exec: true, meta: metaExperimental}, invalid: "requires experimental CLI"},
// This one should work // This one should work
{c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing"}`}}, {name: "valid", c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing"}`}},
{c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing"}`, allowExperimental: true}}, {name: "valid + allowing experimental", c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing"}`, allowExperimental: true}},
{c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing", "Experimental": true}`, allowExperimental: true}}, {name: "experimental + allowing experimental", c: &fakeCandidate{path: goodPluginPath, exec: true, meta: metaExperimental, allowExperimental: true}},
} { } {
p, err := newPlugin(tc.c, fakeroot, tc.c.allowExperimental) t.Run(tc.name, func(t *testing.T) {
if tc.err != "" { p, err := newPlugin(tc.c, fakeroot, tc.c.allowExperimental)
assert.ErrorContains(t, err, tc.err) if tc.err != "" {
} else if tc.invalid != "" { assert.ErrorContains(t, err, tc.err)
assert.NilError(t, err) } else if tc.invalid != "" {
assert.Assert(t, cmp.ErrorType(p.Err, reflect.TypeOf(&pluginError{}))) assert.NilError(t, err)
assert.ErrorContains(t, p.Err, tc.invalid) assert.Assert(t, cmp.ErrorType(p.Err, reflect.TypeOf(&pluginError{})))
} else { assert.ErrorContains(t, p.Err, tc.invalid)
assert.NilError(t, err) } else {
assert.Equal(t, NamePrefix+p.Name, goodPluginName) assert.NilError(t, err)
assert.Equal(t, p.SchemaVersion, "0.1.0") assert.Equal(t, NamePrefix+p.Name, goodPluginName)
assert.Equal(t, p.Vendor, "e2e-testing") assert.Equal(t, p.SchemaVersion, "0.1.0")
} assert.Equal(t, p.Vendor, "e2e-testing")
}
})
} }
} }