@@ -32,14 +32,12 @@ func (c *fakeCandidate) Metadata() ([]byte, error) {
3232func TestValidateCandidate (t * testing.T ) {
3333 const (
3434 goodPluginName = metadata .NamePrefix + "goodplugin"
35+ builtinName = metadata .NamePrefix + "builtin"
36+ builtinAlias = metadata .NamePrefix + "alias"
3537
36- builtinName = metadata .NamePrefix + "builtin"
37- builtinAlias = metadata .NamePrefix + "alias"
38-
39- badPrefixPath = "/usr/local/libexec/cli-plugins/wobble"
40- badNamePath = "/usr/local/libexec/cli-plugins/docker-123456"
41- goodPluginPath = "/usr/local/libexec/cli-plugins/" + goodPluginName
42- metaExperimental = `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing", "Experimental": true}`
38+ badPrefixPath = "/usr/local/libexec/cli-plugins/wobble"
39+ badNamePath = "/usr/local/libexec/cli-plugins/docker-123456"
40+ goodPluginPath = "/usr/local/libexec/cli-plugins/" + goodPluginName
4341 )
4442
4543 fakeroot := & cobra.Command {Use : "docker" }
@@ -51,31 +49,83 @@ func TestValidateCandidate(t *testing.T) {
5149 })
5250
5351 for _ , tc := range []struct {
54- name string
55- c * fakeCandidate
52+ name string
53+ plugin * fakeCandidate
5654
5755 // Either err or invalid may be non-empty, but not both (both can be empty for a good plugin).
5856 err string
5957 invalid string
6058 }{
61- /* Each failing one of the tests */
62- {name : "empty path" , c : & fakeCandidate {path : "" }, err : "plugin candidate path cannot be empty" },
63- {name : "bad prefix" , c : & fakeCandidate {path : badPrefixPath }, err : fmt .Sprintf ("does not have %q prefix" , metadata .NamePrefix )},
64- {name : "bad path" , c : & fakeCandidate {path : badNamePath }, invalid : "did not match" },
65- {name : "builtin command" , c : & fakeCandidate {path : builtinName }, invalid : `plugin "builtin" duplicates builtin command` },
66- {name : "builtin alias" , c : & fakeCandidate {path : builtinAlias }, invalid : `plugin "alias" duplicates an alias of builtin command "builtin"` },
67- {name : "fetch failure" , c : & fakeCandidate {path : goodPluginPath , exec : false }, invalid : fmt .Sprintf ("failed to fetch metadata: faked a failure to exec %q" , goodPluginPath )},
68- {name : "metadata not json" , c : & fakeCandidate {path : goodPluginPath , exec : true , meta : `xyzzy` }, invalid : "invalid character" },
69- {name : "empty schemaversion" , c : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{}` }, invalid : `plugin SchemaVersion "" is not valid` },
70- {name : "invalid schemaversion" , c : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{"SchemaVersion": "xyzzy"}` }, invalid : `plugin SchemaVersion "xyzzy" is not valid` },
71- {name : "no vendor" , c : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{"SchemaVersion": "0.1.0"}` }, invalid : "plugin metadata does not define a vendor" },
72- {name : "empty vendor" , c : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{"SchemaVersion": "0.1.0", "Vendor": ""}` }, invalid : "plugin metadata does not define a vendor" },
73- // This one should work
74- {name : "valid" , c : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing"}` }},
75- {name : "experimental + allowing experimental" , c : & fakeCandidate {path : goodPluginPath , exec : true , meta : metaExperimental }},
59+ // Invalid cases.
60+ {
61+ name : "empty path" ,
62+ plugin : & fakeCandidate {path : "" },
63+ err : "plugin candidate path cannot be empty" ,
64+ },
65+ {
66+ name : "bad prefix" ,
67+ plugin : & fakeCandidate {path : badPrefixPath },
68+ err : fmt .Sprintf ("does not have %q prefix" , metadata .NamePrefix ),
69+ },
70+ {
71+ name : "bad path" ,
72+ plugin : & fakeCandidate {path : badNamePath },
73+ invalid : "did not match" ,
74+ },
75+ {
76+ name : "builtin command" ,
77+ plugin : & fakeCandidate {path : builtinName },
78+ invalid : `plugin "builtin" duplicates builtin command` ,
79+ },
80+ {
81+ name : "builtin alias" ,
82+ plugin : & fakeCandidate {path : builtinAlias },
83+ invalid : `plugin "alias" duplicates an alias of builtin command "builtin"` ,
84+ },
85+ {
86+ name : "fetch failure" ,
87+ plugin : & fakeCandidate {path : goodPluginPath , exec : false },
88+ invalid : fmt .Sprintf ("failed to fetch metadata: faked a failure to exec %q" , goodPluginPath ),
89+ },
90+ {
91+ name : "metadata not json" ,
92+ plugin : & fakeCandidate {path : goodPluginPath , exec : true , meta : `xyzzy` },
93+ invalid : "invalid character" ,
94+ },
95+ {
96+ name : "empty schemaversion" ,
97+ plugin : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{}` },
98+ invalid : `plugin SchemaVersion "" is not valid` ,
99+ },
100+ {
101+ name : "invalid schemaversion" ,
102+ plugin : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{"SchemaVersion": "xyzzy"}` },
103+ invalid : `plugin SchemaVersion "xyzzy" is not valid` ,
104+ },
105+ {
106+ name : "no vendor" ,
107+ plugin : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{"SchemaVersion": "0.1.0"}` },
108+ invalid : "plugin metadata does not define a vendor" ,
109+ },
110+ {
111+ name : "empty vendor" ,
112+ plugin : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{"SchemaVersion": "0.1.0", "Vendor": ""}` },
113+ invalid : "plugin metadata does not define a vendor" ,
114+ },
115+
116+ // Valid cases.
117+ {
118+ name : "valid" ,
119+ plugin : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing"}` },
120+ },
121+ {
122+ // Including the deprecated "experimental" field should not break processing.
123+ name : "with legacy experimental" ,
124+ plugin : & fakeCandidate {path : goodPluginPath , exec : true , meta : `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing", "Experimental": true}` },
125+ },
76126 } {
77127 t .Run (tc .name , func (t * testing.T ) {
78- p , err := newPlugin (tc .c , fakeroot .Commands ())
128+ p , err := newPlugin (tc .plugin , fakeroot .Commands ())
79129 switch {
80130 case tc .err != "" :
81131 assert .ErrorContains (t , err , tc .err )
0 commit comments