github.com/jaylevin/jenkins-library@v1.230.4/cmd/gctsRollback_test.go (about)

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