-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain_test.go
More file actions
517 lines (498 loc) · 18.5 KB
/
main_test.go
File metadata and controls
517 lines (498 loc) · 18.5 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package main_test
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"github.com/r3labs/diff/v3"
"github.com/stretchr/testify/assert"
"github.com/grafana/plugin-validator/pkg/prettyprint"
)
type Issue struct {
Severity string `json:"severity"`
Title string `json:"title"`
Detail string `json:"detail"`
Name string `json:"name"`
}
type JsonReport struct {
Id string `json:"id"`
Version string `json:"version"`
PluginValidator map[string][]Issue `json:"plugin-validator"`
}
type tc struct {
name string
file string
extraArgs string
outputToFile string // if set, will add -output-to-file flag and verify file creation
jsonReport JsonReport
}
func TestIntegration(t *testing.T) {
basePath := "./testdata"
binary := filepath.Join(
"..",
"..",
"..",
"bin",
runtime.GOOS+"_"+runtime.GOARCH,
"plugincheck2",
)
env := []string{
"DEBUG=0",
"OPENAI_API_KEY=",
"GEMINI_API_KEY=",
}
var tcs = []tc{
{
file: "grafana-clock-panel-2.1.5.any.zip",
extraArgs: "",
jsonReport: JsonReport{
Id: "grafana-clock-panel",
Version: "2.1.5",
PluginValidator: map[string][]Issue{
"grafanadependency": {
{
Severity: "warning",
Title: "Grafana dependency \">=8.0.0\" has no pre-release value",
Detail: "The value of grafanaDependency in plugin.json (\">=8.0.0\") is missing a pre-release value. This may make the plugin uninstallable in Grafana Cloud. Please add \"-0\" as a suffix of your grafanaDependency value (\">=8.0.0-0\")",
Name: "missing-cloud-pre-release",
},
},
"jargon": {
{
Severity: "warning",
Title: "README.md contains developer jargon: (yarn)",
Detail: "Move any developer and contributor documentation to a separate file and link to it from the README.md. For example, CONTRIBUTING.md, DEVELOPMENT.md, etc.",
Name: "developer-jargon",
},
},
"sponsorshiplink": {
{
Severity: "recommendation",
Title: "You can include a sponsorship link if you want users to support your work",
Detail: "Consider to add a sponsorship link in your plugin.json file (Info.Links section: with Name: 'sponsor' or Name: 'sponsorship'), which will be shown on the plugin details page to allow users to support your work if they wish.",
Name: "sponsorshiplink",
},
},
},
},
},
{
file: "alexanderzobnin-zabbix-app-4.4.9.linux_amd64.zip",
extraArgs: "",
jsonReport: JsonReport{
Id: "alexanderzobnin-zabbix-app",
Version: "4.4.9",
PluginValidator: map[string][]Issue{
"grafanadependency": {
{
Severity: "warning",
Title: "Grafana dependency \">=9.3.0\" has no pre-release value",
Detail: "The value of grafanaDependency in plugin.json (\">=9.3.0\") is missing a pre-release value. This may make the plugin uninstallable in Grafana Cloud. Please add \"-0\" as a suffix of your grafanaDependency value (\">=9.3.0-0\")",
Name: "missing-cloud-pre-release",
},
},
"includesnested": {
{
Severity: "error",
Title: "Nested plugin datasource/plugin.json is not declared parent plugin.json",
Detail: "Found a plugin datasource/plugin.json nested inside your archive but not declared in plugin.json. Make sure to declare the type and path of the nested plugin",
Name: "nested-plugins-not-declared",
},
{
Severity: "error",
Title: "Nested plugin panel-triggers/plugin.json is not declared parent plugin.json",
Detail: "Found a plugin panel-triggers/plugin.json nested inside your archive but not declared in plugin.json. Make sure to declare the type and path of the nested plugin",
Name: "nested-plugins-not-declared",
},
},
"llmreview": {
{
Severity: "suspected",
Title: "LLM review skipped due to errors in manifest",
Detail: "Fix the errors reported by manifest before LLM review can run.",
Name: "llm-review-skipped",
},
},
"manifest": {
{
Severity: "error",
Title: "invalid file checksum",
Detail: "checksum for file README.md is invalid",
Name: "invalid-sha-sum",
},
},
"signature": {
{
Severity: "warning",
Title: "MANIFEST.txt: plugin has been modified since it was signed",
Detail: "The plugin might had been modified after it was signed.",
Name: "modified-signature",
},
},
"sponsorshiplink": {
{
Severity: "recommendation",
Title: "You can include a sponsorship link if you want users to support your work",
Detail: "Consider to add a sponsorship link in your plugin.json file (Info.Links section: with Name: 'sponsor' or Name: 'sponsorship'), which will be shown on the plugin details page to allow users to support your work if they wish.",
Name: "sponsorshiplink",
},
},
"codediff": {
{
Severity: "suspected",
Title: "Code diff skipped due to errors in manifest",
Detail: "Fix the errors reported by manifest before code diff can run.",
Name: "code-diff-skipped",
},
},
},
},
},
{
file: "yesoreyeram-infinity-datasource-2.6.3.linux_amd64.zip",
extraArgs: "",
jsonReport: JsonReport{
Id: "yesoreyeram-infinity-datasource",
Version: "2.6.3",
PluginValidator: map[string][]Issue{
"grafanadependency": {
{
Severity: "warning",
Title: "Grafana dependency \">=9.5.15\" has no pre-release value",
Detail: "The value of grafanaDependency in plugin.json (\">=9.5.15\") is missing a pre-release value. This may make the plugin uninstallable in Grafana Cloud. Please add \"-0\" as a suffix of your grafanaDependency value (\">=9.5.15-0\")",
Name: "missing-cloud-pre-release",
},
},
"sponsorshiplink": {
{
Severity: "recommendation",
Title: "You can include a sponsorship link if you want users to support your work",
Detail: "Consider to add a sponsorship link in your plugin.json file (Info.Links section: with Name: 'sponsor' or Name: 'sponsorship'), which will be shown on the plugin details page to allow users to support your work if they wish.",
Name: "sponsorshiplink",
},
},
},
},
},
{
file: "invalid.zip",
extraArgs: "",
jsonReport: JsonReport{
Id: "invalid-panel",
Version: "1.0.0",
PluginValidator: map[string][]Issue{
"archive": {
{
Severity: "error",
Title: "Archive contains more than one directory",
Detail: "Archive should contain only one directory named after plugin id. Found 7 directories. Please see https://grafana.com/developers/plugin-tools/publish-a-plugin/package-a-plugin for more information on how to package a plugin.",
Name: "more-than-one-dir",
},
{
Severity: "error",
Title: "Plugin archive is improperly structured",
Detail: "It is possible your plugin archive structure is incorrect. Please see https://grafana.com/developers/plugin-tools/publish-a-plugin/package-a-plugin for more information on how to package a plugin.",
Name: "zip-invalid",
},
},
"llmreview": {
{
Severity: "suspected",
Title: "LLM review skipped due to errors in archive",
Detail: "Fix the errors reported by archive before LLM review can run.",
Name: "llm-review-skipped",
},
},
"codediff": {
{
Severity: "suspected",
Title: "Code diff skipped due to errors in archive",
Detail: "Fix the errors reported by archive before code diff can run.",
Name: "code-diff-skipped",
},
},
},
},
},
{
file: "invalid2.zip",
extraArgs: "",
jsonReport: JsonReport{
Id: "invalid-panel",
Version: "1.0.0",
PluginValidator: map[string][]Issue{
"archivename": {
{
Severity: "error",
Title: "Archive should contain a directory named invalid-panel",
Detail: "The plugin archive file should contain a directory named after the plugin ID. This directory should contain the plugin's dist files. Please see https://grafana.com/developers/plugin-tools/publish-a-plugin/package-a-plugin for more information on how to package a plugin.",
Name: "no-ident-root-dir",
},
},
"pluginname": {
{
Severity: "warning",
Title: "plugin.json: plugin id should follow the format org-name-type",
Detail: "The plugin ID should be in the format org-name-type (e.g., myorg-myplugin-panel). It must have at least 3 parts separated by hyphens.",
Name: "invalid-id-format",
},
},
"license": {
{
Severity: "error",
Title: "LICENSE file could not be parsed.",
Detail: "Could not parse the license file inside the plugin archive. Please make sure to include a valid license in your LICENSE file in your archive.",
Name: "license-not-provided",
},
},
"manifest": {
{
Severity: "warning",
Title: "unsigned plugin",
Detail: "This is a new (unpublished) plugin. This is expected during the initial review process. Please allow the review to continue, and a member of our team will inform you when your plugin can be signed.",
Name: "unsigned-plugin",
},
},
"metadatapaths": {
{
Severity: "error",
Title: "plugin.json: small logo path doesn't exists: img/logo.svg",
Detail: "Refer only existing files. Make sure the files referred in plugin.json are included in the archive.",
Name: "path-not-exists",
},
{
Severity: "error",
Title: "plugin.json: large logo path doesn't exists: img/logo.svg",
Detail: "Refer only existing files. Make sure the files referred in plugin.json are included in the archive.",
Name: "path-not-exists",
},
},
"metadatavalid": {
{
Severity: "error",
Title: "plugin.json: dependencies: grafanaDependency is required",
Detail: "The plugin.json file is not following the schema. Please refer to the documentation for more information. https://grafana.com/docs/grafana/latest/developers/plugins/metadata/",
Name: "invalid-metadata",
},
},
"readme": {
{
Severity: "error",
Title: "README.md is empty",
Detail: "A README.md file is required for plugins. The contents of the file will be displayed in the Plugin catalog.",
Name: "missing-readme",
},
},
"screenshots": {
{
Severity: "warning",
Title: "plugin.json: should include screenshots for the Plugin catalog",
Detail: "Screenshots are displayed in the Plugin catalog. Please add at least one screenshot to your plugin.json.",
Name: "screenshots",
},
},
"sponsorshiplink": {
{
Severity: "recommendation",
Title: "You can include a sponsorship link if you want users to support your work",
Detail: "Consider to add a sponsorship link in your plugin.json file (Info.Links section: with Name: 'sponsor' or Name: 'sponsorship'), which will be shown on the plugin details page to allow users to support your work if they wish.",
Name: "sponsorshiplink",
},
},
"llmreview": {
{
Severity: "suspected",
Title: "LLM review skipped due to errors in metadatavalid",
Detail: "Fix the errors reported by metadatavalid before LLM review can run.",
Name: "llm-review-skipped",
},
},
"codediff": {
{
Severity: "suspected",
Title: "Code diff skipped due to errors in metadatavalid",
Detail: "Fix the errors reported by metadatavalid before code diff can run.",
Name: "code-diff-skipped",
},
},
},
},
},
{
name: "analyzer-flag-test",
file: "invalid2.zip",
extraArgs: "-analyzer=metadatavalid",
jsonReport: JsonReport{
Id: "invalid-panel",
Version: "1.0.0",
PluginValidator: map[string][]Issue{
"metadatavalid": {
{
Severity: "error",
Title: "plugin.json: dependencies: grafanaDependency is required",
Detail: "The plugin.json file is not following the schema. Please refer to the documentation for more information. https://grafana.com/docs/grafana/latest/developers/plugins/metadata/",
Name: "invalid-metadata",
},
},
},
},
},
{
name: "severity-flag-test",
file: "invalid2.zip",
extraArgs: "-analyzer=metadatavalid -analyzerSeverity=warning",
jsonReport: JsonReport{
Id: "invalid-panel",
Version: "1.0.0",
PluginValidator: map[string][]Issue{
"metadatavalid": {
{
Severity: "warning",
Title: "plugin.json: dependencies: grafanaDependency is required",
Detail: "The plugin.json file is not following the schema. Please refer to the documentation for more information. https://grafana.com/docs/grafana/latest/developers/plugins/metadata/",
Name: "invalid-metadata",
},
},
},
},
},
{
name: "severity-flag-no-changes-when-analyzer-not-set",
file: "grafana-clock-panel-2.1.5.any.zip",
extraArgs: "-analyzerSeverity=ok",
jsonReport: JsonReport{
Id: "grafana-clock-panel",
Version: "2.1.5",
PluginValidator: map[string][]Issue{
"grafanadependency": {
{
Severity: "warning",
Title: "Grafana dependency \">=8.0.0\" has no pre-release value",
Detail: "The value of grafanaDependency in plugin.json (\">=8.0.0\") is missing a pre-release value. This may make the plugin uninstallable in Grafana Cloud. Please add \"-0\" as a suffix of your grafanaDependency value (\">=8.0.0-0\")",
Name: "missing-cloud-pre-release",
},
},
"jargon": {
{
Severity: "warning",
Title: "README.md contains developer jargon: (yarn)",
Detail: "Move any developer and contributor documentation to a separate file and link to it from the README.md. For example, CONTRIBUTING.md, DEVELOPMENT.md, etc.",
Name: "developer-jargon",
},
},
"sponsorshiplink": {
{
Severity: "recommendation",
Title: "You can include a sponsorship link if you want users to support your work",
Detail: "Consider to add a sponsorship link in your plugin.json file (Info.Links section: with Name: 'sponsor' or Name: 'sponsorship'), which will be shown on the plugin details page to allow users to support your work if they wish.",
Name: "sponsorshiplink",
},
},
},
},
},
{
name: "output-to-file-test",
file: "grafana-clock-panel-2.1.5.any.zip",
outputToFile: "/tmp/test-output.json",
jsonReport: JsonReport{
Id: "grafana-clock-panel",
Version: "2.1.5",
PluginValidator: map[string][]Issue{
"grafanadependency": {
{
Severity: "warning",
Title: "Grafana dependency \">=8.0.0\" has no pre-release value",
Detail: "The value of grafanaDependency in plugin.json (\">=8.0.0\") is missing a pre-release value. This may make the plugin uninstallable in Grafana Cloud. Please add \"-0\" as a suffix of your grafanaDependency value (\">=8.0.0-0\")",
Name: "missing-cloud-pre-release",
},
},
"jargon": {
{
Severity: "warning",
Title: "README.md contains developer jargon: (yarn)",
Detail: "Move any developer and contributor documentation to a separate file and link to it from the README.md. For example, CONTRIBUTING.md, DEVELOPMENT.md, etc.",
Name: "developer-jargon",
},
},
"sponsorshiplink": {
{
Severity: "recommendation",
Title: "You can include a sponsorship link if you want users to support your work",
Detail: "Consider to add a sponsorship link in your plugin.json file (Info.Links section: with Name: 'sponsor' or Name: 'sponsorship'), which will be shown on the plugin details page to allow users to support your work if they wish.",
Name: "sponsorshiplink",
},
},
},
},
},
}
configFile := filepath.Join(basePath, "integration-tests.yaml")
t.Logf("Running integration tests. Total: %d\n", len(tcs))
for _, tc := range tcs {
currentFile := tc.file
tcName := tc.name
if tcName == "" {
tcName = currentFile
}
t.Run(tcName, func(t *testing.T) {
file := currentFile
// Allows the test case to run in parallel with other ones
t.Parallel()
t.Logf("Running %s", tcName)
extraArgs := ""
if tc.extraArgs != "" {
extraArgs = tc.extraArgs + " "
}
if tc.outputToFile != "" {
extraArgs += "-output-to-file " + tc.outputToFile + " "
}
command := fmt.Sprintf(
"%s -config %s %s",
binary,
configFile,
extraArgs+filepath.Join(basePath, file),
)
t.Logf("Running command: %s\n", command)
cmd := exec.Command("sh", "-c", command)
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
cmd.Env = append(os.Environ(), env...)
err := cmd.Run()
if err != nil && len(outb.String()) == 0 {
prettyprint.Print(errb.String())
assert.NoError(t, err)
}
// marshall the output into a JsonReport
var report JsonReport
err = json.Unmarshal(outb.Bytes(), &report)
assert.NoError(t, err)
// Check if output-to-file was used and verify the file was created
if tc.outputToFile != "" {
// Check that the file was created
fileContent, err := os.ReadFile(tc.outputToFile)
assert.NoError(t, err, "Output file should be created when -output-to-file is used")
// Check that the file contains valid JSON
var fileReport JsonReport
err = json.Unmarshal(fileContent, &fileReport)
assert.NoError(t, err, "Output file should contain valid JSON")
// Check that the file content matches stdout content
assert.Equal(t, report, fileReport, "File content should match stdout JSON")
t.Logf("Successfully verified output file: %s", tc.outputToFile)
}
changelog, err := diff.Diff(tc.jsonReport, report)
assert.NoError(t, err)
if len(changelog) > 0 {
t.Logf("Changes found: %d in %s\n", len(changelog), tcName)
prettyJson, _ := json.MarshalIndent(changelog, "", "\t")
fmt.Println(string(prettyJson))
}
assert.Len(t, changelog, 0)
})
}
}