Debian Codegraph @

plugin/integration_test.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package plugin
15
16 import (
17 "context"
18 "io"
19 "os"
20 "os/exec"
21 "path/filepath"
22 "reflect"
23 "testing"
24
25 "github.com/notaryproject/notation-go/dir"
26 "github.com/notaryproject/notation-plugin-framework-go/plugin"
27 )
28
29 var exampleMetadata = plugin.GetMetadataResponse{
30 Name: "foo",
31 Description: "friendly",
32 Version: "1",
33 URL: "example.com",
34 SupportedContractVersions: []string{"1.0"},
35 Capabilities: []plugin.Capability{"cap"}}
36
37 func preparePlugin(t *testing.T) string {
38 root := t.TempDir()
39 src, err := os.Open("./testdata/main.go")
40 if err != nil {
41 t.Fatal(err)
42 }
43 defer src.Close()
44
45 dst, err := os.Create(filepath.Join(root, "main.go"))
46 if err != nil {
47 t.Fatal(err)
48 }
49 defer dst.Close()
50 _, err = io.Copy(dst, src)
51 if err != nil {
52 t.Fatal(err)
53 }
54 err = os.WriteFile(filepath.Join(root, "go.mod"), []byte("module main"), 0600)
55 if err != nil {
56 t.Fatal(err)
57 }
58 err = os.Mkdir(filepath.Join(root, "foo"), 0700)
59 if err != nil {
60 t.Fatal(err)
61 }
62 out := filepath.Join(root, "foo", binName("foo"))
63 cmd := exec.Command("go", "build", "-o", out)
64 cmd.Dir = root
65 err = cmd.Run()
66 if err != nil {
67 t.Fatal(err)
68 }
69 return root
70 }
71
72 func TestIntegration(t *testing.T) {
73 if _, err := exec.LookPath("go"); err != nil {
74 t.Skip()
75 }
76 root := preparePlugin(t)
77 fsys := dir.NewSysFS(root)
78 mgr := NewCLIManager(fsys)
79
80 // check list
81 plugins, err := mgr.List(context.Background())
82 if err != nil {
83 t.Fatal(err)
84 }
85 if len(plugins) != 1 {
86 t.Fatalf("Manager.List() len got %d, want 1", len(plugins))
87 }
88
89 // validate and create
90 pl, err := mgr.Get(context.Background(), "foo")
91 if err != nil {
92 t.Fatal(err)
93 }
94 metadata, err := pl.GetMetadata(context.Background(), &plugin.GetMetadataRequest{})
95 if err != nil {
96 t.Fatal(err)
97 }
98
99 if !reflect.DeepEqual(&exampleMetadata, metadata) {
100 t.Fatalf("Metadata error. want: %+v, got: %+v", exampleMetadata, metadata)
101 }
102 }