Skip to content

Commit fc5044e

Browse files
authored
refactor: update plugin for notation package (#199)
Replaced the old internal/plugin with new plugin package in notation package Signed-off-by: Junjie Gao <[email protected]>
1 parent d7c82a3 commit fc5044e

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

internal/mock/mocks.go

+40-22
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
_ "embed"
66

77
"github.com/notaryproject/notation-core-go/signature"
8-
"github.com/notaryproject/notation-go/internal/plugin"
9-
"github.com/notaryproject/notation-go/internal/plugin/manager"
8+
"github.com/notaryproject/notation-go/plugin"
9+
"github.com/notaryproject/notation-go/plugin/proto"
1010
"github.com/opencontainers/go-digest"
1111
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1212
)
@@ -101,40 +101,58 @@ func (t Repository) PushSignature(ctx context.Context, mediaType string, blob []
101101
return ocispec.Descriptor{}, ocispec.Descriptor{}, nil
102102
}
103103

104+
type PluginMock struct {
105+
Metadata proto.GetMetadataResponse
106+
ExecuteResponse interface{}
107+
ExecuteError error
108+
}
109+
110+
func (p *PluginMock) GetMetadata(ctx context.Context, req *proto.GetMetadataRequest) (*proto.GetMetadataResponse, error) {
111+
return &p.Metadata, nil
112+
}
113+
114+
func (p *PluginMock) VerifySignature(ctx context.Context, req *proto.VerifySignatureRequest) (*proto.VerifySignatureResponse, error) {
115+
if resp, ok := p.ExecuteResponse.(*proto.VerifySignatureResponse); ok {
116+
return resp, nil
117+
}
118+
return nil, p.ExecuteError
119+
}
120+
121+
func (p *PluginMock) DescribeKey(ctx context.Context, req *proto.DescribeKeyRequest) (*proto.DescribeKeyResponse, error) {
122+
panic("not implemented") // TODO: Implement
123+
}
124+
125+
func (p *PluginMock) GenerateSignature(ctx context.Context, req *proto.GenerateSignatureRequest) (*proto.GenerateSignatureResponse, error) {
126+
panic("not implemented") // TODO: Implement
127+
}
128+
129+
func (p *PluginMock) GenerateEnvelope(ctx context.Context, req *proto.GenerateEnvelopeRequest) (*proto.GenerateEnvelopeResponse, error) {
130+
panic("not implemented") // TODO: Implement
131+
}
132+
104133
type PluginManager struct {
105-
PluginCapabilities []plugin.Capability
134+
PluginCapabilities []proto.Capability
106135
GetPluginError error
107136
PluginRunnerLoadError error
108137
PluginRunnerExecuteResponse interface{}
109138
PluginRunnerExecuteError error
110139
}
111140

112-
type PluginRunner struct {
113-
Response interface{}
114-
Error error
115-
}
116-
117-
func (pr PluginRunner) Run(ctx context.Context, req plugin.Request) (interface{}, error) {
118-
return pr.Response, pr.Error
119-
}
120-
121-
func (pm PluginManager) Get(ctx context.Context, name string) (*manager.Plugin, error) {
122-
return &manager.Plugin{
123-
Metadata: plugin.Metadata{
141+
func (pm PluginManager) Get(ctx context.Context, name string) (plugin.Plugin, error) {
142+
return &PluginMock{
143+
Metadata: proto.GetMetadataResponse{
124144
Name: "plugin-name",
125145
Description: "for mocking in unit tests",
126146
Version: "1.0.0",
127147
URL: ".",
128148
SupportedContractVersions: []string{"1.0"},
129149
Capabilities: pm.PluginCapabilities,
130150
},
131-
Path: ".",
132-
Err: nil,
151+
ExecuteResponse: pm.PluginRunnerExecuteResponse,
152+
ExecuteError: pm.PluginRunnerExecuteError,
133153
}, pm.GetPluginError
134154
}
135-
func (pm PluginManager) Runner(name string) (plugin.Runner, error) {
136-
return PluginRunner{
137-
Response: pm.PluginRunnerExecuteResponse,
138-
Error: pm.PluginRunnerExecuteError,
139-
}, pm.PluginRunnerLoadError
155+
156+
func (pm PluginManager) List(ctx context.Context) ([]string, error) {
157+
panic("not implemented")
140158
}

notation_test.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77
"testing"
88

99
"github.com/notaryproject/notation-go/internal/mock"
10-
"github.com/notaryproject/notation-go/internal/plugin"
11-
"github.com/notaryproject/notation-go/internal/plugin/manager"
10+
"github.com/notaryproject/notation-go/plugin"
1211
"github.com/notaryproject/notation-go/verification/trustpolicy"
1312
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1413
)
@@ -99,7 +98,7 @@ func dummyPolicyStatement() (policyStatement trustpolicy.TrustPolicy) {
9998

10099
type dummyVerifier struct {
101100
TrustPolicyDoc *trustpolicy.Document
102-
PluginManager pluginManager
101+
PluginManager plugin.Manager
103102
FailVerify bool
104103
VerificationLevel trustpolicy.VerificationLevel
105104
}
@@ -118,9 +117,3 @@ func (v *dummyVerifier) Verify(ctx context.Context, signature []byte, opts Verif
118117
func (v *dummyVerifier) TrustPolicyDocument() (*trustpolicy.Document, error) {
119118
return v.TrustPolicyDoc, nil
120119
}
121-
122-
// pluginManager is for mocking in unit tests
123-
type pluginManager interface {
124-
Get(ctx context.Context, name string) (*manager.Plugin, error)
125-
Runner(name string) (plugin.Runner, error)
126-
}

0 commit comments

Comments
 (0)