github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/input/builder_test.go (about) 1 package input 2 3 import ( 4 "testing" 5 6 "github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema" 7 "github.com/kyma-project/kyma-environment-broker/internal" 8 "github.com/kyma-project/kyma-environment-broker/internal/broker" 9 "github.com/kyma-project/kyma-environment-broker/internal/fixture" 10 "github.com/kyma-project/kyma-environment-broker/internal/process/input/automock" 11 cloudProvider "github.com/kyma-project/kyma-environment-broker/internal/provider" 12 "github.com/kyma-project/kyma-environment-broker/internal/ptr" 13 "github.com/kyma-project/kyma-environment-broker/internal/runtime" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/mock" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestInputBuilderFactory_IsPlanSupport(t *testing.T) { 20 // given 21 componentsProvider := &automock.ComponentListProvider{} 22 defer componentsProvider.AssertExpectations(t) 23 24 configProvider := mockConfigProvider() 25 26 ibf, err := NewInputBuilderFactory(nil, runtime.NewDisabledComponentsProvider(), componentsProvider, 27 configProvider, Config{}, "1.10", fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 28 assert.NoError(t, err) 29 30 // when/then 31 assert.True(t, ibf.IsPlanSupport(broker.GCPPlanID)) 32 assert.True(t, ibf.IsPlanSupport(broker.AzurePlanID)) 33 assert.True(t, ibf.IsPlanSupport(broker.AWSPlanID)) 34 assert.True(t, ibf.IsPlanSupport(broker.TrialPlanID)) 35 assert.True(t, ibf.IsPlanSupport(broker.FreemiumPlanID)) 36 } 37 38 func TestInputBuilderFactory_ForPlan(t *testing.T) { 39 t.Run("should build RuntimeInput with default version Kyma components and ProvisionRuntimeInput", func(t *testing.T) { 40 // given 41 componentsProvider := &automock.ComponentListProvider{} 42 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), 43 mock.AnythingOfType("*internal.ConfigForPlan")).Return([]internal.KymaComponent{}, nil).Once() 44 defer componentsProvider.AssertExpectations(t) 45 46 configProvider := mockConfigProvider() 47 48 ibf, err := NewInputBuilderFactory(nil, runtime.NewDisabledComponentsProvider(), componentsProvider, 49 configProvider, Config{}, "1.10", fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 50 assert.NoError(t, err) 51 pp := fixProvisioningParameters(broker.GCPPlanID, "") 52 53 // when 54 input, err := ibf.CreateProvisionInput(pp, internal.RuntimeVersionData{ 55 Version: "1.10", 56 Origin: internal.Defaults, 57 }) 58 59 // Then 60 assert.NoError(t, err) 61 require.IsType(t, &RuntimeInput{}, input) 62 63 result := input.(*RuntimeInput) 64 assert.NotNil(t, result.provisionRuntimeInput) 65 assert.Nil(t, result.upgradeRuntimeInput.KymaConfig) 66 67 }) 68 69 t.Run("should build RuntimeInput with default version Kyma components and UpgradeRuntimeInput", func(t *testing.T) { 70 // given 71 componentsProvider := &automock.ComponentListProvider{} 72 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return([]internal. 73 KymaComponent{}, nil) 74 defer componentsProvider.AssertExpectations(t) 75 76 configProvider := mockConfigProvider() 77 78 ibf, err := NewInputBuilderFactory(nil, runtime.NewDisabledComponentsProvider(), componentsProvider, 79 configProvider, Config{}, "1.10", fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 80 assert.NoError(t, err) 81 pp := fixProvisioningParameters(broker.GCPPlanID, "") 82 83 // when 84 input, err := ibf.CreateUpgradeInput(pp, internal.RuntimeVersionData{Version: "1.1.0", Origin: internal.Defaults}) 85 86 // Then 87 assert.NoError(t, err) 88 require.IsType(t, &RuntimeInput{}, input) 89 90 result := input.(*RuntimeInput) 91 assert.NotNil(t, result.upgradeRuntimeInput) 92 }) 93 94 t.Run("should build RuntimeInput with GA version Kyma components and UpgradeRuntimeInput", func(t *testing.T) { 95 // given 96 componentsProvider := &automock.ComponentListProvider{} 97 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return([]internal. 98 KymaComponent{}, nil) 99 defer componentsProvider.AssertExpectations(t) 100 101 configProvider := mockConfigProvider() 102 103 ibf, err := NewInputBuilderFactory(nil, runtime.NewDisabledComponentsProvider(), componentsProvider, 104 configProvider, Config{}, "1.10", fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 105 assert.NoError(t, err) 106 pp := fixProvisioningParameters(broker.GCPPlanID, "") 107 108 // when 109 input, err := ibf.CreateUpgradeInput(pp, internal.RuntimeVersionData{Version: "1.1.0", Origin: internal.AccountMapping}) 110 111 // Then 112 assert.NoError(t, err) 113 require.IsType(t, &RuntimeInput{}, input) 114 115 result := input.(*RuntimeInput) 116 assert.NotNil(t, result.upgradeRuntimeInput) 117 }) 118 119 t.Run("should build RuntimeInput with set version Kyma components", func(t *testing.T) { 120 // given 121 componentsProvider := &automock.ComponentListProvider{} 122 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return([]internal.KymaComponent{}, nil).Once() 123 defer componentsProvider.AssertExpectations(t) 124 125 configProvider := mockConfigProvider() 126 127 ibf, err := NewInputBuilderFactory(nil, runtime.NewDisabledComponentsProvider(), componentsProvider, 128 configProvider, Config{}, "1.10", fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 129 assert.NoError(t, err) 130 pp := fixProvisioningParameters(broker.GCPPlanID, "PR-1") 131 132 // when 133 input, err := ibf.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "PR-1", Origin: internal.Parameters}) 134 135 // Then 136 assert.NoError(t, err) 137 assert.IsType(t, &RuntimeInput{}, input) 138 }) 139 140 t.Run("should build RuntimeInput with proper plan", func(t *testing.T) { 141 // given 142 componentsProvider := &automock.ComponentListProvider{} 143 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return([]internal.KymaComponent{}, nil).Once() 144 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return([]internal.KymaComponent{}, nil).Once() 145 defer componentsProvider.AssertExpectations(t) 146 147 configProvider := mockConfigProvider() 148 149 ibf, err := NewInputBuilderFactory(nil, runtime.NewDisabledComponentsProvider(), componentsProvider, 150 configProvider, Config{}, "1.10", fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 151 assert.NoError(t, err) 152 pp := fixProvisioningParameters(broker.GCPPlanID, "") 153 154 // when 155 input, err := ibf.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.1.0", Origin: internal.Defaults}) 156 157 // Then 158 assert.NoError(t, err) 159 require.IsType(t, &RuntimeInput{}, input) 160 161 result := input.(*RuntimeInput) 162 assert.Equal(t, gqlschema.KymaProfileProduction, *result.provisionRuntimeInput.KymaConfig.Profile) 163 164 // given 165 pp = fixProvisioningParameters(broker.TrialPlanID, "") 166 167 // when 168 input, err = ibf.CreateProvisionInput(pp, internal.RuntimeVersionData{Version: "1.1.0", Origin: internal.Defaults}) 169 170 // Then 171 assert.NoError(t, err) 172 require.IsType(t, &RuntimeInput{}, input) 173 174 result = input.(*RuntimeInput) 175 assert.Equal(t, gqlschema.KymaProfileEvaluation, *result.provisionRuntimeInput.KymaConfig.Profile) 176 177 }) 178 179 t.Run("should build UpgradeRuntimeInput with proper profile", func(t *testing.T) { 180 // given 181 componentsProvider := &automock.ComponentListProvider{} 182 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return([]internal.KymaComponent{}, nil) 183 componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return([]internal.KymaComponent{}, nil) 184 defer componentsProvider.AssertExpectations(t) 185 186 configProvider := mockConfigProvider() 187 188 ibf, err := NewInputBuilderFactory(nil, runtime.NewDisabledComponentsProvider(), componentsProvider, 189 configProvider, Config{}, "1.10", fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 190 assert.NoError(t, err) 191 pp := fixProvisioningParameters(broker.GCPPlanID, "") 192 193 // when 194 input, err := ibf.CreateUpgradeInput(pp, internal.RuntimeVersionData{Version: "1.1.0", Origin: internal.Defaults}) 195 196 // Then 197 assert.NoError(t, err) 198 require.IsType(t, &RuntimeInput{}, input) 199 200 result := input.(*RuntimeInput) 201 assert.NotNil(t, result.upgradeRuntimeInput) 202 assert.NotNil(t, result.upgradeRuntimeInput.KymaConfig.Profile) 203 assert.Equal(t, gqlschema.KymaProfileProduction, *result.upgradeRuntimeInput.KymaConfig.Profile) 204 205 // given 206 pp = fixProvisioningParameters(broker.TrialPlanID, "") 207 provider := internal.GCP 208 pp.Parameters.Provider = &provider 209 // when 210 input, err = ibf.CreateUpgradeInput(pp, internal.RuntimeVersionData{Version: "1.1.0", Origin: internal.Defaults}) 211 212 // Then 213 assert.NoError(t, err) 214 require.IsType(t, &RuntimeInput{}, input) 215 216 result = input.(*RuntimeInput) 217 assert.NotNil(t, result.upgradeRuntimeInput) 218 assert.NotNil(t, result.upgradeRuntimeInput.KymaConfig.Profile) 219 assert.Equal(t, gqlschema.KymaProfileEvaluation, *result.upgradeRuntimeInput.KymaConfig.Profile) 220 }) 221 222 t.Run("should build CreateUpgradeShootInput with proper autoscaler parameters", func(t *testing.T) { 223 // given 224 var provider HyperscalerInputProvider 225 226 componentsProvider := &automock.ComponentListProvider{} 227 defer componentsProvider.AssertExpectations(t) 228 229 configProvider := mockConfigProvider() 230 231 ibf, err := NewInputBuilderFactory(nil, runtime.NewDisabledComponentsProvider(), componentsProvider, 232 configProvider, Config{}, "1.10", fixTrialRegionMapping(), fixTrialProviders(), fixture.FixOIDCConfigDTO()) 233 assert.NoError(t, err) 234 pp := fixProvisioningParameters(broker.GCPPlanID, "") 235 provider = &cloudProvider.GcpInput{} // for broker.GCPPlanID 236 ver := internal.RuntimeVersionData{ 237 Version: "2.4.0", 238 Origin: internal.Defaults, 239 } 240 241 // when 242 input, err := ibf.CreateUpgradeShootInput(pp, ver) 243 244 // Then 245 assert.NoError(t, err) 246 require.IsType(t, &RuntimeInput{}, input) 247 248 result := input.(*RuntimeInput) 249 maxSurge := *result.upgradeShootInput.GardenerConfig.MaxSurge 250 maxUnavailable := *result.upgradeShootInput.GardenerConfig.MaxUnavailable 251 252 assert.Nil(t, result.upgradeShootInput.GardenerConfig.AutoScalerMax) 253 assert.Nil(t, result.upgradeShootInput.GardenerConfig.AutoScalerMin) 254 assert.Equal(t, maxSurge, provider.Defaults().GardenerConfig.MaxSurge) 255 assert.Equal(t, maxUnavailable, provider.Defaults().GardenerConfig.MaxUnavailable) 256 t.Logf("%v, %v", maxSurge, maxUnavailable) 257 }) 258 259 } 260 261 func fixProvisioningParameters(planID, kymaVersion string) internal.ProvisioningParameters { 262 pp := fixture.FixProvisioningParameters("") 263 pp.PlanID = planID 264 pp.Parameters.KymaVersion = kymaVersion 265 pp.Parameters.AutoScalerMin = ptr.Integer(1) 266 pp.Parameters.AutoScalerMax = ptr.Integer(1) 267 268 return pp 269 } 270 271 func fixTrialRegionMapping() map[string]string { 272 return map[string]string{} 273 }