github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/input/input_test.go (about) 1 package input 2 3 import ( 4 "testing" 5 6 "github.com/google/uuid" 7 reconcilerApi "github.com/kyma-incubator/reconciler/pkg/keb" 8 "github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema" 9 "github.com/kyma-project/kyma-environment-broker/common/gardener" 10 "github.com/kyma-project/kyma-environment-broker/internal" 11 "github.com/kyma-project/kyma-environment-broker/internal/broker" 12 "github.com/kyma-project/kyma-environment-broker/internal/fixture" 13 "github.com/kyma-project/kyma-environment-broker/internal/process/input/automock" 14 cloudProvider "github.com/kyma-project/kyma-environment-broker/internal/provider" 15 "github.com/kyma-project/kyma-environment-broker/internal/ptr" 16 "github.com/kyma-project/kyma-environment-broker/internal/runtime" 17 "github.com/kyma-project/kyma-environment-broker/internal/runtime/components" 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/mock" 20 "github.com/stretchr/testify/require" 21 ) 22 23 var emptyVersion = internal.RuntimeVersionData{} 24 25 func TestShouldEnableComponents(t *testing.T) { 26 t.Run("When creating ProvisionRuntimeInput", func(t *testing.T) { 27 // given 28 29 // One base component: dex 30 // Two optional components: Kiali and Tracing 31 // The test checks, if EnableOptionalComponent method adds an optional component 32 optionalComponentsDisablers := runtime.ComponentsDisablers{ 33 components.Kiali: runtime.NewGenericComponentDisabler(components.Kiali), 34 components.Tracing: runtime.NewGenericComponentDisabler(components.Tracing), 35 } 36 componentsProvider := &automock.ComponentListProvider{} 37 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")). 38 Return([]internal.KymaComponent{ 39 {Name: components.Kiali}, 40 {Name: components.Tracing}, 41 {Name: "dex"}, 42 }, nil) 43 44 configProvider := mockConfigProvider() 45 46 builder, err := NewInputBuilderFactory(runtime.NewOptionalComponentsService(optionalComponentsDisablers), runtime.NewDisabledComponentsProvider(), 47 componentsProvider, configProvider, Config{}, "not-important", fixTrialRegionMapping(), fixTrialProviders(), 48 fixture.FixOIDCConfigDTO()) 49 assert.NoError(t, err) 50 51 pp := fixProvisioningParameters(broker.AzurePlanID, "") 52 creator, err := builder.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.1.0", Origin: internal.Defaults}) 53 require.NoError(t, err) 54 55 // when 56 creator.EnableOptionalComponent(components.Kiali) 57 input, err := creator.CreateProvisionRuntimeInput() 58 require.NoError(t, err) 59 60 // then 61 assertComponentExists(t, input.KymaConfig.Components, gqlschema.ComponentConfigurationInput{ 62 Component: components.Kiali, 63 }) 64 assertComponentExists(t, input.KymaConfig.Components, gqlschema.ComponentConfigurationInput{ 65 Component: "dex", 66 }) 67 assert.Len(t, input.KymaConfig.Components, 2) 68 }) 69 70 t.Run("When creating UpgradeRuntimeInput", func(t *testing.T) { 71 // given 72 73 // One base component: dex 74 // Two optional components: Kiali and Tracing 75 // The test checks, if EnableOptionalComponent method adds an optional component 76 optionalComponentsDisablers := runtime.ComponentsDisablers{ 77 components.Kiali: runtime.NewGenericComponentDisabler(components.Kiali), 78 components.Tracing: runtime.NewGenericComponentDisabler(components.Tracing), 79 } 80 componentsProvider := &automock.ComponentListProvider{} 81 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")). 82 Return([]internal.KymaComponent{ 83 {Name: components.Kiali}, 84 {Name: components.Tracing}, 85 {Name: "dex"}, 86 }, nil) 87 88 configProvider := mockConfigProvider() 89 90 builder, err := NewInputBuilderFactory(runtime.NewOptionalComponentsService(optionalComponentsDisablers), runtime.NewDisabledComponentsProvider(), 91 componentsProvider, configProvider, Config{}, "not-important", fixTrialRegionMapping(), fixTrialProviders(), 92 fixture.FixOIDCConfigDTO()) 93 assert.NoError(t, err) 94 95 pp := fixProvisioningParameters(broker.AzurePlanID, "1.14.0") 96 creator, err := builder.CreateUpgradeInput(pp, internal.RuntimeVersionData{Version: "1.14.0", Origin: internal.Defaults}) 97 require.NoError(t, err) 98 99 // when 100 creator.EnableOptionalComponent(components.Kiali) 101 input, err := creator.CreateUpgradeRuntimeInput() 102 require.NoError(t, err) 103 104 // then 105 assertComponentExists(t, input.KymaConfig.Components, gqlschema.ComponentConfigurationInput{ 106 Component: components.Kiali, 107 }) 108 assertComponentExists(t, input.KymaConfig.Components, gqlschema.ComponentConfigurationInput{ 109 Component: "dex", 110 }) 111 assert.Len(t, input.KymaConfig.Components, 2) 112 }) 113 } 114 115 func fixTrialProviders() []string { 116 return []string{"azure", "aws"} 117 } 118 119 func TestShouldDisableComponents(t *testing.T) { 120 t.Run("When creating ProvisionRuntimeInput", func(t *testing.T) { 121 // given 122 pp := fixProvisioningParameters(broker.AzurePlanID, "") 123 124 optionalComponentsDisablers := runtime.ComponentsDisablers{} 125 componentsProvider := &automock.ComponentListProvider{} 126 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")). 127 Return([]internal.KymaComponent{ 128 {Name: components.Kiali}, 129 {Name: components.Tracing}, 130 {Name: components.Backup}, 131 }, nil) 132 133 configProvider := mockConfigProvider() 134 135 builder, err := NewInputBuilderFactory(runtime.NewOptionalComponentsService(optionalComponentsDisablers), runtime.NewDisabledComponentsProvider(), 136 componentsProvider, configProvider, Config{}, "not-important", fixTrialRegionMapping(), fixTrialProviders(), 137 fixture.FixOIDCConfigDTO()) 138 assert.NoError(t, err) 139 creator, err := builder.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.10.0", Origin: internal.Defaults}) 140 require.NoError(t, err) 141 142 // when 143 input, err := creator.CreateProvisionRuntimeInput() 144 require.NoError(t, err) 145 146 // then 147 assertComponentExists(t, input.KymaConfig.Components, gqlschema.ComponentConfigurationInput{ 148 Component: components.Tracing, 149 }) 150 assertComponentExists(t, input.KymaConfig.Components, gqlschema.ComponentConfigurationInput{ 151 Component: components.Kiali, 152 }) 153 assert.Len(t, input.KymaConfig.Components, 2) 154 }) 155 156 t.Run("When creating UpgradeRuntimeInput", func(t *testing.T) { 157 // given 158 pp := fixProvisioningParameters(broker.AzurePlanID, "1.14.0") 159 160 optionalComponentsDisablers := runtime.ComponentsDisablers{} 161 componentsProvider := &automock.ComponentListProvider{} 162 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")). 163 Return([]internal.KymaComponent{ 164 {Name: components.Kiali}, 165 {Name: components.Tracing}, 166 {Name: components.Backup}, 167 }, nil) 168 169 configProvider := mockConfigProvider() 170 171 builder, err := NewInputBuilderFactory(runtime.NewOptionalComponentsService(optionalComponentsDisablers), runtime.NewDisabledComponentsProvider(), 172 componentsProvider, configProvider, Config{}, "not-important", fixTrialRegionMapping(), fixTrialProviders(), 173 fixture.FixOIDCConfigDTO()) 174 assert.NoError(t, err) 175 creator, err := builder.CreateUpgradeInput(pp, internal.RuntimeVersionData{Version: "1.14.0", Origin: internal.Defaults}) 176 require.NoError(t, err) 177 178 // when 179 input, err := creator.CreateUpgradeRuntimeInput() 180 require.NoError(t, err) 181 182 // then 183 assertComponentExists(t, input.KymaConfig.Components, gqlschema.ComponentConfigurationInput{ 184 Component: components.Tracing, 185 }) 186 assertComponentExists(t, input.KymaConfig.Components, gqlschema.ComponentConfigurationInput{ 187 Component: components.Kiali, 188 }) 189 assert.Len(t, input.KymaConfig.Components, 2) 190 }) 191 } 192 193 func TestDisabledComponentsForPlanNotExist(t *testing.T) { 194 // given 195 pp := fixProvisioningParameters("invalid-plan", "") 196 197 optionalComponentsDisablers := runtime.ComponentsDisablers{} 198 componentsProvider := &automock.ComponentListProvider{} 199 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")). 200 Return([]internal.KymaComponent{ 201 {Name: components.Kiali}, 202 {Name: components.Tracing}, 203 {Name: components.Backup}, 204 }, nil) 205 206 configProvider := mockConfigProvider() 207 208 builder, err := NewInputBuilderFactory(runtime.NewOptionalComponentsService(optionalComponentsDisablers), runtime.NewDisabledComponentsProvider(), 209 componentsProvider, configProvider, Config{}, "not-important", fixTrialRegionMapping(), fixTrialProviders(), 210 fixture.FixOIDCConfigDTO()) 211 assert.NoError(t, err) 212 // when 213 _, err = builder.CreateProvisionInput(pp, emptyVersion) 214 require.Error(t, err) 215 } 216 217 func TestInputBuilderFactoryOverrides(t *testing.T) { 218 t.Run("should append overrides for the same components multiple times", func(t *testing.T) { 219 // given 220 var ( 221 dummyOptComponentsSvc = dummyOptionalComponentServiceMock(fixKymaComponentList()) 222 223 overridesA1 = []*gqlschema.ConfigEntryInput{ 224 {Key: "key-1", Value: "pico"}, 225 {Key: "key-2", Value: "bello"}, 226 } 227 overridesA2 = []*gqlschema.ConfigEntryInput{ 228 {Key: "key-3", Value: "hakuna"}, 229 {Key: "key-4", Value: "matata", Secret: ptr.Bool(true)}, 230 } 231 ) 232 233 configProvider := mockConfigProvider() 234 235 pp := fixProvisioningParameters(broker.AzurePlanID, "") 236 componentsProvider := &automock.ComponentListProvider{} 237 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 238 239 builder, err := NewInputBuilderFactory(dummyOptComponentsSvc, runtime.NewDisabledComponentsProvider(), 240 componentsProvider, configProvider, Config{}, "not-important", fixTrialRegionMapping(), fixTrialProviders(), 241 fixture.FixOIDCConfigDTO()) 242 assert.NoError(t, err) 243 creator, err := builder.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.10.0", Origin: internal.Defaults}) 244 require.NoError(t, err) 245 246 // when 247 creator. 248 AppendOverrides("keb", overridesA1). 249 AppendOverrides("keb", overridesA2) 250 251 // then 252 out, err := creator.CreateProvisionRuntimeInput() 253 require.NoError(t, err) 254 255 overriddenComponent, found := find(out.KymaConfig.Components, "keb") 256 require.True(t, found) 257 258 assertContainsAllOverrides(t, overriddenComponent.Configuration, overridesA1, overridesA2) 259 }) 260 261 t.Run("should append global overrides for ProvisionRuntimeInput", func(t *testing.T) { 262 // given 263 var ( 264 optComponentsSvc = dummyOptionalComponentServiceMock(fixKymaComponentList()) 265 266 overridesA1 = []*gqlschema.ConfigEntryInput{ 267 {Key: "key-1", Value: "pico"}, 268 {Key: "key-2", Value: "bello"}, 269 {Key: "key-true", Value: "true"}, 270 } 271 overridesA2 = []*gqlschema.ConfigEntryInput{ 272 {Key: "key-3", Value: "hakuna"}, 273 {Key: "key-4", Value: "matata", Secret: ptr.Bool(true)}, 274 {Key: "key-false", Value: "false"}, 275 } 276 ) 277 componentsProvider := &automock.ComponentListProvider{} 278 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 279 280 pp := fixProvisioningParameters(broker.AzurePlanID, "") 281 282 configProvider := mockConfigProvider() 283 284 builder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 285 componentsProvider, configProvider, Config{}, "not-important", fixTrialRegionMapping(), fixTrialProviders(), 286 fixture.FixOIDCConfigDTO()) 287 assert.NoError(t, err) 288 creator, err := builder.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.10.0", Origin: internal.Defaults}) 289 require.NoError(t, err) 290 291 // when 292 creator. 293 AppendGlobalOverrides(overridesA1). 294 AppendGlobalOverrides(overridesA2) 295 296 // then 297 out, err := creator.CreateProvisionRuntimeInput() 298 require.NoError(t, err) 299 300 assertContainsAllOverrides(t, out.KymaConfig.Configuration, overridesA1, overridesA2) 301 }) 302 303 t.Run("should append global overrides for UpgradeRuntimeInput", func(t *testing.T) { 304 // given 305 var ( 306 optComponentsSvc = dummyOptionalComponentServiceMock(fixKymaComponentList()) 307 308 overridesA1 = []*gqlschema.ConfigEntryInput{ 309 {Key: "key-1", Value: "pico"}, 310 {Key: "key-2", Value: "bello"}, 311 } 312 overridesA2 = []*gqlschema.ConfigEntryInput{ 313 {Key: "key-3", Value: "hakuna"}, 314 {Key: "key-4", Value: "matata", Secret: ptr.Bool(true)}, 315 } 316 ) 317 componentsProvider := &automock.ComponentListProvider{} 318 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 319 320 configProvider := mockConfigProvider() 321 322 pp := fixProvisioningParameters(broker.AzurePlanID, "1.14.0") 323 builder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 324 componentsProvider, configProvider, Config{}, "not-important", fixTrialRegionMapping(), fixTrialProviders(), 325 fixture.FixOIDCConfigDTO()) 326 assert.NoError(t, err) 327 creator, err := builder.CreateUpgradeInput(pp, internal.RuntimeVersionData{Version: "1.14.0", Origin: internal.Defaults}) 328 require.NoError(t, err) 329 330 // when 331 creator. 332 AppendGlobalOverrides(overridesA1). 333 AppendGlobalOverrides(overridesA2) 334 335 // then 336 out, err := creator.CreateUpgradeRuntimeInput() 337 require.NoError(t, err) 338 339 assertContainsAllOverrides(t, out.KymaConfig.Configuration, overridesA1, overridesA2) 340 }) 341 342 t.Run("should overwrite already applied component and global overrides", func(t *testing.T) { 343 // given 344 var ( 345 dummyOptComponentsSvc = dummyOptionalComponentServiceMock(fixKymaComponentList()) 346 347 overridesA1 = []*gqlschema.ConfigEntryInput{ 348 {Key: "key-1", Value: "initial"}, 349 {Key: "key-2", Value: "bello"}, 350 } 351 overridesA2 = []*gqlschema.ConfigEntryInput{ 352 {Key: "key-1", Value: "new"}, 353 {Key: "key-4", Value: "matata", Secret: ptr.Bool(true)}, 354 } 355 globalOverrides1 = []*gqlschema.ConfigEntryInput{ 356 {Key: "key-g-1", Value: "initial-g"}, 357 {Key: "key-g-2", Value: "hakuna", Secret: ptr.Bool(true)}, 358 } 359 globalOverrides2 = []*gqlschema.ConfigEntryInput{ 360 {Key: "key-g-1", Value: "new"}, 361 {Key: "key-g-4", Value: "matata", Secret: ptr.Bool(true)}, 362 } 363 ) 364 365 pp := fixProvisioningParameters(broker.AzurePlanID, "") 366 componentsProvider := &automock.ComponentListProvider{} 367 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 368 369 configProvider := mockConfigProvider() 370 371 builder, err := NewInputBuilderFactory(dummyOptComponentsSvc, runtime.NewDisabledComponentsProvider(), 372 componentsProvider, configProvider, Config{}, "not-important", fixTrialRegionMapping(), fixTrialProviders(), 373 fixture.FixOIDCConfigDTO()) 374 assert.NoError(t, err) 375 creator, err := builder.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.10.0", Origin: internal.Defaults}) 376 require.NoError(t, err) 377 378 // when 379 creator. 380 AppendOverrides("keb", overridesA1). 381 AppendOverrides("keb", overridesA2). 382 AppendGlobalOverrides(globalOverrides1). 383 AppendGlobalOverrides(globalOverrides2) 384 385 // then 386 out, err := creator.CreateProvisionRuntimeInput() 387 require.NoError(t, err) 388 389 overriddenComponent, found := find(out.KymaConfig.Components, "keb") 390 require.True(t, found) 391 392 // assert component overrides 393 assertContainsAllOverrides(t, overriddenComponent.Configuration, []*gqlschema.ConfigEntryInput{ 394 {Key: "key-1", Value: "new"}, 395 {Key: "key-2", Value: "bello"}, 396 {Key: "key-4", Value: "matata", Secret: ptr.Bool(true)}, 397 }) 398 399 // assert global overrides 400 assertContainsAllOverrides(t, out.KymaConfig.Configuration, []*gqlschema.ConfigEntryInput{ 401 {Key: "key-g-1", Value: "new"}, 402 {Key: "key-g-2", Value: "hakuna", Secret: ptr.Bool(true)}, 403 {Key: "key-g-4", Value: "matata", Secret: ptr.Bool(true)}, 404 }) 405 }) 406 } 407 408 func TestInputBuilderFactoryForAzurePlan(t *testing.T) { 409 // given 410 var ( 411 inputComponentList = fixKymaComponentList() 412 mappedComponentList = mapToGQLComponentConfigurationInput(inputComponentList) 413 toDisableComponents = []string{"kiali"} 414 kebOverrides = []*gqlschema.ConfigEntryInput{ 415 {Key: "key-1", Value: "pico"}, 416 {Key: "key-2", Value: "bello", Secret: ptr.Bool(true)}, 417 } 418 ) 419 420 optComponentsSvc := &automock.OptionalComponentService{} 421 defer optComponentsSvc.AssertExpectations(t) 422 optComponentsSvc.On("ComputeComponentsToDisable", []string{}).Return(toDisableComponents) 423 optComponentsSvc.On("ExecuteDisablers", mappedComponentList, toDisableComponents[0]).Return(mappedComponentList, nil) 424 425 config := Config{ 426 URL: "", 427 } 428 componentsProvider := &automock.ComponentListProvider{} 429 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(inputComponentList, nil) 430 defer componentsProvider.AssertExpectations(t) 431 432 configProvider := mockConfigProvider() 433 434 factory, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 435 componentsProvider, configProvider, config, "1.10.0", 436 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 437 assert.NoError(t, err) 438 pp := fixProvisioningParameters(broker.AzurePlanID, "") 439 440 // when 441 builder, err := factory.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.10.0", Origin: internal.Defaults}) 442 443 // then 444 require.NoError(t, err) 445 446 // when 447 shootName := "c-51bcc12" 448 builder. 449 SetProvisioningParameters(internal.ProvisioningParameters{ 450 Parameters: internal.ProvisioningParametersDTO{ 451 Name: "azure-cluster", 452 TargetSecret: ptr.String("azure-secret"), 453 Purpose: ptr.String("development"), 454 }, 455 }). 456 SetShootName(shootName). 457 SetLabel("label1", "value1"). 458 SetShootDomain("shoot.domain.sap"). 459 AppendOverrides("keb", kebOverrides) 460 input, err := builder.CreateProvisionRuntimeInput() 461 require.NoError(t, err) 462 clusterInput, err := builder.CreateProvisionClusterInput() 463 require.NoError(t, err) 464 465 // then 466 assert.Equal(t, input.ClusterConfig, clusterInput.ClusterConfig) 467 assert.Equal(t, input.RuntimeInput, clusterInput.RuntimeInput) 468 assert.Nil(t, clusterInput.KymaConfig) 469 assert.EqualValues(t, mappedComponentList, input.KymaConfig.Components) 470 assert.Contains(t, input.RuntimeInput.Name, "azure-cluster") 471 assert.Equal(t, "azure", input.ClusterConfig.GardenerConfig.Provider) 472 assert.Equal(t, "azure-secret", input.ClusterConfig.GardenerConfig.TargetSecret) 473 require.NotNil(t, input.ClusterConfig.GardenerConfig.Purpose) 474 assert.Equal(t, "development", *input.ClusterConfig.GardenerConfig.Purpose) 475 assert.Nil(t, input.ClusterConfig.GardenerConfig.LicenceType) 476 assert.EqualValues(t, mappedComponentList, input.KymaConfig.Components) 477 assert.Equal(t, shootName, input.ClusterConfig.GardenerConfig.Name) 478 assert.NotNil(t, input.ClusterConfig.Administrators) 479 assert.Equal(t, gqlschema.Labels{ 480 "label1": "value1", 481 }, input.RuntimeInput.Labels) 482 483 assertOverrides(t, "keb", input.KymaConfig.Components, kebOverrides) 484 } 485 486 func TestShouldAdjustRuntimeName(t *testing.T) { 487 for name, tc := range map[string]struct { 488 runtimeName string 489 expectedNameWithoutSuffix string 490 }{ 491 "regular string": { 492 runtimeName: "test", 493 expectedNameWithoutSuffix: "test", 494 }, 495 "too long string": { 496 runtimeName: "this-string-is-too-long-because-it-has-more-than-36-chars", 497 expectedNameWithoutSuffix: "this-string-is-too-long-becaus", 498 }, 499 "string with forbidden chars": { 500 runtimeName: "CLUSTER-?name_123@!", 501 expectedNameWithoutSuffix: "cluster-name123", 502 }, 503 "too long string with forbidden chars": { 504 runtimeName: "ThisStringIsTooLongBecauseItHasMoreThan36Chars", 505 expectedNameWithoutSuffix: "thisstringistoolongbecauseitha", 506 }, 507 } { 508 t.Run(name, func(t *testing.T) { 509 // given 510 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 511 componentsProvider := &automock.ComponentListProvider{} 512 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 513 514 configProvider := mockConfigProvider() 515 516 builder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 517 componentsProvider, configProvider, Config{TrialNodesNumber: 0}, "not-important", fixTrialRegionMapping(), 518 fixTrialProviders(), fixture.FixOIDCConfigDTO()) 519 assert.NoError(t, err) 520 521 pp := fixProvisioningParameters(broker.TrialPlanID, "") 522 pp.Parameters.Name = tc.runtimeName 523 524 creator, err := builder.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.1.0", Origin: internal.Defaults}) 525 require.NoError(t, err) 526 creator.SetProvisioningParameters(pp) 527 528 // when 529 input, err := creator.CreateProvisionRuntimeInput() 530 require.NoError(t, err) 531 clusterInput, err := creator.CreateProvisionClusterInput() 532 require.NoError(t, err) 533 534 // then 535 assert.NotEqual(t, pp.Parameters.Name, input.RuntimeInput.Name) 536 assert.LessOrEqual(t, len(input.RuntimeInput.Name), 36) 537 assert.Equal(t, tc.expectedNameWithoutSuffix, input.RuntimeInput.Name[:len(input.RuntimeInput.Name)-6]) 538 assert.Equal(t, 1, input.ClusterConfig.GardenerConfig.AutoScalerMin) 539 assert.Equal(t, 1, input.ClusterConfig.GardenerConfig.AutoScalerMax) 540 assert.Equal(t, tc.expectedNameWithoutSuffix, clusterInput.RuntimeInput.Name[:len(input.RuntimeInput.Name)-6]) 541 assert.Equal(t, 1, clusterInput.ClusterConfig.GardenerConfig.AutoScalerMin) 542 assert.Equal(t, 1, clusterInput.ClusterConfig.GardenerConfig.AutoScalerMax) 543 }) 544 } 545 } 546 547 func TestShouldSetNumberOfNodesForTrialPlan(t *testing.T) { 548 // given 549 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 550 componentsProvider := &automock.ComponentListProvider{} 551 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 552 553 configProvider := mockConfigProvider() 554 555 builder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 556 componentsProvider, configProvider, Config{TrialNodesNumber: 2}, "not-important", 557 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 558 assert.NoError(t, err) 559 560 pp := fixProvisioningParameters(broker.TrialPlanID, "") 561 562 creator, err := builder.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.17.0", Origin: internal.Defaults}) 563 require.NoError(t, err) 564 creator.SetProvisioningParameters(pp) 565 566 // when 567 input, err := creator.CreateProvisionRuntimeInput() 568 require.NoError(t, err) 569 clusterInput, err := creator.CreateProvisionClusterInput() 570 require.NoError(t, err) 571 572 // then 573 assert.Equal(t, 2, input.ClusterConfig.GardenerConfig.AutoScalerMin) 574 assert.Equal(t, 2, clusterInput.ClusterConfig.GardenerConfig.AutoScalerMax) 575 } 576 577 func TestShouldSetGlobalConfiguration(t *testing.T) { 578 t.Run("When creating ProvisionRuntimeInput", func(t *testing.T) { 579 // given 580 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 581 componentsProvider := &automock.ComponentListProvider{} 582 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 583 584 configProvider := mockConfigProvider() 585 586 builder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 587 componentsProvider, configProvider, Config{}, "", 588 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 589 assert.NoError(t, err) 590 591 pp := fixProvisioningParameters(broker.TrialPlanID, "") 592 593 creator, err := builder.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "", Origin: internal.Defaults}) 594 require.NoError(t, err) 595 creator.SetProvisioningParameters(pp) 596 597 // when 598 input, err := creator.CreateProvisionRuntimeInput() 599 require.NoError(t, err) 600 601 // then 602 expectedStrategy := gqlschema.ConflictStrategyReplace 603 assert.Equal(t, &expectedStrategy, input.KymaConfig.ConflictStrategy) 604 }) 605 606 t.Run("When creating UpgradeRuntimeInput", func(t *testing.T) { 607 // given 608 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 609 componentsProvider := &automock.ComponentListProvider{} 610 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 611 612 configProvider := mockConfigProvider() 613 614 builder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 615 componentsProvider, configProvider, Config{}, "", 616 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 617 assert.NoError(t, err) 618 619 pp := fixProvisioningParameters(broker.TrialPlanID, "") 620 621 creator, err := builder.CreateUpgradeInput(pp, internal.RuntimeVersionData{Version: "1.21.0", Origin: internal.Defaults}) 622 require.NoError(t, err) 623 creator.SetProvisioningParameters(pp) 624 625 // when 626 input, err := creator.CreateUpgradeRuntimeInput() 627 require.NoError(t, err) 628 629 // then 630 expectedStrategy := gqlschema.ConflictStrategyReplace 631 assert.Equal(t, &expectedStrategy, input.KymaConfig.ConflictStrategy) 632 }) 633 } 634 635 func TestCreateProvisionRuntimeInput_ConfigureDNS(t *testing.T) { 636 637 t.Run("should apply provided DNS Providers values", func(t *testing.T) { 638 // given 639 expectedDnsValues := &gqlschema.DNSConfigInput{ 640 Domain: "shoot-name.domain.sap", 641 Providers: []*gqlschema.DNSProviderInput{ 642 &gqlschema.DNSProviderInput{ 643 DomainsInclude: []string{"devtest.kyma.ondemand.com"}, 644 Primary: true, 645 SecretName: "aws_dns_domain_secrets_test_incustom", 646 Type: "route53_type_test", 647 }, 648 }, 649 } 650 651 id := uuid.New().String() 652 653 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 654 componentsProvider := &automock.ComponentListProvider{} 655 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 656 657 configProvider := mockConfigProvider() 658 659 inputBuilder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 660 componentsProvider, configProvider, Config{}, "1.24.4", fixTrialRegionMapping(), 661 fixTrialProviders(), fixture.FixOIDCConfigDTO()) 662 assert.NoError(t, err) 663 664 provisioningParams := fixture.FixProvisioningParameters(id) 665 666 creator, err := inputBuilder.CreateProvisionInput(provisioningParams, internal.RuntimeVersionData{Version: "", Origin: internal.Defaults}) 667 require.NoError(t, err) 668 setRuntimeProperties(creator) 669 670 // when 671 input, err := creator.CreateProvisionRuntimeInput() 672 require.NoError(t, err) 673 clusterInput, err := creator.CreateProvisionClusterInput() 674 require.NoError(t, err) 675 676 // then 677 assert.Equal(t, expectedDnsValues, input.ClusterConfig.GardenerConfig.DNSConfig) 678 assert.Equal(t, expectedDnsValues, clusterInput.ClusterConfig.GardenerConfig.DNSConfig) 679 }) 680 681 t.Run("should apply the DNS Providers values while DNS providers is empty", func(t *testing.T) { 682 // given 683 expectedDnsValues := &gqlschema.DNSConfigInput{ 684 Domain: "shoot-name.domain.sap", 685 } 686 687 id := uuid.New().String() 688 689 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 690 componentsProvider := &automock.ComponentListProvider{} 691 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 692 693 configProvider := mockConfigProvider() 694 695 inputBuilder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 696 componentsProvider, configProvider, Config{}, "1.24.4", 697 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 698 assert.NoError(t, err) 699 700 provisioningParams := fixture.FixProvisioningParameters(id) 701 702 creator, err := inputBuilder.CreateProvisionInput(provisioningParams, internal.RuntimeVersionData{Version: "", Origin: internal.Defaults}) 703 require.NoError(t, err) 704 setRuntimeProperties(creator) 705 creator.SetShootDNSProviders(gardener.DNSProvidersData{}) 706 707 // when 708 input, err := creator.CreateProvisionRuntimeInput() 709 require.NoError(t, err) 710 clusterInput, err := creator.CreateProvisionClusterInput() 711 require.NoError(t, err) 712 713 // then 714 assert.Equal(t, expectedDnsValues, input.ClusterConfig.GardenerConfig.DNSConfig) 715 assert.Equal(t, expectedDnsValues, clusterInput.ClusterConfig.GardenerConfig.DNSConfig) 716 }) 717 718 } 719 720 func TestCreateProvisionRuntimeInput_ConfigureOIDC(t *testing.T) { 721 722 t.Run("should apply default OIDC values when OIDC is nil", func(t *testing.T) { 723 // given 724 expectedOidcValues := &gqlschema.OIDCConfigInput{ 725 ClientID: "9bd05ed7-a930-44e6-8c79-e6defeb7dec9", 726 GroupsClaim: "groups", 727 IssuerURL: "https://kymatest.accounts400.ondemand.com", 728 SigningAlgs: []string{"RS256"}, 729 UsernameClaim: "sub", 730 UsernamePrefix: "-", 731 } 732 733 id := uuid.New().String() 734 735 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 736 componentsProvider := &automock.ComponentListProvider{} 737 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 738 739 configProvider := mockConfigProvider() 740 741 inputBuilder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 742 componentsProvider, configProvider, Config{}, "1.24.0", 743 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 744 assert.NoError(t, err) 745 746 provisioningParams := fixture.FixProvisioningParameters(id) 747 748 creator, err := inputBuilder.CreateProvisionInput(provisioningParams, internal.RuntimeVersionData{Version: "", Origin: internal.Defaults}) 749 require.NoError(t, err) 750 751 // when 752 input, err := creator.CreateProvisionRuntimeInput() 753 require.NoError(t, err) 754 clusterInput, err := creator.CreateProvisionClusterInput() 755 require.NoError(t, err) 756 757 // then 758 assert.Equal(t, expectedOidcValues, input.ClusterConfig.GardenerConfig.OidcConfig) 759 assert.Equal(t, expectedOidcValues, clusterInput.ClusterConfig.GardenerConfig.OidcConfig) 760 }) 761 762 t.Run("should apply default OIDC values when all OIDC fields are empty", func(t *testing.T) { 763 // given 764 expectedOidcValues := &gqlschema.OIDCConfigInput{ 765 ClientID: "9bd05ed7-a930-44e6-8c79-e6defeb7dec9", 766 GroupsClaim: "groups", 767 IssuerURL: "https://kymatest.accounts400.ondemand.com", 768 SigningAlgs: []string{"RS256"}, 769 UsernameClaim: "sub", 770 UsernamePrefix: "-", 771 } 772 773 id := uuid.New().String() 774 775 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 776 componentsProvider := &automock.ComponentListProvider{} 777 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 778 779 configProvider := mockConfigProvider() 780 781 inputBuilder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 782 componentsProvider, configProvider, Config{}, "1.24.0", 783 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 784 assert.NoError(t, err) 785 786 provisioningParams := fixture.FixProvisioningParameters(id) 787 provisioningParams.Parameters.OIDC = &internal.OIDCConfigDTO{} 788 789 creator, err := inputBuilder.CreateProvisionInput(provisioningParams, internal.RuntimeVersionData{Version: "", Origin: internal.Defaults}) 790 require.NoError(t, err) 791 792 // when 793 input, err := creator.CreateProvisionRuntimeInput() 794 require.NoError(t, err) 795 clusterInput, err := creator.CreateProvisionClusterInput() 796 require.NoError(t, err) 797 798 // then 799 assert.Equal(t, expectedOidcValues, input.ClusterConfig.GardenerConfig.OidcConfig) 800 assert.Equal(t, expectedOidcValues, clusterInput.ClusterConfig.GardenerConfig.OidcConfig) 801 }) 802 803 t.Run("should apply provided OIDC values", func(t *testing.T) { 804 // given 805 expectedOidcValues := &gqlschema.OIDCConfigInput{ 806 ClientID: "provided-id", 807 GroupsClaim: "fake-groups-claim", 808 IssuerURL: "https://test.domain.local", 809 SigningAlgs: []string{"RS256", "HS256"}, 810 UsernameClaim: "usernameClaim", 811 UsernamePrefix: "<<", 812 } 813 814 id := uuid.New().String() 815 816 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 817 componentsProvider := &automock.ComponentListProvider{} 818 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 819 820 configProvider := mockConfigProvider() 821 822 inputBuilder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 823 componentsProvider, configProvider, Config{}, "1.24.0", 824 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 825 assert.NoError(t, err) 826 827 provisioningParams := fixture.FixProvisioningParameters(id) 828 provisioningParams.Parameters.OIDC = &internal.OIDCConfigDTO{ 829 ClientID: "provided-id", 830 GroupsClaim: "fake-groups-claim", 831 IssuerURL: "https://test.domain.local", 832 SigningAlgs: []string{"RS256", "HS256"}, 833 UsernameClaim: "usernameClaim", 834 UsernamePrefix: "<<", 835 } 836 837 creator, err := inputBuilder.CreateProvisionInput(provisioningParams, internal.RuntimeVersionData{Version: "", Origin: internal.Defaults}) 838 require.NoError(t, err) 839 840 // when 841 input, err := creator.CreateProvisionRuntimeInput() 842 require.NoError(t, err) 843 clusterInput, err := creator.CreateProvisionClusterInput() 844 require.NoError(t, err) 845 846 // then 847 assert.Equal(t, expectedOidcValues, input.ClusterConfig.GardenerConfig.OidcConfig) 848 assert.Equal(t, expectedOidcValues, clusterInput.ClusterConfig.GardenerConfig.OidcConfig) 849 }) 850 } 851 852 func TestCreateClusterConfiguration_Overrides(t *testing.T) { 853 t.Run("Should apply component and global overrides with proper types", func(t *testing.T) { 854 // given 855 id := uuid.New().String() 856 857 componentList := []internal.KymaComponent{ 858 {Name: "dex", Namespace: "kyma-system"}, 859 {Name: "ory", Namespace: "kyma-system"}, 860 { 861 Name: "custom", 862 Namespace: "kyma-system", 863 Source: &internal.ComponentSource{URL: "http://source.url"}, 864 }, 865 } 866 867 optComponentsSvc := dummyOptionalComponentServiceMock(componentList) 868 componentsProvider := &automock.ComponentListProvider{} 869 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(componentList, nil) 870 871 configProvider := mockConfigProvider() 872 873 inputBuilder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 874 componentsProvider, configProvider, Config{}, "1.24.0", 875 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 876 assert.NoError(t, err) 877 878 provisioningParams := fixture.FixProvisioningParameters(id) 879 creator, err := inputBuilder.CreateProvisionInput(provisioningParams, internal.RuntimeVersionData{Version: "", Origin: internal.Defaults}) 880 require.NoError(t, err) 881 setRuntimeProperties(creator) 882 creator.AppendOverrides("dex", []*gqlschema.ConfigEntryInput{ 883 {Key: "key-1", Value: "pico"}, 884 {Key: "key-false", Value: "false"}, 885 {Key: "key-true", Value: "true"}, 886 {Key: "key-secret", Value: "classified", Secret: ptr.Bool(true)}, 887 }) 888 creator.AppendGlobalOverrides([]*gqlschema.ConfigEntryInput{ 889 {Key: "global-key-string", Value: "global-pico"}, 890 {Key: "global-key-false", Value: "false"}, 891 {Key: "global-key-true", Value: "true"}, 892 {Key: "global-key-secret", Value: "global-classified", Secret: ptr.Bool(true)}, 893 }) 894 895 // when 896 inventoryInput, err := creator.CreateClusterConfiguration() 897 require.NoError(t, err) 898 899 // then 900 assertAllConfigsContainsGlobals(t, inventoryInput.KymaConfig.Components, "shoot-name.domain.sap") 901 assert.Equal(t, reconcilerApi.Component{ 902 URL: "", 903 Component: "dex", 904 Namespace: "kyma-system", 905 Configuration: []reconcilerApi.Configuration{ 906 {Key: "global.domainName", Value: "shoot-name.domain.sap", Secret: false}, 907 {Key: "global-key-string", Value: "global-pico", Secret: false}, 908 {Key: "global-key-false", Value: false, Secret: false}, 909 {Key: "global-key-true", Value: true, Secret: false}, 910 {Key: "global-key-secret", Value: "global-classified", Secret: true}, 911 {Key: "key-1", Value: "pico", Secret: false}, 912 {Key: "key-false", Value: false, Secret: false}, 913 {Key: "key-true", Value: true, Secret: false}, 914 {Key: "key-secret", Value: "classified", Secret: true}, 915 }, 916 }, inventoryInput.KymaConfig.Components[0]) 917 918 // check custom source URL 919 for _, component := range inventoryInput.KymaConfig.Components { 920 if component.Component == "custom" { 921 assert.Equal(t, "http://source.url", component.URL) 922 } 923 } 924 }) 925 926 t.Run("should overwrite already existing component and global overrides", func(t *testing.T) { 927 // given 928 var ( 929 dummyOptComponentsSvc = dummyOptionalComponentServiceMock(fixKymaComponentList()) 930 931 overridesA1 = []*gqlschema.ConfigEntryInput{ 932 {Key: "key-1", Value: "initial"}, 933 {Key: "key-2", Value: "bello"}, 934 } 935 overridesA2 = []*gqlschema.ConfigEntryInput{ 936 {Key: "key-1", Value: "new"}, 937 {Key: "key-4", Value: "matata", Secret: ptr.Bool(true)}, 938 } 939 globalOverrides1 = []*gqlschema.ConfigEntryInput{ 940 {Key: "key-g-1", Value: "initial-g"}, 941 {Key: "key-g-2", Value: "hakuna", Secret: ptr.Bool(true)}, 942 } 943 globalOverrides2 = []*gqlschema.ConfigEntryInput{ 944 {Key: "key-g-1", Value: "new"}, 945 {Key: "key-g-4", Value: "matata", Secret: ptr.Bool(true)}, 946 } 947 ) 948 949 pp := fixProvisioningParameters(broker.AzurePlanID, "2.0.0-rc6") 950 componentsProvider := &automock.ComponentListProvider{} 951 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 952 953 configProvider := mockConfigProvider() 954 955 builder, err := NewInputBuilderFactory(dummyOptComponentsSvc, runtime.NewDisabledComponentsProvider(), 956 componentsProvider, configProvider, Config{}, "not-important", 957 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 958 assert.NoError(t, err) 959 creator, err := builder.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.10.0", Origin: internal.Defaults}) 960 require.NoError(t, err) 961 setRuntimeProperties(creator) 962 963 // when 964 creator. 965 AppendOverrides("keb", overridesA1). 966 AppendOverrides("keb", overridesA2). 967 AppendGlobalOverrides(globalOverrides1). 968 AppendGlobalOverrides(globalOverrides2) 969 970 // then 971 out, err := creator.CreateClusterConfiguration() 972 require.NoError(t, err) 973 t.Logf("out %+v\n", out) 974 975 overriddenComponent, found := findForReconciler(out.KymaConfig.Components, "keb") 976 require.True(t, found) 977 t.Logf("overriddenComponent %+v\n", overriddenComponent) 978 979 assertAllConfigsContainsGlobals(t, []reconcilerApi.Component{overriddenComponent}, "shoot-name.domain.sap") 980 // assert component and global overrides 981 assertContainsAllOverridesForReconciler(t, overriddenComponent.Configuration, []*gqlschema.ConfigEntryInput{ 982 {Key: "global.domainName", Value: "shoot-name.domain.sap"}, 983 {Key: "key-1", Value: "new"}, 984 {Key: "key-2", Value: "bello"}, 985 {Key: "key-4", Value: "matata", Secret: ptr.Bool(true)}, 986 {Key: "key-g-1", Value: "new"}, 987 {Key: "key-g-2", Value: "hakuna", Secret: ptr.Bool(true)}, 988 {Key: "key-g-4", Value: "matata", Secret: ptr.Bool(true)}, 989 }) 990 }) 991 } 992 993 func TestCreateProvisionRuntimeInput_ConfigureAdmins(t *testing.T) { 994 t.Run("should apply default admin from user_id field", func(t *testing.T) { 995 // given 996 expectedAdmins := []string{"fake-user-id"} 997 998 id := uuid.New().String() 999 1000 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 1001 componentsProvider := &automock.ComponentListProvider{} 1002 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 1003 1004 configProvider := mockConfigProvider() 1005 1006 inputBuilder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 1007 componentsProvider, configProvider, Config{}, "1.24.0", 1008 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 1009 assert.NoError(t, err) 1010 1011 provisioningParams := fixture.FixProvisioningParameters(id) 1012 provisioningParams.ErsContext.UserID = expectedAdmins[0] 1013 1014 creator, err := inputBuilder.CreateProvisionInput(provisioningParams, internal.RuntimeVersionData{Version: "", Origin: internal.Defaults}) 1015 require.NoError(t, err) 1016 setRuntimeProperties(creator) 1017 1018 // when 1019 input, err := creator.CreateProvisionRuntimeInput() 1020 require.NoError(t, err) 1021 inventoryInput, err := creator.CreateClusterConfiguration() 1022 require.NoError(t, err) 1023 1024 // then 1025 assert.Equal(t, expectedAdmins, input.ClusterConfig.Administrators) 1026 assert.Equal(t, expectedAdmins, inventoryInput.KymaConfig.Administrators) 1027 }) 1028 1029 t.Run("should apply new admin list", func(t *testing.T) { 1030 // given 1031 expectedAdmins := []string{"newAdmin1@test.com", "newAdmin2@test.com"} 1032 1033 id := uuid.New().String() 1034 1035 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 1036 componentsProvider := &automock.ComponentListProvider{} 1037 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 1038 1039 configProvider := mockConfigProvider() 1040 1041 inputBuilder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 1042 componentsProvider, configProvider, Config{}, "1.24.0", 1043 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 1044 assert.NoError(t, err) 1045 1046 provisioningParams := fixture.FixProvisioningParameters(id) 1047 provisioningParams.Parameters.RuntimeAdministrators = expectedAdmins 1048 1049 creator, err := inputBuilder.CreateProvisionInput(provisioningParams, internal.RuntimeVersionData{Version: "", Origin: internal.Defaults}) 1050 require.NoError(t, err) 1051 setRuntimeProperties(creator) 1052 1053 // when 1054 input, err := creator.CreateProvisionRuntimeInput() 1055 require.NoError(t, err) 1056 inventoryInput, err := creator.CreateClusterConfiguration() 1057 require.NoError(t, err) 1058 1059 // then 1060 assert.Equal(t, expectedAdmins, input.ClusterConfig.Administrators) 1061 assert.Equal(t, expectedAdmins, inventoryInput.KymaConfig.Administrators) 1062 }) 1063 } 1064 1065 func assertAllConfigsContainsGlobals(t *testing.T, components []reconcilerApi.Component, domainName string) { 1066 for _, cmp := range components { 1067 found := false 1068 for _, cfg := range cmp.Configuration { 1069 if cfg.Key == "global.domainName" { 1070 assert.Equal(t, domainName, cfg.Value) 1071 found = true 1072 } 1073 } 1074 assert.True(t, found, "Component %s must contain `global.domainName` config", cmp.Component) 1075 } 1076 } 1077 1078 func setRuntimeProperties(creator internal.ProvisionerInputCreator) { 1079 creator.SetKubeconfig("example kubeconfig payload") 1080 creator.SetRuntimeID("runtimeID") 1081 creator.SetInstanceID("instanceID") 1082 creator.SetShootName("shoot-name") 1083 creator.SetShootDomain("shoot-name.domain.sap") 1084 creator.SetShootDNSProviders(fixture.FixDNSProvidersConfig()) 1085 } 1086 1087 func TestCreateUpgradeRuntimeInput_ConfigureAdmins(t *testing.T) { 1088 t.Run("should not overwrite default admin (from user_id)", func(t *testing.T) { 1089 // given 1090 expectedAdmins := []string{"fake-user-id"} 1091 1092 id := uuid.New().String() 1093 1094 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 1095 componentsProvider := &automock.ComponentListProvider{} 1096 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 1097 1098 configProvider := mockConfigProvider() 1099 1100 inputBuilder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 1101 componentsProvider, configProvider, Config{}, "1.24.0", 1102 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 1103 assert.NoError(t, err) 1104 1105 provisioningParams := fixture.FixProvisioningParameters(id) 1106 provisioningParams.ErsContext.UserID = expectedAdmins[0] 1107 1108 ver := internal.RuntimeVersionData{ 1109 Version: "2.4.0", 1110 Origin: internal.Defaults, 1111 } 1112 1113 creator, err := inputBuilder.CreateUpgradeShootInput(provisioningParams, ver) 1114 require.NoError(t, err) 1115 1116 // when 1117 creator.SetProvisioningParameters(provisioningParams) 1118 input, err := creator.CreateUpgradeShootInput() 1119 require.NoError(t, err) 1120 1121 // then 1122 assert.Equal(t, expectedAdmins, input.Administrators) 1123 }) 1124 1125 t.Run("should overwrite default admin with new admins list", func(t *testing.T) { 1126 // given 1127 userId := "fake-user-id" 1128 expectedAdmins := []string{"newAdmin1@test.com", "newAdmin2@test.com"} 1129 1130 id := uuid.New().String() 1131 1132 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 1133 componentsProvider := &automock.ComponentListProvider{} 1134 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 1135 1136 configProvider := mockConfigProvider() 1137 1138 inputBuilder, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 1139 componentsProvider, configProvider, Config{}, "1.24.0", 1140 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 1141 assert.NoError(t, err) 1142 1143 provisioningParams := fixture.FixProvisioningParameters(id) 1144 provisioningParams.ErsContext.UserID = userId 1145 provisioningParams.Parameters.RuntimeAdministrators = expectedAdmins 1146 1147 ver := internal.RuntimeVersionData{ 1148 Version: "2.4.0", 1149 Origin: internal.Defaults, 1150 } 1151 1152 creator, err := inputBuilder.CreateUpgradeShootInput(provisioningParams, ver) 1153 require.NoError(t, err) 1154 1155 // when 1156 creator.SetProvisioningParameters(provisioningParams) 1157 input, err := creator.CreateUpgradeShootInput() 1158 require.NoError(t, err) 1159 1160 // then 1161 assert.Equal(t, expectedAdmins, input.Administrators) 1162 }) 1163 } 1164 1165 func TestCreateUpgradeShootInput_ConfigureAutoscalerParams(t *testing.T) { 1166 t.Run("should not apply CreateUpgradeShootInput with provisioning autoscaler parameters", func(t *testing.T) { 1167 // given 1168 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 1169 componentsProvider := &automock.ComponentListProvider{} 1170 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 1171 1172 configProvider := mockConfigProvider() 1173 1174 ibf, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 1175 componentsProvider, configProvider, Config{}, "1.24.0", 1176 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 1177 assert.NoError(t, err) 1178 1179 //ar provider HyperscalerInputProvider 1180 1181 pp := fixProvisioningParameters(broker.GCPPlanID, "") 1182 //provider = &cloudProvider.GcpInput{} // for broker.GCPPlanID 1183 1184 ver := internal.RuntimeVersionData{ 1185 Version: "2.4.0", 1186 Origin: internal.Defaults, 1187 } 1188 1189 rtinput, err := ibf.CreateUpgradeShootInput(pp, ver) 1190 1191 assert.NoError(t, err) 1192 require.IsType(t, &RuntimeInput{}, rtinput) 1193 1194 rtinput = rtinput.SetProvisioningParameters(pp) 1195 input, err := rtinput.CreateUpgradeShootInput() 1196 assert.NoError(t, err) 1197 1198 expectMaxSurge := *pp.Parameters.MaxSurge 1199 expectMaxUnavailable := *pp.Parameters.MaxUnavailable 1200 t.Logf("%v, %v", expectMaxSurge, expectMaxUnavailable) 1201 1202 assert.Nil(t, input.GardenerConfig.AutoScalerMin) 1203 assert.Nil(t, input.GardenerConfig.AutoScalerMax) 1204 assert.Equal(t, expectMaxSurge, *input.GardenerConfig.MaxSurge) 1205 assert.Equal(t, expectMaxUnavailable, *input.GardenerConfig.MaxUnavailable) 1206 }) 1207 1208 t.Run("should not apply CreateUpgradeShootInput with provider autoscaler parameters", func(t *testing.T) { 1209 // given 1210 optComponentsSvc := dummyOptionalComponentServiceMock(fixKymaComponentList()) 1211 componentsProvider := &automock.ComponentListProvider{} 1212 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(fixKymaComponentList(), nil) 1213 1214 configProvider := mockConfigProvider() 1215 1216 ibf, err := NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), 1217 componentsProvider, configProvider, Config{}, "1.24.0", 1218 fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 1219 assert.NoError(t, err) 1220 1221 pp := fixProvisioningParameters(broker.GCPPlanID, "") 1222 pp.Parameters.AutoScalerMin = nil 1223 pp.Parameters.AutoScalerMax = nil 1224 pp.Parameters.MaxSurge = nil 1225 pp.Parameters.MaxUnavailable = nil 1226 1227 provider := &cloudProvider.GcpInput{} // for broker.GCPPlanID 1228 1229 ver := internal.RuntimeVersionData{ 1230 Version: "2.4.0", 1231 Origin: internal.Defaults, 1232 } 1233 1234 rtinput, err := ibf.CreateUpgradeShootInput(pp, ver) 1235 1236 assert.NoError(t, err) 1237 require.IsType(t, &RuntimeInput{}, rtinput) 1238 1239 rtinput = rtinput.SetProvisioningParameters(pp) 1240 input, err := rtinput.CreateUpgradeShootInput() 1241 assert.NoError(t, err) 1242 1243 expectMaxSurge := provider.Defaults().GardenerConfig.MaxSurge 1244 expectMaxUnavailable := provider.Defaults().GardenerConfig.MaxUnavailable 1245 1246 assert.Nil(t, input.GardenerConfig.AutoScalerMin) 1247 assert.Nil(t, input.GardenerConfig.AutoScalerMax) 1248 assert.Equal(t, expectMaxSurge, *input.GardenerConfig.MaxSurge) 1249 assert.Equal(t, expectMaxUnavailable, *input.GardenerConfig.MaxUnavailable) 1250 }) 1251 } 1252 1253 func assertOverrides(t *testing.T, componentName string, components internal.ComponentConfigurationInputList, overrides []*gqlschema.ConfigEntryInput) { 1254 overriddenComponent, found := find(components, componentName) 1255 require.True(t, found) 1256 1257 assert.Equal(t, overriddenComponent.Configuration, overrides) 1258 } 1259 1260 func find(in internal.ComponentConfigurationInputList, name string) (*gqlschema.ComponentConfigurationInput, bool) { 1261 for _, c := range in { 1262 if c.Component == name { 1263 return c, true 1264 } 1265 } 1266 return nil, false 1267 } 1268 1269 func findForReconciler(in []reconcilerApi.Component, name string) (reconcilerApi.Component, bool) { 1270 for _, c := range in { 1271 if c.Component == name { 1272 return c, true 1273 } 1274 } 1275 return reconcilerApi.Component{}, false 1276 } 1277 1278 func fixKymaComponentList() []internal.KymaComponent { 1279 return []internal.KymaComponent{ 1280 {Name: "dex", Namespace: "kyma-system"}, 1281 {Name: "ory", Namespace: "kyma-system"}, 1282 {Name: "keb", Namespace: "kyma-system"}, 1283 } 1284 } 1285 1286 func dummyOptionalComponentServiceMock(inputComponentList []internal.KymaComponent) *automock.OptionalComponentService { 1287 mappedComponentList := mapToGQLComponentConfigurationInput(inputComponentList) 1288 1289 optComponentsSvc := &automock.OptionalComponentService{} 1290 optComponentsSvc.On("ComputeComponentsToDisable", []string{}).Return([]string{}) 1291 optComponentsSvc.On("ExecuteDisablers", mappedComponentList).Return(mappedComponentList, nil) 1292 return optComponentsSvc 1293 } 1294 1295 func assertContainsAllOverrides(t *testing.T, gotOverrides []*gqlschema.ConfigEntryInput, expOverrides ...[]*gqlschema.ConfigEntryInput) { 1296 var expected []*gqlschema.ConfigEntryInput 1297 for _, o := range expOverrides { 1298 expected = append(expected, o...) 1299 } 1300 1301 require.Len(t, gotOverrides, len(expected)) 1302 for _, o := range expected { 1303 assert.Contains(t, gotOverrides, o) 1304 } 1305 } 1306 1307 func assertContainsAllOverridesForReconciler(t *testing.T, gotOverrides []reconcilerApi.Configuration, expOverrides []*gqlschema.ConfigEntryInput) { 1308 var expected []reconcilerApi.Configuration 1309 for _, o := range expOverrides { 1310 expected = append(expected, reconcilerApi.Configuration{ 1311 Key: o.Key, 1312 Value: o.Value, 1313 Secret: falseIfNil(o.Secret), 1314 }) 1315 } 1316 1317 require.Len(t, gotOverrides, len(expected)) 1318 for _, o := range expected { 1319 assert.Contains(t, gotOverrides, o) 1320 } 1321 } 1322 1323 func assertComponentExists(t *testing.T, 1324 components []*gqlschema.ComponentConfigurationInput, 1325 expected gqlschema.ComponentConfigurationInput) { 1326 1327 for _, component := range components { 1328 if component.Component == expected.Component { 1329 return 1330 } 1331 } 1332 assert.Failf(t, "component list does not contain %s", expected.Component) 1333 } 1334 1335 func mockConfigProvider() ConfigurationProvider { 1336 configProvider := &automock.ConfigurationProvider{} 1337 configProvider.On("ProvideForGivenVersionAndPlan", 1338 mock.AnythingOfType("string"), 1339 mock.AnythingOfType("string")). 1340 Return(&internal.ConfigForPlan{ 1341 AdditionalComponents: []internal.KymaComponent{ 1342 { 1343 Name: "kyma-component1", 1344 Namespace: "kyma-system", 1345 }, 1346 }, 1347 }, nil) 1348 return configProvider 1349 }