2023-05-03 18:02:11 +02:00
//go:build unit
// +build unit
2020-08-21 14:49:48 +02:00
package cmd
import (
2022-06-30 10:43:33 +02:00
"bytes"
2022-06-29 15:50:33 +02:00
"encoding/json"
2023-08-16 12:57:04 +02:00
"io"
2022-06-30 10:43:33 +02:00
"net/http"
2020-08-21 14:49:48 +02:00
"os"
"testing"
"github.com/SAP/jenkins-library/pkg/abaputils"
2022-06-30 10:43:33 +02:00
"github.com/pkg/errors"
2020-08-21 14:49:48 +02:00
"github.com/stretchr/testify/assert"
)
2022-06-29 15:50:33 +02:00
var executionLogStringClone string
func init ( ) {
2023-03-31 15:26:38 +02:00
executionLog := abaputils . LogProtocolResults {
Results : [ ] abaputils . LogProtocol {
{
ProtocolLine : 1 ,
OverviewIndex : 1 ,
Type : "LogEntry" ,
Description : "S" ,
Timestamp : "/Date(1644332299000+0000)/" ,
2022-06-29 15:50:33 +02:00
} ,
} ,
}
executionLogResponse , _ := json . Marshal ( executionLog )
executionLogStringClone = string ( executionLogResponse )
}
2020-08-21 14:49:48 +02:00
func TestCloneStep ( t * testing . T ) {
2022-06-30 10:43:33 +02:00
t . Run ( "Run Step - Successful with repositories.yml" , func ( t * testing . T ) {
2020-08-21 14:49:48 +02:00
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
2022-07-12 15:19:12 +02:00
dir := t . TempDir ( )
2020-08-21 14:49:48 +02:00
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
} ( )
body := ` -- -
repositories :
- name : / DMO / REPO_A
tag : v - 1.0 .1 - build - 0001
branch : branchA
version : 1.0 .1
- name : / DMO / REPO_B
tag : rel - 2.1 .1 - build - 0001
branch : branchB
version : 2.1 .1
`
file , _ := os . Create ( "filename.yaml" )
2022-07-06 14:29:04 +02:00
_ , err := file . Write ( [ ] byte ( body ) )
assert . NoError ( t , err )
2020-08-21 14:49:48 +02:00
config := abapEnvironmentCloneGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
Repositories : "filename.yaml" ,
}
2022-07-06 14:29:04 +02:00
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 } ] } } ] } } } `
2020-08-21 14:49:48 +02:00
client := & abaputils . ClientMock {
BodyList : [ ] string {
2023-03-31 15:26:38 +02:00
` { "d" : [] } ` ,
2022-06-29 15:50:33 +02:00
` { "d" : ` + executionLogStringClone + ` } ` ,
2022-02-11 11:16:40 +02:00
logResultSuccess ,
2020-08-21 14:49:48 +02:00
` { "d" : { "status" : "S" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
2023-03-31 15:26:38 +02:00
` { "d" : [] } ` ,
2022-06-29 15:50:33 +02:00
` { "d" : ` + executionLogStringClone + ` } ` ,
2022-02-11 11:16:40 +02:00
logResultSuccess ,
2020-08-21 14:49:48 +02:00
` { "d" : { "status" : "S" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
2022-06-30 10:43:33 +02:00
} ,
Token : "myToken" ,
}
2022-07-06 14:29:04 +02:00
err = runAbapEnvironmentCloneGitRepo ( & config , & autils , client )
2022-06-30 10:43:33 +02:00
assert . NoError ( t , err , "Did not expect error" )
assert . Equal ( t , 0 , len ( client . BodyList ) , "Not all requests were done" )
} )
t . Run ( "Run Step - Successful with repositoryName" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
config := abapEnvironmentCloneGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
RepositoryName : "testRepo1" ,
BranchName : "testBranch1" ,
}
2022-07-06 14:29:04 +02:00
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 } ] } } ] } } } `
2022-06-30 10:43:33 +02:00
client := & abaputils . ClientMock {
BodyList : [ ] string {
2023-03-31 15:26:38 +02:00
` { "d" : [] } ` ,
2022-06-29 15:50:33 +02:00
` { "d" : ` + executionLogStringClone + ` } ` ,
2022-02-11 11:16:40 +02:00
logResultSuccess ,
2020-08-21 14:49:48 +02:00
` { "d" : { "status" : "S" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
2021-08-04 17:31:16 +02:00
err := runAbapEnvironmentCloneGitRepo ( & config , & autils , client )
2020-08-21 14:49:48 +02:00
assert . NoError ( t , err , "Did not expect error" )
assert . Equal ( t , 0 , len ( client . BodyList ) , "Not all requests were done" )
} )
t . Run ( "Run Step - failing" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
config := abapEnvironmentCloneGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
RepositoryName : "testRepo1" ,
BranchName : "testBranch1" ,
}
client := & abaputils . ClientMock {
BodyList : [ ] string {
` { "d" : { } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
2021-08-04 17:31:16 +02:00
err := runAbapEnvironmentCloneGitRepo ( & config , & autils , client )
2020-08-21 14:49:48 +02:00
if assert . Error ( t , err , "Expected error" ) {
2021-12-20 18:58:58 +02:00
assert . Equal ( t , "Clone of repository / software component 'testRepo1', branch 'testBranch1' failed on the ABAP system: Request to ABAP System not successful" , err . Error ( ) , "Expected different error message" )
2020-08-21 14:49:48 +02:00
}
} )
}
func TestCloneStepErrorMessages ( t * testing . T ) {
t . Run ( "Status Error" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
2022-07-12 15:19:12 +02:00
dir := t . TempDir ( )
2020-11-02 15:17:13 +02:00
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
} ( )
body := ` -- -
repositories :
- name : / DMO / REPO_A
tag : v - 1.0 .1 - build - 0001
branch : branchA
version : 1.0 .1
commitID : ABCD1234
`
file , _ := os . Create ( "filename.yaml" )
2022-07-06 14:29:04 +02:00
_ , err := file . Write ( [ ] byte ( body ) )
assert . NoError ( t , err )
2020-11-02 15:17:13 +02:00
2020-08-21 14:49:48 +02:00
config := abapEnvironmentCloneGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
2020-11-02 15:17:13 +02:00
Repositories : "filename.yaml" ,
2020-08-21 14:49:48 +02:00
}
2022-07-06 14:29:04 +02:00
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 } ] } } ] } } } `
2020-08-21 14:49:48 +02:00
client := & abaputils . ClientMock {
BodyList : [ ] string {
2022-06-29 15:50:33 +02:00
` { "d" : ` + executionLogStringClone + ` } ` ,
2022-02-11 11:16:40 +02:00
logResultError ,
` { "d" : { "EntitySets" : [ "LogOverviews" ] } } ` ,
2020-08-21 14:49:48 +02:00
` { "d" : { "status" : "E" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
2022-07-06 14:29:04 +02:00
err = runAbapEnvironmentCloneGitRepo ( & config , & autils , client )
2020-08-21 14:49:48 +02:00
if assert . Error ( t , err , "Expected error" ) {
2021-12-20 18:58:58 +02:00
assert . Equal ( t , "Clone of repository / software component '/DMO/REPO_A', branch 'branchA', commit 'ABCD1234' failed on the ABAP System" , err . Error ( ) , "Expected different error message" )
2020-08-21 14:49:48 +02:00
}
} )
t . Run ( "Poll Request Error" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
config := abapEnvironmentCloneGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
RepositoryName : "testRepo1" ,
BranchName : "testBranch1" ,
}
client := & abaputils . ClientMock {
BodyList : [ ] string {
` { "d" : { } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
2021-08-04 17:31:16 +02:00
err := runAbapEnvironmentCloneGitRepo ( & config , & autils , client )
2020-08-21 14:49:48 +02:00
if assert . Error ( t , err , "Expected error" ) {
2021-12-20 18:58:58 +02:00
assert . Equal ( t , "Clone of repository / software component 'testRepo1', branch 'testBranch1' failed on the ABAP system: Request to ABAP System not successful" , err . Error ( ) , "Expected different error message" )
2020-08-21 14:49:48 +02:00
}
} )
t . Run ( "Trigger Clone Error" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
config := abapEnvironmentCloneGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
RepositoryName : "testRepo1" ,
BranchName : "testBranch1" ,
}
client := & abaputils . ClientMock {
BodyList : [ ] string {
` { "d" : { } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
2021-08-04 17:31:16 +02:00
err := runAbapEnvironmentCloneGitRepo ( & config , & autils , client )
2020-08-21 14:49:48 +02:00
if assert . Error ( t , err , "Expected error" ) {
2021-12-20 18:58:58 +02:00
assert . Equal ( t , "Clone of repository / software component 'testRepo1', branch 'testBranch1' failed on the ABAP system: Request to ABAP System not successful" , err . Error ( ) , "Expected different error message" )
2020-08-21 14:49:48 +02:00
}
} )
t . Run ( "Missing file error" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
config := abapEnvironmentCloneGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
Repositories : "filename.yaml" ,
}
client := & abaputils . ClientMock {
BodyList : [ ] string {
` { "d" : { } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
2021-08-04 17:31:16 +02:00
err := runAbapEnvironmentCloneGitRepo ( & config , & autils , client )
2020-08-21 14:49:48 +02:00
if assert . Error ( t , err , "Expected error" ) {
2020-10-05 14:38:35 +02:00
assert . Equal ( t , "Something failed during the clone: Could not find filename.yaml" , err . Error ( ) , "Expected different error message" )
2020-08-21 14:49:48 +02:00
}
} )
2022-06-30 10:43:33 +02:00
t . Run ( "Config overload" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
config := abapEnvironmentCloneGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
Repositories : "filename.yaml" ,
RepositoryName : "/DMO/REPO" ,
BranchName : "Branch" ,
}
2022-07-06 14:29:04 +02:00
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 } ] } } ] } } } `
2022-06-30 10:43:33 +02:00
client := & abaputils . ClientMock {
BodyList : [ ] string {
logResultError ,
` { "d" : { "EntitySets" : [ "LogOverviews" ] } } ` ,
` { "d" : { "status" : "E" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
err := runAbapEnvironmentCloneGitRepo ( & config , & autils , client )
if assert . Error ( t , err , "Expected error" ) {
assert . Equal ( t , "The provided configuration is not allowed: It is not allowed to configure the parameters `repositories`and `repositoryName` at the same time" , err . Error ( ) , "Expected different error message" )
}
} )
}
func TestALreadyCloned ( t * testing . T ) {
t . Run ( "Already Cloned" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . Host = "example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
2022-07-06 14:29:04 +02:00
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 } ] } } ] } } } `
2022-06-30 10:43:33 +02:00
client := & abaputils . ClientMock {
BodyList : [ ] string {
2023-03-31 15:26:38 +02:00
` { "d" : } ` ,
2022-06-30 10:43:33 +02:00
` { "d" : ` + executionLogStringClone + ` } ` ,
logResultSuccess ,
` { "d" : { "status" : "S" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
2023-03-31 15:26:38 +02:00
` { "d" : } ` ,
2022-06-30 10:43:33 +02:00
` { "d" : ` + executionLogStringClone + ` } ` ,
logResultSuccess ,
` { "d" : { "status" : "S" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
bodyString := ` { "error" : { "code" : "A4C_A2G/257", "message" : { "lang" : "de", "value" : "Already Cloned"} } } `
body := [ ] byte ( bodyString )
resp := http . Response {
Status : "400 Bad Request" ,
StatusCode : 400 ,
2023-08-16 12:57:04 +02:00
Body : io . NopCloser ( bytes . NewReader ( body ) ) ,
2022-06-30 10:43:33 +02:00
}
repo := abaputils . Repository {
Name : "Test" ,
Branch : "Branch" ,
CommitID : "abcd1234" ,
}
err := errors . New ( "Custom Error" )
err , _ = handleCloneError ( & resp , err , autils . ReturnedConnectionDetailsHTTP , client , repo )
assert . NoError ( t , err , "Did not expect error" )
} )
t . Run ( "Already Cloned, Pull fails" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . Host = "example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
2022-07-06 14:29:04 +02:00
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 } ] } } ] } } } `
2022-06-30 10:43:33 +02:00
client := & abaputils . ClientMock {
BodyList : [ ] string {
` { "d" : ` + executionLogStringClone + ` } ` ,
logResultSuccess ,
` { "d" : { "EntitySets" : [ "LogOverviews" ] } } ` ,
` { "d" : { "status" : "E" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : ` + executionLogStringClone + ` } ` ,
logResultSuccess ,
` { "d" : { "EntitySets" : [ "LogOverviews" ] } } ` ,
` { "d" : { "status" : "S" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
bodyString := ` { "error" : { "code" : "A4C_A2G/257", "message" : { "lang" : "de", "value" : "Already Cloned"} } } `
body := [ ] byte ( bodyString )
resp := http . Response {
Status : "400 Bad Request" ,
StatusCode : 400 ,
2023-08-16 12:57:04 +02:00
Body : io . NopCloser ( bytes . NewReader ( body ) ) ,
2022-06-30 10:43:33 +02:00
}
repo := abaputils . Repository {
Name : "Test" ,
Branch : "Branch" ,
CommitID : "abcd1234" ,
}
err := errors . New ( "Custom Error" )
err , _ = handleCloneError ( & resp , err , autils . ReturnedConnectionDetailsHTTP , client , repo )
if assert . Error ( t , err , "Expected error" ) {
2023-03-31 15:26:38 +02:00
assert . Equal ( t , "Pull of the repository / software component 'Test', commit 'abcd1234' failed on the ABAP system: Request to ABAP System not successful" , err . Error ( ) , "Expected different error message" )
2022-06-30 10:43:33 +02:00
}
} )
t . Run ( "Already Cloned, checkout fails" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . Host = "example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
2022-07-06 14:29:04 +02:00
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 } ] } } ] } } } `
2022-06-30 10:43:33 +02:00
client := & abaputils . ClientMock {
BodyList : [ ] string {
logResultSuccess ,
` { "d" : { "EntitySets" : [ "LogOverviews" ] } } ` ,
` { "d" : { "status" : "S" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
logResultSuccess ,
` { "d" : { "EntitySets" : [ "LogOverviews" ] } } ` ,
` { "d" : { "status" : "E" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
bodyString := ` { "error" : { "code" : "A4C_A2G/257", "message" : { "lang" : "de", "value" : "Already Cloned"} } } `
body := [ ] byte ( bodyString )
resp := http . Response {
Status : "400 Bad Request" ,
StatusCode : 400 ,
2023-08-16 12:57:04 +02:00
Body : io . NopCloser ( bytes . NewReader ( body ) ) ,
2022-06-30 10:43:33 +02:00
}
repo := abaputils . Repository {
Name : "Test" ,
Branch : "Branch" ,
CommitID : "abcd1234" ,
}
err := errors . New ( "Custom Error" )
err , _ = handleCloneError ( & resp , err , autils . ReturnedConnectionDetailsHTTP , client , repo )
if assert . Error ( t , err , "Expected error" ) {
assert . Equal ( t , "Something failed during the checkout: Checkout failed: Checkout of branch Branch failed on the ABAP System" , err . Error ( ) , "Expected different error message" )
}
} )
t . Run ( "Already Cloned, checkout fails" , func ( t * testing . T ) {
var autils = abaputils . AUtilsMock { }
defer autils . Cleanup ( )
autils . ReturnedConnectionDetailsHTTP . Password = "password"
autils . ReturnedConnectionDetailsHTTP . User = "user"
autils . ReturnedConnectionDetailsHTTP . URL = "https://example.com"
autils . ReturnedConnectionDetailsHTTP . Host = "example.com"
autils . ReturnedConnectionDetailsHTTP . XCsrfToken = "xcsrftoken"
client := & abaputils . ClientMock {
BodyList : [ ] string {
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
bodyString := ` { "error" : { "code" : "A4C_A2G/258", "message" : { "lang" : "de", "value" : "Some error message"} } } `
body := [ ] byte ( bodyString )
resp := http . Response {
Status : "400 Bad Request" ,
StatusCode : 400 ,
2023-08-16 12:57:04 +02:00
Body : io . NopCloser ( bytes . NewReader ( body ) ) ,
2022-06-30 10:43:33 +02:00
}
repo := abaputils . Repository {
Name : "Test" ,
Branch : "Branch" ,
CommitID : "abcd1234" ,
}
err := errors . New ( "Custom Error" )
err , _ = handleCloneError ( & resp , err , autils . ReturnedConnectionDetailsHTTP , client , repo )
if assert . Error ( t , err , "Expected error" ) {
assert . Equal ( t , "Custom Error: A4C_A2G/258 - Some error message" , err . Error ( ) , "Expected different error message" )
}
} )
2020-08-21 14:49:48 +02:00
}