github.com/jaylevin/jenkins-library@v1.230.4/cmd/gctsDeploy_test.go (about) 1 package cmd 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestGctsPullByCommitSuccess(t *testing.T) { 10 11 config := gctsDeployOptions{ 12 Host: "http://testHost.com:50000", 13 Client: "000", 14 Repository: "testRepo", 15 Username: "testUser", 16 Password: "testPassword", 17 } 18 19 t.Run("deploy latest commit", func(t *testing.T) { 20 21 httpClient := httpMockGcts{StatusCode: 200, ResponseBody: `{ 22 "trkorr": "SIDK1234567", 23 "fromCommit": "f1cdb6a032c1d8187c0990b51e94e8d8bb9898b2", 24 "toCommit": "f1cdb6a032c1d8187c0990b51e94e8d8bb9898b2", 25 "log": [ 26 { 27 "time": 20180606130524, 28 "user": "JENKINS", 29 "section": "REPOSITORY_FACTORY", 30 "action": "CREATE_REPOSITORY", 31 "severity": "INFO", 32 "message": "Start action CREATE_REPOSITORY review", 33 "code": "GCTS.API.410" 34 } 35 ] 36 }`} 37 38 err := pullByCommit(&config, nil, nil, &httpClient) 39 40 if assert.NoError(t, err) { 41 42 t.Run("check url", func(t *testing.T) { 43 assert.Equal(t, "http://testHost.com:50000/sap/bc/cts_abapvcs/repository/testRepo/pullByCommit?sap-client=000&request=", httpClient.URL) 44 }) 45 46 t.Run("check method", func(t *testing.T) { 47 assert.Equal(t, "GET", httpClient.Method) 48 }) 49 50 t.Run("check user", func(t *testing.T) { 51 assert.Equal(t, "testUser", httpClient.Options.Username) 52 }) 53 54 t.Run("check password", func(t *testing.T) { 55 assert.Equal(t, "testPassword", httpClient.Options.Password) 56 }) 57 58 } 59 60 }) 61 } 62 63 func TestGctsPullByCommitFailure(t *testing.T) { 64 65 config := gctsDeployOptions{ 66 Host: "http://testHost.com:50000", 67 Client: "000", 68 Repository: "testRepo", 69 Username: "testUser", 70 Password: "testPassword", 71 } 72 73 t.Run("http error occurred", func(t *testing.T) { 74 75 httpClient := httpMockGcts{StatusCode: 500, ResponseBody: `{ 76 "exception": "No relation between system and repository" 77 }`} 78 79 err := pullByCommit(&config, nil, nil, &httpClient) 80 81 assert.EqualError(t, err, "a http error occurred") 82 83 }) 84 85 } 86 87 func TestGctsGetRepositorySuccess(t *testing.T) { 88 config := gctsDeployOptions{ 89 Host: "http://testHost.com:50000", 90 Client: "000", 91 Repository: "testRepo", 92 Username: "testUser", 93 Password: "testPassword", 94 } 95 t.Run("Get Repository Success Test", func(t *testing.T) { 96 var httpClient httpMockGcts 97 if config.Repository == "testRepo" { 98 httpClient = httpMockGcts{StatusCode: 200, ResponseBody: `{ 99 "result": { 100 "rid": "testrepo", 101 "name": "testRepo", 102 "role": "SOURCE", 103 "type": "GIT", 104 "vsid": "GIT", 105 "status": "READY", 106 "branch": "dummy_branch", 107 "url": "https://example.git.com/testRepo", 108 "createdBy": "testUser", 109 "createdDate": "dummy_date", 110 "config": [ 111 { 112 "key": "CURRENT_COMMIT", 113 "value": "dummy_commit_number", 114 "category": "GENERAL", 115 "scope": "local" 116 } 117 ], 118 "objects": 1, 119 "currentCommit": "dummy_commit_number", 120 "connection": "ssl" 121 } 122 }`} 123 } 124 125 repository, err := getRepository(&config, &httpClient) 126 127 if assert.NoError(t, err) { 128 t.Run("check url", func(t *testing.T) { 129 assert.Equal(t, "https://example.git.com/testRepo", repository.Result.Url) 130 }) 131 t.Run("check rid", func(t *testing.T) { 132 assert.Equal(t, "testrepo", repository.Result.Rid) 133 }) 134 t.Run("check commit id", func(t *testing.T) { 135 assert.Equal(t, "dummy_commit_number", repository.Result.CurrentCommit) 136 }) 137 } 138 }) 139 } 140 141 func TestGctsGetRepositoryFailure(t *testing.T) { 142 config := gctsDeployOptions{ 143 Host: "http://testHost.com:50000", 144 Client: "000", 145 Repository: "testRepoNotExists", 146 Username: "testUser", 147 Password: "testPassword", 148 } 149 t.Run("Get Repository Success Test", func(t *testing.T) { 150 var httpClient httpMockGcts 151 if config.Repository == "testRepoNotExists" { 152 httpClient = httpMockGcts{StatusCode: 500, ResponseBody: `{ 153 "exception": "No relation between system and repository" 154 }`} 155 } 156 157 _, err := getRepository(&config, &httpClient) 158 159 assert.EqualError(t, err, "a http error occurred") 160 }) 161 162 } 163 164 func TestGctsSwitchBranchSuccess(t *testing.T) { 165 config := gctsDeployOptions{ 166 Host: "http://testHost.com:50000", 167 Client: "000", 168 Repository: "testRepo", 169 Branch: "dummyBranch", 170 Username: "testUser", 171 Password: "testPassword", 172 } 173 174 t.Run("Switch Branch success", func(t *testing.T) { 175 var httpClient httpMockGcts 176 if config.Branch == "dummyBranch" { 177 httpClient = httpMockGcts{StatusCode: 200, ResponseBody: `{ 178 "result": { 179 "rid": "testrepo", 180 "checkoutTime": 20210413082242, 181 "fromCommit": "from_dummy_commit", 182 "toCommit": "to_dummy_commit", 183 "caller": "testUser", 184 "request": "GITKUKDUMMY", 185 "type": "BRANCH_SW", 186 "state": "DONE", 187 "rc": "0000" 188 } 189 }`} 190 } 191 192 responseBody, err := switchBranch(&config, &httpClient, "dummyCurrentBranch", "dummyTargetBranch") 193 194 if assert.NoError(t, err) { 195 t.Run("check from commit", func(t *testing.T) { 196 assert.Equal(t, "from_dummy_commit", responseBody.Result.FromCommit) 197 }) 198 t.Run("check to commit", func(t *testing.T) { 199 assert.Equal(t, "to_dummy_commit", responseBody.Result.ToCommit) 200 }) 201 } 202 }) 203 } 204 205 func TestGctsSwitchBranchFailure(t *testing.T) { 206 config := gctsDeployOptions{ 207 Host: "http://testHost.com:50000", 208 Client: "000", 209 Repository: "testRepo", 210 Branch: "dummyBranchNotExists", 211 Username: "testUser", 212 Password: "testPassword", 213 } 214 t.Run("Switch Branch failure Test", func(t *testing.T) { 215 var httpClient httpMockGcts 216 if config.Branch == "dummyBranchNotExists" { 217 httpClient = httpMockGcts{StatusCode: 500, ResponseBody: `{ 218 "errorLog": [ 219 { 220 "time": 20210414102742, 221 "severity": "ERROR", 222 "message": "The branch to switch to - 'feature1' - does not exist", 223 "code": "GCTS.CLIENT.1320" 224 } 225 ], 226 "log": [ 227 { 228 "time": 20210414102742, 229 "user": "testUser", 230 "section": "REPOSITORY", 231 "action": "SWITCH_BRANCH", 232 "severity": "ERROR", 233 "message": "20210414102742: Error action SWITCH_BRANCH 20210414_102740_B4EC329722B5C611B35B345F3B5F8FAA" 234 }, 235 { 236 "time": 20210414102742, 237 "user": "testUser", 238 "section": "REPOSITORY", 239 "action": "SWITCH_BRANCH", 240 "severity": "ERROR", 241 "message": "20210414102742: Error action SWITCH_BRANCH Client error" 242 } 243 ], 244 "exception": "Cannot switch branch of local repository to selected branch." 245 }`} 246 } 247 248 _, err := getRepository(&config, &httpClient) 249 250 assert.EqualError(t, err, "a http error occurred") 251 }) 252 253 } 254 255 func TestCreateRepositorySuccess(t *testing.T) { 256 config := gctsCreateRepositoryOptions{ 257 Host: "http://testHost.com:50000", 258 Client: "000", 259 Repository: "testRepo", 260 Username: "testUser", 261 Password: "testPassword", 262 RemoteRepositoryURL: "http://testRepoUrl.com", 263 Role: "dummyRole", 264 VSID: "dummyVsid", 265 Type: "dummyType", 266 } 267 t.Run("Create Repository Success", func(t *testing.T) { 268 var httpClient httpMockGcts 269 if config.Repository == "testRepo" { 270 httpClient = httpMockGcts{StatusCode: 200, ResponseBody: `{ 271 "repository": { 272 "rid": "testrepo", 273 "name": "testRepo", 274 "role": "dummyRole", 275 "type": "dummyType", 276 "vsid": "dummyVsid", 277 "status": "CREATED", 278 "branch": "dummyBranch", 279 "url": "http://testRepoUrl.com", 280 "createdBy": "testUser", 281 "createdDate": "2021-04-14", 282 "config": [ 283 { 284 "key": "CLIENT_VCS_CONNTYPE", 285 "value": "ssl", 286 "category": "CONNECTION", 287 "scope": "local" 288 }, 289 { 290 "key": "CLIENT_VCS_URI", 291 "value": "http://testRepoUrl.com", 292 "category": "CONNECTION", 293 "scope": "local" 294 } 295 ], 296 "connection": "ssl" 297 } 298 }`} 299 } 300 301 err := createRepositoryForDeploy(&config, nil, nil, &httpClient, nil) 302 assert.NoError(t, err) 303 }) 304 } 305 306 func TestCreateRepositoryFailure(t *testing.T) { 307 config := gctsCreateRepositoryOptions{ 308 Host: "http://testHost.com:50000", 309 Client: "000", 310 Repository: "testRepoExists", 311 Username: "testUser", 312 Password: "testPassword", 313 RemoteRepositoryURL: "http://testRepoUrlFail.com", 314 Role: "dummyRole", 315 VSID: "dummyVsid", 316 Type: "dummyType", 317 } 318 t.Run("Create Repository Failure", func(t *testing.T) { 319 var httpClient httpMockGcts 320 if config.Repository == "testRepoExists" { 321 httpClient = httpMockGcts{StatusCode: 500, ResponseBody: `{ 322 "errorLog": [ 323 { 324 "time": 20210506153611, 325 "user": "testUser", 326 "section": "SYSTEM", 327 "action": "CREATE_REPOSITORY", 328 "severity": "ERROR", 329 "message": "20210506153611: Error action CREATE_REPOSITORY Repository already exists" 330 } 331 ], 332 "log": [ 333 { 334 "time": 20210506153611, 335 "user": "testUser", 336 "section": "SYSTEM", 337 "action": "CREATE_REPOSITORY", 338 "severity": "ERROR", 339 "message": "20210506153611: Error action CREATE_REPOSITORY Repository already exists" 340 } 341 ], 342 "exception": "Some Error" 343 }`} 344 } 345 346 err := createRepositoryForDeploy(&config, nil, nil, &httpClient, nil) 347 assert.EqualError(t, err, "creating repository on the ABAP system http://testHost.com:50000 failed: a http error occurred") 348 }) 349 t.Run("Create Repository Failure", func(t *testing.T) { 350 var httpClient httpMockGcts 351 if config.Repository == "testRepoExists" { 352 httpClient = httpMockGcts{StatusCode: 500, ResponseBody: `{ 353 "errorLog": [ 354 { 355 "time": 20210506153611, 356 "user": "testUser", 357 "section": "SYSTEM", 358 "action": "CREATE_REPOSITORY", 359 "severity": "ERROR", 360 "message": "20210506153611: Error action CREATE_REPOSITORY Repository already exists" 361 } 362 ], 363 "log": [ 364 { 365 "time": 20210506153611, 366 "user": "testUser", 367 "section": "SYSTEM", 368 "action": "CREATE_REPOSITORY", 369 "severity": "ERROR", 370 "message": "20210506153611: Error action CREATE_REPOSITORY Repository already exists" 371 } 372 ], 373 "exception": "Repository already exists" 374 }`} 375 } 376 377 err := createRepositoryForDeploy(&config, nil, nil, &httpClient, nil) 378 assert.NoError(t, err) 379 }) 380 } 381 382 func TestGctsSetConfigByKeySuccess(t *testing.T) { 383 config := gctsDeployOptions{ 384 Host: "http://testHost.com:50000", 385 Client: "000", 386 Repository: "testRepo", 387 Branch: "dummyBranch", 388 Username: "testUser", 389 Password: "testPassword", 390 } 391 configKey := setConfigKeyBody{ 392 Key: "dummy_key", 393 Value: "dummy_value", 394 } 395 t.Run("Set Config By key Success", func(t *testing.T) { 396 var httpClient httpMockGcts 397 if config.Repository == "testRepo" { 398 httpClient = httpMockGcts{StatusCode: 200, ResponseBody: `{ 399 "result": { 400 "key": "dummy_key", 401 "value": "dummy_value" 402 } 403 }`} 404 } 405 406 err := setConfigKey(&config, &httpClient, &configKey) 407 408 assert.NoError(t, err) 409 }) 410 411 } 412 413 func TestGctsSetConfigByKeyFailure(t *testing.T) { 414 config := gctsDeployOptions{ 415 Host: "http://testHost.com:50000", 416 Client: "000", 417 Repository: "testRepoNotExists", 418 Branch: "dummyBranchNotExists", 419 Username: "testUser", 420 Password: "testPassword", 421 } 422 configKey := setConfigKeyBody{ 423 Key: "dummy_key", 424 Value: "dummy_value", 425 } 426 t.Run("Set Config By key Success", func(t *testing.T) { 427 var httpClient httpMockGcts 428 if config.Repository == "testRepoNotExists" { 429 httpClient = httpMockGcts{StatusCode: 500, ResponseBody: `{ 430 "exception": "No relation between system and repository" 431 }`} 432 } 433 434 err := setConfigKey(&config, &httpClient, &configKey) 435 436 assert.EqualError(t, err, "a http error occurred") 437 }) 438 439 } 440 441 func TestGctsDeleteConfigByKeySuccess(t *testing.T) { 442 config := gctsDeployOptions{ 443 Host: "http://testHost.com:50000", 444 Client: "000", 445 Repository: "testRepo", 446 Branch: "dummyBranch", 447 Username: "testUser", 448 Password: "testPassword", 449 } 450 t.Run("Delete Config By key Success", func(t *testing.T) { 451 var httpClient httpMockGcts 452 if config.Repository == "testRepo" { 453 httpClient = httpMockGcts{StatusCode: 200, ResponseBody: `{ 454 }`} 455 } 456 457 err := deleteConfigKey(&config, &httpClient, "dummy_config") 458 459 assert.NoError(t, err) 460 }) 461 462 } 463 464 func TestGctsDeleteConfigByKeyFailure(t *testing.T) { 465 config := gctsDeployOptions{ 466 Host: "http://testHost.com:50000", 467 Client: "000", 468 Repository: "testRepoNotExists", 469 Branch: "dummyBranchNotExists", 470 Username: "testUser", 471 Password: "testPassword", 472 } 473 t.Run("Delete Config By key Failure", func(t *testing.T) { 474 var httpClient httpMockGcts 475 if config.Repository == "testRepoNotExists" { 476 httpClient = httpMockGcts{StatusCode: 500, ResponseBody: `{ 477 "exception": "No relation between system and repository" 478 }`} 479 } 480 481 err := deleteConfigKey(&config, &httpClient, "dummy_config") 482 483 assert.EqualError(t, err, "a http error occurred") 484 }) 485 486 } 487 488 func TestGctsConfigMetadataSuccess(t *testing.T) { 489 config := gctsDeployOptions{ 490 Host: "http://testHost.com:50000", 491 Client: "000", 492 Repository: "testRepo", 493 Branch: "dummyBranch", 494 Username: "testUser", 495 Password: "testPassword", 496 } 497 t.Run("Test Config Metadata Success", func(t *testing.T) { 498 var httpClient httpMockGcts 499 if config.Repository == "testRepo" { 500 httpClient = httpMockGcts{StatusCode: 200, ResponseBody: `{ 501 "config": [ 502 { 503 "ckey": "dummy_key_system", 504 "ctype": "SYSTEM", 505 "cvisible": "X", 506 "datatype": "STRING", 507 "defaultValue": "dummy_default_system", 508 "description": "Dummy Key System", 509 "category": "SYSTEM", 510 "example": "dummy" 511 }, 512 { 513 "ckey": "dummy_key_repo", 514 "ctype": "REPOSITORY", 515 "cvisible": "X", 516 "datatype": "STRING", 517 "defaultValue": "dummy_default", 518 "description": "Dummy Key repository", 519 "category": "INTERNAL", 520 "example": "dummy" 521 } 522 ] 523 }`} 524 } 525 526 configMetadata, err := getConfigurationMetadata(&config, &httpClient) 527 528 if assert.NoError(t, err) { 529 t.Run("Check if system config matches", func(t *testing.T) { 530 for _, config := range configMetadata.Config { 531 if config.Ctype == "SYSTEM" { 532 assert.Equal(t, "dummy_key_system", config.Ckey) 533 } else if config.Ctype == "REPOSITORY" { 534 assert.Equal(t, "dummy_key_repo", config.Ckey) 535 } 536 } 537 538 }) 539 } 540 541 }) 542 543 } 544 545 func TestGctsConfigMetadataFailure(t *testing.T) { 546 config := gctsDeployOptions{ 547 Host: "http://testHostNotregistered.com:50000", 548 Client: "000", 549 Repository: "testRepo", 550 Branch: "dummyBranch", 551 Username: "testUser", 552 Password: "testPassword", 553 } 554 t.Run("Test Config Metadata Failure", func(t *testing.T) { 555 var httpClient httpMockGcts 556 if config.Host == "http://testHostNotregistered.com:50000" { 557 httpClient = httpMockGcts{StatusCode: 500, ResponseBody: `{ 558 }`} 559 } 560 561 _, err := getConfigurationMetadata(&config, &httpClient) 562 563 assert.EqualError(t, err, "a http error occurred") 564 565 }) 566 567 } 568 569 func TestDeployToAbapSystemSuccess(t *testing.T) { 570 config := gctsDeployOptions{ 571 Host: "http://testHost.com:50000", 572 Client: "000", 573 Repository: "testRepo", 574 Username: "testUser", 575 Password: "testPassword", 576 Scope: "dummyScope", 577 } 578 579 t.Run("Deploy to ABAP system sucess", func(t *testing.T) { 580 var httpClient httpMockGcts 581 if config.Repository == "testRepo" { 582 httpClient = httpMockGcts{StatusCode: 200, ResponseBody: `{ 583 "result": { 584 "rid": "testrepo", 585 "name": "testRepo", 586 "role": "dummyRole", 587 "type": "dummyType", 588 "vsid": "dummyVsid", 589 "status": "CREATED", 590 "branch": "dummyBranch", 591 "url": "http://testRepoUrl.com", 592 "createdBy": "testUser", 593 "createdDate": "2021-04-14", 594 "config": [ 595 { 596 "key": "CLIENT_VCS_CONNTYPE", 597 "value": "ssl", 598 "category": "CONNECTION", 599 "scope": "local" 600 }, 601 { 602 "key": "CLIENT_VCS_URI", 603 "value": "http://testRepoUrl.com", 604 "category": "CONNECTION", 605 "scope": "local" 606 } 607 ], 608 "connection": "ssl" 609 } 610 }`} 611 } 612 613 err := deployCommitToAbapSystem(&config, &httpClient) 614 assert.NoError(t, err) 615 }) 616 } 617 618 func TestGctsDeployToAbapSystemFailure(t *testing.T) { 619 config := gctsDeployOptions{ 620 Host: "http://testHost.com:50000", 621 Client: "000", 622 Repository: "testRepoNotExists", 623 Username: "testUser", 624 Password: "testPassword", 625 Scope: "dummyScope", 626 } 627 t.Run("Deploy to ABAP system Failure", func(t *testing.T) { 628 var httpClient httpMockGcts 629 if config.Repository == "testRepoNotExists" { 630 httpClient = httpMockGcts{StatusCode: 500, ResponseBody: `{ 631 "exception": "No relation between system and repository" 632 }`} 633 } 634 635 err := deployCommitToAbapSystem(&config, &httpClient) 636 637 assert.EqualError(t, err, "a http error occurred") 638 639 }) 640 641 } 642 643 func TestGctsSplitConfigurationToMap(t *testing.T) { 644 config := []configMetadata{ 645 { 646 Ckey: "dummyKey1", 647 Ctype: "REPOSITORY", 648 Datatype: "BOOLEAN", 649 Example: "X", 650 }, 651 { 652 Ckey: "dummyKey2", 653 Ctype: "REPOSITORY", 654 Datatype: "BOOLEAN", 655 Example: "true", 656 }, 657 { 658 Ckey: "dummyKey3", 659 Ctype: "REPOSITORY", 660 Datatype: "STRING", 661 Example: "dummyValue", 662 }, 663 } 664 configMetadata := configurationMetadataBody{ 665 Config: config, 666 } 667 668 configMap := map[string]interface{}{ 669 "dummyKey1": "true", 670 "dummyKey2": "true", 671 "dummyKey3": "dummyValue2", 672 } 673 674 t.Run("Config Mapping test", func(t *testing.T) { 675 repoConfig, err := splitConfigurationToMap(configMap, configMetadata) 676 677 if assert.NoError(t, err) { 678 for _, config := range repoConfig { 679 if config.Key == "dummyKey1" { 680 assert.Equal(t, "X", config.Value) 681 } else if config.Key == "dummyKey2" { 682 assert.Equal(t, "true", config.Value) 683 } else if config.Key == "dummyKey3" { 684 assert.Equal(t, "dummyValue2", config.Value) 685 } 686 } 687 } 688 }) 689 }