github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/runtime/provider_test.go (about) 1 package runtime_test 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 "path" 7 "testing" 8 9 "github.com/kyma-project/kyma-environment-broker/internal" 10 kebError "github.com/kyma-project/kyma-environment-broker/internal/error" 11 "github.com/kyma-project/kyma-environment-broker/internal/runtime" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 "gopkg.in/yaml.v2" 16 ) 17 18 func TestRuntimeComponentProviderGetSuccess(t *testing.T) { 19 type given struct { 20 kymaVersion internal.RuntimeVersionData 21 managedRuntimeComponentsYAMLPath string 22 newAdditionalRuntimeComponentsYAMLPath string 23 } 24 tests := []struct { 25 name string 26 given given 27 expectedRequestURL string 28 }{ 29 { 30 name: "Provide release Kyma version 1.x", 31 given: given{ 32 kymaVersion: internal.RuntimeVersionData{Version: "1.9.0", MajorVersion: 1}, 33 managedRuntimeComponentsYAMLPath: path.Join("testdata", "managed-runtime-components.yaml"), 34 newAdditionalRuntimeComponentsYAMLPath: path.Join("testdata", "additional-runtime-components.yaml"), 35 }, 36 expectedRequestURL: "https://storage.googleapis.com/kyma-prow-artifacts/1.9.0/kyma-installer-cluster.yaml", 37 }, 38 { 39 name: "Provide on-demand Kyma version based on 1.x", 40 given: given{ 41 kymaVersion: internal.RuntimeVersionData{Version: "main-ece6e5d9", MajorVersion: 1}, 42 managedRuntimeComponentsYAMLPath: path.Join("testdata", "managed-runtime-components.yaml"), 43 newAdditionalRuntimeComponentsYAMLPath: path.Join("testdata", "additional-runtime-components.yaml"), 44 }, 45 expectedRequestURL: "https://storage.googleapis.com/kyma-development-artifacts/main-ece6e5d9/kyma-installer-cluster.yaml", 46 }, 47 { 48 name: "Provide release Kyma version 2.0.0", 49 given: given{ 50 kymaVersion: internal.RuntimeVersionData{Version: "2.0.0", MajorVersion: 2}, 51 managedRuntimeComponentsYAMLPath: path.Join("testdata", "managed-runtime-components.yaml"), 52 newAdditionalRuntimeComponentsYAMLPath: path.Join("testdata", "additional-runtime-components.yaml"), 53 }, 54 expectedRequestURL: "https://github.com/kyma-project/kyma/releases/download/2.0.0/kyma-components.yaml", 55 }, 56 { 57 name: "Provide on-demand Kyma version based on 2.0", 58 given: given{ 59 kymaVersion: internal.RuntimeVersionData{Version: "main-ece6e5d9", MajorVersion: 2}, 60 managedRuntimeComponentsYAMLPath: path.Join("testdata", "managed-runtime-components.yaml"), 61 newAdditionalRuntimeComponentsYAMLPath: path.Join("testdata", "additional-runtime-components.yaml"), 62 }, 63 expectedRequestURL: "https://storage.googleapis.com/kyma-development-artifacts/main-ece6e5d9/kyma-components.yaml", 64 }, 65 } 66 for _, tc := range tests { 67 t.Run(tc.name, func(t *testing.T) { 68 // given 69 installerYAML := runtime.ReadYAMLFromFile(t, "kyma-installer-cluster.yaml") 70 componentsYAML := runtime.ReadYAMLFromFile(t, "kyma-components.yaml") 71 fakeHTTPClient := runtime.NewTestClient(t, installerYAML, componentsYAML, http.StatusOK) 72 73 listProvider := runtime.NewComponentsListProvider( 74 tc.given.managedRuntimeComponentsYAMLPath, 75 tc.given.newAdditionalRuntimeComponentsYAMLPath).WithHTTPClient(fakeHTTPClient) 76 77 expAdditionalComponents := make([]internal.KymaComponent, 0) 78 if tc.given.kymaVersion.MajorVersion > 1 { 79 expAdditionalComponents = readManagedComponentsFromFile(t, tc.given.newAdditionalRuntimeComponentsYAMLPath) 80 } else { 81 expAdditionalComponents = readManagedComponentsFromFile(t, tc.given.managedRuntimeComponentsYAMLPath) 82 } 83 84 // when 85 allComponents, err := listProvider.AllComponents(tc.given.kymaVersion, nil) 86 87 // then 88 require.NoError(t, err) 89 assert.NotNil(t, allComponents) 90 91 assert.Equal(t, tc.expectedRequestURL, fakeHTTPClient.RequestURL) 92 assertManagedComponentsAtTheEndOfList(t, allComponents, expAdditionalComponents) 93 }) 94 } 95 } 96 97 func TestRuntimeComponentProviderGetFailures(t *testing.T) { 98 type given struct { 99 kymaVersion internal.RuntimeVersionData 100 managedRuntimeComponentsYAMLPath string 101 newAdditionalRuntimeComponentsYAMLPath string 102 httpErrMessage string 103 } 104 tests := []struct { 105 name string 106 given given 107 returnStatusCode int 108 tempError bool 109 expErrMessage string 110 }{ 111 { 112 name: "Provide release version not found", 113 given: given{ 114 kymaVersion: internal.RuntimeVersionData{Version: "111.000.111", MajorVersion: 1}, 115 managedRuntimeComponentsYAMLPath: path.Join("testdata", "managed-runtime-components.yaml"), 116 newAdditionalRuntimeComponentsYAMLPath: path.Join("testdata", "additional-runtime-components.yaml"), 117 httpErrMessage: "Not Found", 118 }, 119 returnStatusCode: http.StatusNotFound, 120 tempError: false, 121 expErrMessage: "while getting Kyma components: while checking response status code for Kyma components list: got unexpected status code, want 200, got 404, url: https://storage.googleapis.com/kyma-prow-artifacts/111.000.111/kyma-installer-cluster.yaml, body: Not Found", 122 }, 123 { 124 name: "Provide on-demand version not found", 125 given: given{ 126 kymaVersion: internal.RuntimeVersionData{Version: "main-123123", MajorVersion: 1}, 127 managedRuntimeComponentsYAMLPath: path.Join("testdata", "managed-runtime-components.yaml"), 128 newAdditionalRuntimeComponentsYAMLPath: path.Join("testdata", "additional-runtime-components.yaml"), 129 httpErrMessage: "Not Found", 130 }, 131 returnStatusCode: http.StatusNotFound, 132 tempError: false, 133 expErrMessage: "while getting Kyma components: while checking response status code for Kyma components list: got unexpected status code, want 200, got 404, url: https://storage.googleapis.com/kyma-development-artifacts/main-123123/kyma-installer-cluster.yaml, body: Not Found", 134 }, 135 { 136 name: "Provide on-demand version not found, temporary server error", 137 given: given{ 138 kymaVersion: internal.RuntimeVersionData{Version: "main-123123", MajorVersion: 1}, 139 managedRuntimeComponentsYAMLPath: path.Join("testdata", "managed-runtime-components.yaml"), 140 newAdditionalRuntimeComponentsYAMLPath: path.Join("testdata", "additional-runtime-components.yaml"), 141 httpErrMessage: "Internal Server Error", 142 }, 143 returnStatusCode: http.StatusInternalServerError, 144 tempError: true, 145 expErrMessage: "while getting Kyma components: while checking response status code for Kyma components list: got unexpected status code, want 200, got 500, url: https://storage.googleapis.com/kyma-development-artifacts/main-123123/kyma-installer-cluster.yaml, body: Internal Server Error", 146 }, 147 } 148 for _, tc := range tests { 149 t.Run(tc.name, func(t *testing.T) { 150 // given 151 fakeHTTPClient := runtime.NewTestClient(t, tc.given.httpErrMessage, tc.given.httpErrMessage, tc.returnStatusCode) 152 153 listProvider := runtime.NewComponentsListProvider( 154 tc.given.managedRuntimeComponentsYAMLPath, 155 tc.given.newAdditionalRuntimeComponentsYAMLPath). 156 WithHTTPClient(fakeHTTPClient) 157 158 // when 159 components, err := listProvider.AllComponents(tc.given.kymaVersion, nil) 160 161 // then 162 assert.Nil(t, components) 163 assert.EqualError(t, err, tc.expErrMessage) 164 assert.Equal(t, tc.tempError, kebError.IsTemporaryError(err)) 165 }) 166 } 167 } 168 169 func assertManagedComponentsAtTheEndOfList(t *testing.T, allComponents, managedComponents []internal.KymaComponent) { 170 t.Helper() 171 172 assert.NotPanics(t, func() { 173 idx := len(allComponents) - len(managedComponents) 174 endOfList := allComponents[idx:] 175 176 assert.Equal(t, endOfList, managedComponents) 177 }) 178 } 179 180 func readManagedComponentsFromFile(t *testing.T, path string) []internal.KymaComponent { 181 t.Helper() 182 183 yamlFile, err := ioutil.ReadFile(path) 184 require.NoError(t, err) 185 186 var managedList struct { 187 Components []internal.KymaComponent `json:"components"` 188 } 189 err = yaml.Unmarshal(yamlFile, &managedList) 190 require.NoError(t, err) 191 192 return managedList.Components 193 }