github.com/xgoffin/jenkins-library@v1.154.0/cmd/gctsCloneRepository_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestGctsCloneRepositorySuccess(t *testing.T) {
    10  
    11  	config := gctsCloneRepositoryOptions{
    12  		Host:       "http://testHost.com:50000",
    13  		Client:     "000",
    14  		Repository: "testRepo",
    15  		Username:   "testUser",
    16  		Password:   "testPassword",
    17  	}
    18  
    19  	t.Run("cloning successful", func(t *testing.T) {
    20  
    21  		httpClient := httpMockGcts{StatusCode: 200, ResponseBody: `{
    22  			"result": {
    23  				"rid": "my-repository",
    24  				"checkoutTime": 20180606130524,
    25  				"fromCommit": "f1cdb6a032c1d8187c0990b51e94e8d8bb9898b2",
    26  				"toCommit": "f1cdb6a032c1d8187c0990b51e94e8d8bb9898b2",
    27  				"caller": "JOHNDOE",
    28  				"request": "SIDK1234567",
    29  				"type": "PULL"
    30  			},
    31  			"log": [
    32  				{
    33  					"time": 20180606130524,
    34  					"user": "JENKINS",
    35  					"section": "REPOSITORY_FACTORY",
    36  					"action": "CREATE_REPOSITORY",
    37  					"severity": "INFO",
    38  					"message": "Start action CREATE_REPOSITORY review",
    39  					"code": "GCTS.API.410"
    40  				}
    41  			]
    42  		}`}
    43  
    44  		err := cloneRepository(&config, nil, &httpClient)
    45  
    46  		if assert.NoError(t, err) {
    47  
    48  			t.Run("check url", func(t *testing.T) {
    49  				assert.Equal(t, "http://testHost.com:50000/sap/bc/cts_abapvcs/repository/testRepo/clone?sap-client=000", httpClient.URL)
    50  			})
    51  
    52  			t.Run("check method", func(t *testing.T) {
    53  				assert.Equal(t, "POST", httpClient.Method)
    54  			})
    55  
    56  			t.Run("check user", func(t *testing.T) {
    57  				assert.Equal(t, "testUser", httpClient.Options.Username)
    58  			})
    59  
    60  			t.Run("check password", func(t *testing.T) {
    61  				assert.Equal(t, "testPassword", httpClient.Options.Password)
    62  			})
    63  
    64  		}
    65  
    66  	})
    67  
    68  	t.Run("repository has already been cloned", func(t *testing.T) {
    69  
    70  		httpClient := httpMockGcts{StatusCode: 500, ResponseBody: `{
    71  			"errorLog": [
    72  				{
    73  					"time": 20200414112900,
    74  					"user": "USER",
    75  					"section": "CLIENT_TP",
    76  					"action": "GET_SOURCE_FROM_REMOTE",
    77  					"severity": "ERROR",
    78  					"message": "20200414112900: Error action GET_SOURCE_FROM_REMOTE",
    79  					"protocol": [
    80  						{
    81  							"type": "Paramters",
    82  							"protocol": [
    83  								{
    84  									"repouri": "https://github.com/test-repo",
    85  									"logfile": "/usr/sap/SID/D00/gcts/repos/gcts/repo-name/log/20200414_112900_AD0F43952A5A3F47133637329BA760D8.log",
    86  									"repodir": "/usr/sap/SID/D00/gcts/repos/gcts/repo-name/data/",
    87  									"loglevel": "warning",
    88  									"command": "clone"
    89  								}
    90  							]
    91  						},
    92  						{
    93  							"type": "Client Log",
    94  							"protocol": [
    95  								"protocol logs"
    96  							]
    97  						},
    98  						{
    99  							"type": "Client Stack Log",
   100  							"protocol": [
   101  								{
   102  									"client stack log key": "client stack log value",
   103  									"stack": [
   104  										"java",
   105  										"stack"
   106  									]
   107  								}
   108  							]
   109  						}
   110  					]
   111  				},
   112  				{
   113  					"severity": "ERROR",
   114  					"message": "Cloning a repository into a new working directory failed: Destination path 'data' already exists and is not an empty directory",
   115  					"code": "GCTS.CLIENT.1420"
   116  				},
   117  				{
   118  					"time": 20200414112900,
   119  					"user": "USER",
   120  					"section": "REPOSITORY",
   121  					"action": "CLONE",
   122  					"severity": "ERROR",
   123  					"message": "20200414112900: Error action CLONE 20200414_112900_AD0F43952A5A3F47133637329BA760D8"
   124  				}
   125  			]
   126  		}`}
   127  
   128  		err := cloneRepository(&config, nil, &httpClient)
   129  		assert.NoError(t, err)
   130  
   131  	})
   132  
   133  }
   134  
   135  func TestGctsCloneRepositoryFailure(t *testing.T) {
   136  
   137  	config := gctsCloneRepositoryOptions{
   138  		Host:       "http://testHost.com:50000",
   139  		Client:     "000",
   140  		Repository: "testRepo",
   141  		Username:   "testUser",
   142  		Password:   "testPassword",
   143  	}
   144  
   145  	t.Run("a http error occurred", func(t *testing.T) {
   146  		httpClient := httpMockGcts{StatusCode: 500, ResponseBody: `{
   147  			"errorLog": [
   148  				{
   149  					"time": 20200414112900,
   150  					"user": "USER",
   151  					"section": "CLIENT_TP",
   152  					"action": "GET_SOURCE_FROM_REMOTE",
   153  					"severity": "ERROR",
   154  					"message": "20200414112900: Error action GET_SOURCE_FROM_REMOTE",
   155  					"protocol": [
   156  						{
   157  							"type": "Paramters",
   158  							"protocol": [
   159  								{
   160  									"repouri": "https://github.com/test-repo",
   161  									"logfile": "/usr/sap/SID/D00/gcts/repos/gcts/repo-name/log/20200414_112900_AD0F43952A5A3F47133637329BA760D8.log",
   162  									"repodir": "/usr/sap/SID/D00/gcts/repos/gcts/repo-name/data/",
   163  									"loglevel": "warning",
   164  									"command": "clone"
   165  								}
   166  							]
   167  						},
   168  						{
   169  							"type": "Client Log",
   170  							"protocol": [
   171  								"protocol logs"
   172  							]
   173  						},
   174  						{
   175  							"type": "Client Stack Log",
   176  							"protocol": [
   177  								{
   178  									"client stack log key": "client stack log value",
   179  									"stack": [
   180  										"java",
   181  										"stack"
   182  									]
   183  								}
   184  							]
   185  						}
   186  					]
   187  				},
   188  				{
   189  					"severity": "ERROR",
   190  					"message": "Cloning a repository into a new working directory failed: Destination path 'data' already exists and is not an empty directory",
   191  					"code": "GCTS.CLIENT.9999"
   192  				},
   193  				{
   194  					"time": 20200414112900,
   195  					"user": "USER",
   196  					"section": "REPOSITORY",
   197  					"action": "CLONE",
   198  					"severity": "ERROR",
   199  					"message": "20200414112900: Error action CLONE 20200414_112900_AD0F43952A5A3F47133637329BA760D8"
   200  				}
   201  			]
   202  		}`}
   203  
   204  		err := cloneRepository(&config, nil, &httpClient)
   205  		assert.EqualError(t, err, "cloning the repository failed: a http error occurred")
   206  
   207  	})
   208  }