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