Skip to content

Commit 67a4e9b

Browse files
committed
leave http resolver alone for now.
1 parent 0296d44 commit 67a4e9b

File tree

2 files changed

+54
-130
lines changed

2 files changed

+54
-130
lines changed

pkg/remoteresolution/resolver/http/resolver.go

Lines changed: 1 addition & 34 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"
@@ -102,39 +101,7 @@ func (r *Resolver) Resolve(ctx context.Context, req *v1beta1.ResolutionRequestSp
102101
return nil, err
103102
}
104103

105-
// Check if caching is enabled
106-
useCache := params[http.CacheParam] == "true"
107-
108-
if useCache {
109-
// Initialize cache logger
110-
cache.GetGlobalCache().InitializeLogger(ctx)
111-
112-
// Generate cache key
113-
cacheKey, err := cache.GenerateCacheKey(LabelValueHttpResolverType, oParams)
114-
if err != nil {
115-
return nil, err
116-
}
117-
118-
// Check cache first
119-
if cached, ok := cache.GetGlobalCache().Get(cacheKey); ok {
120-
if resource, ok := cached.(resolutionframework.ResolvedResource); ok {
121-
return resource, nil
122-
}
123-
}
124-
}
125-
126-
resource, err := http.FetchHttpResource(ctx, params, r.kubeClient, r.logger)
127-
if err != nil {
128-
return nil, err
129-
}
130-
131-
// Cache the result if caching is enabled
132-
if useCache {
133-
cacheKey, _ := cache.GenerateCacheKey(LabelValueHttpResolverType, oParams)
134-
cache.GetGlobalCache().Add(cacheKey, resource)
135-
}
136-
137-
return resource, nil
104+
return http.FetchHttpResource(ctx, params, r.kubeClient, r.logger)
138105
}
139106
// Remove this error once resolution of url has been implemented.
140107
return nil, errors.New("the Resolve method has not been implemented.")

pkg/remoteresolution/resolver/http/resolver_test.go

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

1919
import (
20-
"bytes"
2120
"context"
2221
"crypto/sha256"
2322
"encoding/base64"
@@ -26,6 +25,7 @@ import (
2625
"fmt"
2726
"net/http"
2827
"net/http/httptest"
28+
"regexp"
2929
"testing"
3030
"time"
3131

@@ -105,22 +105,15 @@ func TestValidate(t *testing.T) {
105105
for _, tc := range testCases {
106106
t.Run(tc.name, func(t *testing.T) {
107107
resolver := Resolver{}
108-
params := []pipelinev1.Param{}
108+
params := map[string]string{}
109109
if tc.url != "nourl" {
110-
params = append(params, pipelinev1.Param{
111-
Name: "url",
112-
Value: *pipelinev1.NewStructuredValues(tc.url),
113-
})
110+
params[httpresolution.UrlParam] = tc.url
114111
} else {
115112
// inject a fake param so that it can validate that the url is actually missing.
116-
params = append(params, pipelinev1.Param{
117-
Name: "foo",
118-
Value: *pipelinev1.NewStructuredValues("bar"),
119-
})
113+
params["foo"] = "bar"
120114
}
121-
122115
req := v1beta1.ResolutionRequestSpec{
123-
Params: params,
116+
Params: toParams(params),
124117
}
125118
err := resolver.Validate(contextWithConfig(defaultHttpTimeoutValue), &req)
126119
if tc.expectedErr != nil {
@@ -133,115 +126,79 @@ func TestValidate(t *testing.T) {
133126
}
134127

135128
func TestResolve(t *testing.T) {
136-
testCases := []struct {
129+
tests := []struct {
137130
name string
138-
expectedError error
131+
expectedErr string
139132
input string
140-
paramsSet bool
141-
useCache bool
133+
paramSet bool
142134
expectedStatus int
143135
}{
144136
{
145-
name: "valid params set with cache",
146-
expectedError: nil,
147-
input: "https://example.com/script.yaml",
148-
paramsSet: true,
149-
useCache: true,
150-
expectedStatus: http.StatusOK,
151-
},
152-
{
153-
name: "valid params set without cache",
154-
expectedError: nil,
155-
input: "https://example.com/script.yaml",
156-
paramsSet: true,
157-
useCache: false,
158-
expectedStatus: http.StatusOK,
159-
},
160-
{
161-
name: "missing required params",
162-
expectedError: fmt.Errorf("missing required param: url"),
163-
input: "",
164-
paramsSet: false,
165-
useCache: false,
166-
expectedStatus: http.StatusBadRequest,
167-
},
168-
{
169-
name: "not found error",
170-
expectedError: fmt.Errorf("error accessing resource from \"https://example.com/not-found.yaml\": 404 Not Found"),
171-
input: "https://example.com/not-found.yaml",
172-
paramsSet: true,
173-
useCache: false,
137+
name: "good/params set",
138+
input: "task",
139+
paramSet: true,
140+
}, {
141+
name: "bad/params not set",
142+
input: "task",
143+
expectedErr: `missing required http resolver params: url`,
144+
}, {
145+
name: "bad/not found",
146+
input: "task",
147+
paramSet: true,
174148
expectedStatus: http.StatusNotFound,
149+
expectedErr: `requested URL 'http://([^']*)' is not found`,
175150
},
176151
}
177-
178-
for _, tc := range testCases {
152+
for _, tc := range tests {
179153
t.Run(tc.name, func(t *testing.T) {
180-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
181-
w.WriteHeader(tc.expectedStatus)
182-
if tc.expectedStatus == http.StatusOK {
183-
w.Write([]byte("test content"))
154+
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
155+
if tc.expectedStatus != 0 {
156+
w.WriteHeader(tc.expectedStatus)
184157
}
158+
fmt.Fprint(w, tc.input)
185159
}))
186-
defer server.Close()
187-
188-
resolver := &Resolver{}
189160
params := []pipelinev1.Param{}
190-
if tc.paramsSet {
161+
if tc.paramSet {
191162
params = append(params, pipelinev1.Param{
192-
Name: "url",
193-
Value: *pipelinev1.NewStructuredValues(tc.input),
163+
Name: httpresolution.UrlParam,
164+
Value: *pipelinev1.NewStructuredValues(svr.URL),
165+
})
166+
} else {
167+
params = append(params, pipelinev1.Param{
168+
Name: "foo",
169+
Value: *pipelinev1.NewStructuredValues("bar"),
194170
})
195-
if tc.useCache {
196-
params = append(params, pipelinev1.Param{
197-
Name: "cache",
198-
Value: *pipelinev1.NewStructuredValues("true"),
199-
})
200-
}
201171
}
202-
172+
resolver := Resolver{}
203173
req := v1beta1.ResolutionRequestSpec{
204174
Params: params,
205175
}
206-
207-
ctx := contextWithConfig(defaultHttpTimeoutValue)
208-
resolved, err := resolver.Resolve(ctx, &req)
209-
210-
if tc.expectedError != nil {
211-
if err == nil {
212-
t.Errorf("expected error %v but got nil", tc.expectedError)
213-
} else if err.Error() != tc.expectedError.Error() {
214-
t.Errorf("expected error %v but got %v", tc.expectedError, err)
176+
output, err := resolver.Resolve(contextWithConfig(defaultHttpTimeoutValue), &req)
177+
if tc.expectedErr != "" {
178+
re := regexp.MustCompile(tc.expectedErr)
179+
if !re.MatchString(err.Error()) {
180+
t.Fatalf("expected error '%v' but got '%v'", tc.expectedErr, err)
215181
}
216182
return
183+
} else if err != nil {
184+
t.Fatalf("unexpected error validating params: %v", err)
217185
}
218-
219-
if err != nil {
220-
t.Errorf("unexpected error: %v", err)
221-
return
186+
if o := cmp.Diff(tc.input, string(output.Data())); o != "" {
187+
t.Fatalf("expected output '%v' but got '%v'", tc.input, string(output.Data()))
188+
}
189+
if o := cmp.Diff(svr.URL, output.RefSource().URI); o != "" {
190+
t.Fatalf("expected url '%v' but got '%v'", svr.URL, output.RefSource().URI)
222191
}
223192

224-
if resolved == nil {
225-
t.Error("expected resolved resource but got nil")
226-
return
193+
eSum := sha256.New()
194+
eSum.Write([]byte(tc.input))
195+
eSha256 := hex.EncodeToString(eSum.Sum(nil))
196+
if o := cmp.Diff(eSha256, output.RefSource().Digest["sha256"]); o != "" {
197+
t.Fatalf("expected sha256 '%v' but got '%v'", eSha256, output.RefSource().Digest["sha256"])
227198
}
228199

229-
// Test cache functionality
230-
if tc.useCache {
231-
// Try resolving again with the same parameters
232-
cached, err := resolver.Resolve(ctx, &req)
233-
if err != nil {
234-
t.Errorf("unexpected error when resolving cached resource: %v", err)
235-
return
236-
}
237-
if cached == nil {
238-
t.Error("expected cached resource but got nil")
239-
return
240-
}
241-
// Verify that the cached resource matches the original
242-
if !bytes.Equal(resolved.Data(), cached.Data()) {
243-
t.Error("cached resource does not match original resource")
244-
}
200+
if output.Annotations() != nil {
201+
t.Fatalf("output annotations should be nil")
245202
}
246203
})
247204
}

0 commit comments

Comments
 (0)