istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/resource/version_test.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package resource 16 17 import ( 18 "fmt" 19 "strconv" 20 "testing" 21 ) 22 23 func TestCompareIstioVersion(t *testing.T) { 24 tcs := []struct { 25 a, b IstioVersion 26 result int 27 }{ 28 { 29 IstioVersion("1.4"), 30 IstioVersion("1.5"), 31 -1, 32 }, 33 { 34 IstioVersion("1.9.0"), 35 IstioVersion("1.10"), 36 -1, 37 }, 38 { 39 IstioVersion("1.8.0"), 40 IstioVersion("1.8.1"), 41 -1, 42 }, 43 { 44 IstioVersion("1.9.1"), 45 IstioVersion("1.9.1"), 46 0, 47 }, 48 { 49 IstioVersion("1.12"), 50 IstioVersion("1.3"), 51 1, 52 }, 53 { 54 IstioVersion(""), 55 IstioVersion(""), 56 0, 57 }, 58 { 59 IstioVersion(""), 60 IstioVersion("1.9"), 61 1, 62 }, 63 } 64 65 for _, tc := range tcs { 66 t.Run(fmt.Sprintf("compare version %s->%s", tc.a, tc.b), func(t *testing.T) { 67 r := tc.a.Compare(tc.b) 68 if r != tc.result { 69 t.Errorf("expected %d, got %d", tc.result, r) 70 } 71 }) 72 } 73 } 74 75 func TestMinimumIstioVersion(t *testing.T) { 76 tcs := []struct { 77 name string 78 versions IstioVersions 79 result IstioVersion 80 }{ 81 { 82 "two versions", 83 IstioVersions([]IstioVersion{ 84 "1.4", "1.5", 85 }), 86 IstioVersion("1.4"), 87 }, 88 { 89 "three versions", 90 IstioVersions([]IstioVersion{ 91 "1.9", "1.23", "1.10", 92 }), 93 IstioVersion("1.9"), 94 }, 95 { 96 "single version", 97 IstioVersions([]IstioVersion{ 98 "1.9", 99 }), 100 IstioVersion("1.9"), 101 }, 102 } 103 104 for _, tc := range tcs { 105 t.Run(tc.name, func(t *testing.T) { 106 min := tc.versions.Minimum() 107 if min != tc.result { 108 t.Errorf("expected %v, got %v", tc.result, min) 109 } 110 }) 111 } 112 } 113 114 func TestAtLeast(t *testing.T) { 115 tcs := []struct { 116 name string 117 versions RevVerMap 118 version IstioVersion 119 result bool 120 }{ 121 { 122 "not at least", 123 makeRevVerMap("1.4", "1.5"), 124 IstioVersion("1.8"), 125 false, 126 }, 127 { 128 "tied with minimum", 129 makeRevVerMap("1.4", "1.5"), 130 IstioVersion("1.4"), 131 true, 132 }, 133 { 134 "lower than minimum", 135 makeRevVerMap("1.4", "1.5"), 136 IstioVersion("1.3"), 137 true, 138 }, 139 { 140 "no versions", 141 makeRevVerMap(), 142 IstioVersion("1.8"), 143 true, 144 }, 145 } 146 147 for _, tc := range tcs { 148 t.Run(tc.name, func(t *testing.T) { 149 min := tc.versions.AtLeast(tc.version) 150 if min != tc.result { 151 t.Errorf("expected %v, got %v", tc.result, min) 152 } 153 }) 154 } 155 } 156 157 func makeRevVerMap(versions ...string) RevVerMap { 158 m := make(map[string]IstioVersion) 159 for i, v := range versions { 160 m[strconv.Itoa(i)] = IstioVersion(v) 161 } 162 return m 163 }