1 package k8s
2
3 import (
4 "testing"
5 )
6
7 func TestGetK8sVersion(t *testing.T) {
8 t.Run("Correctly parses a Version string", func(t *testing.T) {
9 versions := map[string][3]int{
10 "v1.8.4": {1, 8, 4},
11 "v2.7.1": {2, 7, 1},
12 "v2.0.1": {2, 0, 1},
13 "v1.9.0-beta.2": {1, 9, 0},
14 "v1.7.9+7f63532e4ff4f": {1, 7, 9},
15 }
16
17 for k, expectedVersion := range versions {
18 actualVersion, err := getK8sVersion(k)
19 if err != nil {
20 t.Fatalf("Error parsing string: %v", err)
21 }
22
23 if actualVersion != expectedVersion {
24 t.Fatalf("Expecting %s to be parsed into %v but got %v", k, expectedVersion, actualVersion)
25 }
26 }
27 })
28
29 t.Run("Returns error if Version string looks broken", func(t *testing.T) {
30 versions := []string{
31 "",
32 "1",
33 "1.8.",
34 "1.9-beta.2",
35 "v1.7+7f63532e4ff4f",
36 "Client Version: v1.8.4",
37 "Version.Info{Major:\"1\", Minor:\"8\", GitVersion:\"v1.8.4\", GitCommit:\"9befc2b8928a9426501d3bf62f72849d5cbcd5a3\", GitTreeState:\"clean\", BuildDate:\"2017-11-20T05:28:34Z\", GoVersion:\"go1.8.3\", Compiler:\"gc\", Platform:\"darwin/amd64\"}",
38 }
39
40 for _, invalidVersion := range versions {
41 _, err := getK8sVersion(invalidVersion)
42
43 if err == nil {
44 t.Fatalf("Expected error parsing string: %s", invalidVersion)
45 }
46 }
47 })
48 }
49
50 func TestIsCompatibleVersion(t *testing.T) {
51 t.Run("Success when compatible versions", func(t *testing.T) {
52 compatibleVersions := map[[3]int][3]int{
53 {1, 8, 4}: {1, 8, 4},
54 {1, 9, 2}: {1, 9, 4},
55 {1, 1, 1}: {1, 1, 1},
56 {1, 1, 1}: {2, 1, 2},
57 {1, 1, 1}: {1, 2, 1},
58 {1, 1, 1}: {1, 1, 2},
59 {1, 1, 1}: {100, 1, 2},
60 }
61
62 for e, a := range compatibleVersions {
63 if !isCompatibleVersion(e, a) {
64 t.Fatalf("Expected required version [%v] to be compatible with [%v] but it wasn't", e, a)
65 }
66 }
67 })
68
69 t.Run("Fail when incompatible versions", func(t *testing.T) {
70 inCompatibleVersions := map[[3]int][3]int{
71 {1, 8, 4}: {1, 7, 1},
72 {1, 9, 2}: {1, 9, 0},
73 {10, 10, 10}: {9, 10, 10},
74 {10, 10, 10}: {10, 9, 10},
75 {10, 10, 10}: {10, 10, 9},
76 {10, 10, 10}: {0, 10, 9},
77 }
78 for e, a := range inCompatibleVersions {
79 if isCompatibleVersion(e, a) {
80 t.Fatalf("Expected required version [%v] to NOT be compatible with [%v] but it was'", e, a)
81 }
82 }
83 })
84 }
85
View as plain text