|
| 1 | +/* |
| 2 | +Copyright 2026. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/go-logr/logr" |
| 23 | +) |
| 24 | + |
| 25 | +func TestDetectRHOSOVersion(t *testing.T) { |
| 26 | + logger := logr.Discard() |
| 27 | + |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + ocpVersion string |
| 31 | + expected string |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "Version below bound returns mapped RHOSO version", |
| 35 | + ocpVersion: "4.16", |
| 36 | + expected: "18.0", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "Version at bound returns mapped RHOSO version", |
| 40 | + ocpVersion: "4.21", |
| 41 | + expected: "18.0", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Version with patch at bound returns mapped RHOSO version", |
| 45 | + ocpVersion: "4.21.3", |
| 46 | + expected: "18.0", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "Version above all known bounds falls back to default", |
| 50 | + ocpVersion: "4.22", |
| 51 | + expected: OKPDefaultRHOSOVersion, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "Far future version falls back to default", |
| 55 | + ocpVersion: "5.0", |
| 56 | + expected: OKPDefaultRHOSOVersion, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Invalid version string falls back to default", |
| 60 | + ocpVersion: "not-a-version", |
| 61 | + expected: OKPDefaultRHOSOVersion, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "Empty version string falls back to default", |
| 65 | + ocpVersion: "", |
| 66 | + expected: OKPDefaultRHOSOVersion, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + result := detectRHOSOVersion(tt.ocpVersion, logger) |
| 73 | + if result != tt.expected { |
| 74 | + t.Errorf("detectRHOSOVersion(%q) = %q, want %q", tt.ocpVersion, result, tt.expected) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestDetectRHOSOVersionMapOrdering(t *testing.T) { |
| 81 | + logger := logr.Discard() |
| 82 | + |
| 83 | + // Save and restore the global map so this test is self-contained. |
| 84 | + original := ocpToRHOSOVersionMap |
| 85 | + t.Cleanup(func() { ocpToRHOSOVersionMap = original }) |
| 86 | + |
| 87 | + ocpToRHOSOVersionMap = []ocpVersionBound{ |
| 88 | + {"4.21", "18.0"}, |
| 89 | + {"5.99", "19.0"}, |
| 90 | + } |
| 91 | + |
| 92 | + tests := []struct { |
| 93 | + name string |
| 94 | + ocpVersion string |
| 95 | + expected string |
| 96 | + }{ |
| 97 | + { |
| 98 | + name: "Version matched by first entry", |
| 99 | + ocpVersion: "4.16", |
| 100 | + expected: "18.0", |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "Version at boundary of first entry", |
| 104 | + ocpVersion: "4.21", |
| 105 | + expected: "18.0", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "Version matched by second entry", |
| 109 | + ocpVersion: "5.0", |
| 110 | + expected: "19.0", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "Version at boundary of second entry", |
| 114 | + ocpVersion: "5.99", |
| 115 | + expected: "19.0", |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "Version above all bounds falls back to default", |
| 119 | + ocpVersion: "6.0", |
| 120 | + expected: OKPDefaultRHOSOVersion, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + result := detectRHOSOVersion(tt.ocpVersion, logger) |
| 127 | + if result != tt.expected { |
| 128 | + t.Errorf("detectRHOSOVersion(%q) = %q, want %q", tt.ocpVersion, result, tt.expected) |
| 129 | + } |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments