-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathremote_test.go
More file actions
368 lines (326 loc) · 10.8 KB
/
Copy pathremote_test.go
File metadata and controls
368 lines (326 loc) · 10.8 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright 2026 DataRobot, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plugin
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestLivePluginRegistrySchema validates the actual docs/plugins/index.json file
func TestLivePluginRegistrySchema(t *testing.T) {
// Find the project root by looking for go.mod
projectRoot, err := findProjectRoot()
require.NoError(t, err, "Failed to find project root")
indexPath := filepath.Join(projectRoot, "docs", "plugins", "index.json")
data, err := os.ReadFile(indexPath)
require.NoError(t, err, "Failed to read docs/plugins/index.json - file must exist")
var registry PluginRegistry
err = json.Unmarshal(data, ®istry)
require.NoError(t, err, "docs/plugins/index.json must be valid JSON")
// Validate required fields
assert.NotEmpty(t, registry.Version, "index.json must have a version field")
assert.NotEmpty(t, registry.Plugins, "index.json must have at least one plugin")
// Validate each plugin entry
for pluginName, plugin := range registry.Plugins {
t.Run("Plugin_"+pluginName, func(t *testing.T) {
assert.NotEmpty(t, plugin.Name, "Plugin %s must have a name", pluginName)
assert.NotEmpty(t, plugin.Description, "Plugin %s must have a description", pluginName)
assert.NotEmpty(t, plugin.Versions, "Plugin %s must have at least one version", pluginName)
// Validate each version entry
for i, version := range plugin.Versions {
t.Run("Version_"+version.Version, func(t *testing.T) {
assert.NotEmpty(t, version.Version, "Plugin %s version %d must have a version", pluginName, i)
assert.NotEmpty(t, version.URL, "Plugin %s version %s must have a URL", pluginName, version.Version)
// URL can be absolute (with ://) or relative (without ://)
// Examples:
// - Absolute: https://cli.datarobot.com/plugins/assist/assist-0.1.6.tar.xz
// - Relative: assist/assist-0.1.6.tar.xz
isAbsolute := strings.Contains(version.URL, "://")
isRelative := !isAbsolute && (strings.HasSuffix(version.URL, ".tar.xz") || strings.HasSuffix(version.URL, ".tar.gz"))
assert.True(t, isAbsolute || isRelative, "Plugin %s version %s URL must be either absolute (with ://) or relative path ending in .tar.xz/.tar.gz", pluginName, version.Version)
// Warn if SHA256 is missing (not required but recommended)
if version.SHA256 == "" {
t.Logf("WARNING: Plugin %s version %s is missing SHA256 checksum", pluginName, version.Version)
}
// Validate SHA256 format if present
if version.SHA256 != "" {
assert.Len(t, version.SHA256, 64, "Plugin %s version %s SHA256 must be 64 hex characters", pluginName, version.Version)
assert.Regexp(t, "^[a-f0-9]+$", version.SHA256, "Plugin %s version %s SHA256 must be lowercase hex", pluginName, version.Version)
}
// Validate version format (basic semver check)
assert.Regexp(t, `^v?\d+\.\d+\.\d+`, version.Version, "Plugin %s version %s must be semver format", pluginName, version.Version)
})
}
})
}
}
// TestPluginManifestSchema validates plugin manifest JSON schema
func TestPluginManifestSchema(t *testing.T) {
tests := []struct {
name string
json string
expectError bool
validate func(*testing.T, *PluginManifest)
}{
{
name: "valid minimal manifest",
json: `{"name":"test"}`,
validate: func(t *testing.T, m *PluginManifest) {
assert.Equal(t, "test", m.Name)
assert.Empty(t, m.Version)
assert.Empty(t, m.Description)
},
},
{
name: "valid full manifest with authentication",
json: `{
"name":"test",
"version":"1.0.0",
"description":"Test plugin",
"cliVersion":"0.2.0",
"authentication":true,
"scripts":{
"posix":"scripts/test.sh",
"windows":"scripts/test.ps1"
}
}`,
validate: func(t *testing.T, m *PluginManifest) {
assert.Equal(t, "test", m.Name)
assert.Equal(t, "1.0.0", m.Version)
assert.Equal(t, "Test plugin", m.Description)
assert.Equal(t, "0.2.0", m.CLIVersion)
assert.True(t, m.Authentication)
require.NotNil(t, m.Scripts)
assert.Equal(t, "scripts/test.sh", m.Scripts.Posix)
assert.Equal(t, "scripts/test.ps1", m.Scripts.Windows)
},
},
{
name: "wrong field name - authenticated instead of authentication",
json: `{
"name":"test",
"version":"1.0.0",
"description":"Test plugin",
"authenticated":true,
"scripts":{
"posix":"scripts/test.sh"
}
}`,
validate: func(t *testing.T, m *PluginManifest) {
assert.Equal(t, "test", m.Name)
assert.Equal(t, "1.0.0", m.Version)
assert.Equal(t, "Test plugin", m.Description)
assert.False(t, m.Authentication, "authenticated field should be ignored, authentication should be false")
require.NotNil(t, m.Scripts)
assert.Equal(t, "scripts/test.sh", m.Scripts.Posix)
},
},
{
name: "invalid JSON",
json: `{invalid}`,
expectError: true,
},
{
name: "extra fields ignored",
json: `{
"name":"test",
"unknownField":"value"
}`,
validate: func(t *testing.T, m *PluginManifest) {
assert.Equal(t, "test", m.Name)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var manifest PluginManifest
err := json.Unmarshal([]byte(tt.json), &manifest)
if tt.expectError {
assert.Error(t, err)
} else {
require.NoError(t, err)
if tt.validate != nil {
tt.validate(t, &manifest)
}
}
})
}
}
// TestPluginRegistryParsing validates plugin registry JSON parsing
func TestPluginRegistryParsing(t *testing.T) {
tests := []struct {
name string
json string
expectError bool
validate func(*testing.T, *PluginRegistry)
}{
{
name: "valid index with one plugin",
json: `{
"version":"1",
"plugins":{
"test":{
"name":"test",
"description":"Test plugin",
"repository":"https://github.com/test/test",
"versions":[
{
"version":"1.0.0",
"url":"https://example.com/test-1.0.0.tar.xz",
"sha256":"abc123"
}
]
}
}
}`,
validate: func(t *testing.T, reg *PluginRegistry) {
assert.Equal(t, "1", reg.Version)
assert.Len(t, reg.Plugins, 1)
plugin := reg.Plugins["test"]
assert.Equal(t, "test", plugin.Name)
assert.Equal(t, "Test plugin", plugin.Description)
assert.Len(t, plugin.Versions, 1)
assert.Equal(t, "1.0.0", plugin.Versions[0].Version)
assert.Equal(t, "https://example.com/test-1.0.0.tar.xz", plugin.Versions[0].URL)
},
},
{
name: "multiple plugins and versions",
json: `{
"version":"1",
"plugins":{
"plugin1":{"name":"plugin1","versions":[{"version":"1.0.0","url":"https://example.com/p1.tar.xz"}]},
"plugin2":{"name":"plugin2","versions":[
{"version":"2.0.0","url":"https://example.com/p2-2.tar.xz"},
{"version":"1.0.0","url":"https://example.com/p2-1.tar.xz"}
]}
}
}`,
validate: func(t *testing.T, reg *PluginRegistry) {
assert.Len(t, reg.Plugins, 2)
assert.Len(t, reg.Plugins["plugin2"].Versions, 2)
},
},
{
name: "invalid JSON",
json: `{invalid}`,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var registry PluginRegistry
err := json.Unmarshal([]byte(tt.json), ®istry)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
if tt.validate != nil {
tt.validate(t, ®istry)
}
}
})
}
}
// TestInstalledPluginMetadata validates installed plugin metadata JSON
func TestInstalledPluginMetadata(t *testing.T) {
meta := InstalledPlugin{
Name: "test",
Version: "1.0.0",
Source: "https://example.com/test.tar.xz",
InstalledAt: "2026-01-27T12:00:00Z",
}
data, err := json.Marshal(meta)
require.NoError(t, err)
var decoded InstalledPlugin
err = json.Unmarshal(data, &decoded)
require.NoError(t, err)
assert.Equal(t, meta.Name, decoded.Name)
assert.Equal(t, meta.Version, decoded.Version)
assert.Equal(t, meta.Source, decoded.Source)
assert.Equal(t, meta.InstalledAt, decoded.InstalledAt)
}
// TestResolveVersion tests semver constraint resolution
func TestResolveVersion(t *testing.T) {
versions := []RegistryVersion{
{Version: "2.1.0", URL: "url-2.1.0"},
{Version: "2.0.0", URL: "url-2.0.0"},
{Version: "1.5.0", URL: "url-1.5.0"},
{Version: "1.2.3", URL: "url-1.2.3"},
{Version: "1.2.0", URL: "url-1.2.0"},
{Version: "1.0.0", URL: "url-1.0.0"},
}
tests := []struct {
name string
constraint string
expectedVer string
expectError bool
errorContains string
}{
{"latest", "latest", "2.1.0", false, ""},
{"empty defaults to latest", "", "2.1.0", false, ""},
{"exact match", "1.2.3", "1.2.3", false, ""},
{"exact with v prefix", "1.2.0", "1.2.0", false, ""}, // Note: versions in test don't have v prefix
{"caret same major", "^1.2.0", "1.5.0", false, ""},
{"caret newer major excluded", "^1.0.0", "1.5.0", false, ""},
{"tilde same minor", "~1.2.0", "1.2.3", false, ""},
{"tilde newer minor excluded", "~1.0.0", "1.0.0", false, ""},
{"gte constraint", ">=1.2.0", "2.1.0", false, ""},
{"gte exact match", ">=1.5.0", "2.1.0", false, ""},
{"version not found", "3.0.0", "", true, "No version matching constraint found"},
{"invalid constraint", "@invalid", "", true, "Invalid version constraint"}, // Invalid semver syntax
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := ResolveVersion(versions, tt.constraint)
if tt.expectError {
require.Error(t, err)
if tt.errorContains != "" {
assert.Contains(t, err.Error(), tt.errorContains)
}
} else {
require.NoError(t, err)
assert.Equal(t, tt.expectedVer, result.Version)
}
})
}
}
// TestResolveVersionEmpty tests error handling for empty version list
func TestResolveVersionEmpty(t *testing.T) {
_, err := ResolveVersion([]RegistryVersion{}, "latest")
require.Error(t, err)
assert.Contains(t, err.Error(), "No versions available")
}
// Helper function to find project root
func findProjectRoot() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir, nil
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
return "", os.ErrNotExist
}