-
Notifications
You must be signed in to change notification settings - Fork 39.4k
/
version_test.go
598 lines (557 loc) · 16.6 KB
/
version_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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
Copyright 2016 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 util
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
"testing"
"time"
"github.com/pkg/errors"
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
)
func TestMain(m *testing.M) {
KubernetesReleaseVersion = kubernetesReleaseVersionTest
os.Exit(m.Run())
}
func kubernetesReleaseVersionTest(version string) (string, error) {
fetcher := func(string, time.Duration) (string, error) {
return constants.DefaultKubernetesPlaceholderVersion.String(), nil
}
return kubernetesReleaseVersion(version, fetcher)
}
func TestKubernetesReleaseVersion(t *testing.T) {
tests := []struct {
name string
input string
expectedOutput string
expectedError bool
}{
{
name: "empty input",
input: "",
expectedOutput: "",
expectedError: true,
},
{
name: "label as input",
input: "stable",
expectedOutput: normalizedBuildVersion(constants.DefaultKubernetesPlaceholderVersion.String()),
expectedError: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
output, err := KubernetesReleaseVersion(tc.input)
if (err != nil) != tc.expectedError {
t.Errorf("expected error: %v, got: %v, error: %v", tc.expectedError, err != nil, err)
}
if output != tc.expectedOutput {
t.Errorf("expected output: %s, got: %s", tc.expectedOutput, output)
}
})
}
}
func TestValidVersion(t *testing.T) {
validVersions := []string{
"v1.3.0",
"v1.4.0-alpha.0",
"v1.4.5",
"v1.4.0-beta.0",
"v2.0.0",
"v1.6.0-alpha.0.536+d60d9f3269288f",
"v1.5.0-alpha.0.1078+1044b6822497da-pull",
"v1.5.0-alpha.1.822+49b9e32fad9f32-pull-gke-gci",
"v1.6.1+coreos.0",
"1.7.1",
}
for _, s := range validVersions {
t.Run(s, func(t *testing.T) {
ver, err := kubernetesReleaseVersion(s, errorFetcher)
t.Log("Valid: ", s, ver, err)
if err != nil {
t.Errorf("kubernetesReleaseVersion unexpected error for version %q: %v", s, err)
}
if ver != s && ver != "v"+s {
t.Errorf("kubernetesReleaseVersion should return same valid version string. %q != %q", s, ver)
}
})
}
}
func TestInvalidVersion(t *testing.T) {
invalidVersions := []string{
"v1.3",
"1.4",
"b1.4.0",
"c1.4.5+git",
"something1.2",
}
for _, s := range invalidVersions {
t.Run(s, func(t *testing.T) {
ver, err := kubernetesReleaseVersion(s, errorFetcher)
t.Log("Invalid: ", s, ver, err)
if err == nil {
t.Errorf("kubernetesReleaseVersion error expected for version %q, but returned successfully", s)
}
if ver != "" {
t.Errorf("kubernetesReleaseVersion should return empty string in case of error. Returned %q for version %q", ver, s)
}
})
}
}
func TestValidConvenientForUserVersion(t *testing.T) {
validVersions := []string{
"1.4.0",
"1.4.5+git",
"1.6.1_coreos.0",
}
for _, s := range validVersions {
t.Run(s, func(t *testing.T) {
ver, err := kubernetesReleaseVersion(s, errorFetcher)
t.Log("Valid: ", s, ver, err)
if err != nil {
t.Errorf("kubernetesReleaseVersion unexpected error for version %q: %v", s, err)
}
if ver != "v"+s {
t.Errorf("kubernetesReleaseVersion should return semantic version string. %q vs. %q", s, ver)
}
})
}
}
func TestVersionFromNetwork(t *testing.T) {
type T struct {
Content string
Expected string
FetcherErrorExpected bool
ErrorExpected bool
}
currentVersion := normalizedBuildVersion(constants.CurrentKubernetesVersion.String())
cases := map[string]T{
"stable": {"stable-1", "v1.4.6", false, false}, // recursive pointer to stable-1
"stable-1": {"v1.4.6", "v1.4.6", false, false},
"stable-1.3": {"v1.3.10", "v1.3.10", false, false},
"latest": {"v1.6.0-alpha.0", "v1.6.0-alpha.0", false, false},
"latest-1.3": {"v1.3.11-beta.0", "v1.3.11-beta.0", false, false},
"latest-1.5": {"", currentVersion, true, false}, // fallback to currentVersion on fetcher error
"invalid-version": {"", "", false, true}, // invalid version cannot be parsed
}
for k, v := range cases {
t.Run(k, func(t *testing.T) {
fileFetcher := func(url string, timeout time.Duration) (string, error) {
key := strings.TrimSuffix(path.Base(url), ".txt")
res, found := cases[key]
if found {
if v.FetcherErrorExpected {
return "error", errors.New("expected error")
}
return res.Content, nil
}
return "Unknown test case key!", errors.New("unknown test case key")
}
ver, err := kubernetesReleaseVersion(k, fileFetcher)
t.Logf("Key: %q. Result: %q, Error: %v", k, ver, err)
switch {
case err != nil && !v.ErrorExpected:
t.Errorf("kubernetesReleaseVersion: unexpected error for %q. Error: %+v", k, err)
case err == nil && v.ErrorExpected:
t.Errorf("kubernetesReleaseVersion: error expected for key %q, but result is %q", k, ver)
case ver != v.Expected:
t.Errorf("kubernetesReleaseVersion: unexpected result for key %q. Expected: %q Actual: %q", k, v.Expected, ver)
}
})
}
}
func TestVersionToTag(t *testing.T) {
type T struct {
input string
expected string
}
cases := []T{
// NOP
{"", ""},
// Official releases
{"v1.0.0", "v1.0.0"},
// CI or custom builds
{"v10.1.2-alpha.1.100+0123456789abcdef+SOMETHING", "v10.1.2-alpha.1.100_0123456789abcdef_SOMETHING"},
// random and invalid input: should return safe value
{"v1,0!0+üñµ", "v1_0_0____"},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("input:%s/expected:%s", tc.input, tc.expected), func(t *testing.T) {
tag := KubernetesVersionToImageTag(tc.input)
t.Logf("kubernetesVersionToImageTag: Input: %q. Result: %q. Expected: %q", tc.input, tag, tc.expected)
if tag != tc.expected {
t.Errorf("failed KubernetesVersionToImageTag: Input: %q. Result: %q. Expected: %q", tc.input, tag, tc.expected)
}
})
}
}
func TestSplitVersion(t *testing.T) {
type T struct {
input string
bucket string
label string
valid bool
}
cases := []T{
// Release area
{"v1.7.0", "https://dl.k8s.io/release", "v1.7.0", true},
{"v1.8.0-alpha.2.1231+afabd012389d53a", "https://dl.k8s.io/release", "v1.8.0-alpha.2.1231+afabd012389d53a", true},
{"release/v1.7.0", "https://dl.k8s.io/release", "v1.7.0", true},
{"release/latest-1.7", "https://dl.k8s.io/release", "latest-1.7", true},
// CI builds area
{"ci/latest", "https://storage.googleapis.com/k8s-release-dev/ci", "latest", true},
{"ci/latest-1.7", "https://storage.googleapis.com/k8s-release-dev/ci", "latest-1.7", true},
// unknown label in default (release) area: splitVersion validate only areas.
{"unknown-1", "https://dl.k8s.io/release", "unknown-1", true},
// unknown area, not valid input.
{"unknown/latest-1", "", "", false},
// invalid input
{"", "", "", false},
{"ci/", "", "", false},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("input:%s/label:%s", tc.input, tc.label), func(t *testing.T) {
bucket, label, err := splitVersion(tc.input)
switch {
case err != nil && tc.valid:
t.Errorf("splitVersion: unexpected error for %q. Error: %v", tc.input, err)
case err == nil && !tc.valid:
t.Errorf("splitVersion: error expected for key %q, but result is %q, %q", tc.input, bucket, label)
case bucket != tc.bucket:
t.Errorf("splitVersion: unexpected bucket result for key %q. Expected: %q Actual: %q", tc.input, tc.bucket, bucket)
case label != tc.label:
t.Errorf("splitVersion: unexpected label result for key %q. Expected: %q Actual: %q", tc.input, tc.label, label)
}
})
}
}
func TestKubernetesIsCIVersion(t *testing.T) {
type T struct {
input string
expected bool
}
cases := []T{
{"", false},
// Official releases
{"v1.0.0", false},
{"release/v1.0.0", false},
// CI builds
{"ci/latest-1", true},
{"ci/v1.9.0-alpha.1.123+acbcbfd53bfa0a", true},
{"ci/", false},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("input:%s/expected:%t", tc.input, tc.expected), func(t *testing.T) {
result := KubernetesIsCIVersion(tc.input)
t.Logf("kubernetesIsCIVersion: Input: %q. Result: %v. Expected: %v", tc.input, result, tc.expected)
if result != tc.expected {
t.Errorf("failed KubernetesIsCIVersion: Input: %q. Result: %v. Expected: %v", tc.input, result, tc.expected)
}
})
}
}
// Validate kubernetesReleaseVersion but with bucket prefixes
func TestCIBuildVersion(t *testing.T) {
type T struct {
input string
expected string
valid bool
}
cases := []T{
// Official releases
{"v1.7.0", "v1.7.0", true},
{"release/v1.8.0", "v1.8.0", true},
{"1.4.0-beta.0", "v1.4.0-beta.0", true},
{"release/0invalid", "", false},
// CI or custom builds
{"ci/v1.9.0-alpha.1.123+acbcbfd53bfa0a", "v1.9.0-alpha.1.123+acbcbfd53bfa0a", true},
{"ci/1.9.0-alpha.1.123+acbcbfd53bfa0a", "v1.9.0-alpha.1.123+acbcbfd53bfa0a", true},
{"ci/0invalid", "", false},
{"0invalid", "", false},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("input:%s/expected:%s", tc.input, tc.expected), func(t *testing.T) {
fileFetcher := func(url string, timeout time.Duration) (string, error) {
if tc.valid {
return tc.expected, nil
}
return "Unknown test case key!", errors.New("unknown test case key")
}
ver, err := kubernetesReleaseVersion(tc.input, fileFetcher)
t.Logf("Input: %q. Result: %q, Error: %v", tc.input, ver, err)
switch {
case err != nil && tc.valid:
t.Errorf("kubernetesReleaseVersion: unexpected error for input %q. Error: %v", tc.input, err)
case err == nil && !tc.valid:
t.Errorf("kubernetesReleaseVersion: error expected for input %q, but result is %q", tc.input, ver)
case ver != tc.expected:
t.Errorf("kubernetesReleaseVersion: unexpected result for input %q. Expected: %q Actual: %q", tc.input, tc.expected, ver)
}
})
}
}
func TestNormalizedBuildVersionVersion(t *testing.T) {
type T struct {
input string
expected string
}
cases := []T{
{"v1.7.0", "v1.7.0"},
{"v1.8.0-alpha.2.1231+afabd012389d53a", "v1.8.0-alpha.2.1231+afabd012389d53a"},
{"1.7.0", "v1.7.0"},
{"unknown-1", ""},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("input:%s/expected:%s", tc.input, tc.expected), func(t *testing.T) {
output := normalizedBuildVersion(tc.input)
if output != tc.expected {
t.Errorf("normalizedBuildVersion: unexpected output %q for input %q. Expected: %q", output, tc.input, tc.expected)
}
})
}
}
func TestKubeadmVersion(t *testing.T) {
type T struct {
name string
input string
output string
outputError bool
parsingError bool
}
cases := []T{
{
name: "valid version with label and metadata",
input: "v1.8.0-alpha.2.1231+afabd012389d53a",
output: "v1.8.0-alpha.2",
},
{
name: "valid version with label and extra metadata",
input: "v1.8.0-alpha.2.1231+afabd012389d53a.extra",
output: "v1.8.0-alpha.2",
},
{
name: "valid patch version with label and extra metadata",
input: "v1.11.3-beta.0.38+135cc4c1f47994",
output: "v1.11.2",
},
{
name: "valid version with label extra",
input: "v1.8.0-alpha.2.1231",
output: "v1.8.0-alpha.2",
},
{
name: "valid patch version with label",
input: "v1.9.11-beta.0",
output: "v1.9.10",
},
{
name: "handle version with partial label",
input: "v1.8.0-alpha",
output: "v1.8.0-alpha.0",
},
{
name: "handle version missing 'v'",
input: "1.11.0",
output: "v1.11.0",
},
{
name: "valid version without label and metadata",
input: "v1.8.0",
output: "v1.8.0",
},
{
name: "valid patch version without label and metadata",
input: "v1.8.2",
output: "v1.8.2",
},
{
name: "invalid version",
input: "foo",
parsingError: true,
},
{
name: "invalid version with stray dash",
input: "v1.9.11-",
parsingError: true,
},
{
name: "invalid version without patch release",
input: "v1.9",
parsingError: true,
},
{
name: "invalid version with label and stray dot",
input: "v1.8.0-alpha.2.",
parsingError: true,
},
{
name: "invalid version with label and metadata",
input: "v1.8.0-alpha.2.1231+afabd012389d53a",
output: "v1.8.0-alpha.3",
outputError: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
output, err := kubeadmVersion(tc.input)
if (err != nil) != tc.parsingError {
t.Fatalf("expected error: %v, got: %v", tc.parsingError, err != nil)
}
if (output != tc.output) != tc.outputError {
t.Fatalf("expected output: %s, got: %s, for input: %s", tc.output, output, tc.input)
}
})
}
}
func TestValidateStableVersion(t *testing.T) {
type T struct {
name string
remoteVersion string
clientVersion string
output string
expectedError bool
}
cases := []T{
{
name: "valid: remote version is newer; return stable label [1]",
remoteVersion: "v1.12.0",
clientVersion: "v1.11.0",
output: "stable-1.11",
},
{
name: "valid: remote version is newer; return stable label [2]",
remoteVersion: "v2.0.0",
clientVersion: "v1.11.0",
output: "stable-1.11",
},
{
name: "valid: remote version is newer; return stable label [3]",
remoteVersion: "v2.1.5",
clientVersion: "v1.11.5",
output: "stable-1.11",
},
{
name: "valid: return the remote version as it is part of the same release",
remoteVersion: "v1.11.5",
clientVersion: "v1.11.0",
output: "v1.11.5",
},
{
name: "valid: return the same version",
remoteVersion: "v1.11.0",
clientVersion: "v1.11.0",
output: "v1.11.0",
},
{
name: "invalid: client version is empty",
remoteVersion: "v1.12.1",
clientVersion: "",
expectedError: true,
},
{
name: "invalid: error parsing the remote version",
remoteVersion: "invalid-version",
clientVersion: "v1.12.0",
expectedError: true,
},
{
name: "invalid: error parsing the client version",
remoteVersion: "v1.12.0",
clientVersion: "invalid-version",
expectedError: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
output, err := validateStableVersion(tc.remoteVersion, tc.clientVersion)
if (err != nil) != tc.expectedError {
t.Fatalf("expected error: %v, got: %v", tc.expectedError, err != nil)
}
if output != tc.output {
t.Fatalf("expected output: %s, got: %s", tc.output, output)
}
})
}
}
func errorFetcher(url string, timeout time.Duration) (string, error) {
return "should not make internet calls", errors.Errorf("should not make internet calls, tried to request url: %s", url)
}
func TestFetchFromURL(t *testing.T) {
tests := []struct {
name string
url string
expected string
timeout time.Duration
code int
body string
expectErr bool
}{
{
name: "normal success",
url: "/normal",
code: http.StatusOK,
body: "normal response",
expected: "normal response",
expectErr: false,
},
{
name: "HTTP error status",
url: "/error",
code: http.StatusBadRequest,
body: "bad request",
expected: "bad request",
expectErr: true,
},
{
name: "Request timeout",
url: "/timeout",
timeout: time.Millisecond * 50,
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tt.code != 0 {
w.WriteHeader(tt.code)
}
if tt.body != "" {
if _, err := w.Write([]byte(tt.body)); err != nil {
t.Error("Write body failed.")
}
}
if tt.timeout == time.Millisecond*50 {
time.Sleep(time.Millisecond * 200)
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte("Delayed response")); err != nil {
t.Error("Write body failed.")
}
}
})
ts := httptest.NewServer(handler)
defer ts.Close()
url := ts.URL + tt.url
result, err := fetchFromURL(url, tt.timeout)
if (err != nil) != tt.expectErr {
t.Errorf("expected error: %v, got: %v", tt.expectErr, err)
}
if tt.expected != result {
t.Errorf("expected result: %q, got: %q", tt.expected, result)
}
})
}
}