github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/config/provider_test.go (about) 1 package config_test 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "strings" 8 "testing" 9 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/config" 13 "github.com/sirupsen/logrus" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 "sigs.k8s.io/controller-runtime/pkg/client/fake" 17 ) 18 19 const wrongConfigPlan = "wrong" 20 21 func TestConfigProvider(t *testing.T) { 22 // setup 23 ctx := context.TODO() 24 cfgMap, err := fixConfigMap() 25 require.NoError(t, err) 26 27 fakeK8sClient := fake.NewClientBuilder().WithRuntimeObjects(cfgMap).Build() 28 logger := logrus.New() 29 logger.SetFormatter(&logrus.JSONFormatter{}) 30 cfgReader := config.NewConfigMapReader(ctx, fakeK8sClient, logger, kymaVersion) 31 cfgValidator := config.NewConfigMapKeysValidator() 32 cfgConverter := config.NewConfigMapConverter() 33 cfgProvider := config.NewConfigProvider(cfgReader, cfgValidator, cfgConverter) 34 35 t.Run("should provide config for Kyma 2.4.0 azure plan", func(t *testing.T) { 36 // given 37 expectedCfg := fixAzureConfig() 38 // when 39 cfg, err := cfgProvider.ProvideForGivenVersionAndPlan(kymaVersion, broker.AzurePlanName) 40 41 // then 42 require.NoError(t, err) 43 assert.Len(t, cfg.AdditionalComponents, len(expectedCfg.AdditionalComponents)) 44 assert.ObjectsAreEqual(expectedCfg, cfg) 45 }) 46 47 t.Run("should provide config for a default", func(t *testing.T) { 48 // given 49 expectedCfg := fixDefault() 50 // when 51 cfg, err := cfgProvider.ProvideForGivenVersionAndPlan(kymaVersion, broker.AWSPlanName) 52 53 // then 54 require.NoError(t, err) 55 assert.Len(t, cfg.AdditionalComponents, len(expectedCfg.AdditionalComponents)) 56 assert.ObjectsAreEqual(expectedCfg, cfg) 57 }) 58 59 t.Run("should provide config for default Kyma version and azure plan when PR-* Kyma version is passed", func(t *testing.T) { 60 // given 61 expectedCfg := fixAzureConfig() 62 customKymaVer := "PR-1234" 63 // when 64 cfg, err := cfgProvider.ProvideForGivenVersionAndPlan(customKymaVer, broker.AzurePlanName) 65 66 // then 67 require.NoError(t, err) 68 assert.Len(t, cfg.AdditionalComponents, len(expectedCfg.AdditionalComponents)) 69 assert.ObjectsAreEqual(expectedCfg, cfg) 70 }) 71 72 t.Run("should provide config for default Kyma version and azure plan when main-* Kyma version is passed", func(t *testing.T) { 73 // given 74 expectedCfg := fixAzureConfig() 75 customKymaVer := "main-fffff" 76 // when 77 cfg, err := cfgProvider.ProvideForGivenVersionAndPlan(customKymaVer, broker.AzurePlanName) 78 79 // then 80 require.NoError(t, err) 81 assert.Len(t, cfg.AdditionalComponents, len(expectedCfg.AdditionalComponents)) 82 assert.ObjectsAreEqual(expectedCfg, cfg) 83 }) 84 85 t.Run("validator should return error indicating missing required fields", func(t *testing.T) { 86 // given 87 expectedMissingConfigKeys := []string{ 88 "additional-components", 89 } 90 expectedErrMsg := fmt.Sprintf("missing required configuration entires: %s", strings.Join(expectedMissingConfigKeys, ",")) 91 // when 92 cfg, err := cfgProvider.ProvideForGivenVersionAndPlan(kymaVersion, wrongConfigPlan) 93 94 // then 95 require.Error(t, err) 96 assert.ErrorContains(t, err, expectedErrMsg) 97 assert.Nil(t, cfg) 98 }) 99 100 t.Run("reader should return error indicating missing configmap", func(t *testing.T) { 101 // given 102 err = fakeK8sClient.Delete(ctx, cfgMap) 103 require.NoError(t, err) 104 105 // when 106 cfg, err := cfgProvider.ProvideForGivenVersionAndPlan(kymaVersion, broker.AzurePlanName) 107 108 // then 109 require.Error(t, err) 110 assert.Equal(t, "configmap with configuration does not exist", errors.Unwrap(err).Error()) 111 assert.Nil(t, cfg) 112 }) 113 } 114 115 func fixAzureConfig() *internal.ConfigForPlan { 116 return &internal.ConfigForPlan{ 117 AdditionalComponents: []internal.KymaComponent{ 118 { 119 Name: "additional-component1", 120 Namespace: "kyma-system", 121 }, 122 { 123 Name: "additional-component2", 124 Namespace: "test-system", 125 }, 126 { 127 Name: "azure-component", 128 Namespace: "azure-system", 129 Source: &internal.ComponentSource{URL: "https://azure.domain/component/azure-component.git"}, 130 }, 131 }} 132 } 133 134 func fixDefault() *internal.ConfigForPlan { 135 return &internal.ConfigForPlan{ 136 KymaTemplate: `apiVersion: operator.kyma-project.io/v1beta2 137 kind: Kyma 138 metadata: 139 name: my-kyma1 140 namespace: kyma-system 141 spec: 142 channel: stable 143 modules: 144 - name: istio`, 145 AdditionalComponents: []internal.KymaComponent{ 146 { 147 Name: "additional-component1", 148 Namespace: "kyma-system", 149 }, 150 { 151 Name: "additional-component2", 152 Namespace: "test-system", 153 }, 154 { 155 Name: "azure-component", 156 Namespace: "azure-system", 157 Source: &internal.ComponentSource{URL: "https://azure.domain/component/azure-component.git"}, 158 }, 159 }} 160 }