github.com/SAP/jenkins-library@v1.362.0/cmd/gctsRollback_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "net/url" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestGetLastSuccessfullCommitSuccess(t *testing.T) { 14 15 config := gctsRollbackOptions{ 16 Host: "http://testHost.com:50000", 17 Client: "000", 18 Repository: "testRepo", 19 GithubPersonalAccessToken: "3a09064f3029f5a304d69987ef8f95d1dfa6da44", 20 } 21 22 t.Run("return last successful commit", func(t *testing.T) { 23 24 httpClient := httpMockGcts{StatusCode: 200, ResponseBody: ` 25 { 26 "state": "success", 27 "statuses": [ 28 { 29 "url": "https://github.com/repos/testUser/testRepo/statuses/c316a4af470991f9a3ca51a12c44354e72729e3d", 30 "avatar_url": "https://github.com/avatars/u/50615?", 31 "id": 81586547, 32 "node_id": "MDEzOlN0YXR1c0iUdnRleHQ4MTU4NjkyNg==", 33 "state": "success", 34 "description": "This commit looks good", 35 "target_url": "https://jenkins.instance.com/job/jobName/job/test/job/master/41/display/redirect", 36 "context": "continuous-integration/jenkins/branch", 37 "created_at": "2020-04-24T07:25:59Z", 38 "updated_at": "2020-04-24T07:25:59Z" 39 } 40 ], 41 "sha": "c316a4af470991f9a3ca51a12c44354e72729e3d", 42 "total_count": 1, 43 "repository": { 44 "id": 348933, 45 "node_id": "MDEwOlJlcG9zaXRdkgjzNDg3NDM=", 46 "name": "testRepo", 47 "full_name": "testUser/testRepo", 48 "private": true, 49 "owner": { 50 "login": "testUser", 51 "id": 50613, 52 "node_id": "MDQ6VXKdigUwNjEz", 53 "avatar_url": "https://github.com/avatars/u/50653?", 54 "gravatar_id": "", 55 "url": "https://github.com/users/testUser", 56 "html_url": "https://github.com/testUser", 57 "followers_url": "https://github.com/users/testUser/followers", 58 "following_url": "https://github.com/users/testUser/following{/other_user}", 59 "gists_url": "https://github.com/users/testUser/gists{/gist_id}", 60 "starred_url": "https://github.com/users/testUser/starred{/owner}{/repo}", 61 "subscriptions_url": "https://github.com/users/testUser/subscriptions", 62 "organizations_url": "https://github.com/users/testUser/orgs", 63 "repos_url": "https://github.com/users/testUser/repos", 64 "events_url": "https://github.com/users/testUser/events{/privacy}", 65 "received_events_url": "https://github.com/users/testUser/received_events", 66 "type": "User", 67 "site_admin": false 68 }, 69 "html_url": "https://github.com/testUser/testRepo", 70 "description": "testing go lib", 71 "fork": false, 72 "url": "https://github.com/repos/testUser/testRepo", 73 "forks_url": "https://github.com/repos/testUser/testRepo/forks", 74 "keys_url": "https://github.com/repos/testUser/testRepo/keys{/key_id}", 75 "collaborators_url": "https://github.com/repos/testUser/testRepo/collaborators{/collaborator}", 76 "teams_url": "https://github.com/repos/testUser/testRepo/teams", 77 "hooks_url": "https://github.com/repos/testUser/testRepo/hooks", 78 "issue_events_url": "https://github.com/repos/testUser/testRepo/issues/events{/number}", 79 "events_url": "https://github.com/repos/testUser/testRepo/events", 80 "assignees_url": "https://github.com/repos/testUser/testRepo/assignees{/user}", 81 "branches_url": "https://github.com/repos/testUser/testRepo/branches{/branch}", 82 "tags_url": "https://github.com/repos/testUser/testRepo/tags", 83 "blobs_url": "https://github.com/repos/testUser/testRepo/git/blobs{/sha}", 84 "git_tags_url": "https://github.com/repos/testUser/testRepo/git/tags{/sha}", 85 "git_refs_url": "https://github.com/repos/testUser/testRepo/git/refs{/sha}", 86 "trees_url": "https://github.com/repos/testUser/testRepo/git/trees{/sha}", 87 "statuses_url": "https://github.com/repos/testUser/testRepo/statuses/{sha}", 88 "languages_url": "https://github.com/repos/testUser/testRepo/languages", 89 "stargazers_url": "https://github.com/repos/testUser/testRepo/stargazers", 90 "contributors_url": "https://github.com/repos/testUser/testRepo/contributors", 91 "subscribers_url": "https://github.com/repos/testUser/testRepo/subscribers", 92 "subscription_url": "https://github.com/repos/testUser/testRepo/subscription", 93 "commits_url": "https://github.com/repos/testUser/testRepo/commits{/sha}", 94 "git_commits_url": "https://github.com/repos/testUser/testRepo/git/commits{/sha}", 95 "comments_url": "https://github.com/repos/testUser/testRepo/comments{/number}", 96 "issue_comment_url": "https://github.com/repos/testUser/testRepo/issues/comments{/number}", 97 "contents_url": "https://github.com/repos/testUser/testRepo/contents/{+path}", 98 "compare_url": "https://github.com/repos/testUser/testRepo/compare/{base}...{head}", 99 "merges_url": "https://github.com/repos/testUser/testRepo/merges", 100 "archive_url": "https://github.com/repos/testUser/testRepo/{archive_format}{/ref}", 101 "downloads_url": "https://github.com/repos/testUser/testRepo/downloads", 102 "issues_url": "https://github.com/repos/testUser/testRepo/issues{/number}", 103 "pulls_url": "https://github.com/repos/testUser/testRepo/pulls{/number}", 104 "milestones_url": "https://github.com/repos/testUser/testRepo/milestones{/number}", 105 "notifications_url": "https://github.com/repos/testUser/testRepo/notifications{?since,all,participating}", 106 "labels_url": "https://github.com/repos/testUser/testRepo/labels{/name}", 107 "releases_url": "https://github.com/repos/testUser/testRepo/releases{/id}", 108 "deployments_url": "https://github.com/repos/testUser/testRepo/deployments" 109 }, 110 "commit_url": "https://github.com/repos/testUser/testRepo/commits/c316a4af470991f9a3ca51a12c44354e72729e3d", 111 "url": "https://github.com/repos/testUser/testRepo/commits/c316a4af470991f9a3ca51a12c44354e72729e3d/status" 112 } 113 `} 114 115 parsedURL, _ := url.Parse("https://github.com/testUser/testRepo") 116 commitList := []string{"c316a4af470991f9a3ca51a12c44354e72729e3d", "579ba54ef025377319b6bb2e01f034c4f9b72026", "6bf1e447581c41ba421a3cce45495d369e99c8e5", "7852bb9be857a9439e1a3674f830b121d3fbd7c4"} 117 118 successfullCommit, err := getLastSuccessfullCommit(&config, nil, &httpClient, parsedURL, commitList) 119 120 if assert.NoError(t, err) { 121 122 t.Run("check url", func(t *testing.T) { 123 assert.Equal(t, "https://api.github.com/repos/testUser/testRepo/commits/c316a4af470991f9a3ca51a12c44354e72729e3d/status", httpClient.URL) 124 }) 125 126 t.Run("check method", func(t *testing.T) { 127 assert.Equal(t, "GET", httpClient.Method) 128 }) 129 130 t.Run("check token", func(t *testing.T) { 131 assert.Equal(t, "Bearer 3a09064f3029f5a304d69987ef8f95d1dfa6da44", httpClient.Options.Token) 132 }) 133 134 t.Run("check commit list", func(t *testing.T) { 135 assert.Equal(t, "c316a4af470991f9a3ca51a12c44354e72729e3d", successfullCommit) 136 }) 137 } 138 }) 139 } 140 141 func TestGetLastSuccessfullCommitFailure(t *testing.T) { 142 143 config := gctsRollbackOptions{ 144 Host: "http://testHost.com:50000", 145 Client: "000", 146 Repository: "testRepo", 147 Username: "testUser", 148 Password: "testPassword", 149 } 150 151 t.Run("a http error occurred", func(t *testing.T) { 152 153 httpClient := httpMockGcts{StatusCode: 500} 154 155 parsedURL, _ := url.Parse("https://github.com/testUser/testRepo") 156 commitList := []string{"c316a4af470991f9a3ca51a12c44354e72729e3d", "579ba54ef025377319b6bb2e01f034c4f9b72026", "6bf1e447581c41ba421a3cce45495d369e99c8e5", "7852bb9be857a9439e1a3674f830b121d3fbd7c4"} 157 158 _, err := getLastSuccessfullCommit(&config, nil, &httpClient, parsedURL, commitList) 159 160 assert.EqualError(t, err, "a http error occurred") 161 }) 162 } 163 func TestGetCommitsSuccess(t *testing.T) { 164 165 config := gctsRollbackOptions{ 166 Host: "http://testHost.com:50000", 167 Client: "000", 168 Repository: "testRepo", 169 Username: "testUser", 170 Password: "testPassword", 171 } 172 173 t.Run("return list of commits", func(t *testing.T) { 174 175 httpClient := httpMockGcts{StatusCode: 200, ResponseBody: ` 176 { 177 "commits": [ 178 { 179 "id": "c316a4af470991f9a3ca51a12c44354e72729e3d", 180 "author": "Test User", 181 "authorMail": "test.user@example.com", 182 "message": "test", 183 "description": "test\n", 184 "date": "2020-04-24 09:07:50" 185 }, 186 { 187 "id": "579ba54ef025377319b6bb2e01f034c4f9b72026", 188 "author": "Test User", 189 "authorMail": "test.user@example.com", 190 "message": "add major feature", 191 "description": "add major feature\n", 192 "date": "2020-04-24 09:01:49" 193 }, 194 { 195 "id": "6bf1e447581c41ba421a3cce45495d369e99c8e5", 196 "author": "Test User", 197 "authorMail": "test.user@example.com", 198 "message": "minor fix", 199 "description": "minor fix\n", 200 "date": "2020-04-24 08:56:58" 201 }, 202 { 203 "id": "7852bb9be857a9439e1a3674f830b121d3fbd7c4", 204 "author": "Test User", 205 "authorMail": "test.user@example.com", 206 "message": "updated", 207 "description": "updated\n", 208 "date": "2020-04-24 08:56:51" 209 } 210 ] 211 } 212 `} 213 214 commitList, err := getCommits(&config, nil, &httpClient) 215 216 if assert.NoError(t, err) { 217 218 t.Run("check url", func(t *testing.T) { 219 assert.Equal(t, "http://testHost.com:50000/sap/bc/cts_abapvcs/repository/testRepo/getCommit?sap-client=000", httpClient.URL) 220 }) 221 222 t.Run("check method", func(t *testing.T) { 223 assert.Equal(t, "GET", httpClient.Method) 224 }) 225 226 t.Run("check commit list", func(t *testing.T) { 227 assert.Equal(t, []string{"c316a4af470991f9a3ca51a12c44354e72729e3d", "579ba54ef025377319b6bb2e01f034c4f9b72026", "6bf1e447581c41ba421a3cce45495d369e99c8e5", "7852bb9be857a9439e1a3674f830b121d3fbd7c4"}, commitList) 228 }) 229 230 } 231 232 }) 233 } 234 func TestGetCommitsFailure(t *testing.T) { 235 236 config := gctsRollbackOptions{ 237 Host: "http://testHost.com:50000", 238 Client: "000", 239 Repository: "testRepo", 240 Username: "testUser", 241 Password: "testPassword", 242 } 243 244 t.Run("a http error occurred", func(t *testing.T) { 245 246 httpClient := httpMockGcts{StatusCode: 500, ResponseBody: ` 247 } 248 "log": [ 249 { 250 "time": 20180606130524, 251 "user": "JENKINS", 252 "section": "REPOSITORY_FACTORY", 253 "action": "CREATE_REPOSITORY", 254 "severity": "INFO", 255 "message": "Start action CREATE_REPOSITORY review", 256 "code": "GCTS.API.410" 257 } 258 ], 259 "errorLog": [ 260 { 261 "time": 20180606130524, 262 "user": "JENKINS", 263 "section": "REPOSITORY_FACTORY", 264 "action": "CREATE_REPOSITORY", 265 "severity": "INFO", 266 "message": "Start action CREATE_REPOSITORY review", 267 "code": "GCTS.API.410" 268 } 269 ], 270 "exception": { 271 "message": "repository_not_found", 272 "description": "Repository not found", 273 "code": 404 274 } 275 } 276 `} 277 278 _, err := getCommits(&config, nil, &httpClient) 279 280 assert.EqualError(t, err, "a http error occurred") 281 }) 282 } 283 func TestGetRepoInfoSuccess(t *testing.T) { 284 285 config := gctsRollbackOptions{ 286 Host: "http://testHost.com:50000", 287 Client: "000", 288 Repository: "testRepo", 289 Username: "testUser", 290 Password: "testPassword", 291 } 292 293 t.Run("return struct of repository information", func(t *testing.T) { 294 295 httpClient := httpMockGcts{StatusCode: 200, ResponseBody: ` 296 { 297 "result": { 298 "rid": "testRepo", 299 "name": "testRepo", 300 "role": "SOURCE", 301 "type": "GIT", 302 "vsid": "BCH", 303 "status": "READY", 304 "branch": "master", 305 "url": "https://github.com/testUser/testRepo", 306 "createdBy": "testUser", 307 "createdDate": "2020-04-27", 308 "config": [ 309 { 310 "key": "CLIENT_VCS_CONNTYPE", 311 "value": "ssl", 312 "category": "CONNECTION" 313 }, 314 { 315 "key": "CLIENT_VCS_URI", 316 "value": "https://github.com/testUser/testRepo", 317 "category": "CONNECTION" 318 } 319 ], 320 "objects": 2, 321 "currentCommit": "c316a4af470991f9a3ca51a12c44354e72729e3d", 322 "connection": "ssl" 323 } 324 } 325 `} 326 327 repoInfo, err := getRepoInfo(&config, nil, &httpClient) 328 329 repoInfoExpected := &getRepoInfoResponseBody{ 330 Result: struct { 331 Rid string `json:"rid"` 332 Name string `json:"name"` 333 Role string `json:"role"` 334 Type string `json:"type"` 335 Vsid string `json:"vsid"` 336 Status string `json:"status"` 337 Branch string `json:"branch"` 338 URL string `json:"url"` 339 Version string `json:"version"` 340 Objects int `json:"objects"` 341 CurrentCommit string `json:"currentCommit"` 342 Connection string `json:"connection"` 343 Config []struct { 344 Key string `json:"key"` 345 Value string `json:"value"` 346 } `json:"config"` 347 }{ 348 Rid: "testRepo", 349 Name: "testRepo", 350 Role: "SOURCE", 351 Type: "GIT", 352 Vsid: "BCH", 353 Status: "READY", 354 Branch: "master", 355 URL: "https://github.com/testUser/testRepo", 356 Version: "", 357 Objects: 2, 358 CurrentCommit: "c316a4af470991f9a3ca51a12c44354e72729e3d", 359 Connection: "ssl", 360 Config: []struct { 361 Key string `json:"key"` 362 Value string `json:"value"` 363 }{ 364 {"CLIENT_VCS_CONNTYPE", "ssl"}, 365 {"CLIENT_VCS_URI", "https://github.com/testUser/testRepo"}, 366 }, 367 }, 368 } 369 370 if assert.NoError(t, err) { 371 372 t.Run("check url", func(t *testing.T) { 373 assert.Equal(t, "http://testHost.com:50000/sap/bc/cts_abapvcs/repository/testRepo?sap-client=000", httpClient.URL) 374 }) 375 376 t.Run("check method", func(t *testing.T) { 377 assert.Equal(t, "GET", httpClient.Method) 378 }) 379 380 t.Run("check commit list", func(t *testing.T) { 381 assert.Equal(t, repoInfoExpected, repoInfo) 382 }) 383 } 384 }) 385 } 386 387 func TestGetRepoInfoFailure(t *testing.T) { 388 389 config := gctsRollbackOptions{ 390 Host: "http://testHost.com:50000", 391 Client: "000", 392 Repository: "testRepo", 393 Username: "testUser", 394 Password: "testPassword", 395 } 396 397 t.Run("a http error occurred", func(t *testing.T) { 398 399 httpClient := httpMockGcts{StatusCode: 500, ResponseBody: ` 400 } 401 "log": [ 402 { 403 "time": 20180606130524, 404 "user": "JENKINS", 405 "section": "REPOSITORY_FACTORY", 406 "action": "CREATE_REPOSITORY", 407 "severity": "INFO", 408 "message": "Start action CREATE_REPOSITORY review", 409 "code": "GCTS.API.410" 410 } 411 ], 412 "errorLog": [ 413 { 414 "time": 20180606130524, 415 "user": "JENKINS", 416 "section": "REPOSITORY_FACTORY", 417 "action": "CREATE_REPOSITORY", 418 "severity": "INFO", 419 "message": "Start action CREATE_REPOSITORY review", 420 "code": "GCTS.API.410" 421 } 422 ], 423 "exception": { 424 "message": "repository_not_found", 425 "description": "Repository not found", 426 "code": 404 427 } 428 } 429 `} 430 431 _, err := getRepoInfo(&config, nil, &httpClient) 432 433 assert.EqualError(t, err, "a http error occurred") 434 }) 435 } 436 func TestGetRepoHistorySuccess(t *testing.T) { 437 438 config := gctsRollbackOptions{ 439 Host: "http://testHost.com:50000", 440 Client: "000", 441 Repository: "testRepo", 442 Username: "testUser", 443 Password: "testPassword", 444 } 445 446 t.Run("return struct of repository history", func(t *testing.T) { 447 448 httpClient := httpMockGcts{StatusCode: 200, ResponseBody: ` 449 { 450 "result": [ 451 { 452 "rid": "com.example", 453 "checkoutTime": 20180606130524, 454 "fromCommit": "f1cdb6a032c1d8187c0990b51e94e8d8bb9898b2", 455 "toCommit": "f1cdb6a032c1d8187c0990b51e94e8d8bb9898b2", 456 "caller": "JOHNDOE", 457 "request": "SIDK1234567", 458 "type": "PULL" 459 } 460 ] 461 } 462 `} 463 464 repoHistory, err := getRepoHistory(&config, nil, &httpClient) 465 466 repoHistoryExpected := &getRepoHistoryResponseBody{ 467 Result: []struct { 468 Rid string `json:"rid"` 469 CheckoutTime int64 `json:"checkoutTime"` 470 FromCommit string `json:"fromCommit"` 471 ToCommit string `json:"toCommit"` 472 Caller string `json:"caller"` 473 Request string `json:"request"` 474 Type string `json:"type"` 475 }{ 476 { 477 Rid: "com.example", 478 CheckoutTime: 20180606130524, 479 FromCommit: "f1cdb6a032c1d8187c0990b51e94e8d8bb9898b2", 480 ToCommit: "f1cdb6a032c1d8187c0990b51e94e8d8bb9898b2", 481 Caller: "JOHNDOE", 482 Request: "SIDK1234567", 483 Type: "PULL", 484 }, 485 }, 486 } 487 488 if assert.NoError(t, err) { 489 490 t.Run("check url", func(t *testing.T) { 491 assert.Equal(t, "http://testHost.com:50000/sap/bc/cts_abapvcs/repository/testRepo/getHistory?sap-client=000", httpClient.URL) 492 }) 493 494 t.Run("check method", func(t *testing.T) { 495 assert.Equal(t, "GET", httpClient.Method) 496 }) 497 498 t.Run("check commit list", func(t *testing.T) { 499 assert.Equal(t, repoHistoryExpected, repoHistory) 500 }) 501 } 502 }) 503 } 504 505 func TestGetRepoHistoryFailure(t *testing.T) { 506 507 config := gctsRollbackOptions{ 508 Host: "http://testHost.com:50000", 509 Client: "000", 510 Repository: "testRepo", 511 Username: "testUser", 512 Password: "testPassword", 513 } 514 515 t.Run("a http error occurred", func(t *testing.T) { 516 517 httpClient := httpMockGcts{StatusCode: 500, ResponseBody: ` 518 } 519 "log": [ 520 { 521 "time": 20180606130524, 522 "user": "JENKINS", 523 "section": "REPOSITORY_FACTORY", 524 "action": "CREATE_REPOSITORY", 525 "severity": "INFO", 526 "message": "Start action CREATE_REPOSITORY review", 527 "code": "GCTS.API.410" 528 } 529 ], 530 "errorLog": [ 531 { 532 "time": 20180606130524, 533 "user": "JENKINS", 534 "section": "REPOSITORY_FACTORY", 535 "action": "CREATE_REPOSITORY", 536 "severity": "INFO", 537 "message": "Start action CREATE_REPOSITORY review", 538 "code": "GCTS.API.410" 539 } 540 ], 541 "exception": { 542 "message": "repository_not_found", 543 "description": "Repository not found", 544 "code": 404 545 } 546 } 547 `} 548 549 _, err := getRepoHistory(&config, nil, &httpClient) 550 551 assert.EqualError(t, err, "a http error occurred") 552 }) 553 }