-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider_test.go
325 lines (295 loc) · 8.18 KB
/
provider_test.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package k6provider
import (
"context"
"crypto/rand"
"errors"
"math"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"os/exec"
"path/filepath"
"sync"
"testing"
"github.com/grafana/k6build/pkg/testutils"
"github.com/grafana/k6deps"
)
// checks request has the correct Authorization header
func newAuthorizationProxy(buildSrv string, header string, authorization string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get(header) != authorization {
w.WriteHeader(http.StatusUnauthorized)
return
}
url, _ := url.Parse(buildSrv)
httputil.NewSingleHostReverseProxy(url).ServeHTTP(w, r)
}
}
// Pass through requests
func newTransparentProxy(upstream string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
url, _ := url.Parse(upstream)
httputil.NewSingleHostReverseProxy(url).ServeHTTP(w, r)
}
}
// fail with the given error up to a number of times
func newUnreliableProxy(upstream string, status int, failures int) http.HandlerFunc {
requests := 0
return func(w http.ResponseWriter, r *http.Request) {
requests++
if requests <= failures {
w.WriteHeader(status)
return
}
url, _ := url.Parse(upstream)
httputil.NewSingleHostReverseProxy(url).ServeHTTP(w, r)
}
}
// returns a corrupted random content
func newCorruptedProxy() http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
buffer := make([]byte, 1024)
_, _ = rand.Read(buffer)
_, _ = w.Write(buffer)
}
}
func Test_Provider(t *testing.T) { //nolint:tparallel
t.Parallel()
testEnv, err := testutils.NewTestEnv(
testutils.TestEnvConfig{
WorkDir: t.TempDir(),
CatalogURL: "testdata/catalog.json",
},
)
if err != nil {
t.Fatalf("test env setup %v", err)
}
t.Cleanup(testEnv.Cleanup)
testCases := []struct {
title string
opts *k6deps.Options
buildProxy http.HandlerFunc
downloadProxy http.HandlerFunc
config Config
expectErr error
expect string
}{
{
title: "build k6 from env variable",
config: Config{},
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
},
{
title: "test authentication using bearer token",
config: Config{
BuildServiceAuth: "token",
},
buildProxy: newAuthorizationProxy(testEnv.BuildServiceURL(), "Authorization", "Bearer token"),
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
expectErr: nil,
},
{
title: "test authentication using custom header",
config: Config{
BuildServiceHeaders: map[string]string{
"Custom-Auth": "token",
},
},
buildProxy: newAuthorizationProxy(testEnv.BuildServiceURL(), "Custom-Auth", "token"),
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
expectErr: nil,
},
{
title: "test authentication failed (missing bearer token)",
config: Config{
BuildServiceAuth: "",
},
buildProxy: newAuthorizationProxy(testEnv.BuildServiceURL(), "Authorization", "Bearer token"),
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
expectErr: ErrBuild,
},
{
title: "test download using proxy",
downloadProxy: newTransparentProxy(testEnv.StoreServiceURL()),
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
},
{
title: "test download proxy unavailable",
config: Config{
DownloadConfig: DownloadConfig{
ProxyURL: "http://127.0.0.1:12345",
},
},
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
expectErr: ErrDownload,
},
{
title: "test download authentication using bearer token",
config: Config{
BuildServiceAuth: "token",
DownloadConfig: DownloadConfig{
Authorization: "token",
},
},
downloadProxy: newAuthorizationProxy(testEnv.StoreServiceURL(), "Authorization", "Bearer token"),
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
expectErr: nil,
},
{
title: "test download authentication failed (missing bearer token)",
config: Config{
DownloadConfig: DownloadConfig{
Authorization: "",
},
},
downloadProxy: newAuthorizationProxy(testEnv.StoreServiceURL(), "Authorization", "Bearer token"),
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
expectErr: ErrDownload,
},
{
title: "test download with default retries",
downloadProxy: newUnreliableProxy(testEnv.StoreServiceURL(), http.StatusServiceUnavailable, 1),
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
},
{
title: "test we don't retry forever",
config: Config{DownloadConfig: DownloadConfig{Retries: 1}},
downloadProxy: newUnreliableProxy(testEnv.StoreServiceURL(), http.StatusServiceUnavailable, math.MaxInt),
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
expectErr: ErrDownload,
},
{
title: "detect corrupted binary",
downloadProxy: newCorruptedProxy(),
opts: &k6deps.Options{
Env: k6deps.Source{Name: "K6_DEPS", Contents: []byte("k6=v0.50.0")},
},
expectErr: ErrDownload,
},
}
for _, tc := range testCases { //nolint:paralleltest
t.Run(tc.title, func(t *testing.T) {
// by default, we use the build service, but if there's a
// proxy defined, we use it
testSrvURL := testEnv.BuildServiceURL()
if tc.buildProxy != nil {
testSrv := httptest.NewServer(tc.buildProxy)
defer testSrv.Close()
testSrvURL = testSrv.URL
}
// if there's a download proxy, we use it
testStoreProxy := ""
if tc.downloadProxy != nil {
downloadProxy := httptest.NewServer(tc.downloadProxy)
defer downloadProxy.Close()
testStoreProxy = downloadProxy.URL
}
config := tc.config
config.BinDir = filepath.Join(t.TempDir(), "provider")
config.BuildServiceURL = testSrvURL
// FIXME: override download proxy if not set in the test. This is needed to test wrong proxy URL
if config.DownloadConfig.ProxyURL == "" {
config.DownloadConfig.ProxyURL = testStoreProxy
}
provider, err := NewProvider(config)
if err != nil {
t.Fatalf("initializing provider %v", err)
}
deps, err := k6deps.Analyze(tc.opts)
if err != nil {
t.Fatalf("analyzing dependencies %v", err)
}
k6, err := provider.GetBinary(context.TODO(), deps)
if !errors.Is(err, tc.expectErr) {
t.Fatalf("expected %v got %v", tc.expectErr, err)
}
// in case of error the binary should not be downloaded
if tc.expectErr != nil {
_, err := os.Stat(k6.Path)
if !os.IsNotExist(err) {
t.Fatalf("expected binary not to be downloaded")
}
return
}
// in case of not error, we expect the binary to be downloaded
cmd := exec.Command(k6.Path, "version")
out, err := cmd.Output()
if err != nil {
t.Fatalf("running command %v", err)
}
t.Log(string(out))
})
}
}
func Test_ConcurentDownload(t *testing.T) {
t.Parallel()
testEnv, err := testutils.NewTestEnv(
testutils.TestEnvConfig{
WorkDir: t.TempDir(),
CatalogURL: "testdata/catalog.json",
},
)
if err != nil {
t.Fatalf("test env setup %v", err)
}
t.Cleanup(testEnv.Cleanup)
provider, err := NewProvider(Config{
BinDir: filepath.Join(t.TempDir(), "provider"),
BuildServiceURL: testEnv.BuildServiceURL(),
})
if err != nil {
t.Fatalf("initializing provider %v", err)
}
deps := k6deps.Dependencies{}
err = deps.UnmarshalText([]byte("k6=v0.50.0"))
if err != nil {
t.Fatalf("analyzing dependencies %v", err)
}
wg := sync.WaitGroup{}
errs := make(chan error, 10)
for range 3 {
wg.Add(1)
go func() {
defer wg.Done()
k6, err := provider.GetBinary(context.TODO(), deps)
if err != nil {
errs <- err
return
}
cmd := exec.Command(k6.Path, "version")
err = cmd.Run()
if err != nil {
errs <- err
}
}()
}
wg.Wait()
select {
case err := <-errs:
t.Fatalf("expected no error, got %v", err)
default:
}
}