|
| 1 | +package manager |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "gotest.tools/assert" |
| 11 | + "gotest.tools/assert/cmp" |
| 12 | +) |
| 13 | + |
| 14 | +type fakeCandidate struct { |
| 15 | + path string |
| 16 | + exec bool |
| 17 | + meta string |
| 18 | +} |
| 19 | + |
| 20 | +func (c *fakeCandidate) Path() string { |
| 21 | + return c.path |
| 22 | +} |
| 23 | + |
| 24 | +func (c *fakeCandidate) Metadata() ([]byte, error) { |
| 25 | + if !c.exec { |
| 26 | + return nil, fmt.Errorf("faked a failure to exec %q", c.path) |
| 27 | + } |
| 28 | + return []byte(c.meta), nil |
| 29 | +} |
| 30 | + |
| 31 | +func TestValidateCandidate(t *testing.T) { |
| 32 | + var ( |
| 33 | + goodPluginName = NamePrefix + "goodplugin" |
| 34 | + |
| 35 | + builtinName = NamePrefix + "builtin" |
| 36 | + builtinAlias = NamePrefix + "alias" |
| 37 | + |
| 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 |
| 41 | + ) |
| 42 | + |
| 43 | + fakeroot := &cobra.Command{Use: "docker"} |
| 44 | + fakeroot.AddCommand(&cobra.Command{ |
| 45 | + Use: strings.TrimPrefix(builtinName, NamePrefix), |
| 46 | + Aliases: []string{ |
| 47 | + strings.TrimPrefix(builtinAlias, NamePrefix), |
| 48 | + }, |
| 49 | + }) |
| 50 | + |
| 51 | + for _, tc := range []struct { |
| 52 | + c *fakeCandidate |
| 53 | + |
| 54 | + // Either err or invalid may be non-empty, but not both (both can be empty for a good plugin). |
| 55 | + err string |
| 56 | + invalid string |
| 57 | + }{ |
| 58 | + /* Each failing one of the tests */ |
| 59 | + {c: &fakeCandidate{path: ""}, err: "plugin candidate path cannot be empty"}, |
| 60 | + {c: &fakeCandidate{path: badPrefixPath}, err: fmt.Sprintf("does not have %q prefix", NamePrefix)}, |
| 61 | + {c: &fakeCandidate{path: badNamePath}, invalid: "did not match"}, |
| 62 | + {c: &fakeCandidate{path: builtinName}, invalid: `plugin "builtin" duplicates builtin command`}, |
| 63 | + {c: &fakeCandidate{path: builtinAlias}, invalid: `plugin "alias" duplicates an alias of builtin command "builtin"`}, |
| 64 | + {c: &fakeCandidate{path: goodPluginPath, exec: false}, invalid: fmt.Sprintf("failed to fetch metadata: faked a failure to exec %q", goodPluginPath)}, |
| 65 | + {c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `xyzzy`}, invalid: "invalid character"}, |
| 66 | + {c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{}`}, invalid: `plugin SchemaVersion "" is not valid`}, |
| 67 | + {c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "xyzzy"}`}, invalid: `plugin SchemaVersion "xyzzy" is not valid`}, |
| 68 | + {c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0"}`}, invalid: "plugin metadata does not define a vendor"}, |
| 69 | + {c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": ""}`}, invalid: "plugin metadata does not define a vendor"}, |
| 70 | + // This one should work |
| 71 | + {c: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing"}`}}, |
| 72 | + } { |
| 73 | + p, err := newPlugin(tc.c, fakeroot) |
| 74 | + if tc.err != "" { |
| 75 | + assert.ErrorContains(t, err, tc.err) |
| 76 | + } else if tc.invalid != "" { |
| 77 | + assert.NilError(t, err) |
| 78 | + assert.Assert(t, cmp.ErrorType(p.Err, reflect.TypeOf(&pluginError{}))) |
| 79 | + assert.ErrorContains(t, p.Err, tc.invalid) |
| 80 | + } else { |
| 81 | + assert.NilError(t, err) |
| 82 | + assert.Equal(t, NamePrefix+p.Name, goodPluginName) |
| 83 | + assert.Equal(t, p.SchemaVersion, "0.1.0") |
| 84 | + assert.Equal(t, p.Vendor, "e2e-testing") |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestCandidatePath(t *testing.T) { |
| 90 | + exp := "/some/path" |
| 91 | + cand := &candidate{path: exp} |
| 92 | + assert.Equal(t, exp, cand.Path()) |
| 93 | +} |
0 commit comments