File: candidate_test.go

package info (click to toggle)
docker.io 28.5.2%2Bdfsg3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 68,176 kB
  • sloc: sh: 5,867; makefile: 863; ansic: 184; python: 162; asm: 159
file content (170 lines) | stat: -rw-r--r-- 5,128 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package manager

import (
	"fmt"
	"reflect"
	"strings"
	"testing"

	"github.com/docker/cli/cli-plugins/metadata"
	"github.com/spf13/cobra"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

type fakeCandidate struct {
	path string
	exec bool
	meta string
}

func (c *fakeCandidate) Path() string {
	return c.path
}

func (c *fakeCandidate) Metadata() ([]byte, error) {
	if !c.exec {
		return nil, fmt.Errorf("faked a failure to exec %q", c.path)
	}
	return []byte(c.meta), nil
}

func TestValidateCandidate(t *testing.T) {
	const (
		goodPluginName = metadata.NamePrefix + "goodplugin"
		builtinName    = metadata.NamePrefix + "builtin"
		builtinAlias   = metadata.NamePrefix + "alias"

		badPrefixPath  = "/usr/local/libexec/cli-plugins/wobble"
		badNamePath    = "/usr/local/libexec/cli-plugins/docker-123456"
		goodPluginPath = "/usr/local/libexec/cli-plugins/" + goodPluginName
	)

	fakeroot := &cobra.Command{Use: "docker"}
	fakeroot.AddCommand(&cobra.Command{
		Use: strings.TrimPrefix(builtinName, metadata.NamePrefix),
		Aliases: []string{
			strings.TrimPrefix(builtinAlias, metadata.NamePrefix),
		},
	})

	for _, tc := range []struct {
		name   string
		plugin *fakeCandidate

		// Either err or invalid may be non-empty, but not both (both can be empty for a good plugin).
		err     string
		invalid string
		expVer  string
	}{
		// Invalid cases.
		{
			name:   "empty path",
			plugin: &fakeCandidate{path: ""},
			err:    "plugin candidate path cannot be empty",
		},
		{
			name:   "bad prefix",
			plugin: &fakeCandidate{path: badPrefixPath},
			err:    fmt.Sprintf("does not have %q prefix", metadata.NamePrefix),
		},
		{
			name:    "bad path",
			plugin:  &fakeCandidate{path: badNamePath},
			invalid: "did not match",
		},
		{
			name:    "builtin command",
			plugin:  &fakeCandidate{path: builtinName},
			invalid: `plugin "builtin" duplicates builtin command`,
		},
		{
			name:    "builtin alias",
			plugin:  &fakeCandidate{path: builtinAlias},
			invalid: `plugin "alias" duplicates an alias of builtin command "builtin"`,
		},
		{
			name:    "fetch failure",
			plugin:  &fakeCandidate{path: goodPluginPath, exec: false},
			invalid: fmt.Sprintf("failed to fetch metadata: faked a failure to exec %q", goodPluginPath),
		},
		{
			name:    "metadata not json",
			plugin:  &fakeCandidate{path: goodPluginPath, exec: true, meta: `xyzzy`},
			invalid: "invalid character",
		},
		{
			name:    "empty schemaversion",
			plugin:  &fakeCandidate{path: goodPluginPath, exec: true, meta: `{}`},
			invalid: `plugin SchemaVersion version cannot be empty`,
		},
		{
			name:    "invalid schemaversion",
			plugin:  &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "xyzzy"}`},
			invalid: `plugin SchemaVersion "xyzzy" has wrong format: must be <major>.<minor>.<patch>`,
		},
		{
			name:    "invalid schemaversion major",
			plugin:  &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "2.0.0"}`},
			invalid: `plugin SchemaVersion "2.0.0" is not supported: must be lower than 2.0.0`,
		},
		{
			name:    "no vendor",
			plugin:  &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0"}`},
			invalid: "plugin metadata does not define a vendor",
		},
		{
			name:    "empty vendor",
			plugin:  &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": ""}`},
			invalid: "plugin metadata does not define a vendor",
		},

		// Valid cases.
		{
			name:   "valid",
			plugin: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing"}`},
			expVer: "0.1.0",
		},
		{
			// Including the deprecated "experimental" field should not break processing.
			name:   "with legacy experimental",
			plugin: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.1.0", "Vendor": "e2e-testing", "Experimental": true}`},
			expVer: "0.1.0",
		},
		{
			// note that this may not be supported by older CLIs
			name:   "new minor schema version",
			plugin: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "0.2.0", "Vendor": "e2e-testing"}`},
			expVer: "0.2.0",
		},
		{
			// note that this may not be supported by older CLIs
			name:   "new major schema version",
			plugin: &fakeCandidate{path: goodPluginPath, exec: true, meta: `{"SchemaVersion": "1.0.0", "Vendor": "e2e-testing"}`},
			expVer: "1.0.0",
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			p, err := newPlugin(tc.plugin, fakeroot.Commands())
			switch {
			case tc.err != "":
				assert.ErrorContains(t, err, tc.err)
			case tc.invalid != "":
				assert.NilError(t, err)
				assert.Assert(t, is.ErrorType(p.Err, reflect.TypeOf(&pluginError{})))
				assert.ErrorContains(t, p.Err, tc.invalid)
			default:
				assert.NilError(t, err)
				assert.Equal(t, metadata.NamePrefix+p.Name, goodPluginName)
				assert.Equal(t, p.SchemaVersion, tc.expVer)
				assert.Equal(t, p.Vendor, "e2e-testing")
			}
		})
	}
}

func TestCandidatePath(t *testing.T) {
	exp := "/some/path"
	cand := &candidate{path: exp}
	assert.Equal(t, exp, cand.Path())
}