github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/runtimeversion/runtimeversion_test.go (about) 1 package runtimeversion 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/kyma-project/kyma-environment-broker/internal" 9 "github.com/sirupsen/logrus" 10 "github.com/stretchr/testify/require" 11 coreV1 "k8s.io/api/core/v1" 12 metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 "k8s.io/apimachinery/pkg/runtime" 14 "sigs.k8s.io/controller-runtime/pkg/client/fake" 15 ) 16 17 func Test_RuntimeVersionConfigurator_ForProvisioning_FromParameters(t *testing.T) { 18 t.Run("should return version from ProvisioningParameters when version provided", func(t *testing.T) { 19 // given 20 runtimeVer := "1.1.1" 21 rvc := NewRuntimeVersionConfigurator("not-relevant", &AccountVersionMapping{}, nil) 22 23 // when 24 ver, err := rvc.ForProvisioning( 25 internal.Operation{ 26 ProvisioningParameters: internal.ProvisioningParameters{Parameters: internal.ProvisioningParametersDTO{KymaVersion: runtimeVer}}, 27 }) 28 29 // then 30 require.NoError(t, err) 31 require.Equal(t, runtimeVer, ver.Version) 32 require.Equal(t, 1, ver.MajorVersion) 33 require.Equal(t, internal.Parameters, ver.Origin) 34 }) 35 t.Run("should return version from Defaults when version not provided", func(t *testing.T) { 36 // given 37 runtimeVer := "1.1.1" 38 operation := internal.Operation{ 39 ProvisioningParameters: internal.ProvisioningParameters{}, 40 } 41 rvc := NewRuntimeVersionConfigurator(runtimeVer, fixAccountVersionMapping(t, map[string]string{}), nil) 42 43 // when 44 ver, err := rvc.ForProvisioning(operation) 45 46 // then 47 require.NoError(t, err) 48 require.Equal(t, runtimeVer, ver.Version) 49 require.Equal(t, 1, ver.MajorVersion) 50 require.Equal(t, internal.Defaults, ver.Origin) 51 }) 52 t.Run("should return version from GlobalAccount mapping when only GlobalAccount mapping provided", func(t *testing.T) { 53 // given 54 runtimeVer := "1.12" 55 operation := internal.Operation{ 56 ProvisioningParameters: internal.ProvisioningParameters{ 57 ErsContext: internal.ERSContext{GlobalAccountID: fixGlobalAccountID, SubAccountID: versionForSA}, 58 }, 59 } 60 rvc := NewRuntimeVersionConfigurator(runtimeVer, fixAccountVersionMapping(t, map[string]string{ 61 fmt.Sprintf("%s%s", globalAccountPrefix, fixGlobalAccountID): versionForGA, 62 }), nil) 63 64 // when 65 ver, err := rvc.ForProvisioning(operation) 66 67 // then 68 require.NoError(t, err) 69 require.Equal(t, versionForGA, ver.Version) 70 require.Equal(t, 1, ver.MajorVersion) 71 require.Equal(t, internal.AccountMapping, ver.Origin) 72 }) 73 t.Run("should return version from SubAccount mapping when both GA and SA mapping provided", func(t *testing.T) { 74 // given 75 runtimeVer := "1.12" 76 operation := internal.Operation{ 77 ProvisioningParameters: internal.ProvisioningParameters{ 78 ErsContext: internal.ERSContext{GlobalAccountID: fixGlobalAccountID, 79 SubAccountID: fixSubAccountID}, 80 }, 81 } 82 rvc := NewRuntimeVersionConfigurator(runtimeVer, fixAccountVersionMapping(t, map[string]string{ 83 fmt.Sprintf("%s%s", globalAccountPrefix, fixGlobalAccountID): versionForGA, 84 fmt.Sprintf("%s%s", subaccountPrefix, fixSubAccountID): versionForSA, 85 }), nil) 86 87 // when 88 ver, err := rvc.ForProvisioning(operation) 89 90 // then 91 require.NoError(t, err) 92 require.Equal(t, versionForSA, ver.Version) 93 require.Equal(t, 1, ver.MajorVersion) 94 require.Equal(t, internal.AccountMapping, ver.Origin) 95 }) 96 t.Run("should return Kyma Version from ProvisioningParameters even when version provided", func(t *testing.T) { 97 // given 98 runtimeVer := "1.0.0" 99 100 operation := internal.Operation{ 101 ProvisioningParameters: internal.ProvisioningParameters{ 102 Parameters: internal.ProvisioningParametersDTO{KymaVersion: runtimeVer}, 103 }, 104 } 105 106 rvc := NewRuntimeVersionConfigurator(runtimeVer, fixAccountVersionMapping(t, map[string]string{}), nil) 107 108 // when 109 ver, err := rvc.ForProvisioning(operation) 110 111 // then 112 require.NoError(t, err) 113 require.Equal(t, runtimeVer, ver.Version) 114 require.Equal(t, 1, ver.MajorVersion) 115 }) 116 t.Run("should panic if version not provided", func(t *testing.T) { 117 118 defer func() { 119 if r := recover(); r == nil { 120 t.Errorf("Panic expected") 121 } 122 }() 123 124 // given 125 runtimeVer := "" 126 127 operation := internal.Operation{ 128 ProvisioningParameters: internal.ProvisioningParameters{ 129 Parameters: internal.ProvisioningParametersDTO{KymaVersion: runtimeVer}, 130 }, 131 } 132 133 rvc := NewRuntimeVersionConfigurator(runtimeVer, fixAccountVersionMapping(t, map[string]string{}), nil) 134 135 // when 136 ver, err := rvc.ForProvisioning(operation) 137 138 // then 139 require.NoError(t, err) 140 require.Equal(t, 2, ver.MajorVersion) 141 }) 142 t.Run("should return SA version when both GA and SA mapping provided", func(t *testing.T) { 143 // given 144 runtimeVer := "1.0.0" 145 146 operation := internal.Operation{ 147 ProvisioningParameters: internal.ProvisioningParameters{ 148 ErsContext: internal.ERSContext{GlobalAccountID: fixGlobalAccountID, 149 SubAccountID: fixSubAccountID}, 150 }, 151 } 152 153 rvc := NewRuntimeVersionConfigurator(runtimeVer, fixAccountVersionMapping(t, map[string]string{ 154 fmt.Sprintf("%s%s", globalAccountPrefix, fixGlobalAccountID): versionForGA, 155 fmt.Sprintf("%s%s", subaccountPrefix, fixSubAccountID): versionForSA, 156 }), nil) 157 158 // when 159 ver, err := rvc.ForProvisioning(operation) 160 161 // then 162 require.NoError(t, err) 163 require.Equal(t, versionForSA, ver.Version) 164 require.Equal(t, 1, ver.MajorVersion) 165 }) 166 t.Run("should return custom version from ProvisioningParameters and default Kyma major version", func(t *testing.T) { 167 // given 168 runtimeVer := "1.24.5" 169 customVer := "PR-123" 170 operation := internal.Operation{ 171 ProvisioningParameters: internal.ProvisioningParameters{ 172 Parameters: internal.ProvisioningParametersDTO{ 173 KymaVersion: customVer, 174 }, 175 }, 176 } 177 178 rvc := NewRuntimeVersionConfigurator(runtimeVer, &AccountVersionMapping{}, nil) 179 180 // when 181 ver, err := rvc.ForProvisioning(operation) 182 183 // then 184 require.NoError(t, err) 185 require.Equal(t, customVer, ver.Version) 186 require.Equal(t, 1, ver.MajorVersion) 187 }) 188 t.Run("should return custom version from ProvisioningParameters and default Kyma major version", func(t *testing.T) { 189 // given 190 runtimeVer := "1.24.5" 191 customVer := "PR-123" 192 operation := internal.Operation{ 193 ProvisioningParameters: internal.ProvisioningParameters{ 194 Parameters: internal.ProvisioningParametersDTO{ 195 KymaVersion: customVer, 196 }, 197 }, 198 } 199 200 rvc := NewRuntimeVersionConfigurator(runtimeVer, &AccountVersionMapping{}, nil) 201 202 // when 203 ver, err := rvc.ForProvisioning(operation) 204 205 // then 206 require.NoError(t, err) 207 require.Equal(t, customVer, ver.Version) 208 require.Equal(t, 1, ver.MajorVersion) 209 }) 210 t.Run("should return custom version from GlobalAccount mapping and default Kyma major version when only GlobalAccount mapping provided", func(t *testing.T) { 211 // given 212 runtimeVer := "1.24.5" 213 customVerGA := "PR-123" 214 operation := internal.Operation{ 215 ProvisioningParameters: internal.ProvisioningParameters{ 216 ErsContext: internal.ERSContext{GlobalAccountID: fixGlobalAccountID, SubAccountID: versionForSA}, 217 }, 218 Type: internal.OperationTypeProvision, 219 } 220 221 rvc := NewRuntimeVersionConfigurator(runtimeVer, fixAccountVersionMapping(t, map[string]string{ 222 fmt.Sprintf("%s%s", globalAccountPrefix, fixGlobalAccountID): customVerGA, 223 }), nil) 224 225 // when 226 ver, err := rvc.ForProvisioning(operation) 227 228 // then 229 require.NoError(t, err) 230 require.Equal(t, customVerGA, ver.Version) 231 require.Equal(t, 1, ver.MajorVersion) 232 require.Equal(t, internal.AccountMapping, ver.Origin) 233 }) 234 t.Run("should return version from SubAccount mapping and default Kyma major version when both GA and SA mapping provided", func(t *testing.T) { 235 // given 236 runtimeVer := "1.24.5" 237 customVerGA := "PR-123" 238 customVerSA := "PR-456" 239 operation := internal.Operation{ 240 ProvisioningParameters: internal.ProvisioningParameters{ 241 ErsContext: internal.ERSContext{GlobalAccountID: fixGlobalAccountID, 242 SubAccountID: fixSubAccountID}, 243 }, 244 } 245 rvc := NewRuntimeVersionConfigurator(runtimeVer, fixAccountVersionMapping(t, map[string]string{ 246 fmt.Sprintf("%s%s", globalAccountPrefix, fixGlobalAccountID): customVerGA, 247 fmt.Sprintf("%s%s", subaccountPrefix, fixSubAccountID): customVerSA, 248 }), nil) 249 250 // when 251 ver, err := rvc.ForProvisioning(operation) 252 253 // then 254 require.NoError(t, err) 255 require.Equal(t, customVerSA, ver.Version) 256 require.Equal(t, 1, ver.MajorVersion) 257 require.Equal(t, internal.AccountMapping, ver.Origin) 258 }) 259 t.Run("should return version and default Kyma major version when only custom GlobalAccount mapping provided", func(t *testing.T) { 260 // given 261 runtimeVer := "1.24.5" 262 customVerGA := "PR-123" 263 operation := internal.Operation{ 264 ProvisioningParameters: internal.ProvisioningParameters{ 265 ErsContext: internal.ERSContext{GlobalAccountID: fixGlobalAccountID, SubAccountID: versionForSA}, 266 }, 267 } 268 rvc := NewRuntimeVersionConfigurator(runtimeVer, fixAccountVersionMapping(t, map[string]string{ 269 fmt.Sprintf("%s%s", globalAccountPrefix, fixGlobalAccountID): customVerGA, 270 }), nil) 271 272 // when 273 ver, err := rvc.ForProvisioning(operation) 274 275 // then 276 require.NoError(t, err) 277 require.Equal(t, 1, ver.MajorVersion) 278 require.Equal(t, internal.AccountMapping, ver.Origin) 279 }) 280 t.Run("should return default version and Kyma major version when both GA and SA mapping provided", func(t *testing.T) { 281 // given 282 runtimeVer := "1.24.5" 283 customVerGA := "PR-123" 284 customVerSA := "PR-456" 285 operation := internal.Operation{ 286 ProvisioningParameters: internal.ProvisioningParameters{ 287 ErsContext: internal.ERSContext{GlobalAccountID: fixGlobalAccountID, 288 SubAccountID: fixSubAccountID}, 289 }, 290 } 291 292 rvc := NewRuntimeVersionConfigurator(runtimeVer, fixAccountVersionMapping(t, map[string]string{ 293 fmt.Sprintf("%s%s", globalAccountPrefix, fixGlobalAccountID): customVerGA, 294 fmt.Sprintf("%s%s", subaccountPrefix, fixSubAccountID): customVerSA, 295 }), nil) 296 297 // when 298 ver, err := rvc.ForProvisioning(operation) 299 300 // then 301 require.NoError(t, err) 302 require.Equal(t, 1, ver.MajorVersion) 303 }) 304 } 305 306 func fixAccountVersionMapping(t *testing.T, mapping map[string]string) *AccountVersionMapping { 307 sch := runtime.NewScheme() 308 require.NoError(t, coreV1.AddToScheme(sch)) 309 client := fake.NewFakeClientWithScheme(sch, &coreV1.ConfigMap{ 310 ObjectMeta: metaV1.ObjectMeta{ 311 Name: cmName, 312 Namespace: namespace, 313 }, 314 Data: mapping, 315 }) 316 317 return NewAccountVersionMapping(context.TODO(), client, namespace, cmName, logrus.New()) 318 }