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