Skip to content

Commit 76731e4

Browse files
committed
leave hub resolver alone for now.
1 parent 67a4e9b commit 76731e4

File tree

2 files changed

+88
-125
lines changed

2 files changed

+88
-125
lines changed

pkg/remoteresolution/resolver/hub/resolver.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"errors"
1919

2020
"github.com/tektoncd/pipeline/pkg/apis/resolution/v1beta1"
21-
"github.com/tektoncd/pipeline/pkg/remoteresolution/cache"
2221
"github.com/tektoncd/pipeline/pkg/remoteresolution/resolver/framework"
2322
"github.com/tektoncd/pipeline/pkg/resolution/common"
2423
resolutionframework "github.com/tektoncd/pipeline/pkg/resolution/resolver/framework"
@@ -59,11 +58,10 @@ func (r *Resolver) GetName(context.Context) string {
5958

6059
// GetConfigName returns the name of the bundle resolver's configmap.
6160
func (r *Resolver) GetConfigName(context.Context) string {
62-
return "hub-resolver-config"
61+
return "hubresolver-config"
6362
}
6463

65-
// GetSelector returns the labels that resource requests are required to have for
66-
// the hub resolver to process them.
64+
// GetSelector returns a map of labels to match requests to this resolver.
6765
func (r *Resolver) GetSelector(context.Context) map[string]string {
6866
return map[string]string{
6967
common.LabelKeyResolverType: LabelValueHubResolverType,
@@ -82,30 +80,7 @@ func (r *Resolver) Validate(ctx context.Context, req *v1beta1.ResolutionRequestS
8280
// Resolve uses the given params to resolve the requested file or resource.
8381
func (r *Resolver) Resolve(ctx context.Context, req *v1beta1.ResolutionRequestSpec) (resolutionframework.ResolvedResource, error) {
8482
if len(req.Params) > 0 {
85-
origParams := req.Params
86-
87-
// Generate cache key
88-
cacheKey, err := cache.GenerateCacheKey(LabelValueHubResolverType, origParams)
89-
if err != nil {
90-
return nil, err
91-
}
92-
93-
// Check cache first
94-
if cached, ok := cache.GetGlobalCache().Get(cacheKey); ok {
95-
if resource, ok := cached.(resolutionframework.ResolvedResource); ok {
96-
return resource, nil
97-
}
98-
}
99-
100-
resource, err := hub.Resolve(ctx, origParams, r.TektonHubURL, r.ArtifactHubURL)
101-
if err != nil {
102-
return nil, err
103-
}
104-
105-
// Cache the result
106-
cache.GetGlobalCache().Add(cacheKey, resource)
107-
108-
return resource, nil
83+
return hub.Resolve(ctx, req.Params, r.TektonHubURL, r.ArtifactHubURL)
10984
}
11085
// Remove this error once resolution of url has been implemented.
11186
return nil, errors.New("the Resolve method has not been implemented.")

pkg/remoteresolution/resolver/hub/resolver_test.go

Lines changed: 85 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package hub
1818

1919
import (
20-
"bytes"
2120
"context"
2221
"errors"
2322
"fmt"
@@ -31,10 +30,7 @@ import (
3130
resolutioncommon "github.com/tektoncd/pipeline/pkg/resolution/common"
3231
resolutionframework "github.com/tektoncd/pipeline/pkg/resolution/resolver/framework"
3332
hubresolver "github.com/tektoncd/pipeline/pkg/resolution/resolver/hub"
34-
)
35-
36-
const (
37-
defaultHttpTimeoutValue = "1m"
33+
"github.com/tektoncd/pipeline/test/diff"
3834
)
3935

4036
func TestGetSelector(t *testing.T) {
@@ -97,7 +93,7 @@ func TestValidate(t *testing.T) {
9793
req := v1beta1.ResolutionRequestSpec{
9894
Params: toParams(params),
9995
}
100-
err := resolver.Validate(contextWithConfig(defaultHttpTimeoutValue), &req)
96+
err := resolver.Validate(contextWithConfig(), &req)
10197
if tc.expectedErr != nil {
10298
checkExpectedErr(t, tc.expectedErr, err)
10399
} else if err != nil {
@@ -119,7 +115,7 @@ func TestValidateMissing(t *testing.T) {
119115
req := v1beta1.ResolutionRequestSpec{
120116
Params: toParams(paramsMissingName),
121117
}
122-
err = resolver.Validate(contextWithConfig(defaultHttpTimeoutValue), &req)
118+
err = resolver.Validate(contextWithConfig(), &req)
123119
if err == nil {
124120
t.Fatalf("expected missing name err")
125121
}
@@ -131,7 +127,7 @@ func TestValidateMissing(t *testing.T) {
131127
req = v1beta1.ResolutionRequestSpec{
132128
Params: toParams(paramsMissingVersion),
133129
}
134-
err = resolver.Validate(contextWithConfig(defaultHttpTimeoutValue), &req)
130+
err = resolver.Validate(contextWithConfig(), &req)
135131

136132
if err == nil {
137133
t.Fatalf("expected missing version err")
@@ -175,7 +171,7 @@ func TestValidateConflictingKindName(t *testing.T) {
175171
req := v1beta1.ResolutionRequestSpec{
176172
Params: toParams(params),
177173
}
178-
err := resolver.Validate(contextWithConfig(defaultHttpTimeoutValue), &req)
174+
err := resolver.Validate(contextWithConfig(), &req)
179175
if err == nil {
180176
t.Fatalf("expected err due to conflicting param")
181177
}
@@ -185,113 +181,106 @@ func TestValidateConflictingKindName(t *testing.T) {
185181

186182
func TestResolve(t *testing.T) {
187183
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
194193
}{
195194
{
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"),
202213
},
203214
{
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(""),
210223
},
211224
{
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"),
218233
},
219234
{
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"),
226251
},
227252
}
228253

229254
for _, tc := range testCases {
230255
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)
236258
}))
237-
defer server.Close()
238259

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,
256263
}
257264

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,
273271
}
274-
275-
if resolved == nil {
276-
t.Error("expected resolved resource but got nil")
277-
return
272+
req := v1beta1.ResolutionRequestSpec{
273+
Params: toParams(params),
278274
}
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 {
284279
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)
291281
}
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))
295284
}
296285
}
297286
})
@@ -311,13 +300,12 @@ func toParams(m map[string]string) []pipelinev1.Param {
311300
return params
312301
}
313302

314-
func contextWithConfig(timeout string) context.Context {
303+
func contextWithConfig() context.Context {
315304
config := map[string]string{
316305
"default-tekton-hub-catalog": "Tekton",
317306
"default-artifact-hub-task-catalog": "tekton-catalog-tasks",
318307
"default-artifact-hub-pipeline-catalog": "tekton-catalog-pipelines",
319308
"default-type": "artifact",
320-
"default-timeout": timeout,
321309
}
322310

323311
return resolutionframework.InjectResolverConfigToContext(context.Background(), config)

0 commit comments

Comments
 (0)