github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/runtime/components_provider_test.go (about) 1 package runtime_test 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/kyma-project/kyma-environment-broker/internal" 10 "github.com/kyma-project/kyma-environment-broker/internal/runtime" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 const ( 16 kymaVersion = "2.2.0" 17 requiredComponentsURLFormat = "https://github.com/kyma-project/kyma/releases/download/%s/kyma-components.yaml" 18 ) 19 20 func TestComponentsProviderSuccessFlow(t *testing.T) { 21 t.Run("should fetch required components and additional components", func(t *testing.T) { 22 // given 23 cfg := fixConfigForPlan() 24 componentsProvider := runtime.NewFakeComponentsProvider() 25 expectedPrerequisiteComponent := internal.KymaComponent{ 26 Name: "cluster-essentials", 27 Namespace: "kyma-system", 28 } 29 expectedRequiredComponent := internal.KymaComponent{ 30 Name: "serverless", 31 Namespace: "kyma-system", 32 } 33 expectedAdditionalComponent := internal.KymaComponent{ 34 Name: "new-component1", 35 Namespace: "kyma-system", 36 Source: &internal.ComponentSource{ 37 URL: "https://local.test/kyma-additional-components/new-component1.tgz"}, 38 } 39 unexpectedAdditionalComponent := internal.KymaComponent{ 40 Name: "test-component1", 41 Namespace: "kyma-system", 42 } 43 44 // when 45 allComponents, err := componentsProvider.AllComponents(internal.RuntimeVersionData{ 46 Version: kymaVersion, 47 Origin: internal.Parameters, 48 MajorVersion: 2, 49 }, cfg) 50 51 // then 52 require.NoError(t, err) 53 assert.NotEmpty(t, allComponents) 54 assert.Contains(t, allComponents, expectedPrerequisiteComponent) 55 assert.Contains(t, allComponents, expectedRequiredComponent) 56 assert.Contains(t, allComponents, expectedAdditionalComponent) 57 assert.NotContains(t, allComponents, unexpectedAdditionalComponent) 58 }) 59 } 60 61 func TestComponentsProviderErrors(t *testing.T) { 62 t.Run("should return 404 Not Found when required components are not available for given Kyma version", func(t *testing.T) { 63 // given 64 cfg := fixConfigForPlan() 65 componentsProvider := runtime.NewFakeComponentsProvider() 66 67 wrongVer := "test-2.2.0" 68 69 errMsg := fmt.Sprintf("while checking response status code for Kyma components list: "+ 70 "got unexpected status code, want %d, got %d, url: %s, body: %s", 71 http.StatusOK, 404, fmt.Sprintf(requiredComponentsURLFormat, wrongVer), "") 72 expectedErr := throwError(errMsg) 73 74 // when 75 _, err := componentsProvider.AllComponents(internal.RuntimeVersionData{ 76 Version: wrongVer, 77 Origin: internal.Parameters, 78 MajorVersion: 2, 79 }, cfg) 80 81 // then 82 require.Error(t, err) 83 assert.Equal(t, expectedErr, errors.Unwrap(err)) 84 }) 85 } 86 87 func throwError(message string) error { 88 return fmt.Errorf(message) 89 } 90 91 func fixConfigForPlan() *internal.ConfigForPlan { 92 return &internal.ConfigForPlan{AdditionalComponents: []internal.KymaComponent{ 93 { 94 Name: "compass-runtime-agent", 95 Namespace: "kyma-system", 96 }, 97 { 98 Name: "new-component1", 99 Namespace: "kyma-system", 100 Source: &internal.ComponentSource{URL: "https://local.test/kyma-additional-components/new-component1.tgz"}, 101 }, 102 { 103 Name: "new-component2", 104 Namespace: "kyma-system", 105 Source: &internal.ComponentSource{URL: "https://local.test/kyma-additional-components/new-component2.tgz"}, 106 }, 107 { 108 Name: "new-component3", 109 Namespace: "kyma-system", 110 }, 111 }} 112 }