2020-02-04 12:43:27 +02:00
package cmd
import (
2020-10-05 14:38:35 +02:00
"io/ioutil"
"os"
2020-02-04 12:43:27 +02:00
"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 ) {
2021-02-04 16:19:42 +02:00
2020-07-31 14:43:23 +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"
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 ,
}
2021-08-04 17:31:16 +02:00
err := runAbapEnvironmentPullGitRepo ( & config , & autils , client )
2020-07-31 14:43:23 +02:00
assert . NoError ( t , err , "Did not expect error" )
} )
2020-11-02 15:17:13 +02:00
2020-10-05 14:38:35 +02:00
t . Run ( "Run Step Failure" , func ( t * testing . T ) {
expectedErrorMessage := "Something failed during the pull of the repositories: Checking configuration failed: You have not specified any repository configuration to be pulled into the ABAP Environment System. Please make sure that you specified the repositories that should be pulled either in a dedicated file or via the parameter 'repositoryNames'. For more information please read the User documentation"
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"
receivedURI := "example.com/Entity"
tokenExpected := "myToken"
client := & abaputils . ClientMock {
Body : ` { "d" : { "__metadata" : { "uri" : " ` + receivedURI + ` " } } } ` ,
Token : tokenExpected ,
StatusCode : 200 ,
}
config := abapEnvironmentPullGitRepoOptions { }
2021-08-04 17:31:16 +02:00
err := runAbapEnvironmentPullGitRepo ( & config , & autils , client )
2020-10-05 14:38:35 +02:00
assert . Equal ( t , expectedErrorMessage , err . Error ( ) , "Different error message expected" )
} )
2020-11-02 15:17:13 +02:00
2020-10-05 14:38:35 +02:00
t . Run ( "Success case: pull repos from file config" , 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"
receivedURI := "example.com/Branches"
client := & abaputils . ClientMock {
Body : ` { "d" : { "__metadata" : { "uri" : " ` + receivedURI + ` " } } } ` ,
Token : "myToken" ,
StatusCode : 200 ,
}
dir , err := ioutil . TempDir ( "" , "test pull repos" )
if err != nil {
t . Fatal ( "Failed to create temporary directory" )
}
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
_ = os . RemoveAll ( dir )
} ( )
manifestFileString := `
repositories :
- name : ' testRepo '
branch : ' testBranch '
2020-11-02 15:17:13 +02:00
commitID : ' 9 caede7f31028cd52333eb496434275687fefb47 '
2020-10-05 14:38:35 +02:00
- name : ' testRepo2 '
branch : ' testBranch2 '
- name : ' testRepo3 '
branch : ' testBranch3 ' `
err = ioutil . WriteFile ( "repositoriesTest.yml" , [ ] byte ( manifestFileString ) , 0644 )
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
Repositories : "repositoriesTest.yml" ,
}
2021-08-04 17:31:16 +02:00
err = runAbapEnvironmentPullGitRepo ( & config , & autils , client )
2020-10-05 14:38:35 +02:00
assert . NoError ( t , err )
} )
2020-11-02 15:17:13 +02:00
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"
dir , errDir := ioutil . TempDir ( "" , "test read addon descriptor" )
if errDir != nil {
t . Fatal ( "Failed to create temporary directory" )
}
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
_ = os . RemoveAll ( dir )
} ( )
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" )
file . Write ( [ ] byte ( body ) )
config := abapEnvironmentPullGitRepoOptions {
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" : { "status" : "E" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
2021-08-04 17:31:16 +02:00
err := runAbapEnvironmentPullGitRepo ( & config , & autils , client )
2020-11-02 15:17:13 +02:00
if assert . Error ( t , err , "Expected error" ) {
assert . Equal ( t , "Something failed during the pull of the repositories: Pull of '/DMO/REPO_A', commit 'ABCD1234' failed on the ABAP System" , err . Error ( ) , "Expected different error message" )
}
} )
t . Run ( "Status Error, Ignore Commit" , 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"
dir , errDir := ioutil . TempDir ( "" , "test read addon descriptor" )
if errDir != nil {
t . Fatal ( "Failed to create temporary directory" )
}
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
_ = os . RemoveAll ( dir )
} ( )
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" )
file . Write ( [ ] byte ( body ) )
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
Repositories : "filename.yaml" ,
IgnoreCommit : true ,
}
client := & abaputils . ClientMock {
BodyList : [ ] string {
` { "d" : { "status" : "E" } } ` ,
` { "d" : { "status" : "R" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
2021-08-04 17:31:16 +02:00
err := runAbapEnvironmentPullGitRepo ( & config , & autils , client )
2020-11-02 15:17:13 +02:00
if assert . Error ( t , err , "Expected error" ) {
assert . Equal ( t , "Something failed during the pull of the repositories: Pull of '/DMO/REPO_A' failed on the ABAP System" , err . Error ( ) , "Expected different error message" )
}
} )
2020-10-05 14:38:35 +02:00
t . Run ( "Failure case: pull repos from empty file config" , func ( t * testing . T ) {
expectedErrorMessage := "Something failed during the pull of the repositories: Error in config file repositoriesTest.yml, AddonDescriptor doesn't contain any repositories"
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"
receivedURI := "example.com/Branches"
client := & abaputils . ClientMock {
Body : ` { "d" : { "__metadata" : { "uri" : " ` + receivedURI + ` " } } } ` ,
Token : "myToken" ,
StatusCode : 200 ,
}
dir , err := ioutil . TempDir ( "" , "test pull repos" )
if err != nil {
t . Fatal ( "Failed to create temporary directory" )
}
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
_ = os . RemoveAll ( dir )
} ( )
manifestFileString := ` `
manifestFileStringBody := [ ] byte ( manifestFileString )
err = ioutil . WriteFile ( "repositoriesTest.yml" , manifestFileStringBody , 0644 )
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
Repositories : "repositoriesTest.yml" ,
}
2021-08-04 17:31:16 +02:00
err = runAbapEnvironmentPullGitRepo ( & config , & autils , client )
2020-10-05 14:38:35 +02:00
assert . EqualError ( t , err , expectedErrorMessage )
} )
2020-11-02 15:17:13 +02:00
2020-10-05 14:38:35 +02:00
t . Run ( "Failure case: pull repos from wrong file config" , func ( t * testing . T ) {
expectedErrorMessage := "Something failed during the pull of the repositories: Could not unmarshal repositoriesTest.yml"
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"
pollIntervall := abaputils . AUtilsMock { }
defer pollIntervall . Cleanup ( )
receivedURI := "example.com/Branches"
client := & abaputils . ClientMock {
Body : ` { "d" : { "__metadata" : { "uri" : " ` + receivedURI + ` " } } } ` ,
Token : "myToken" ,
StatusCode : 200 ,
}
dir , err := ioutil . TempDir ( "" , "test pull repos" )
if err != nil {
t . Fatal ( "Failed to create temporary directory" )
}
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
// clean up tmp dir
defer func ( ) {
_ = os . Chdir ( oldCWD )
_ = os . RemoveAll ( dir )
} ( )
manifestFileString := `
- repo : ' testRepo '
- repo : ' testRepo2 ' `
manifestFileStringBody := [ ] byte ( manifestFileString )
err = ioutil . WriteFile ( "repositoriesTest.yml" , manifestFileStringBody , 0644 )
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKeyName : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
Repositories : "repositoriesTest.yml" ,
}
2021-08-04 17:31:16 +02:00
err = runAbapEnvironmentPullGitRepo ( & config , & autils , client )
2020-10-05 14:38:35 +02:00
assert . EqualError ( t , err , expectedErrorMessage )
} )
2020-07-31 14:43:23 +02:00
}
2020-02-04 12:43:27 +02:00
func TestTriggerPull ( t * testing . T ) {
2021-02-04 16:19:42 +02:00
2020-02-04 12:43:27 +02:00
t . Run ( "Test trigger pull: success case" , func ( t * testing . T ) {
2021-02-04 16:19:42 +02:00
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
}
2020-11-02 15:17:13 +02:00
repoName := "testRepo1"
testCommit := "9caede7f31028cd52333eb496434275687fefb47"
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-11-02 15:17:13 +02:00
entityConnection , err := triggerPull ( abaputils . Repository { Name : repoName , CommitID : testCommit } , 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 ) {
2021-02-04 16:19:42 +02:00
2020-04-08 12:43:41 +02:00
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 ) ,
}
2020-11-02 15:17:13 +02:00
repoName := "testRepo1"
testCommit := "9caede7f31028cd52333eb496434275687fefb47"
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-11-02 15:17:13 +02:00
_ , err := triggerPull ( abaputils . Repository { Name : repoName , CommitID : testCommit } , con , client )
2020-04-08 12:43:41 +02:00
assert . Equal ( t , combinedErrorMessage , err . Error ( ) , "Different error message expected" )
} )
2020-10-05 14:38:35 +02:00
}
2020-04-08 12:43:41 +02:00
2020-10-05 14:38:35 +02:00
func TestPullConfigChecker ( t * testing . T ) {
t . Run ( "Success case: check config file" , func ( t * testing . T ) {
config := abapEnvironmentPullGitRepoOptions {
Repositories : "test.file" ,
}
err := checkPullRepositoryConfiguration ( config )
assert . NoError ( t , err )
} )
t . Run ( "Success case: check config" , func ( t * testing . T ) {
config := abapEnvironmentPullGitRepoOptions {
RepositoryNames : [ ] string { "testRepo" , "testRepo2" } ,
}
err := checkPullRepositoryConfiguration ( config )
assert . NoError ( t , err )
} )
t . Run ( "Failure case: empty config" , func ( t * testing . T ) {
errorMessage := "Checking configuration failed: You have not specified any repository configuration to be pulled into the ABAP Environment System. Please make sure that you specified the repositories that should be pulled either in a dedicated file or via the parameter 'repositoryNames'. For more information please read the User documentation"
config := abapEnvironmentPullGitRepoOptions { }
err := checkPullRepositoryConfiguration ( config )
assert . Equal ( t , errorMessage , err . Error ( ) , "Different error message expected" )
} )
2020-02-04 12:43:27 +02:00
}
2020-11-02 15:17:13 +02:00
func TestHelpFunctions ( t * testing . T ) {
t . Run ( "Ignore Commit" , func ( t * testing . T ) {
repo1 := abaputils . Repository {
Name : "Repo1" ,
CommitID : "ABCD1234" ,
}
repo2 := abaputils . Repository {
Name : "Repo2" ,
}
repoList := [ ] abaputils . Repository { repo1 , repo2 }
handleIgnoreCommit ( repoList , true )
assert . Equal ( t , "" , repoList [ 0 ] . CommitID , "Expected emtpy CommitID" )
assert . Equal ( t , "" , repoList [ 1 ] . CommitID , "Expected emtpy CommitID" )
} )
t . Run ( "Not Ignore Commit" , func ( t * testing . T ) {
repo1 := abaputils . Repository {
Name : "Repo1" ,
CommitID : "ABCD1234" ,
}
repo2 := abaputils . Repository {
Name : "Repo2" ,
}
repoList := [ ] abaputils . Repository { repo1 , repo2 }
handleIgnoreCommit ( repoList , false )
assert . Equal ( t , "ABCD1234" , repoList [ 0 ] . CommitID , "Expected CommitID" )
assert . Equal ( t , "" , repoList [ 1 ] . CommitID , "Expected emtpy CommitID" )
} )
}