@@ -17,7 +17,6 @@ limitations under the License.
17
17
package hub
18
18
19
19
import (
20
- "bytes"
21
20
"context"
22
21
"errors"
23
22
"fmt"
@@ -31,10 +30,7 @@ import (
31
30
resolutioncommon "github.com/tektoncd/pipeline/pkg/resolution/common"
32
31
resolutionframework "github.com/tektoncd/pipeline/pkg/resolution/resolver/framework"
33
32
hubresolver "github.com/tektoncd/pipeline/pkg/resolution/resolver/hub"
34
- )
35
-
36
- const (
37
- defaultHttpTimeoutValue = "1m"
33
+ "github.com/tektoncd/pipeline/test/diff"
38
34
)
39
35
40
36
func TestGetSelector (t * testing.T ) {
@@ -97,7 +93,7 @@ func TestValidate(t *testing.T) {
97
93
req := v1beta1.ResolutionRequestSpec {
98
94
Params : toParams (params ),
99
95
}
100
- err := resolver .Validate (contextWithConfig (defaultHttpTimeoutValue ), & req )
96
+ err := resolver .Validate (contextWithConfig (), & req )
101
97
if tc .expectedErr != nil {
102
98
checkExpectedErr (t , tc .expectedErr , err )
103
99
} else if err != nil {
@@ -119,7 +115,7 @@ func TestValidateMissing(t *testing.T) {
119
115
req := v1beta1.ResolutionRequestSpec {
120
116
Params : toParams (paramsMissingName ),
121
117
}
122
- err = resolver .Validate (contextWithConfig (defaultHttpTimeoutValue ), & req )
118
+ err = resolver .Validate (contextWithConfig (), & req )
123
119
if err == nil {
124
120
t .Fatalf ("expected missing name err" )
125
121
}
@@ -131,7 +127,7 @@ func TestValidateMissing(t *testing.T) {
131
127
req = v1beta1.ResolutionRequestSpec {
132
128
Params : toParams (paramsMissingVersion ),
133
129
}
134
- err = resolver .Validate (contextWithConfig (defaultHttpTimeoutValue ), & req )
130
+ err = resolver .Validate (contextWithConfig (), & req )
135
131
136
132
if err == nil {
137
133
t .Fatalf ("expected missing version err" )
@@ -175,7 +171,7 @@ func TestValidateConflictingKindName(t *testing.T) {
175
171
req := v1beta1.ResolutionRequestSpec {
176
172
Params : toParams (params ),
177
173
}
178
- err := resolver .Validate (contextWithConfig (defaultHttpTimeoutValue ), & req )
174
+ err := resolver .Validate (contextWithConfig (), & req )
179
175
if err == nil {
180
176
t .Fatalf ("expected err due to conflicting param" )
181
177
}
@@ -185,113 +181,106 @@ func TestValidateConflictingKindName(t *testing.T) {
185
181
186
182
func TestResolve (t * testing.T ) {
187
183
testCases := []struct {
188
- name string
189
- expectedError error
190
- input string
191
- paramsSet bool
192
- useCache bool
193
- expectedStatus int
184
+ name string
185
+ kind string
186
+ imageName string
187
+ version string
188
+ catalog string
189
+ hubType string
190
+ input string
191
+ expectedRes []byte
192
+ expectedErr error
194
193
}{
195
194
{
196
- name : "valid params set with cache" ,
197
- expectedError : nil ,
198
- input : "git-clone" ,
199
- paramsSet : true ,
200
- useCache : true ,
201
- expectedStatus : http .StatusOK ,
195
+ name : "valid response from Tekton Hub" ,
196
+ kind : "task" ,
197
+ imageName : "foo" ,
198
+ version : "baz" ,
199
+ catalog : "Tekton" ,
200
+ hubType : TektonHubType ,
201
+ input : `{"data":{"yaml":"some content"}}` ,
202
+ expectedRes : []byte ("some content" ),
203
+ },
204
+ {
205
+ name : "valid response from Artifact Hub" ,
206
+ kind : "task" ,
207
+ imageName : "foo" ,
208
+ version : "baz" ,
209
+ catalog : "Tekton" ,
210
+ hubType : ArtifactHubType ,
211
+ input : `{"data":{"manifestRaw":"some content"}}` ,
212
+ expectedRes : []byte ("some content" ),
202
213
},
203
214
{
204
- name : "valid params set without cache" ,
205
- expectedError : nil ,
206
- input : "git-clone" ,
207
- paramsSet : true ,
208
- useCache : false ,
209
- expectedStatus : http .StatusOK ,
215
+ name : "not-found response from hub" ,
216
+ kind : "task" ,
217
+ imageName : "foo" ,
218
+ version : "baz" ,
219
+ catalog : "Tekton" ,
220
+ hubType : TektonHubType ,
221
+ input : `{"name":"not-found","id":"aaaaaaaa","message":"resource not found","temporary":false,"timeout":false,"fault":false}` ,
222
+ expectedRes : []byte ("" ),
210
223
},
211
224
{
212
- name : "missing required params" ,
213
- expectedError : fmt .Errorf ("missing required param: name" ),
214
- input : "" ,
215
- paramsSet : false ,
216
- useCache : false ,
217
- expectedStatus : http .StatusBadRequest ,
225
+ name : "response with bad formatting error" ,
226
+ kind : "task" ,
227
+ imageName : "foo" ,
228
+ version : "baz" ,
229
+ catalog : "Tekton" ,
230
+ hubType : TektonHubType ,
231
+ input : `value` ,
232
+ expectedErr : errors .New ("fail to fetch Tekton Hub resource: error unmarshalling json response: invalid character 'v' looking for beginning of value" ),
218
233
},
219
234
{
220
- name : "not found error" ,
221
- expectedError : fmt .Errorf ("error accessing resource from \" not-found\" : 404 Not Found" ),
222
- input : "not-found" ,
223
- paramsSet : true ,
224
- useCache : false ,
225
- expectedStatus : http .StatusNotFound ,
235
+ name : "response with empty body error from Tekton Hub" ,
236
+ kind : "task" ,
237
+ imageName : "foo" ,
238
+ version : "baz" ,
239
+ catalog : "Tekton" ,
240
+ hubType : TektonHubType ,
241
+ expectedErr : errors .New ("fail to fetch Tekton Hub resource: error unmarshalling json response: unexpected end of JSON input" ),
242
+ },
243
+ {
244
+ name : "response with empty body error from Artifact Hub" ,
245
+ kind : "task" ,
246
+ imageName : "foo" ,
247
+ version : "baz" ,
248
+ catalog : "Tekton" ,
249
+ hubType : ArtifactHubType ,
250
+ expectedErr : errors .New ("fail to fetch Artifact Hub resource: error unmarshalling json response: unexpected end of JSON input" ),
226
251
},
227
252
}
228
253
229
254
for _ , tc := range testCases {
230
255
t .Run (tc .name , func (t * testing.T ) {
231
- server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
232
- w .WriteHeader (tc .expectedStatus )
233
- if tc .expectedStatus == http .StatusOK {
234
- w .Write ([]byte ("test content" ))
235
- }
256
+ svr := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
257
+ fmt .Fprint (w , tc .input )
236
258
}))
237
- defer server .Close ()
238
259
239
- resolver := & Resolver {}
240
- params := []pipelinev1.Param {}
241
- if tc .paramsSet {
242
- params = append (params , pipelinev1.Param {
243
- Name : "name" ,
244
- Value : * pipelinev1 .NewStructuredValues (tc .input ),
245
- })
246
- if tc .useCache {
247
- params = append (params , pipelinev1.Param {
248
- Name : "cache" ,
249
- Value : * pipelinev1 .NewStructuredValues ("true" ),
250
- })
251
- }
252
- }
253
-
254
- req := v1beta1.ResolutionRequestSpec {
255
- Params : params ,
260
+ resolver := & Resolver {
261
+ TektonHubURL : svr .URL ,
262
+ ArtifactHubURL : svr .URL ,
256
263
}
257
264
258
- ctx := contextWithConfig (defaultHttpTimeoutValue )
259
- resolved , err := resolver .Resolve (ctx , & req )
260
-
261
- if tc .expectedError != nil {
262
- if err == nil {
263
- t .Errorf ("expected error %v but got nil" , tc .expectedError )
264
- } else if err .Error () != tc .expectedError .Error () {
265
- t .Errorf ("expected error %v but got %v" , tc .expectedError , err )
266
- }
267
- return
268
- }
269
-
270
- if err != nil {
271
- t .Errorf ("unexpected error: %v" , err )
272
- return
265
+ params := map [string ]string {
266
+ hubresolver .ParamKind : tc .kind ,
267
+ hubresolver .ParamName : tc .imageName ,
268
+ hubresolver .ParamVersion : tc .version ,
269
+ hubresolver .ParamCatalog : tc .catalog ,
270
+ hubresolver .ParamType : tc .hubType ,
273
271
}
274
-
275
- if resolved == nil {
276
- t .Error ("expected resolved resource but got nil" )
277
- return
272
+ req := v1beta1.ResolutionRequestSpec {
273
+ Params : toParams (params ),
278
274
}
279
-
280
- // Test cache functionality
281
- if tc .useCache {
282
- // Try resolving again with the same parameters
283
- cached , err := resolver .Resolve (ctx , & req )
275
+ output , err := resolver .Resolve (contextWithConfig (), & req )
276
+ if tc .expectedErr != nil {
277
+ checkExpectedErr (t , tc .expectedErr , err )
278
+ } else {
284
279
if err != nil {
285
- t .Errorf ("unexpected error when resolving cached resource: %v" , err )
286
- return
287
- }
288
- if cached == nil {
289
- t .Error ("expected cached resource but got nil" )
290
- return
280
+ t .Fatalf ("unexpected error resolving: %v" , err )
291
281
}
292
- // Verify that the cached resource matches the original
293
- if ! bytes .Equal (resolved .Data (), cached .Data ()) {
294
- t .Error ("cached resource does not match original resource" )
282
+ if d := cmp .Diff (tc .expectedRes , output .Data ()); d != "" {
283
+ t .Errorf ("unexpected resource from Resolve: %s" , diff .PrintWantGot (d ))
295
284
}
296
285
}
297
286
})
@@ -311,13 +300,12 @@ func toParams(m map[string]string) []pipelinev1.Param {
311
300
return params
312
301
}
313
302
314
- func contextWithConfig (timeout string ) context.Context {
303
+ func contextWithConfig () context.Context {
315
304
config := map [string ]string {
316
305
"default-tekton-hub-catalog" : "Tekton" ,
317
306
"default-artifact-hub-task-catalog" : "tekton-catalog-tasks" ,
318
307
"default-artifact-hub-pipeline-catalog" : "tekton-catalog-pipelines" ,
319
308
"default-type" : "artifact" ,
320
- "default-timeout" : timeout ,
321
309
}
322
310
323
311
return resolutionframework .InjectResolverConfigToContext (context .Background (), config )
0 commit comments