2020-02-04 12:43:27 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-07-23 10:26:50 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/abaputils"
|
2020-04-08 12:43:41 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-02-04 12:43:27 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-08-21 14:49:48 +02:00
|
|
|
func TestPullStep(t *testing.T) {
|
2020-07-31 14:43:23 +02:00
|
|
|
t.Run("Run Step Successful", 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 := abapEnvironmentPullGitRepoOptions{
|
|
|
|
CfAPIEndpoint: "https://api.endpoint.com",
|
|
|
|
CfOrg: "testOrg",
|
|
|
|
CfSpace: "testSpace",
|
|
|
|
CfServiceInstance: "testInstance",
|
|
|
|
CfServiceKeyName: "testServiceKey",
|
|
|
|
Username: "testUser",
|
|
|
|
Password: "testPassword",
|
|
|
|
RepositoryNames: []string{"testRepo1"},
|
|
|
|
}
|
|
|
|
|
2020-08-04 17:52:28 +02:00
|
|
|
client := &abaputils.ClientMock{
|
2020-07-31 14:43:23 +02:00
|
|
|
BodyList: []string{
|
|
|
|
`{"d" : { "status" : "S" } }`,
|
2020-08-07 11:09:58 +02:00
|
|
|
`{"d" : { "status" : "R" } }`,
|
|
|
|
`{"d" : { "status" : "R" } }`,
|
|
|
|
`{"d" : { "status" : "R" } }`,
|
2020-07-31 14:43:23 +02:00
|
|
|
},
|
|
|
|
Token: "myToken",
|
|
|
|
StatusCode: 200,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := runAbapEnvironmentPullGitRepo(&config, nil, &autils, client)
|
|
|
|
assert.NoError(t, err, "Did not expect error")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-04 12:43:27 +02:00
|
|
|
func TestTriggerPull(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("Test trigger pull: success case", func(t *testing.T) {
|
|
|
|
|
2020-04-08 12:43:41 +02:00
|
|
|
receivedURI := "example.com/Entity"
|
|
|
|
uriExpected := receivedURI + "?$expand=to_Execution_log,to_Transport_log"
|
2020-02-04 12:43:27 +02:00
|
|
|
tokenExpected := "myToken"
|
|
|
|
|
2020-08-04 17:52:28 +02:00
|
|
|
client := &abaputils.ClientMock{
|
2020-04-08 12:43:41 +02:00
|
|
|
Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`,
|
|
|
|
Token: tokenExpected,
|
|
|
|
StatusCode: 200,
|
2020-02-04 12:43:27 +02:00
|
|
|
}
|
|
|
|
config := abapEnvironmentPullGitRepoOptions{
|
|
|
|
CfAPIEndpoint: "https://api.endpoint.com",
|
|
|
|
CfOrg: "testOrg",
|
|
|
|
CfSpace: "testSpace",
|
|
|
|
CfServiceInstance: "testInstance",
|
2020-06-30 15:46:07 +02:00
|
|
|
CfServiceKeyName: "testServiceKey",
|
2020-02-04 12:43:27 +02:00
|
|
|
Username: "testUser",
|
|
|
|
Password: "testPassword",
|
2020-05-07 15:51:11 +02:00
|
|
|
RepositoryNames: []string{"testRepo1", "testRepo2"},
|
2020-02-04 12:43:27 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 10:26:50 +02:00
|
|
|
con := abaputils.ConnectionDetailsHTTP{
|
2020-02-04 12:43:27 +02:00
|
|
|
User: "MY_USER",
|
|
|
|
Password: "MY_PW",
|
|
|
|
URL: "https://api.endpoint.com/Entity/",
|
|
|
|
}
|
2020-05-07 15:51:11 +02:00
|
|
|
entityConnection, err := triggerPull(config.RepositoryNames[0], con, client)
|
2020-04-08 12:43:41 +02:00
|
|
|
assert.Nil(t, err)
|
2020-02-04 12:43:27 +02:00
|
|
|
assert.Equal(t, uriExpected, entityConnection.URL)
|
|
|
|
assert.Equal(t, tokenExpected, entityConnection.XCsrfToken)
|
|
|
|
})
|
|
|
|
|
2020-04-08 12:43:41 +02:00
|
|
|
t.Run("Test trigger pull: ABAP Error", func(t *testing.T) {
|
|
|
|
|
|
|
|
errorMessage := "ABAP Error Message"
|
|
|
|
errorCode := "ERROR/001"
|
|
|
|
HTTPErrorMessage := "HTTP Error Message"
|
|
|
|
combinedErrorMessage := "HTTP Error Message: ERROR/001 - ABAP Error Message"
|
|
|
|
|
2020-08-04 17:52:28 +02:00
|
|
|
client := &abaputils.ClientMock{
|
2020-04-08 12:43:41 +02:00
|
|
|
Body: `{"error" : { "code" : "` + errorCode + `", "message" : { "lang" : "en", "value" : "` + errorMessage + `" } } }`,
|
|
|
|
Token: "myToken",
|
|
|
|
StatusCode: 400,
|
|
|
|
Error: errors.New(HTTPErrorMessage),
|
|
|
|
}
|
|
|
|
config := abapEnvironmentPullGitRepoOptions{
|
|
|
|
CfAPIEndpoint: "https://api.endpoint.com",
|
|
|
|
CfOrg: "testOrg",
|
|
|
|
CfSpace: "testSpace",
|
|
|
|
CfServiceInstance: "testInstance",
|
2020-06-30 15:46:07 +02:00
|
|
|
CfServiceKeyName: "testServiceKey",
|
2020-04-08 12:43:41 +02:00
|
|
|
Username: "testUser",
|
|
|
|
Password: "testPassword",
|
2020-05-07 15:51:11 +02:00
|
|
|
RepositoryNames: []string{"testRepo1", "testRepo2"},
|
2020-04-08 12:43:41 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 10:26:50 +02:00
|
|
|
con := abaputils.ConnectionDetailsHTTP{
|
2020-04-08 12:43:41 +02:00
|
|
|
User: "MY_USER",
|
|
|
|
Password: "MY_PW",
|
|
|
|
URL: "https://api.endpoint.com/Entity/",
|
|
|
|
}
|
2020-05-07 15:51:11 +02:00
|
|
|
_, err := triggerPull(config.RepositoryNames[0], con, client)
|
2020-04-08 12:43:41 +02:00
|
|
|
assert.Equal(t, combinedErrorMessage, err.Error(), "Different error message expected")
|
|
|
|
})
|
|
|
|
|
2020-02-04 12:43:27 +02:00
|
|
|
}
|