File: manager_test.go

package info (click to toggle)
golang-github-notaryproject-notation-go 1.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,652 kB
  • sloc: makefile: 21
file content (682 lines) | stat: -rw-r--r-- 26,582 bytes parent folder | download | duplicates (2)
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright The Notary Project Authors.
// 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 (
	"context"
	"encoding/json"
	"io/fs"
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"testing"
	"testing/fstest"

	"github.com/notaryproject/notation-go/internal/mock/mockfs"
	"github.com/notaryproject/notation-go/plugin/proto"
)

type testCommander struct {
	stdout []byte
	stderr []byte
	err    error
}

func (t testCommander) Output(ctx context.Context, path string, command proto.Command, req []byte) ([]byte, []byte, error) {
	return t.stdout, t.stderr, t.err
}

type testInstallCommander struct {
	existedPluginFilePath string
	existedPluginStdout   []byte
	existedPluginStderr   []byte
	existedPluginErr      error
	newPluginFilePath     string
	newPluginStdout       []byte
	newPluginStderr       []byte
	newPluginErr          error
	err                   error
}

func (t testInstallCommander) Output(ctx context.Context, path string, command proto.Command, req []byte) ([]byte, []byte, error) {
	if path == t.existedPluginFilePath {
		return t.existedPluginStdout, t.existedPluginStderr, t.existedPluginErr
	}
	if path == t.newPluginFilePath {
		return t.newPluginStdout, t.newPluginStderr, t.newPluginErr
	}
	return nil, nil, t.err
}

var validMetadata = proto.GetMetadataResponse{
	Name: "foo", Description: "friendly", Version: "1.0.0", URL: "example.com",
	SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
}

var validMetadataHigherVersion = proto.GetMetadataResponse{
	Name: "foo", Description: "friendly", Version: "1.1.0", URL: "example.com",
	SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
}

var validMetadataLowerVersion = proto.GetMetadataResponse{
	Name: "foo", Description: "friendly", Version: "0.1.0", URL: "example.com",
	SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
}

var validMetadataBar = proto.GetMetadataResponse{
	Name: "bar", Description: "friendly", Version: "1.0.0", URL: "example.com",
	SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
}

var validMetadataBarExample = proto.GetMetadataResponse{
	Name: "bar.example.plugin", Description: "friendly", Version: "1.0.0", URL: "example.com",
	SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
}

var invalidMetadataName = proto.GetMetadataResponse{
	Name: "foobar", Description: "friendly", Version: "1", URL: "example.com",
	SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
}

var invalidContractVersionMetadata = proto.GetMetadataResponse{
	Name: "foo", Description: "friendly", Version: "1", URL: "example.com",
	SupportedContractVersions: []string{"110.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
}

func TestManager_Get(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("skipping test on Windows")
	}
	executor = testCommander{stdout: metadataJSON(validMetadata)}
	mgr := NewCLIManager(mockfs.NewSysFSWithRootMock(fstest.MapFS{}, "./testdata/plugins"))
	_, err := mgr.Get(context.Background(), "foo")
	if err != nil {
		t.Errorf("Manager.Get() err %v, want nil", err)
	}
}

func TestManager_List(t *testing.T) {
	t.Run("empty fsys", func(t *testing.T) {
		mgr := NewCLIManager(mockfs.NewSysFSMock(fstest.MapFS{}))
		plugins, err := mgr.List(context.Background())
		if err != nil {
			t.Fatalf("should no error. got err = %v", err)
		}
		if len(plugins) != 0 {
			t.Fatalf("should no plugins. got plugins = %v", plugins)
		}
	})

	t.Run("fsys with plugins", func(t *testing.T) {
		mgr := NewCLIManager(mockfs.NewSysFSMock(fstest.MapFS{
			"foo": &fstest.MapFile{Mode: fs.ModeDir},
			"baz": &fstest.MapFile{Mode: fs.ModeDir},
		}))
		plugins, err := mgr.List(context.Background())
		if err != nil {
			t.Fatalf("should no error. got err = %v", err)
		}
		want := []string{"foo", "bar"}
		if reflect.DeepEqual(want, plugins) {
			t.Fatalf("got plugins = %v, want %v", plugins, want)
		}
	})
}

func TestManager_Install(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("skipping test on Windows")
	}
	existedPluginFilePath := "testdata/plugins/foo/notation-foo"
	newPluginFilePath := "testdata/foo/notation-foo"
	newPluginDir := filepath.Dir(newPluginFilePath)
	if err := os.MkdirAll(newPluginDir, 0777); err != nil {
		t.Fatalf("failed to create %s: %v", newPluginDir, err)
	}
	defer os.RemoveAll(newPluginDir)
	if err := createFileAndChmod(newPluginFilePath, 0700); err != nil {
		t.Fatal(err)
	}
	mgr := NewCLIManager(mockfs.NewSysFSWithRootMock(fstest.MapFS{}, "testdata/plugins"))

	t.Run("success install with higher version", func(t *testing.T) {
		executor = testInstallCommander{
			existedPluginFilePath: existedPluginFilePath,
			newPluginFilePath:     newPluginFilePath,
			existedPluginStdout:   metadataJSON(validMetadata),
			newPluginStdout:       metadataJSON(validMetadataHigherVersion),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		existingPluginMetadata, newPluginMetadata, err := mgr.Install(context.Background(), installOpts)
		if err != nil {
			t.Fatalf("expecting error to be nil, but got %v", err)
		}
		if existingPluginMetadata.Version != validMetadata.Version {
			t.Fatalf("existing plugin version mismatch, existing plugin version: %s, but got: %s", validMetadata.Version, existingPluginMetadata.Version)
		}
		if newPluginMetadata.Version != validMetadataHigherVersion.Version {
			t.Fatalf("new plugin version mismatch, new plugin version: %s, but got: %s", validMetadataHigherVersion.Version, newPluginMetadata.Version)
		}
	})

	t.Run("success install with lower version and overwrite", func(t *testing.T) {
		executor = testInstallCommander{
			existedPluginFilePath: existedPluginFilePath,
			newPluginFilePath:     newPluginFilePath,
			existedPluginStdout:   metadataJSON(validMetadata),
			newPluginStdout:       metadataJSON(validMetadataLowerVersion),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
			Overwrite:  true,
		}
		if _, _, err := mgr.Install(context.Background(), installOpts); err != nil {
			t.Fatalf("expecting error to be nil, but got %v", err)
		}
	})

	t.Run("success install without existing plugin", func(t *testing.T) {
		newPluginFilePath := "testdata/bar/notation-bar"
		newPluginDir := filepath.Dir(newPluginFilePath)
		if err := os.MkdirAll(newPluginDir, 0777); err != nil {
			t.Fatalf("failed to create %s: %v", newPluginDir, err)
		}
		defer os.RemoveAll(newPluginDir)
		if err := createFileAndChmod(newPluginFilePath, 0700); err != nil {
			t.Fatal(err)
		}
		executor = testInstallCommander{
			newPluginFilePath: newPluginFilePath,
			newPluginStdout:   metadataJSON(validMetadataBar),
		}
		defer mgr.Uninstall(context.Background(), "bar")
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		existingPluginMetadata, newPluginMetadata, err := mgr.Install(context.Background(), installOpts)
		if err != nil {
			t.Fatalf("expecting error to be nil, but got %v", err)
		}
		if existingPluginMetadata != nil {
			t.Fatalf("expecting existingPluginMetadata to be nil, but got %v", existingPluginMetadata)
		}
		if newPluginMetadata.Version != validMetadataBar.Version {
			t.Fatalf("new plugin version mismatch, new plugin version: %s, but got: %s", validMetadataBar.Version, newPluginMetadata.Version)
		}
	})

	t.Run("success install with file extension", func(t *testing.T) {
		newPluginFilePath := "testdata/bar/notation-bar.example.plugin"
		newPluginDir := filepath.Dir(newPluginFilePath)
		if err := os.MkdirAll(newPluginDir, 0777); err != nil {
			t.Fatalf("failed to create %s: %v", newPluginDir, err)
		}
		defer os.RemoveAll(newPluginDir)
		if err := createFileAndChmod(newPluginFilePath, 0700); err != nil {
			t.Fatal(err)
		}
		executor = testInstallCommander{
			newPluginFilePath: newPluginFilePath,
			newPluginStdout:   metadataJSON(validMetadataBarExample),
		}
		defer mgr.Uninstall(context.Background(), "bar.example.plugin")
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		existingPluginMetadata, newPluginMetadata, err := mgr.Install(context.Background(), installOpts)
		if err != nil {
			t.Fatalf("expecting error to be nil, but got %v", err)
		}
		if existingPluginMetadata != nil {
			t.Fatalf("expecting existingPluginMetadata to be nil, but got %v", existingPluginMetadata)
		}
		if newPluginMetadata.Version != validMetadataBar.Version {
			t.Fatalf("new plugin version mismatch, new plugin version: %s, but got: %s", validMetadataBar.Version, newPluginMetadata.Version)
		}
	})

	t.Run("fail to install due to equal version", func(t *testing.T) {
		executor = testInstallCommander{
			existedPluginFilePath: existedPluginFilePath,
			newPluginFilePath:     newPluginFilePath,
			existedPluginStdout:   metadataJSON(validMetadata),
			newPluginStdout:       metadataJSON(validMetadata),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		expectedErrorMsg := "plugin foo with version 1.0.0 already exists"
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
		}
	})

	t.Run("fail to install due to lower version", func(t *testing.T) {
		executor = testInstallCommander{
			existedPluginFilePath: existedPluginFilePath,
			newPluginFilePath:     newPluginFilePath,
			existedPluginStdout:   metadataJSON(validMetadata),
			newPluginStdout:       metadataJSON(validMetadataLowerVersion),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		expectedErrorMsg := "failed to install plugin foo. The installing plugin version 0.1.0 is lower than the existing plugin version 1.0.0"
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
		}
	})

	t.Run("fail to install due to wrong plugin executable file name format", func(t *testing.T) {
		newPluginFilePath := "testdata/bar/bar"
		newPluginDir := filepath.Dir(newPluginFilePath)
		if err := os.MkdirAll(newPluginDir, 0777); err != nil {
			t.Fatalf("failed to create %s: %v", newPluginDir, err)
		}
		defer os.RemoveAll(newPluginDir)
		if err := createFileAndChmod(newPluginFilePath, 0700); err != nil {
			t.Fatal(err)
		}
		executor = testInstallCommander{
			newPluginFilePath: newPluginFilePath,
			newPluginStdout:   metadataJSON(validMetadataBar),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		expectedErrorMsg := "failed to read plugin name from input file bar: invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got bar"
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
		}
	})

	t.Run("fail to install due to plugin executable file name missing plugin name", func(t *testing.T) {
		newPluginFilePath := "testdata/bar/notation-"
		newPluginDir := filepath.Dir(newPluginFilePath)
		if err := os.MkdirAll(newPluginDir, 0777); err != nil {
			t.Fatalf("failed to create %s: %v", newPluginDir, err)
		}
		defer os.RemoveAll(newPluginDir)
		if err := createFileAndChmod(newPluginFilePath, 0700); err != nil {
			t.Fatal(err)
		}
		executor = testInstallCommander{
			newPluginFilePath: newPluginFilePath,
			newPluginStdout:   metadataJSON(validMetadataBar),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		expectedErrorMsg := "failed to read plugin name from input file notation-: invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got notation-"
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
		}
	})

	t.Run("fail to install due to wrong plugin file permission", func(t *testing.T) {
		newPluginFilePath := "testdata/bar/notation-bar"
		newPluginDir := filepath.Dir(newPluginFilePath)
		if err := os.MkdirAll(newPluginDir, 0777); err != nil {
			t.Fatalf("failed to create %s: %v", newPluginDir, err)
		}
		defer os.RemoveAll(newPluginDir)
		if err := createFileAndChmod(newPluginFilePath, 0600); err != nil {
			t.Fatal(err)
		}
		executor = testInstallCommander{
			newPluginFilePath: newPluginFilePath,
			newPluginStdout:   metadataJSON(validMetadataBar),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		expectedErrorMsg := "input file notation-bar is not executable"
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
		}
	})

	t.Run("fail to install due to new plugin executable file does not exist", func(t *testing.T) {
		newPluginFilePath := "testdata/bar/notation-bar"
		executor = testInstallCommander{
			newPluginFilePath: newPluginFilePath,
			newPluginStdout:   metadataJSON(validMetadataBar),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		expectedErrorMsg := "failed to read plugin from input directory: stat testdata/bar/notation-bar: no such file or directory"
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
		}
	})

	t.Run("fail to install due to invalid new plugin metadata", func(t *testing.T) {
		newPluginFilePath := "testdata/bar/notation-bar"
		newPluginDir := filepath.Dir(newPluginFilePath)
		if err := os.MkdirAll(newPluginDir, 0777); err != nil {
			t.Fatalf("failed to create %s: %v", newPluginDir, err)
		}
		defer os.RemoveAll(newPluginDir)
		if err := createFileAndChmod(newPluginFilePath, 0700); err != nil {
			t.Fatal(err)
		}
		executor = testInstallCommander{
			newPluginFilePath: newPluginFilePath,
			newPluginStdout:   metadataJSON(invalidMetadataName),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		expectedErrorMsg := "failed to get metadata of new plugin: plugin executable file name must be \"notation-foobar\" instead of \"notation-bar\""
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
		}
	})

	t.Run("fail to install due to invalid existing plugin metadata", func(t *testing.T) {
		executor = testInstallCommander{
			existedPluginFilePath: existedPluginFilePath,
			newPluginFilePath:     newPluginFilePath,
			existedPluginStdout:   metadataJSON(validMetadataBar),
			newPluginStdout:       metadataJSON(validMetadata),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
		}
		expectedErrorMsg := "failed to get metadata of existing plugin: plugin executable file name must be \"notation-bar\" instead of \"notation-foo\""
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
		}
	})

	t.Run("success to install with overwrite and invalid existing plugin metadata", func(t *testing.T) {
		executor = testInstallCommander{
			existedPluginFilePath: existedPluginFilePath,
			newPluginFilePath:     newPluginFilePath,
			existedPluginStdout:   metadataJSON(validMetadataBar),
			newPluginStdout:       metadataJSON(validMetadata),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginFilePath,
			Overwrite:  true,
		}
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err != nil {
			t.Fatalf("expecting error to be nil, but got %v", err)
		}
	})

	t.Run("success to install from plugin dir", func(t *testing.T) {
		existedPluginFilePath := "testdata/plugins/foo/notation-foo"
		newPluginFilePath := "testdata/foo/notation-foo"
		newPluginLibPath := "testdata/foo/notation-libfoo"
		newPluginDir := filepath.Dir(newPluginFilePath)
		if err := os.MkdirAll(newPluginDir, 0777); err != nil {
			t.Fatalf("failed to create %s: %v", newPluginDir, err)
		}
		defer os.RemoveAll(newPluginDir)
		if err := createFileAndChmod(newPluginFilePath, 0700); err != nil {
			t.Fatal(err)
		}
		if err := createFileAndChmod(newPluginLibPath, 0600); err != nil {
			t.Fatal(err)
		}
		executor = testInstallCommander{
			existedPluginFilePath: existedPluginFilePath,
			newPluginFilePath:     newPluginFilePath,
			existedPluginStdout:   metadataJSON(validMetadata),
			newPluginStdout:       metadataJSON(validMetadataHigherVersion),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginDir,
		}
		existingPluginMetadata, newPluginMetadata, err := mgr.Install(context.Background(), installOpts)
		if err != nil {
			t.Fatalf("expecting nil error, but got %v", err)
		}
		if existingPluginMetadata.Version != "1.0.0" {
			t.Fatalf("expecting existing plugin metadata to be 1.0.0, but got %s", existingPluginMetadata.Version)
		}
		if newPluginMetadata.Version != "1.1.0" {
			t.Fatalf("expecting new plugin metadata to be 1.1.0, but got %s", newPluginMetadata.Version)
		}
	})

	t.Run("success to install from plugin dir with no executable file and one valid candidate file", func(t *testing.T) {
		existedPluginFilePath := "testdata/plugins/foo/notation-foo"
		newPluginFilePath := "testdata/foo/notation-foo"
		newPluginLibPath := "testdata/foo/libfoo"
		newPluginDir := filepath.Dir(newPluginFilePath)
		if err := os.MkdirAll(newPluginDir, 0777); err != nil {
			t.Fatalf("failed to create %s: %v", newPluginDir, err)
		}
		defer os.RemoveAll(newPluginDir)
		if err := createFileAndChmod(newPluginFilePath, 0600); err != nil {
			t.Fatal(err)
		}
		if err := createFileAndChmod(newPluginLibPath, 0600); err != nil {
			t.Fatal(err)
		}
		executor = testInstallCommander{
			existedPluginFilePath: existedPluginFilePath,
			newPluginFilePath:     newPluginFilePath,
			existedPluginStdout:   metadataJSON(validMetadata),
			newPluginStdout:       metadataJSON(validMetadataHigherVersion),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginDir,
		}
		existingPluginMetadata, newPluginMetadata, err := mgr.Install(context.Background(), installOpts)
		if err != nil {
			t.Fatalf("expecting nil error, but got %v", err)
		}
		if existingPluginMetadata.Version != "1.0.0" {
			t.Fatalf("expecting existing plugin metadata to be 1.0.0, but got %s", existingPluginMetadata.Version)
		}
		if newPluginMetadata.Version != "1.1.0" {
			t.Fatalf("expecting new plugin metadata to be 1.1.0, but got %s", newPluginMetadata.Version)
		}
	})

	t.Run("fail to install from plugin dir due to more than one candidate plugin executable files", func(t *testing.T) {
		existedPluginFilePath := "testdata/plugins/foo/notation-foo"
		newPluginFilePath := "testdata/foo/notation-foo1"
		newPluginFilePath2 := "testdata/foo/notation-foo2"
		newPluginDir := filepath.Dir(newPluginFilePath)
		if err := os.MkdirAll(newPluginDir, 0777); err != nil {
			t.Fatalf("failed to create %s: %v", newPluginDir, err)
		}
		defer os.RemoveAll(newPluginDir)
		if err := createFileAndChmod(newPluginFilePath, 0600); err != nil {
			t.Fatal(err)
		}
		if err := createFileAndChmod(newPluginFilePath2, 0600); err != nil {
			t.Fatal(err)
		}
		executor = testInstallCommander{
			existedPluginFilePath: existedPluginFilePath,
			newPluginFilePath:     newPluginFilePath,
			existedPluginStdout:   metadataJSON(validMetadata),
			newPluginStdout:       metadataJSON(validMetadataHigherVersion),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginDir,
		}
		expectedErrorMsg := "failed to read plugin from input directory: no plugin executable file was found"
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
		}
	})

	t.Run("fail to install from plugin dir due to more than one plugin executable files", func(t *testing.T) {
		existedPluginFilePath := "testdata/plugins/foo/notation-foo"
		newPluginFilePath := "testdata/foo/notation-foo1"
		newPluginFilePath2 := "testdata/foo/notation-foo2"
		newPluginDir := filepath.Dir(newPluginFilePath)
		if err := os.MkdirAll(newPluginDir, 0777); err != nil {
			t.Fatalf("failed to create %s: %v", newPluginDir, err)
		}
		defer os.RemoveAll(newPluginDir)
		if err := createFileAndChmod(newPluginFilePath, 0700); err != nil {
			t.Fatal(err)
		}
		if err := createFileAndChmod(newPluginFilePath2, 0700); err != nil {
			t.Fatal(err)
		}
		executor = testInstallCommander{
			existedPluginFilePath: existedPluginFilePath,
			newPluginFilePath:     newPluginFilePath,
			existedPluginStdout:   metadataJSON(validMetadata),
			newPluginStdout:       metadataJSON(validMetadataHigherVersion),
		}
		installOpts := CLIInstallOptions{
			PluginPath: newPluginDir,
		}
		expectedErrorMsg := "failed to read plugin from input directory: found more than one plugin executable files"
		_, _, err := mgr.Install(context.Background(), installOpts)
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
		}
	})
}

func TestManager_Uninstall(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("skipping test on Windows")
	}
	executor = testCommander{stdout: metadataJSON(validMetadata)}
	mgr := NewCLIManager(mockfs.NewSysFSWithRootMock(fstest.MapFS{}, "./testdata/plugins"))
	if err := os.MkdirAll("./testdata/plugins/toUninstall", 0777); err != nil {
		t.Fatalf("failed to create toUninstall dir: %v", err)
	}
	defer os.RemoveAll("./testdata/plugins/toUninstall")
	pluginFile, err := os.Create("./testdata/plugins/toUninstall/toUninstall")
	if err != nil {
		t.Fatalf("failed to create toUninstall file: %v", err)
	}
	if err := pluginFile.Close(); err != nil {
		t.Fatalf("failed to close toUninstall file: %v", err)
	}
	// test uninstall valid plugin
	if err := mgr.Uninstall(context.Background(), "toUninstall"); err != nil {
		t.Fatalf("Manager.Uninstall() err %v, want nil", err)
	}
	// test uninstall non-exist plugin
	expectedErrorMsg := "stat testdata/plugins/non-exist: no such file or directory"
	if err := mgr.Uninstall(context.Background(), "non-exist"); err == nil || err.Error() != expectedErrorMsg {
		t.Fatalf("Manager.Uninstall() err %v, want %s", err, expectedErrorMsg)
	}
}

func TestParsePluginName(t *testing.T) {
	if runtime.GOOS == "windows" {
		pluginName, err := parsePluginName("notation-my-plugin.exe")
		if err != nil {
			t.Fatalf("expected nil err, but got %v", err)
		}
		if pluginName != "my-plugin" {
			t.Fatalf("expected plugin name my-plugin, but got %s", pluginName)
		}

		expectedErrorMsg := "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got notation-com.plugin"
		_, err = parsePluginName("notation-com.plugin")
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
		}

		expectedErrorMsg = "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got my-plugin.exe"
		_, err = parsePluginName("my-plugin.exe")
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
		}

		expectedErrorMsg = "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got notation-.exe"
		_, err = parsePluginName("notation-.exe")
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
		}

		expectedErrorMsg = "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got my-plugin"
		_, err = parsePluginName("my-plugin")
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
		}
	} else {
		pluginName, err := parsePluginName("notation-my-plugin")
		if err != nil {
			t.Fatalf("expected nil err, but got %v", err)
		}
		if pluginName != "my-plugin" {
			t.Fatalf("expected plugin name my-plugin, but got %s", pluginName)
		}

		pluginName, err = parsePluginName("notation-com.example.plugin")
		if err != nil {
			t.Fatalf("expected nil err, but got %v", err)
		}
		if pluginName != "com.example.plugin" {
			t.Fatalf("expected plugin name com.example.plugin, but got %s", pluginName)
		}

		expectedErrorMsg := "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got myPlugin"
		_, err = parsePluginName("myPlugin")
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
		}

		expectedErrorMsg = "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got my-plugin"
		_, err = parsePluginName("my-plugin")
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
		}

		expectedErrorMsg = "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got notation-"
		_, err = parsePluginName("notation-")
		if err == nil || err.Error() != expectedErrorMsg {
			t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
		}
	}
}

func metadataJSON(m proto.GetMetadataResponse) []byte {
	d, err := json.Marshal(m)
	if err != nil {
		panic(err)
	}
	return d
}

func createFileAndChmod(path string, mode fs.FileMode) error {
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	if err := f.Chmod(mode); err != nil {
		return err
	}
	return f.Close()
}