github.com/SAP/jenkins-library@v1.362.0/cmd/abapEnvironmentCheckoutBranch_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "encoding/json" 8 "os" 9 "testing" 10 "time" 11 12 "github.com/SAP/jenkins-library/pkg/abaputils" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 var executionLogStringCheckout string 17 18 func init() { 19 executionLog := abaputils.LogProtocolResults{ 20 Results: []abaputils.LogProtocol{ 21 { 22 ProtocolLine: 1, 23 OverviewIndex: 1, 24 Type: "LogEntry", 25 Description: "S", 26 Timestamp: "/Date(1644332299000+0000)/", 27 }, 28 }, 29 } 30 executionLogResponse, _ := json.Marshal(executionLog) 31 executionLogStringCheckout = string(executionLogResponse) 32 } 33 34 func TestCheckoutBranchStep(t *testing.T) { 35 t.Run("Run Step Successful - repositoryName and branchName config", func(t *testing.T) { 36 37 var autils = abaputils.AUtilsMock{} 38 defer autils.Cleanup() 39 autils.ReturnedConnectionDetailsHTTP.Password = "password" 40 autils.ReturnedConnectionDetailsHTTP.User = "user" 41 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 42 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 43 44 config := abapEnvironmentCheckoutBranchOptions{ 45 CfAPIEndpoint: "https://api.endpoint.com", 46 CfOrg: "testOrg", 47 CfSpace: "testSpace", 48 CfServiceInstance: "testInstance", 49 CfServiceKeyName: "testServiceKey", 50 Username: "testUser", 51 Password: "testPassword", 52 RepositoryName: "testRepo1", 53 BranchName: "testBranch", 54 } 55 56 logResultSuccess := `{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Success", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }` 57 client := &abaputils.ClientMock{ 58 BodyList: []string{ 59 `{"d" : [] }`, 60 `{"d" : ` + executionLogStringCheckout + `}`, 61 logResultSuccess, 62 `{"d" : { "status" : "S" } }`, 63 `{"d" : { "status" : "S" } }`, 64 `{"d" : { "status" : "S" } }`, 65 }, 66 Token: "myToken", 67 StatusCode: 200, 68 } 69 70 apiManager = &abaputils.SoftwareComponentApiManager{Client: client, PollIntervall: 1 * time.Nanosecond, Force0510: true} 71 err := runAbapEnvironmentCheckoutBranch(&config, &autils, apiManager) 72 assert.NoError(t, err, "Did not expect error") 73 }) 74 t.Run("Run Step Failure - empty config", func(t *testing.T) { 75 expectedErrorMessage := "Configuration is not consistent: You have not specified any repository or branch configuration to be checked out in the ABAP Environment System. Please make sure that you specified the repositories with their branches that should be checked out either in a dedicated file or via the parameters 'repositoryName' and 'branchName'. For more information please read the user documentation" 76 77 var autils = abaputils.AUtilsMock{} 78 defer autils.Cleanup() 79 autils.ReturnedConnectionDetailsHTTP.Password = "password" 80 autils.ReturnedConnectionDetailsHTTP.User = "user" 81 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 82 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 83 84 config := abapEnvironmentCheckoutBranchOptions{} 85 86 logResultError := `{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }` 87 client := &abaputils.ClientMock{ 88 BodyList: []string{ 89 `{"d" : ` + executionLogStringCheckout + `}`, 90 logResultError, 91 `{"d" : { "status" : "E" } }`, 92 `{"d" : { "status" : "E" } }`, 93 `{"d" : { "status" : "E" } }`, 94 }, 95 Token: "myToken", 96 StatusCode: 200, 97 } 98 99 apiManager = &abaputils.SoftwareComponentApiManager{Client: client, PollIntervall: 1 * time.Nanosecond, Force0510: true} 100 err := runAbapEnvironmentCheckoutBranch(&config, &autils, apiManager) 101 assert.EqualError(t, err, expectedErrorMessage) 102 }) 103 t.Run("Run Step Failure - wrong status", func(t *testing.T) { 104 expectedErrorMessage := "Something failed during the checkout: Checkout failed: Checkout of branch testBranch failed on the ABAP System" 105 106 var autils = abaputils.AUtilsMock{} 107 defer autils.Cleanup() 108 autils.ReturnedConnectionDetailsHTTP.Password = "password" 109 autils.ReturnedConnectionDetailsHTTP.User = "user" 110 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 111 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 112 113 config := abapEnvironmentCheckoutBranchOptions{ 114 CfAPIEndpoint: "https://api.endpoint.com", 115 CfOrg: "testOrg", 116 CfSpace: "testSpace", 117 CfServiceInstance: "testInstance", 118 CfServiceKeyName: "testServiceKey", 119 Username: "testUser", 120 Password: "testPassword", 121 RepositoryName: "testRepo1", 122 BranchName: "testBranch", 123 } 124 125 logResultError := `{"d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } }` 126 client := &abaputils.ClientMock{ 127 BodyList: []string{ 128 `{"d" : ` + executionLogStringCheckout + `}`, 129 logResultError, 130 `{"d" : { "status" : "E" } }`, 131 `{"d" : { "status" : "E" } }`, 132 `{"d" : { "status" : "E" } }`, 133 }, 134 Token: "myToken", 135 StatusCode: 200, 136 } 137 138 apiManager = &abaputils.SoftwareComponentApiManager{Client: client, PollIntervall: 1 * time.Nanosecond, Force0510: true} 139 err := runAbapEnvironmentCheckoutBranch(&config, &autils, apiManager) 140 assert.EqualError(t, err, expectedErrorMessage) 141 }) 142 t.Run("Success case: checkout Branches from file config", func(t *testing.T) { 143 var autils = abaputils.AUtilsMock{} 144 defer autils.Cleanup() 145 autils.ReturnedConnectionDetailsHTTP.Password = "password" 146 autils.ReturnedConnectionDetailsHTTP.User = "user" 147 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 148 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 149 150 receivedURI := "example.com/Branches" 151 client := &abaputils.ClientMock{ 152 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 153 Token: "myToken", 154 StatusCode: 200, 155 } 156 157 dir := t.TempDir() 158 oldCWD, _ := os.Getwd() 159 _ = os.Chdir(dir) 160 // clean up tmp dir 161 162 defer func() { 163 _ = os.Chdir(oldCWD) 164 }() 165 166 manifestFileString := ` 167 repositories: 168 - name: 'testRepo' 169 branch: 'testBranch' 170 - name: 'testRepo2' 171 branch: 'testBranch2' 172 - name: 'testRepo3' 173 branch: 'testBranch3'` 174 175 err := os.WriteFile("repositoriesTest.yml", []byte(manifestFileString), 0644) 176 177 config := abapEnvironmentCheckoutBranchOptions{ 178 CfAPIEndpoint: "https://api.endpoint.com", 179 CfOrg: "testOrg", 180 CfSpace: "testSpace", 181 CfServiceInstance: "testInstance", 182 CfServiceKeyName: "testServiceKey", 183 Username: "testUser", 184 Password: "testPassword", 185 Repositories: "repositoriesTest.yml", 186 } 187 apiManager = &abaputils.SoftwareComponentApiManager{Client: client, PollIntervall: 1 * time.Nanosecond, Force0510: true} 188 err = runAbapEnvironmentCheckoutBranch(&config, &autils, apiManager) 189 assert.NoError(t, err) 190 }) 191 t.Run("Failure case: checkout Branches from empty file config", func(t *testing.T) { 192 expectedErrorMessage := "Could not read repositories: Error in config file repositoriesTest.yml, AddonDescriptor doesn't contain any repositories" 193 194 var autils = abaputils.AUtilsMock{} 195 defer autils.Cleanup() 196 autils.ReturnedConnectionDetailsHTTP.Password = "password" 197 autils.ReturnedConnectionDetailsHTTP.User = "user" 198 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 199 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 200 201 receivedURI := "example.com/Branches" 202 client := &abaputils.ClientMock{ 203 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 204 Token: "myToken", 205 StatusCode: 200, 206 } 207 208 dir := t.TempDir() 209 oldCWD, _ := os.Getwd() 210 _ = os.Chdir(dir) 211 // clean up tmp dir 212 defer func() { 213 _ = os.Chdir(oldCWD) 214 }() 215 216 manifestFileString := `` 217 218 manifestFileStringBody := []byte(manifestFileString) 219 err := os.WriteFile("repositoriesTest.yml", manifestFileStringBody, 0644) 220 221 config := abapEnvironmentCheckoutBranchOptions{ 222 CfAPIEndpoint: "https://api.endpoint.com", 223 CfOrg: "testOrg", 224 CfSpace: "testSpace", 225 CfServiceInstance: "testInstance", 226 CfServiceKeyName: "testServiceKey", 227 Username: "testUser", 228 Password: "testPassword", 229 Repositories: "repositoriesTest.yml", 230 } 231 apiManager = &abaputils.SoftwareComponentApiManager{Client: client, PollIntervall: 1 * time.Nanosecond, Force0510: true} 232 err = runAbapEnvironmentCheckoutBranch(&config, &autils, apiManager) 233 assert.EqualError(t, err, expectedErrorMessage) 234 }) 235 t.Run("Failure case: checkout Branches from wrong file config", func(t *testing.T) { 236 expectedErrorMessage := "Could not read repositories: Could not unmarshal repositoriesTest.yml" 237 238 var autils = abaputils.AUtilsMock{} 239 defer autils.Cleanup() 240 autils.ReturnedConnectionDetailsHTTP.Password = "password" 241 autils.ReturnedConnectionDetailsHTTP.User = "user" 242 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 243 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 244 245 pollIntervall := abaputils.AUtilsMock{} 246 defer pollIntervall.Cleanup() 247 248 receivedURI := "example.com/Branches" 249 client := &abaputils.ClientMock{ 250 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 251 Token: "myToken", 252 StatusCode: 200, 253 } 254 255 dir := t.TempDir() 256 oldCWD, _ := os.Getwd() 257 _ = os.Chdir(dir) 258 // clean up tmp dir 259 defer func() { 260 _ = os.Chdir(oldCWD) 261 }() 262 263 manifestFileString := ` 264 - repo: 'testRepo' 265 - repo: 'testRepo2'` 266 267 manifestFileStringBody := []byte(manifestFileString) 268 err := os.WriteFile("repositoriesTest.yml", manifestFileStringBody, 0644) 269 270 config := abapEnvironmentCheckoutBranchOptions{ 271 CfAPIEndpoint: "https://api.endpoint.com", 272 CfOrg: "testOrg", 273 CfSpace: "testSpace", 274 CfServiceInstance: "testInstance", 275 CfServiceKeyName: "testServiceKey", 276 Username: "testUser", 277 Password: "testPassword", 278 Repositories: "repositoriesTest.yml", 279 } 280 apiManager = &abaputils.SoftwareComponentApiManager{Client: client, PollIntervall: 1 * time.Nanosecond, Force0510: true} 281 err = runAbapEnvironmentCheckoutBranch(&config, &autils, apiManager) 282 assert.EqualError(t, err, expectedErrorMessage) 283 }) 284 } 285 286 func TestCheckoutConfigChecker(t *testing.T) { 287 t.Run("Success case: check config", func(t *testing.T) { 288 config := abapEnvironmentCheckoutBranchOptions{ 289 RepositoryName: "testRepo1", 290 BranchName: "feature-unit-test", 291 } 292 err := checkCheckoutBranchRepositoryConfiguration(config) 293 assert.NoError(t, err) 294 }) 295 t.Run("Success case: check file config", func(t *testing.T) { 296 config := abapEnvironmentCheckoutBranchOptions{ 297 Repositories: "test.file", 298 BranchName: "feature-unit-test", 299 } 300 err := checkCheckoutBranchRepositoryConfiguration(config) 301 assert.NoError(t, err) 302 }) 303 t.Run("Failure case: check empty config", func(t *testing.T) { 304 expectedErrorMessage := "You have not specified any repository or branch configuration to be checked out in the ABAP Environment System. Please make sure that you specified the repositories with their branches that should be checked out either in a dedicated file or via the parameters 'repositoryName' and 'branchName'. For more information please read the user documentation" 305 306 config := abapEnvironmentCheckoutBranchOptions{} 307 err := checkCheckoutBranchRepositoryConfiguration(config) 308 assert.Equal(t, expectedErrorMessage, err.Error(), "Different error message expected") 309 }) 310 }