forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 41
/
job_history_test.go
178 lines (169 loc) · 4.27 KB
/
job_history_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
/*
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 main
import (
"net/url"
"testing"
)
func TestJobHistURL(t *testing.T) {
cases := []struct {
name string
address string
bktName string
root string
id int64
expErr bool
}{
{
address: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e",
bktName: "foo-bucket",
root: "logs/bar-e2e",
id: emptyID,
},
{
address: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=",
bktName: "foo-bucket",
root: "logs/bar-e2e",
id: emptyID,
},
{
address: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=123456789123456789",
bktName: "foo-bucket",
root: "logs/bar-e2e",
id: 123456789123456789,
},
{
address: "http://www.example.com/job-history/foo-bucket",
expErr: true,
},
{
address: "http://www.example.com/job-history/foo-bucket/",
expErr: true,
},
{
address: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=-738",
expErr: true,
},
{
address: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=nope",
expErr: true,
},
}
for _, tc := range cases {
u, _ := url.Parse(tc.address)
bktName, root, id, err := parseJobHistURL(u)
if tc.expErr {
if err == nil && tc.expErr {
t.Errorf("parsing %q: expected error", tc.address)
}
continue
}
if err != nil {
t.Errorf("parsing %q: unexpected error: %v", tc.address, err)
}
if bktName != tc.bktName {
t.Errorf("parsing %q: expected bucket %s, got %s", tc.address, tc.bktName, bktName)
}
if root != tc.root {
t.Errorf("parsing %q: expected root %s, got %s", tc.address, tc.root, root)
}
if id != tc.id {
t.Errorf("parsing %q: expected id %d, got %d", tc.address, tc.id, id)
}
}
}
func eq(a, b []int64) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
func TestCropResults(t *testing.T) {
cases := []struct {
a []int64
max int64
exp []int64
p int
q int
}{
{
a: []int64{},
max: 42,
exp: []int64{},
p: -1,
q: 0,
},
{
a: []int64{81, 27, 9, 3, 1},
max: 100,
exp: []int64{81, 27, 9, 3, 1},
p: 0,
q: 4,
},
{
a: []int64{81, 27, 9, 3, 1},
max: 50,
exp: []int64{27, 9, 3, 1},
p: 1,
q: 4,
},
{
a: []int64{25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
max: 23,
exp: []int64{23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4},
p: 2,
q: 21,
},
}
for _, tc := range cases {
actual, firstIndex, lastIndex := cropResults(tc.a, tc.max)
if !eq(actual, tc.exp) || firstIndex != tc.p || lastIndex != tc.q {
t.Errorf("cropResults(%v, %d) expected (%v, %d, %d), got (%v, %d, %d)",
tc.a, tc.max, tc.exp, tc.p, tc.q, actual, firstIndex, lastIndex)
}
}
}
func TestLinkID(t *testing.T) {
cases := []struct {
startAddr string
id int64
expAddr string
}{
{
startAddr: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e",
id: -1,
expAddr: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=",
},
{
startAddr: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e",
id: 23,
expAddr: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=23",
},
}
for _, tc := range cases {
u, _ := url.Parse(tc.startAddr)
actual := linkID(u, tc.id)
if actual != tc.expAddr {
t.Errorf("adding id param %d expected %s, got %s", tc.id, tc.expAddr, actual)
}
again, _ := url.Parse(tc.startAddr)
if again.String() != u.String() {
t.Errorf("linkID incorrectly mutated URL (expected %s, got %s)", u.String(), again.String())
}
}
}