mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
766a233c01
* Change to make pollIntervall configurable * Adapt checkout
118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/abaputils"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestStep(t *testing.T) {
|
|
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"},
|
|
}
|
|
|
|
client := &abaputils.ClientMock{
|
|
BodyList: []string{
|
|
`{"d" : { "status" : "S" } }`,
|
|
`{"d" : { "status" : "R" } }`,
|
|
`{"d" : { "status" : "R" } }`,
|
|
`{"d" : { "status" : "R" } }`,
|
|
},
|
|
Token: "myToken",
|
|
StatusCode: 200,
|
|
}
|
|
|
|
err := runAbapEnvironmentPullGitRepo(&config, nil, &autils, client)
|
|
assert.NoError(t, err, "Did not expect error")
|
|
})
|
|
}
|
|
|
|
func TestTriggerPull(t *testing.T) {
|
|
|
|
t.Run("Test trigger pull: success case", func(t *testing.T) {
|
|
|
|
receivedURI := "example.com/Entity"
|
|
uriExpected := receivedURI + "?$expand=to_Execution_log,to_Transport_log"
|
|
tokenExpected := "myToken"
|
|
|
|
client := &abaputils.ClientMock{
|
|
Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`,
|
|
Token: tokenExpected,
|
|
StatusCode: 200,
|
|
}
|
|
config := abapEnvironmentPullGitRepoOptions{
|
|
CfAPIEndpoint: "https://api.endpoint.com",
|
|
CfOrg: "testOrg",
|
|
CfSpace: "testSpace",
|
|
CfServiceInstance: "testInstance",
|
|
CfServiceKeyName: "testServiceKey",
|
|
Username: "testUser",
|
|
Password: "testPassword",
|
|
RepositoryNames: []string{"testRepo1", "testRepo2"},
|
|
}
|
|
|
|
con := abaputils.ConnectionDetailsHTTP{
|
|
User: "MY_USER",
|
|
Password: "MY_PW",
|
|
URL: "https://api.endpoint.com/Entity/",
|
|
}
|
|
entityConnection, err := triggerPull(config.RepositoryNames[0], con, client)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, uriExpected, entityConnection.URL)
|
|
assert.Equal(t, tokenExpected, entityConnection.XCsrfToken)
|
|
})
|
|
|
|
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"
|
|
|
|
client := &abaputils.ClientMock{
|
|
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",
|
|
CfServiceKeyName: "testServiceKey",
|
|
Username: "testUser",
|
|
Password: "testPassword",
|
|
RepositoryNames: []string{"testRepo1", "testRepo2"},
|
|
}
|
|
|
|
con := abaputils.ConnectionDetailsHTTP{
|
|
User: "MY_USER",
|
|
Password: "MY_PW",
|
|
URL: "https://api.endpoint.com/Entity/",
|
|
}
|
|
_, err := triggerPull(config.RepositoryNames[0], con, client)
|
|
assert.Equal(t, combinedErrorMessage, err.Error(), "Different error message expected")
|
|
})
|
|
|
|
}
|