github.com/giantswarm/apiextensions/v2@v2.6.2/pkg/apis/provider/v1alpha1/status_funcs_test.go (about) 1 package v1alpha1 2 3 import ( 4 "reflect" 5 "testing" 6 "time" 7 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 ) 10 11 func Test_Provider_Status_LatestVersion(t *testing.T) { 12 testCases := []struct { 13 Name string 14 StatusCluster StatusCluster 15 ExpectedSemver string 16 }{ 17 { 18 Name: "case 0", 19 StatusCluster: StatusCluster{ 20 Versions: []StatusClusterVersion{}, 21 }, 22 ExpectedSemver: "", 23 }, 24 { 25 Name: "case 1", 26 StatusCluster: StatusCluster{ 27 Versions: []StatusClusterVersion{ 28 { 29 Date: metav1.Time{Time: time.Unix(10, 0)}, 30 Semver: "1.0.0", 31 }, 32 }, 33 }, 34 ExpectedSemver: "1.0.0", 35 }, 36 { 37 Name: "case 2", 38 StatusCluster: StatusCluster{ 39 Versions: []StatusClusterVersion{ 40 { 41 Date: metav1.Time{Time: time.Unix(10, 0)}, 42 Semver: "1.0.0", 43 }, 44 { 45 Date: metav1.Time{Time: time.Unix(20, 0)}, 46 Semver: "2.0.0", 47 }, 48 }, 49 }, 50 ExpectedSemver: "2.0.0", 51 }, 52 { 53 Name: "case 3", 54 StatusCluster: StatusCluster{ 55 Versions: []StatusClusterVersion{ 56 { 57 Date: metav1.Time{Time: time.Unix(10, 0)}, 58 Semver: "1.0.0", 59 }, 60 { 61 Date: metav1.Time{Time: time.Unix(20, 0)}, 62 Semver: "2.0.0", 63 }, 64 { 65 Date: metav1.Time{Time: time.Unix(30, 0)}, 66 Semver: "3.0.0", 67 }, 68 }, 69 }, 70 ExpectedSemver: "3.0.0", 71 }, 72 { 73 Name: "case 4", 74 StatusCluster: StatusCluster{ 75 Versions: []StatusClusterVersion{ 76 { 77 Date: metav1.Time{Time: time.Unix(20, 0)}, 78 Semver: "2.0.0", 79 }, 80 { 81 Date: metav1.Time{Time: time.Unix(30, 0)}, 82 Semver: "3.0.0", 83 }, 84 { 85 Date: metav1.Time{Time: time.Unix(10, 0)}, 86 Semver: "1.0.0", 87 }, 88 }, 89 }, 90 ExpectedSemver: "3.0.0", 91 }, 92 { 93 Name: "case 5", 94 StatusCluster: StatusCluster{ 95 Versions: []StatusClusterVersion{ 96 { 97 LastTransitionTime: metav1.Time{ 98 Time: time.Unix(20, 0), 99 }, 100 Date: metav1.Time{}, 101 Semver: "2.0.0", 102 }, 103 { 104 LastTransitionTime: metav1.Time{ 105 Time: time.Unix(30, 0), 106 }, 107 Date: metav1.Time{}, 108 Semver: "3.0.0", 109 }, 110 { 111 LastTransitionTime: metav1.Time{ 112 Time: time.Unix(10, 0), 113 }, 114 Date: metav1.Time{}, 115 Semver: "1.0.0", 116 }, 117 }, 118 }, 119 ExpectedSemver: "3.0.0", 120 }, 121 } 122 123 for _, tc := range testCases { 124 semver := tc.StatusCluster.LatestVersion() 125 126 if semver != tc.ExpectedSemver { 127 t.Fatalf("expected %#v got %#v", tc.ExpectedSemver, semver) 128 } 129 } 130 } 131 132 func Test_Provider_Status_withCondition(t *testing.T) { 133 testTime := time.Unix(20, 0) 134 135 testCases := []struct { 136 Name string 137 Conditions []StatusClusterCondition 138 Search string 139 Replace string 140 Status string 141 ExpectedConditions []StatusClusterCondition 142 }{ 143 { 144 Name: "case 0", 145 Conditions: []StatusClusterCondition{}, 146 Search: StatusClusterTypeCreating, 147 Replace: StatusClusterTypeCreated, 148 Status: StatusClusterStatusTrue, 149 ExpectedConditions: []StatusClusterCondition{ 150 { 151 LastTransitionTime: metav1.Time{Time: testTime}, 152 Status: StatusClusterStatusTrue, 153 Type: StatusClusterTypeCreated, 154 }, 155 }, 156 }, 157 { 158 Name: "case 1", 159 Conditions: []StatusClusterCondition{ 160 { 161 LastTransitionTime: metav1.Time{Time: testTime}, 162 Status: StatusClusterStatusTrue, 163 Type: StatusClusterTypeCreating, 164 }, 165 }, 166 Search: StatusClusterTypeCreating, 167 Replace: StatusClusterTypeCreated, 168 Status: StatusClusterStatusTrue, 169 ExpectedConditions: []StatusClusterCondition{ 170 { 171 LastTransitionTime: metav1.Time{Time: testTime}, 172 Status: StatusClusterStatusTrue, 173 Type: StatusClusterTypeCreated, 174 }, 175 }, 176 }, 177 } 178 179 for _, tc := range testCases { 180 conditions := withCondition(tc.Conditions, tc.Search, tc.Replace, tc.Status, testTime) 181 182 if !reflect.DeepEqual(conditions, tc.ExpectedConditions) { 183 t.Fatalf("%s: expected %#v got %#v", tc.Name, tc.ExpectedConditions, conditions) 184 } 185 } 186 } 187 188 func Test_Provider_Status_withVersion(t *testing.T) { 189 testCases := []struct { 190 Name string 191 Versions []StatusClusterVersion 192 Version StatusClusterVersion 193 Limit int 194 ExpectedVersions []StatusClusterVersion 195 }{ 196 { 197 Name: "case 0: list with zero items results in a list with one item", 198 Versions: []StatusClusterVersion{}, 199 Version: StatusClusterVersion{ 200 Date: metav1.Time{Time: time.Unix(10, 0)}, 201 Semver: "1.0.0", 202 }, 203 Limit: 3, 204 ExpectedVersions: []StatusClusterVersion{ 205 { 206 Date: metav1.Time{Time: time.Unix(10, 0)}, 207 Semver: "1.0.0", 208 }, 209 }, 210 }, 211 { 212 Name: "case 1: list with one item results in a list with two items", 213 Versions: []StatusClusterVersion{ 214 { 215 Date: metav1.Time{Time: time.Unix(10, 0)}, 216 Semver: "1.0.0", 217 }, 218 }, 219 Version: StatusClusterVersion{ 220 Date: metav1.Time{Time: time.Unix(20, 0)}, 221 Semver: "1.1.0", 222 }, 223 Limit: 3, 224 ExpectedVersions: []StatusClusterVersion{ 225 { 226 Date: metav1.Time{Time: time.Unix(10, 0)}, 227 Semver: "1.0.0", 228 }, 229 { 230 Date: metav1.Time{Time: time.Unix(20, 0)}, 231 Semver: "1.1.0", 232 }, 233 }, 234 }, 235 { 236 Name: "case 2: list with two items results in a list with three items", 237 Versions: []StatusClusterVersion{ 238 { 239 Date: metav1.Time{Time: time.Unix(10, 0)}, 240 Semver: "1.0.0", 241 }, 242 { 243 Date: metav1.Time{Time: time.Unix(20, 0)}, 244 Semver: "1.1.0", 245 }, 246 }, 247 Version: StatusClusterVersion{ 248 Date: metav1.Time{Time: time.Unix(30, 0)}, 249 Semver: "1.5.0", 250 }, 251 Limit: 3, 252 ExpectedVersions: []StatusClusterVersion{ 253 { 254 Date: metav1.Time{Time: time.Unix(10, 0)}, 255 Semver: "1.0.0", 256 }, 257 { 258 Date: metav1.Time{Time: time.Unix(20, 0)}, 259 Semver: "1.1.0", 260 }, 261 { 262 Date: metav1.Time{Time: time.Unix(30, 0)}, 263 Semver: "1.5.0", 264 }, 265 }, 266 }, 267 { 268 Name: "case 3: list with three items results in a list with three items due to limit", 269 Versions: []StatusClusterVersion{ 270 { 271 Date: metav1.Time{Time: time.Unix(10, 0)}, 272 Semver: "1.0.0", 273 }, 274 { 275 Date: metav1.Time{Time: time.Unix(20, 0)}, 276 Semver: "1.1.0", 277 }, 278 { 279 Date: metav1.Time{Time: time.Unix(30, 0)}, 280 Semver: "1.5.0", 281 }, 282 }, 283 Version: StatusClusterVersion{ 284 Date: metav1.Time{Time: time.Unix(40, 0)}, 285 Semver: "3.0.0", 286 }, 287 Limit: 3, 288 ExpectedVersions: []StatusClusterVersion{ 289 { 290 Date: metav1.Time{Time: time.Unix(20, 0)}, 291 Semver: "1.1.0", 292 }, 293 { 294 Date: metav1.Time{Time: time.Unix(30, 0)}, 295 Semver: "1.5.0", 296 }, 297 { 298 Date: metav1.Time{Time: time.Unix(40, 0)}, 299 Semver: "3.0.0", 300 }, 301 }, 302 }, 303 { 304 Name: "case 4: list with five items results in a list with three items due to limit", 305 Versions: []StatusClusterVersion{ 306 { 307 Date: metav1.Time{Time: time.Unix(10, 0)}, 308 Semver: "1.0.0", 309 }, 310 { 311 Date: metav1.Time{Time: time.Unix(20, 0)}, 312 Semver: "1.1.0", 313 }, 314 { 315 Date: metav1.Time{Time: time.Unix(30, 0)}, 316 Semver: "1.5.0", 317 }, 318 { 319 Date: metav1.Time{Time: time.Unix(40, 0)}, 320 Semver: "3.0.0", 321 }, 322 { 323 Date: metav1.Time{Time: time.Unix(50, 0)}, 324 Semver: "3.2.0", 325 }, 326 }, 327 Version: StatusClusterVersion{ 328 Date: metav1.Time{Time: time.Unix(60, 0)}, 329 Semver: "3.3.0", 330 }, 331 Limit: 3, 332 ExpectedVersions: []StatusClusterVersion{ 333 { 334 Date: metav1.Time{Time: time.Unix(40, 0)}, 335 Semver: "3.0.0", 336 }, 337 { 338 Date: metav1.Time{Time: time.Unix(50, 0)}, 339 Semver: "3.2.0", 340 }, 341 { 342 Date: metav1.Time{Time: time.Unix(60, 0)}, 343 Semver: "3.3.0", 344 }, 345 }, 346 }, 347 { 348 Name: "case 5: same as 4 but checking items are ordered by date before cutting off", 349 Versions: []StatusClusterVersion{ 350 { 351 Date: metav1.Time{Time: time.Unix(40, 0)}, 352 Semver: "3.0.0", 353 }, 354 { 355 Date: metav1.Time{Time: time.Unix(20, 0)}, 356 Semver: "1.1.0", 357 }, 358 { 359 Date: metav1.Time{Time: time.Unix(10, 0)}, 360 Semver: "1.0.0", 361 }, 362 { 363 Date: metav1.Time{Time: time.Unix(50, 0)}, 364 Semver: "3.2.0", 365 }, 366 { 367 Date: metav1.Time{Time: time.Unix(30, 0)}, 368 Semver: "1.5.0", 369 }, 370 }, 371 Version: StatusClusterVersion{ 372 Date: metav1.Time{Time: time.Unix(60, 0)}, 373 Semver: "3.3.0", 374 }, 375 Limit: 3, 376 ExpectedVersions: []StatusClusterVersion{ 377 { 378 Date: metav1.Time{Time: time.Unix(40, 0)}, 379 Semver: "3.0.0", 380 }, 381 { 382 Date: metav1.Time{Time: time.Unix(50, 0)}, 383 Semver: "3.2.0", 384 }, 385 { 386 Date: metav1.Time{Time: time.Unix(60, 0)}, 387 Semver: "3.3.0", 388 }, 389 }, 390 }, 391 { 392 Name: "case 6: list with one item results in a list with one item in case the version already exists", 393 Versions: []StatusClusterVersion{ 394 { 395 Date: metav1.Time{Time: time.Unix(10, 0)}, 396 Semver: "1.0.0", 397 }, 398 }, 399 Version: StatusClusterVersion{ 400 Date: metav1.Time{Time: time.Unix(20, 0)}, 401 Semver: "1.0.0", 402 }, 403 Limit: 3, 404 ExpectedVersions: []StatusClusterVersion{ 405 { 406 Date: metav1.Time{Time: time.Unix(10, 0)}, 407 Semver: "1.0.0", 408 }, 409 }, 410 }, 411 } 412 413 for _, tc := range testCases { 414 t.Run(tc.Name, func(t *testing.T) { 415 versions := withVersion(tc.Versions, tc.Version, tc.Limit) 416 417 if !reflect.DeepEqual(versions, tc.ExpectedVersions) { 418 t.Fatalf("expected %#v got %#v", tc.ExpectedVersions, versions) 419 } 420 }) 421 } 422 }