forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorageartifact_fetcher_test.go
451 lines (432 loc) · 14.9 KB
/
storageartifact_fetcher_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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
Copyright 2018 The Kubernetes 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 spyglass
import (
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"k8s.io/apimachinery/pkg/util/sets"
prowv1 "k8s.io/test-infra/prow/apis/prowjobs/v1"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/io"
)
func TestNewGCSJobSource(t *testing.T) {
testCases := []struct {
name string
src string
exJobPrefix string
exBucket string
exName string
exBuildID string
exLinkPrefix string
expectedErr error
}{
{
name: "Test standard GCS link (old format)",
src: "test-bucket/logs/example-ci-run/403",
exBucket: "test-bucket",
exJobPrefix: "logs/example-ci-run/403/",
exName: "example-ci-run",
exBuildID: "403",
exLinkPrefix: "gs://",
expectedErr: nil,
},
{
name: "Test GCS link with trailing / (old format)",
src: "test-bucket/logs/example-ci-run/403/",
exBucket: "test-bucket",
exJobPrefix: "logs/example-ci-run/403/",
exName: "example-ci-run",
exBuildID: "403",
exLinkPrefix: "gs://",
expectedErr: nil,
},
{
name: "Test GCS link with org name (old format)",
src: "test-bucket/logs/sig-flexing/example-ci-run/403",
exBucket: "test-bucket",
exJobPrefix: "logs/sig-flexing/example-ci-run/403/",
exName: "example-ci-run",
exBuildID: "403",
exLinkPrefix: "gs://",
expectedErr: nil,
},
{
name: "Test standard GCS link (new format)",
src: "gs://test-bucket/logs/example-ci-run/403",
exBucket: "test-bucket",
exJobPrefix: "logs/example-ci-run/403/",
exName: "example-ci-run",
exBuildID: "403",
exLinkPrefix: "gs://",
expectedErr: nil,
},
{
name: "Test GCS link with trailing / (new format)",
src: "gs://test-bucket/logs/example-ci-run/403/",
exBucket: "test-bucket",
exJobPrefix: "logs/example-ci-run/403/",
exName: "example-ci-run",
exBuildID: "403",
exLinkPrefix: "gs://",
expectedErr: nil,
},
{
name: "Test GCS link with org name (new format)",
src: "gs://test-bucket/logs/sig-flexing/example-ci-run/403",
exBucket: "test-bucket",
exJobPrefix: "logs/sig-flexing/example-ci-run/403/",
exName: "example-ci-run",
exBuildID: "403",
exLinkPrefix: "gs://",
expectedErr: nil,
},
{
name: "Test standard S3 link",
src: "s3://test-bucket/logs/example-ci-run/403",
exBucket: "test-bucket",
exJobPrefix: "logs/example-ci-run/403/",
exName: "example-ci-run",
exBuildID: "403",
exLinkPrefix: "s3://",
expectedErr: nil,
},
{
name: "Test S3 link with trailing /",
src: "s3://test-bucket/logs/example-ci-run/403/",
exBucket: "test-bucket",
exJobPrefix: "logs/example-ci-run/403/",
exName: "example-ci-run",
exBuildID: "403",
exLinkPrefix: "s3://",
expectedErr: nil,
},
{
name: "Test S3 link with org name",
src: "s3://test-bucket/logs/sig-flexing/example-ci-run/403",
exBucket: "test-bucket",
exJobPrefix: "logs/sig-flexing/example-ci-run/403/",
exName: "example-ci-run",
exBuildID: "403",
exLinkPrefix: "s3://",
expectedErr: nil,
},
{
name: "Test S3 link which cannot be parsed",
src: "s3;://test-bucket/logs/sig-flexing/example-ci-run/403",
expectedErr: ErrCannotParseSource,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cfg := createConfigGetter("test-bucket")
af := NewStorageArtifactFetcher(nil, cfg, false)
jobSource, err := af.newStorageJobSource(tc.src)
if err != tc.expectedErr {
t.Errorf("Expected err: %v, got err: %v", tc.expectedErr, err)
}
if tc.exBucket != jobSource.bucket {
t.Errorf("Expected bucket %s, got %s", tc.exBucket, jobSource.bucket)
}
if tc.exName != jobSource.jobName {
t.Errorf("Expected jobName %s, got %s", tc.exName, jobSource.jobName)
}
if tc.exJobPrefix != jobSource.jobPrefix {
t.Errorf("Expected jobPrefix %s, got %s", tc.exJobPrefix, jobSource.jobPrefix)
}
if tc.exLinkPrefix != jobSource.linkPrefix {
t.Errorf("Expected linkPrefix %s, got %s", tc.exLinkPrefix, jobSource.linkPrefix)
}
})
}
}
// Tests listing objects associated with the current job in GCS
func TestArtifacts_ListGCS(t *testing.T) {
cfg := createConfigGetter("test-bucket")
fakeGCSClient := fakeGCSServer.Client()
testAf := NewStorageArtifactFetcher(io.NewGCSOpener(fakeGCSClient), cfg, false)
testCases := []struct {
name string
handle artifactHandle
source string
expectedArtifacts []string
}{
{
name: "Test ArtifactFetcher simple list artifacts (old format)",
source: "test-bucket/logs/example-ci-run/403",
expectedArtifacts: []string{
"build-log.txt",
prowv1.StartedStatusFile,
prowv1.FinishedStatusFile,
"junit_01.xml",
"long-log.txt",
},
},
{
name: "Test ArtifactFetcher list artifacts on source with no artifacts (old format)",
source: "test-bucket/logs/example-ci/404",
expectedArtifacts: []string{},
},
{
name: "Test ArtifactFetcher simple list artifacts (new format)",
source: "gs://test-bucket/logs/example-ci-run/403",
expectedArtifacts: []string{
"build-log.txt",
prowv1.StartedStatusFile,
prowv1.FinishedStatusFile,
"junit_01.xml",
"long-log.txt",
},
},
{
name: "Test ArtifactFetcher list artifacts on source with no artifacts (new format)",
source: "gs://test-bucket/logs/example-ci/404",
expectedArtifacts: []string{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(nested *testing.T) {
actualArtifacts, err := testAf.artifacts(context.Background(), tc.source)
if err != nil {
nested.Fatalf("Failed to get artifact names: %v", err)
}
for _, ea := range tc.expectedArtifacts {
found := false
for _, aa := range actualArtifacts {
if ea == aa {
found = true
break
}
}
if !found {
nested.Fatalf("failed to retrieve the following artifact: %s\nRetrieved: %s.", ea, actualArtifacts)
}
}
if len(tc.expectedArtifacts) != len(actualArtifacts) {
nested.Fatalf("produced more artifacts than expected. Expected: %s\nActual: %s.", tc.expectedArtifacts, actualArtifacts)
}
})
}
}
// Tests getting handles to objects associated with the current job in GCS
func TestFetchArtifacts_GCS(t *testing.T) {
cfg := createConfigGetter("test-bucket")
fakeGCSClient := fakeGCSServer.Client()
testAf := NewStorageArtifactFetcher(io.NewGCSOpener(fakeGCSClient), cfg, false)
maxSize := int64(500e6)
testCases := []struct {
name string
artifactName string
source string
expectedSize int64
expectErr bool
}{
{
name: "Fetch build-log.txt from valid source",
artifactName: "build-log.txt",
source: "test-bucket/logs/example-ci-run/403",
expectedSize: 25,
},
{
name: "Fetch build-log.txt from invalid source",
artifactName: "build-log.txt",
source: "test-bucket/logs/example-ci-run/404",
expectErr: true,
},
{
name: "Fetch build-log.txt from valid source",
artifactName: "build-log.txt",
source: "gs://test-bucket/logs/example-ci-run/403",
expectedSize: 25,
},
{
name: "Fetch build-log.txt from invalid source",
artifactName: "build-log.txt",
source: "gs://test-bucket/logs/example-ci-run/404",
expectErr: true,
},
}
for _, tc := range testCases {
artifact, err := testAf.Artifact(context.Background(), tc.source, tc.artifactName, maxSize)
if err != nil {
t.Errorf("Failed to get artifacts: %v", err)
}
size, err := artifact.Size()
if err != nil && !tc.expectErr {
t.Fatalf("%s failed getting size for artifact %s, err: %v", tc.name, artifact.JobPath(), err)
}
if err == nil && tc.expectErr {
t.Errorf("%s expected error, got no error", tc.name)
}
if size != tc.expectedSize {
t.Errorf("%s expected artifact with size %d but got %d", tc.name, tc.expectedSize, size)
}
}
}
func TestSignURL(t *testing.T) {
// This fake key is revoked and thus worthless but still make its contents less obvious
fakeKeyBuf, err := base64.StdEncoding.DecodeString(`
LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tXG5NSUlFdlFJQkFEQU5CZ2txaGtpRzl3MEJBUUVG
QUFTQ0JLY3dnZ1NqQWdFQUFvSUJBUUN4MEF2aW1yMjcwZDdaXG5pamw3b1FRUW1oZTFOb3dpeWMy
UStuQW95aFE1YkQvUW1jb01zcWg2YldneVI0UU90aXVBbHM2VWhJenF4Q25pXG5PazRmbWJqVnhp
STl1Ri9EVTV6ZE5wM0dkQWFiUlVPNW5yWkpMelN0VXhudFBEcjZvK281RHM5YWJJWkNYYUVTXG5o
UWxOdTBrUm5HbHZGUHN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53bS9IVjhl
aThDeHZLMTRoRXpCT3JXQi9aNlo4VFFxWnA0eENnYkNiY0hwY3pLRUxvcDA2K2hqa1N3ZkR2TUJZ
XG5mNnN6U2RSenNYVTI1NndmcG1hRjJ0TlJZZFpVblh2QWc5MFIrb1BFSjhrRHd4cjdiMGZmL3lu
b0UrWUx0ckowXG53dklad3Joc093S0JnQWVPbWlTMHRZeUNnRkwvNHNuZ3ZodEs5WElGQ0w1VU9C
dlp6Qk0xdlJOdjJ5eEFyRi9nXG5zajJqSmVyUWoyTUVpQkRmL2RQelZPYnBwaTByOCthMDNFOEdG
OGZxakpxK2VnbDg2aXBaQjhxOUU5NTFyOUxSXG5Xa1ZtTEFEVVIxTC8rSjFhakxiWHJzOWlzZkxh
ZEI2OUJpT1lXWmpPRk0reitocmNkYkR5blZraEFvR0FJbW42XG50ZU1zN2NNWTh3anZsY0MrZ3Br
SU5GZzgzYVIyajhJQzNIOWtYMGs0N3ovS0ZjbW9TTGxjcEhNc0VJeGozamJXXG5kd0FkZy9TNkpi
RW1SbGdoaWVoaVNRc21RM05ta0xxNlFJWkorcjR4VkZ4RUZnOWFEM0szVUZMT0xickRCSFpJXG5D
M3JRWVpMNkpnY1E1TlBtbTk4QXZIN2RucjRiRGpaVDgzSS9McFVDZ1lFQWttNXlvVUtZY0tXMVQz
R1hadUNIXG40SDNWVGVzZDZyb3pKWUhmTWVkNE9jQ3l1bnBIVmZmSmFCMFIxRjZ2MjFQaitCVWlW
WjBzU010RjEvTE1uQkc4XG5TQVlQUnVxOHVNUUdNQTFpdE1Hc2VhMmg1V2RhbXNGODhXRFd4VEoy
QXVnblJHNERsdmJLUDhPQmVLUFFKeDhEXG5RMzJ2SVpNUVkyV1hVMVhwUkMrNWs5RT1cbi0tLS0t
RU5EIFBSSVZBVEUgS0VZLS0tLS1cbgo=`)
if err != nil {
t.Fatalf("Failed to decode fake key: %v", err)
}
fakePrivateKey := strings.TrimSpace(string(fakeKeyBuf))
cases := []struct {
name string
fakeCreds string
useCookie bool
expected string
contains []string
err string
}{
{
name: "anon auth works",
expected: fmt.Sprintf("https://%s/foo/bar/stuff", io.GSAnonHost),
},
{
name: "cookie auth works",
useCookie: true,
expected: fmt.Sprintf("https://%s/foo/bar/stuff", io.GSCookieHost),
},
{
name: "invalid json file errors",
fakeCreds: "yaml: 123",
err: "dialing: invalid character 'y' looking for beginning of value",
},
{
name: "bad private key errors",
fakeCreds: `{
"type": "service_account",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIE==\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]"
}`,
err: "asn1: structure error: tags don't match (16 vs {class:0 tag:13 length:45 isCompound:true}) {optional:false explicit:false application:false private:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} pkcs1PrivateKey @2",
},
{
name: "bad type errors",
fakeCreds: `{
"type": "user",
"private_key": "` + fakePrivateKey + `",
"client_email": "[email protected]"
}`,
err: "dialing: unknown credential type: \"user\"",
},
{
name: "signed URLs work",
fakeCreds: `{
"type": "service_account",
"private_key": "` + fakePrivateKey + `",
"client_email": "[email protected]"
}`,
contains: []string{
"https://storage.googleapis.com/foo/bar/stuff?",
"GoogleAccessId=fake-user%40k8s.io",
"Signature=", // Do not particularly care about the Signature contents
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var path string
if tc.fakeCreds != "" {
fp, err := ioutil.TempFile("", "fake-creds")
if err != nil {
t.Fatalf("Failed to create fake creds: %v", err)
}
path = fp.Name()
defer os.Remove(path)
if _, err := fp.Write([]byte(tc.fakeCreds)); err != nil {
t.Fatalf("Failed to write fake creds %s: %v", path, err)
}
if err := fp.Close(); err != nil {
t.Fatalf("Failed to close fake creds %s: %v", path, err)
}
}
// We're testing the combination of NewOpener and signURL here
// to make sure that the behaviour is more or less the same as before
// we moved the signURL code to the io package.
// The errors which were previously tested on the signURL method are now
// already returned by newOpener.
// Before, these error should have already lead to errors on gcs client creation, so signURL probably was never able to produce these errors during runtime.
// (because deck crashed on gcsClient creation)
var actual string
cfg := createConfigGetter("test-bucket")
opener, err := io.NewOpener(context.Background(), path, "")
if err == nil {
af := NewStorageArtifactFetcher(opener, cfg, tc.useCookie)
actual, err = af.signURL(context.Background(), "gs://foo/bar/stuff")
}
switch {
case err != nil:
if tc.err != err.Error() {
t.Errorf("expected error: %v, got: %v", tc.err, err)
}
case tc.err != "":
t.Errorf("Failed to receive an expected error, got %q", actual)
case len(tc.contains) == 0 && actual != tc.expected:
t.Errorf("signURL(): got %q, want %q", actual, tc.expected)
default:
for _, part := range tc.contains {
if !strings.Contains(actual, part) {
t.Errorf("signURL(): got %q, does not contain %q", actual, part)
}
}
}
})
}
}
func createConfigGetter(bucketNames ...string) config.Getter {
ca := config.Agent{}
ca.Set(&config.Config{
ProwConfig: config.ProwConfig{
Deck: config.Deck{
AllKnownStorageBuckets: sets.NewString(bucketNames...),
},
},
})
return ca.Config
}