github.com/xgoffin/jenkins-library@v1.154.0/cmd/abapEnvironmentCheckoutBranch_test.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "testing" 8 9 "github.com/SAP/jenkins-library/pkg/abaputils" 10 "github.com/pkg/errors" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestCheckoutBranchStep(t *testing.T) { 15 t.Run("Run Step Successful - repositoryName and branchName config", func(t *testing.T) { 16 17 var autils = abaputils.AUtilsMock{} 18 defer autils.Cleanup() 19 autils.ReturnedConnectionDetailsHTTP.Password = "password" 20 autils.ReturnedConnectionDetailsHTTP.User = "user" 21 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 22 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 23 24 config := abapEnvironmentCheckoutBranchOptions{ 25 CfAPIEndpoint: "https://api.endpoint.com", 26 CfOrg: "testOrg", 27 CfSpace: "testSpace", 28 CfServiceInstance: "testInstance", 29 CfServiceKeyName: "testServiceKey", 30 Username: "testUser", 31 Password: "testPassword", 32 RepositoryName: "testRepo1", 33 BranchName: "testBranch", 34 } 35 36 logResultSuccess := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Success", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`) 37 client := &abaputils.ClientMock{ 38 BodyList: []string{ 39 logResultSuccess, 40 `{"d" : { "EntitySets" : [ "LogOverviews" ] } }`, 41 `{"d" : { "status" : "S" } }`, 42 `{"d" : { "status" : "S" } }`, 43 `{"d" : { "status" : "S" } }`, 44 }, 45 Token: "myToken", 46 StatusCode: 200, 47 } 48 49 err := runAbapEnvironmentCheckoutBranch(&config, &autils, client) 50 assert.NoError(t, err, "Did not expect error") 51 }) 52 t.Run("Run Step Failure - empty config", func(t *testing.T) { 53 expectedErrorMessage := "Something failed during the checkout: Checking configuration failed: You have not specified any repository or branch configuration to be checked out in the ABAP Environment System. Please make sure that you specified the repositories with their branches that should be checked out either in a dedicated file or via the parameters 'repositoryName' and 'branchName'. For more information please read the User documentation" 54 55 var autils = abaputils.AUtilsMock{} 56 defer autils.Cleanup() 57 autils.ReturnedConnectionDetailsHTTP.Password = "password" 58 autils.ReturnedConnectionDetailsHTTP.User = "user" 59 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 60 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 61 62 config := abapEnvironmentCheckoutBranchOptions{} 63 64 logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`) 65 client := &abaputils.ClientMock{ 66 BodyList: []string{ 67 logResultError, 68 `{"d" : { "EntitySets" : [ "LogOverviews" ] } }`, 69 `{"d" : { "status" : "E" } }`, 70 `{"d" : { "status" : "E" } }`, 71 `{"d" : { "status" : "E" } }`, 72 }, 73 Token: "myToken", 74 StatusCode: 200, 75 } 76 77 err := runAbapEnvironmentCheckoutBranch(&config, &autils, client) 78 assert.EqualError(t, err, expectedErrorMessage) 79 }) 80 t.Run("Run Step Failure - wrong status", func(t *testing.T) { 81 expectedErrorMessage := "Something failed during the checkout: Checkout failed: Checkout of branch testBranch failed on the ABAP System" 82 83 var autils = abaputils.AUtilsMock{} 84 defer autils.Cleanup() 85 autils.ReturnedConnectionDetailsHTTP.Password = "password" 86 autils.ReturnedConnectionDetailsHTTP.User = "user" 87 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 88 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 89 90 config := abapEnvironmentCheckoutBranchOptions{ 91 CfAPIEndpoint: "https://api.endpoint.com", 92 CfOrg: "testOrg", 93 CfSpace: "testSpace", 94 CfServiceInstance: "testInstance", 95 CfServiceKeyName: "testServiceKey", 96 Username: "testUser", 97 Password: "testPassword", 98 RepositoryName: "testRepo1", 99 BranchName: "testBranch", 100 } 101 102 logResultError := fmt.Sprintf(`{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }`) 103 client := &abaputils.ClientMock{ 104 BodyList: []string{ 105 logResultError, 106 `{"d" : { "EntitySets" : [ "LogOverviews" ] } }`, 107 `{"d" : { "status" : "E" } }`, 108 `{"d" : { "status" : "E" } }`, 109 `{"d" : { "status" : "E" } }`, 110 }, 111 Token: "myToken", 112 StatusCode: 200, 113 } 114 115 err := runAbapEnvironmentCheckoutBranch(&config, &autils, client) 116 assert.EqualError(t, err, expectedErrorMessage) 117 }) 118 t.Run("Success case: checkout Branches from file config", func(t *testing.T) { 119 var autils = abaputils.AUtilsMock{} 120 defer autils.Cleanup() 121 autils.ReturnedConnectionDetailsHTTP.Password = "password" 122 autils.ReturnedConnectionDetailsHTTP.User = "user" 123 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 124 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 125 126 receivedURI := "example.com/Branches" 127 client := &abaputils.ClientMock{ 128 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 129 Token: "myToken", 130 StatusCode: 200, 131 } 132 133 dir, err := ioutil.TempDir("", "test checkout branches") 134 if err != nil { 135 t.Fatal("Failed to create temporary directory") 136 } 137 oldCWD, _ := os.Getwd() 138 _ = os.Chdir(dir) 139 // clean up tmp dir 140 141 defer func() { 142 _ = os.Chdir(oldCWD) 143 _ = os.RemoveAll(dir) 144 }() 145 146 manifestFileString := ` 147 repositories: 148 - name: 'testRepo' 149 branch: 'testBranch' 150 - name: 'testRepo2' 151 branch: 'testBranch2' 152 - name: 'testRepo3' 153 branch: 'testBranch3'` 154 155 err = ioutil.WriteFile("repositoriesTest.yml", []byte(manifestFileString), 0644) 156 157 config := abapEnvironmentCheckoutBranchOptions{ 158 CfAPIEndpoint: "https://api.endpoint.com", 159 CfOrg: "testOrg", 160 CfSpace: "testSpace", 161 CfServiceInstance: "testInstance", 162 CfServiceKeyName: "testServiceKey", 163 Username: "testUser", 164 Password: "testPassword", 165 Repositories: "repositoriesTest.yml", 166 } 167 err = runAbapEnvironmentCheckoutBranch(&config, &autils, client) 168 assert.NoError(t, err) 169 }) 170 t.Run("Failure case: checkout Branches from empty file config", func(t *testing.T) { 171 expectedErrorMessage := "Something failed during the checkout: Error in config file repositoriesTest.yml, AddonDescriptor doesn't contain any repositories" 172 173 var autils = abaputils.AUtilsMock{} 174 defer autils.Cleanup() 175 autils.ReturnedConnectionDetailsHTTP.Password = "password" 176 autils.ReturnedConnectionDetailsHTTP.User = "user" 177 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 178 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 179 180 receivedURI := "example.com/Branches" 181 client := &abaputils.ClientMock{ 182 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 183 Token: "myToken", 184 StatusCode: 200, 185 } 186 187 dir, err := ioutil.TempDir("", "test checkout branches") 188 if err != nil { 189 t.Fatal("Failed to create temporary directory") 190 } 191 oldCWD, _ := os.Getwd() 192 _ = os.Chdir(dir) 193 // clean up tmp dir 194 defer func() { 195 _ = os.Chdir(oldCWD) 196 _ = os.RemoveAll(dir) 197 }() 198 199 manifestFileString := `` 200 201 manifestFileStringBody := []byte(manifestFileString) 202 err = ioutil.WriteFile("repositoriesTest.yml", manifestFileStringBody, 0644) 203 204 config := abapEnvironmentCheckoutBranchOptions{ 205 CfAPIEndpoint: "https://api.endpoint.com", 206 CfOrg: "testOrg", 207 CfSpace: "testSpace", 208 CfServiceInstance: "testInstance", 209 CfServiceKeyName: "testServiceKey", 210 Username: "testUser", 211 Password: "testPassword", 212 Repositories: "repositoriesTest.yml", 213 } 214 err = runAbapEnvironmentCheckoutBranch(&config, &autils, client) 215 assert.EqualError(t, err, expectedErrorMessage) 216 }) 217 t.Run("Failure case: checkout Branches from wrong file config", func(t *testing.T) { 218 expectedErrorMessage := "Something failed during the checkout: Could not unmarshal repositoriesTest.yml" 219 220 var autils = abaputils.AUtilsMock{} 221 defer autils.Cleanup() 222 autils.ReturnedConnectionDetailsHTTP.Password = "password" 223 autils.ReturnedConnectionDetailsHTTP.User = "user" 224 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 225 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 226 227 pollIntervall := abaputils.AUtilsMock{} 228 defer pollIntervall.Cleanup() 229 230 receivedURI := "example.com/Branches" 231 client := &abaputils.ClientMock{ 232 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 233 Token: "myToken", 234 StatusCode: 200, 235 } 236 237 dir, err := ioutil.TempDir("", "test checkout branches") 238 if err != nil { 239 t.Fatal("Failed to create temporary directory") 240 } 241 oldCWD, _ := os.Getwd() 242 _ = os.Chdir(dir) 243 // clean up tmp dir 244 defer func() { 245 _ = os.Chdir(oldCWD) 246 _ = os.RemoveAll(dir) 247 }() 248 249 manifestFileString := ` 250 - repo: 'testRepo' 251 - repo: 'testRepo2'` 252 253 manifestFileStringBody := []byte(manifestFileString) 254 err = ioutil.WriteFile("repositoriesTest.yml", manifestFileStringBody, 0644) 255 256 config := abapEnvironmentCheckoutBranchOptions{ 257 CfAPIEndpoint: "https://api.endpoint.com", 258 CfOrg: "testOrg", 259 CfSpace: "testSpace", 260 CfServiceInstance: "testInstance", 261 CfServiceKeyName: "testServiceKey", 262 Username: "testUser", 263 Password: "testPassword", 264 Repositories: "repositoriesTest.yml", 265 } 266 err = runAbapEnvironmentCheckoutBranch(&config, &autils, client) 267 assert.EqualError(t, err, expectedErrorMessage) 268 }) 269 } 270 271 func TestTriggerCheckout(t *testing.T) { 272 t.Run("Test trigger checkout: success case", func(t *testing.T) { 273 274 // given 275 receivedURI := "example.com/Branches" 276 uriExpected := receivedURI 277 tokenExpected := "myToken" 278 279 client := &abaputils.ClientMock{ 280 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 281 Token: tokenExpected, 282 StatusCode: 200, 283 } 284 config := abapEnvironmentCheckoutBranchOptions{ 285 CfAPIEndpoint: "https://api.endpoint.com", 286 CfOrg: "testOrg", 287 CfSpace: "testSpace", 288 CfServiceInstance: "testInstance", 289 CfServiceKeyName: "testServiceKey", 290 Username: "testUser", 291 Password: "testPassword", 292 RepositoryName: "testRepo1", 293 BranchName: "feature-unit-test", 294 } 295 con := abaputils.ConnectionDetailsHTTP{ 296 User: "MY_USER", 297 Password: "MY_PW", 298 URL: "https://api.endpoint.com/Branches", 299 } 300 // when 301 entityConnection, err := triggerCheckout(config.RepositoryName, config.BranchName, con, client) 302 303 // then 304 assert.NoError(t, err) 305 assert.Equal(t, uriExpected, entityConnection.URL) 306 assert.Equal(t, tokenExpected, entityConnection.XCsrfToken) 307 }) 308 309 t.Run("Test trigger checkout: ABAP Error case", func(t *testing.T) { 310 311 // given 312 errorMessage := "ABAP Error Message" 313 errorCode := "ERROR/001" 314 HTTPErrorMessage := "HTTP Error Message" 315 combinedErrorMessage := "HTTP Error Message: ERROR/001 - ABAP Error Message" 316 317 client := &abaputils.ClientMock{ 318 Body: `{"error" : { "code" : "` + errorCode + `", "message" : { "lang" : "en", "value" : "` + errorMessage + `" } } }`, 319 Token: "myToken", 320 StatusCode: 400, 321 Error: errors.New(HTTPErrorMessage), 322 } 323 config := abapEnvironmentCheckoutBranchOptions{ 324 CfAPIEndpoint: "https://api.endpoint.com", 325 CfOrg: "testOrg", 326 CfSpace: "testSpace", 327 CfServiceInstance: "testInstance", 328 CfServiceKeyName: "testServiceKey", 329 Username: "testUser", 330 Password: "testPassword", 331 RepositoryName: "testRepo1", 332 BranchName: "feature-unit-test", 333 } 334 con := abaputils.ConnectionDetailsHTTP{ 335 User: "MY_USER", 336 Password: "MY_PW", 337 URL: "https://api.endpoint.com/Branches", 338 } 339 340 // when 341 _, err := triggerCheckout(config.RepositoryName, config.BranchName, con, client) 342 343 // then 344 assert.Equal(t, combinedErrorMessage, err.Error(), "Different error message expected") 345 }) 346 } 347 348 func TestCheckoutConfigChecker(t *testing.T) { 349 t.Run("Success case: check config", func(t *testing.T) { 350 config := abapEnvironmentCheckoutBranchOptions{ 351 RepositoryName: "testRepo1", 352 BranchName: "feature-unit-test", 353 } 354 err := checkCheckoutBranchRepositoryConfiguration(config) 355 assert.NoError(t, err) 356 }) 357 t.Run("Success case: check file config", func(t *testing.T) { 358 config := abapEnvironmentCheckoutBranchOptions{ 359 Repositories: "test.file", 360 BranchName: "feature-unit-test", 361 } 362 err := checkCheckoutBranchRepositoryConfiguration(config) 363 assert.NoError(t, err) 364 }) 365 t.Run("Failure case: check empty config", func(t *testing.T) { 366 expectedErrorMessage := "Checking configuration failed: You have not specified any repository or branch configuration to be checked out in the ABAP Environment System. Please make sure that you specified the repositories with their branches that should be checked out either in a dedicated file or via the parameters 'repositoryName' and 'branchName'. For more information please read the User documentation" 367 368 config := abapEnvironmentCheckoutBranchOptions{} 369 err := checkCheckoutBranchRepositoryConfiguration(config) 370 assert.Equal(t, expectedErrorMessage, err.Error(), "Different error message expected") 371 }) 372 }