forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
podlogartifact_test.go
466 lines (441 loc) · 12.6 KB
/
podlogartifact_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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
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 (
"bytes"
"fmt"
"io"
"testing"
prowapi "k8s.io/test-infra/prow/apis/prowjobs/v1"
"k8s.io/test-infra/prow/kube"
"k8s.io/test-infra/prow/spyglass/api"
"k8s.io/test-infra/prow/spyglass/lenses"
)
const customContainerName = "custom-container"
// fakePodLogJAgent used for pod log artifact dependency injection
type fakePodLogJAgent struct {
}
func (j *fakePodLogJAgent) GetProwJob(job, id string) (prowapi.ProwJob, error) {
return prowapi.ProwJob{}, nil
}
func (j *fakePodLogJAgent) GetJobLog(job, id, container string) ([]byte, error) {
if job == "BFG" && id == "435" {
switch container {
case kube.TestContainerName:
return []byte("frobscottle"), nil
case customContainerName:
return []byte("snozzcumber"), nil
}
} else if job == "Fantastic Mr. Fox" && id == "4" {
return []byte("a hundred smoked hams and fifty sides of bacon"), nil
}
return nil, fmt.Errorf("could not find job %s, id %s, container %s", job, id, container)
}
func TestNewPodLogArtifact(t *testing.T) {
testCases := []struct {
name string
jobName string
buildID string
container string
artifact string
sizeLimit int64
expectedErr error
expectedLink string
}{
{
name: "Create pod log with valid fields",
jobName: "job",
buildID: "123",
container: kube.TestContainerName,
artifact: singleLogName,
sizeLimit: 500e6,
expectedErr: nil,
expectedLink: fmt.Sprintf("/log?container=%s&id=123&job=job", kube.TestContainerName),
},
{
name: "Create pod log with valid fields and custom container",
jobName: "job",
buildID: "123",
container: customContainerName,
artifact: fmt.Sprintf("%s-%s", customContainerName, singleLogName),
sizeLimit: 500e6,
expectedErr: nil,
expectedLink: fmt.Sprintf("/log?container=%s&id=123&job=job", customContainerName),
},
{
name: "Create pod log with no jobName",
jobName: "",
buildID: "123",
container: kube.TestContainerName,
artifact: singleLogName,
sizeLimit: 500e6,
expectedErr: errInsufficientJobInfo,
expectedLink: "",
},
{
name: "Create pod log with no buildID",
jobName: "job",
buildID: "",
container: kube.TestContainerName,
artifact: singleLogName,
sizeLimit: 500e6,
expectedErr: errInsufficientJobInfo,
expectedLink: "",
},
{
name: "Create pod log with negative sizeLimit",
jobName: "job",
buildID: "123",
container: kube.TestContainerName,
artifact: singleLogName,
sizeLimit: -4,
expectedErr: errInvalidSizeLimit,
expectedLink: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
artifact, err := NewPodLogArtifact(tc.jobName, tc.buildID, tc.artifact, tc.container, tc.sizeLimit, &fakePodLogJAgent{})
if err != nil {
if err != tc.expectedErr {
t.Fatalf("failed creating artifact. err: %v", err)
}
return
}
link := artifact.CanonicalLink()
if link != tc.expectedLink {
t.Errorf("Unexpected link, expected %s, got %q", tc.expectedLink, link)
}
})
}
}
func TestReadTail_PodLog(t *testing.T) {
testCases := []struct {
name string
jobName string
buildID string
container string
artifact *PodLogArtifact
n int64
expected []byte
expectErr bool
}{
{
name: "Podlog ReadTail longer than contents",
jobName: "BFG",
buildID: "435",
container: kube.TestContainerName,
n: 50,
expected: []byte("frobscottle"),
},
{
name: "Podlog ReadTail shorter than contents",
jobName: "Fantastic Mr. Fox",
buildID: "4",
container: kube.TestContainerName,
n: 3,
expected: []byte("con"),
},
{
name: "Podlog ReadTail longer for different container",
jobName: "BFG",
buildID: "435",
container: customContainerName,
n: 50,
expected: []byte("snozzcumber"),
},
{
name: "Podlog ReadTail shorter for different container",
jobName: "BFG",
buildID: "435",
container: customContainerName,
n: 3,
expected: []byte("ber"),
},
{
name: "Podlog ReadTail nonexistent pod",
jobName: "Fax",
buildID: "4",
container: kube.TestContainerName,
n: 3,
expectErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
artifact, err := NewPodLogArtifact(tc.jobName, tc.buildID, singleLogName, tc.container, 500e6, &fakePodLogJAgent{})
if err != nil {
t.Fatalf("Pod Log Tests failed to create pod log artifact, err %v", err)
}
res, err := artifact.ReadTail(tc.n)
if err != nil && !tc.expectErr {
t.Fatalf("failed reading bytes of log. did not expect err, got err: %v", err)
}
if err == nil && tc.expectErr {
t.Errorf("expected an error, got none")
}
if !bytes.Equal(tc.expected, res) {
t.Errorf("Unexpected result of reading pod logs, expected %q, got %q", tc.expected, res)
}
})
}
}
func TestReadAt_PodLog(t *testing.T) {
testCases := []struct {
name string
jobName string
buildID string
container string
n int64
offset int64
expectedErr error
expected []byte
}{
{
name: "Podlog ReadAt range longer than contents",
n: 100,
jobName: "BFG",
buildID: "435",
container: kube.TestContainerName,
offset: 3,
expectedErr: io.EOF,
expected: []byte("bscottle"),
},
{
name: "Podlog ReadAt range within contents",
n: 4,
jobName: "Fantastic Mr. Fox",
buildID: "4",
container: kube.TestContainerName,
offset: 2,
expectedErr: nil,
expected: []byte("hund"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
artifact, err := NewPodLogArtifact(tc.jobName, tc.buildID, singleLogName, tc.container, 500e6, &fakePodLogJAgent{})
if err != nil {
t.Fatalf("Pod Log Tests failed to create pod log artifact, err %v", err)
}
res := make([]byte, tc.n)
readBytes, err := artifact.ReadAt(res, tc.offset)
if err != tc.expectedErr {
t.Fatalf("failed reading bytes of log. err: %v, expected err: %v", err, tc.expectedErr)
}
if !bytes.Equal(tc.expected, res[:readBytes]) {
t.Errorf("Unexpected result of reading pod logs, expected %q, got %q", tc.expected, res)
}
})
}
}
func TestReadAtMost_PodLog(t *testing.T) {
testCases := []struct {
name string
n int64
jobName string
buildID string
container string
expectedErr error
expected []byte
}{
{
name: "Podlog ReadAtMost longer than contents",
jobName: "BFG",
buildID: "435",
container: kube.TestContainerName,
n: 100,
expectedErr: io.EOF,
expected: []byte("frobscottle"),
},
{
name: "Podlog ReadAtMost shorter than contents",
n: 3,
jobName: "BFG",
buildID: "435",
container: kube.TestContainerName,
expectedErr: nil,
expected: []byte("fro"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
artifact, err := NewPodLogArtifact(tc.jobName, tc.buildID, singleLogName, tc.container, 500e6, &fakePodLogJAgent{})
if err != nil {
t.Fatalf("Pod Log Tests failed to create pod log artifact, err %v", err)
}
res, err := artifact.ReadAtMost(tc.n)
if err != tc.expectedErr {
t.Fatalf("failed reading bytes of log. err: %v, expected err: %v", err, tc.expectedErr)
}
if !bytes.Equal(tc.expected, res) {
t.Errorf("Unexpected result of reading pod logs, expected %q, got %q", tc.expected, res)
}
})
}
}
func TestReadAll_PodLog(t *testing.T) {
fakePodLogAgent := &fakePodLogJAgent{}
testCases := []struct {
name string
jobName string
buildID string
container string
sizeLimit int64
expectedErr error
expected []byte
}{
{
name: "Podlog readall not found",
jobName: "job",
buildID: "123",
container: kube.TestContainerName,
sizeLimit: 500e6,
expectedErr: fmt.Errorf("error getting pod log size: error getting size of pod log: could not find job job, id 123, container %s", kube.TestContainerName),
expected: nil,
},
{
name: "Simple \"BFG\" Podlog readall",
jobName: "BFG",
buildID: "435",
container: kube.TestContainerName,
sizeLimit: 500e6,
expectedErr: nil,
expected: []byte("frobscottle"),
},
{
name: "Simple \"BFG\" Podlog readall for custom container",
jobName: "BFG",
buildID: "435",
container: customContainerName,
sizeLimit: 500e6,
expectedErr: nil,
expected: []byte("snozzcumber"),
},
{
name: "\"Fantastic Mr. Fox\" Podlog readall",
jobName: "Fantastic Mr. Fox",
buildID: "4",
container: kube.TestContainerName,
sizeLimit: 500e6,
expectedErr: nil,
expected: []byte("a hundred smoked hams and fifty sides of bacon"),
},
{
name: "Podlog readall over size limit",
jobName: "Fantastic Mr. Fox",
buildID: "4",
container: kube.TestContainerName,
sizeLimit: 5,
expectedErr: lenses.ErrFileTooLarge,
expected: nil,
},
}
for _, tc := range testCases {
artifact, err := NewPodLogArtifact(tc.jobName, tc.buildID, singleLogName, tc.container, tc.sizeLimit, fakePodLogAgent)
if err != nil {
t.Fatalf("Pod Log Tests failed to create pod log artifact, err %v", err)
}
res, err := artifact.ReadAll()
if err != nil && err.Error() != tc.expectedErr.Error() {
t.Fatalf("%s failed reading bytes of log. got err: %v, expected err: %v", tc.name, err, tc.expectedErr)
}
if err != nil {
continue
}
if !bytes.Equal(tc.expected, res) {
t.Errorf("Unexpected result of reading pod logs, expected %q, got %q", tc.expected, res)
}
}
}
type fakeAgent struct {
contents string
}
func (f *fakeAgent) GetProwJob(job, id string) (prowapi.ProwJob, error) {
return prowapi.ProwJob{}, nil
}
func (f *fakeAgent) GetJobLog(job, id, container string) ([]byte, error) {
return []byte(f.contents), nil
}
func TestPodLogArtifact_RespectsSizeLimit(t *testing.T) {
contents := "Supercalifragilisticexpialidocious"
numRequestedBytes := int64(10)
testCases := []struct {
name string
expected error
contents string
skipGzip bool
sizeLimit int64
action func(api.Artifact) error
}{
{
name: "ReadAll",
expected: lenses.ErrFileTooLarge,
action: func(a api.Artifact) error {
_, err := a.ReadAll()
return err
},
},
{
name: "ReadAt",
expected: lenses.ErrRequestSizeTooLarge,
action: func(a api.Artifact) error {
buf := make([]byte, numRequestedBytes)
_, err := a.ReadAt(buf, 3)
return err
},
},
{
name: "ReadAtMost",
expected: lenses.ErrRequestSizeTooLarge,
action: func(a api.Artifact) error {
_, err := a.ReadAtMost(numRequestedBytes)
return err
},
},
{
name: "ReadTail",
expected: lenses.ErrRequestSizeTooLarge,
action: func(a api.Artifact) error {
_, err := a.ReadTail(numRequestedBytes)
return err
},
},
}
for _, tc := range testCases {
t.Run(tc.name+"_NoError", func(nested *testing.T) {
sizeLimit := int64(2 * len(contents))
artifact, err := NewPodLogArtifact("job-name", "build-id", "log-name", "container-name", sizeLimit, &fakeAgent{contents: contents})
if err != nil {
nested.Fatalf("error creating test data: %s", err)
}
actual := tc.action(artifact)
if actual != nil {
nested.Fatalf("unexpected error: %s", actual)
}
})
t.Run(tc.name+"_WithError", func(nested *testing.T) {
sizeLimit := int64(5)
artifact, err := NewPodLogArtifact("job-name", "build-id", "log-name", "container-name", sizeLimit, &fakeAgent{contents: contents})
if err != nil {
nested.Fatalf("error creating test data: %s", err)
}
actual := tc.action(artifact)
if actual == nil {
nested.Fatalf("expected error (%s), but got: nil", tc.expected)
} else if tc.expected.Error() != actual.Error() {
nested.Fatalf("expected error (%s), but got: %s", tc.expected, actual)
}
})
}
}