forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolution.go
178 lines (153 loc) · 5.76 KB
/
resolution.go
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
/*
Copyright 2023 The Tekton 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 test
import (
"context"
"errors"
"fmt"
"strings"
"github.com/google/go-cmp/cmp"
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
resolution "github.com/tektoncd/pipeline/pkg/resolution/common"
"github.com/tektoncd/pipeline/test/diff"
)
var _ resolution.Requester = &Requester{}
var _ resolution.ResolvedResource = &ResolvedResource{}
// NewRequester creates a mock requester that resolves to the given
// resource or returns the given error on Submit().
func NewRequester(resource resolution.ResolvedResource, err error) *Requester {
return &Requester{
ResolvedResource: resource,
SubmitErr: err,
}
}
// NewResolvedResource creates a mock resolved resource that is
// populated with the given data and annotations or returns the given
// error from its Data() method.
func NewResolvedResource(data []byte, annotations map[string]string, source *pipelinev1.RefSource, dataErr error) *ResolvedResource {
return &ResolvedResource{
ResolvedData: data,
ResolvedAnnotations: annotations,
ResolvedRefSource: source,
DataErr: dataErr,
}
}
// Requester implements resolution.Requester and makes it easier
// to mock the outcome of a remote pipelineRef or taskRef resolution.
type Requester struct {
// The resolved resource object to return when a request is
// submitted.
ResolvedResource resolution.ResolvedResource
// An error to return when a request is submitted.
SubmitErr error
// Params that should match those on the request in order to return the resolved resource
Params []pipelinev1.Param
}
// Submit implements resolution.Requester, accepting the name of a
// resolver and a request for a specific remote file, and then returns
// whatever mock data was provided on initialization.
func (r *Requester) Submit(ctx context.Context, resolverName resolution.ResolverName, req resolution.Request) (resolution.ResolvedResource, error) {
if len(r.Params) == 0 {
return r.ResolvedResource, r.SubmitErr
}
reqParams := make(map[string]pipelinev1.ParamValue)
for _, p := range req.Params() {
reqParams[p.Name] = p.Value
}
var wrongParams []string
for _, p := range r.Params {
if reqValue, ok := reqParams[p.Name]; !ok {
wrongParams = append(wrongParams, fmt.Sprintf("expected %s param to be %#v, but was %#v", p.Name, p.Value, reqValue))
} else if d := cmp.Diff(p.Value, reqValue); d != "" {
wrongParams = append(wrongParams, fmt.Sprintf("%s param did not match: %s", p.Name, diff.PrintWantGot(d)))
}
}
if len(wrongParams) > 0 {
return nil, errors.New(strings.Join(wrongParams, "; "))
}
return r.ResolvedResource, r.SubmitErr
}
// ResolvedResource implements resolution.ResolvedResource and makes
// it easier to mock the resolved content of a fetched pipeline or task.
type ResolvedResource struct {
// The resolved bytes to return when resolution is complete.
ResolvedData []byte
// An error to return instead of the resolved bytes after
// resolution completes.
DataErr error
// Annotations to return when resolution is complete.
ResolvedAnnotations map[string]string
// ResolvedRefSource to return the source reference of the remote data
ResolvedRefSource *pipelinev1.RefSource
}
// Data implements resolution.ResolvedResource and returns the mock
// data and/or error given to it on initialization.
func (r *ResolvedResource) Data() ([]byte, error) {
return r.ResolvedData, r.DataErr
}
// Annotations implements resolution.ResolvedResource and returns
// the mock annotations given to it on initialization.
func (r *ResolvedResource) Annotations() map[string]string {
return r.ResolvedAnnotations
}
// RefSource is the source reference of the remote data that records where the remote
// file came from including the url, digest and the entrypoint.
func (r *ResolvedResource) RefSource() *pipelinev1.RefSource {
return r.ResolvedRefSource
}
// RawRequest stores the raw request data
type RawRequest struct {
// the request name
Name string
// the request namespace
Namespace string
// the params for the request
Params []pipelinev1.Param
}
// Request returns a Request interface based on the RawRequest.
func (r *RawRequest) Request() resolution.Request {
if r == nil {
r = &RawRequest{}
}
return &Request{
RawRequest: *r,
}
}
// Request implements resolution.Request and makes it easier to mock input for submit
// Using inline structs is to avoid conflicts between field names and method names.
type Request struct {
RawRequest
}
var _ resolution.Request = &Request{}
// NewRequest creates a mock request that is populated with the given name namespace and params
func NewRequest(name, namespace string, params []pipelinev1.Param) *Request {
return &Request{
RawRequest: RawRequest{
Name: name,
Namespace: namespace,
Params: params,
},
}
}
// Name implements resolution.Request and returns the mock name given to it on initialization.
func (r *Request) Name() string {
return r.RawRequest.Name
}
// Namespace implements resolution.Request and returns the mock namespace given to it on initialization.
func (r *Request) Namespace() string {
return r.RawRequest.Namespace
}
// Params implements resolution.Request and returns the mock params given to it on initialization.
func (r *Request) Params() pipelinev1.Params {
return r.RawRequest.Params
}