2020-05-13 14:51:48 +02:00
package cloudfoundry
import (
2020-07-01 14:19:21 +02:00
"fmt"
2020-07-23 10:26:50 +02:00
"testing"
2020-07-01 14:19:21 +02:00
"github.com/SAP/jenkins-library/pkg/mock"
2020-05-13 14:51:48 +02:00
"github.com/stretchr/testify/assert"
)
2020-07-01 14:19:21 +02:00
func loginMockCleanup ( m * mock . ExecMockRunner ) {
m . ShouldFailOnCommand = map [ string ] error { }
m . StdoutReturn = map [ string ] string { }
m . Calls = [ ] mock . ExecCall { }
}
2020-05-13 14:51:48 +02:00
func TestCloudFoundryLoginCheck ( t * testing . T ) {
2020-07-01 14:19:21 +02:00
m := & mock . ExecMockRunner { }
2020-07-15 15:12:28 +02:00
t . Run ( "CF Login check: logged in" , func ( t * testing . T ) {
2020-07-01 14:19:21 +02:00
defer loginMockCleanup ( m )
2020-05-13 14:51:48 +02:00
cfconfig := LoginOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
}
2020-07-15 15:12:28 +02:00
cf := CFUtils { Exec : m , loggedIn : true }
2020-07-10 16:31:17 +02:00
loggedIn , err := cf . LoginCheck ( cfconfig )
2020-07-01 14:19:21 +02:00
if assert . NoError ( t , err ) {
assert . True ( t , loggedIn )
}
2020-05-13 14:51:48 +02:00
} )
2020-07-01 15:19:47 +02:00
2020-07-15 15:12:28 +02:00
t . Run ( "CF Login check: not logged in" , func ( t * testing . T ) {
2020-07-01 15:19:47 +02:00
defer loginMockCleanup ( m )
cfconfig := LoginOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
}
2020-07-15 15:12:28 +02:00
cf := CFUtils { Exec : m , loggedIn : false }
2020-07-10 16:31:17 +02:00
loggedIn , err := cf . LoginCheck ( cfconfig )
2020-07-01 15:19:47 +02:00
if assert . NoError ( t , err ) {
2020-07-15 15:12:28 +02:00
assert . False ( t , loggedIn )
2020-07-01 15:19:47 +02:00
}
} )
2020-05-13 14:51:48 +02:00
}
func TestCloudFoundryLogin ( t * testing . T ) {
2020-07-01 14:19:21 +02:00
m := & mock . ExecMockRunner { }
2020-05-13 14:51:48 +02:00
t . Run ( "CF Login: missing parameter" , func ( t * testing . T ) {
2020-07-01 14:19:21 +02:00
defer loginMockCleanup ( m )
2020-05-13 14:51:48 +02:00
cfconfig := LoginOptions { }
2020-07-10 16:31:17 +02:00
cf := CFUtils { Exec : m }
err := cf . Login ( cfconfig )
2020-05-13 14:51:48 +02:00
assert . EqualError ( t , err , "Failed to login to Cloud Foundry: Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password" )
} )
t . Run ( "CF Login: failure" , func ( t * testing . T ) {
2020-07-01 14:19:21 +02:00
defer loginMockCleanup ( m )
m . ShouldFailOnCommand = map [ string ] error { "cf login .*" : fmt . Errorf ( "wrong password or account does not exist" ) }
2020-05-13 14:51:48 +02:00
cfconfig := LoginOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfSpace : "testSpace" ,
CfOrg : "testOrg" ,
Username : "testUser" ,
Password : "testPassword" ,
}
2020-07-01 14:19:21 +02:00
2020-07-10 16:31:17 +02:00
cf := CFUtils { Exec : m }
err := cf . Login ( cfconfig )
2020-07-01 14:19:21 +02:00
if assert . EqualError ( t , err , "Failed to login to Cloud Foundry: wrong password or account does not exist" ) {
2020-07-15 15:12:28 +02:00
assert . False ( t , cf . loggedIn )
2020-07-01 14:19:21 +02:00
assert . Equal ( t , [ ] mock . ExecCall {
2020-09-24 08:58:53 +02:00
{ Exec : "cf" , Params : [ ] string {
2020-07-01 14:19:21 +02:00
"login" ,
"-a" , "https://api.endpoint.com" ,
"-o" , "testOrg" ,
"-s" , "testSpace" ,
"-u" , "testUser" ,
"-p" , "testPassword" ,
} } ,
} , m . Calls )
}
} )
t . Run ( "CF Login: success" , func ( t * testing . T ) {
defer loginMockCleanup ( m )
m . StdoutReturn = map [ string ] string { "cf api:*" : "Not logged in" }
cfconfig := LoginOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfSpace : "testSpace" ,
CfOrg : "testOrg" ,
Username : "testUser" ,
Password : "testPassword" ,
}
2020-07-10 16:31:17 +02:00
cf := CFUtils { Exec : m }
err := cf . Login ( cfconfig )
2020-07-01 14:19:21 +02:00
if assert . NoError ( t , err ) {
2020-07-15 15:12:28 +02:00
assert . True ( t , cf . loggedIn )
2020-07-01 14:19:21 +02:00
assert . Equal ( t , [ ] mock . ExecCall {
2020-09-24 08:58:53 +02:00
{ Exec : "cf" , Params : [ ] string {
2020-07-01 14:19:21 +02:00
"login" ,
"-a" , "https://api.endpoint.com" ,
"-o" , "testOrg" ,
"-s" , "testSpace" ,
"-u" , "testUser" ,
"-p" , "testPassword" ,
} } ,
} , m . Calls )
}
2020-05-13 14:51:48 +02:00
} )
2020-07-01 15:19:47 +02:00
t . Run ( "CF Login: with additional login options" , func ( t * testing . T ) {
defer loginMockCleanup ( m )
cfconfig := LoginOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfSpace : "testSpace" ,
CfOrg : "testOrg" ,
Username : "testUser" ,
Password : "testPassword" ,
CfLoginOpts : [ ] string {
// should never used in productive environment, but it is useful for rapid prototyping/troubleshooting
"--skip-ssl-validation" ,
"--origin" , "ldap" ,
} ,
}
2020-07-10 16:31:17 +02:00
cf := CFUtils { Exec : m }
err := cf . Login ( cfconfig )
2020-07-01 15:19:47 +02:00
if assert . NoError ( t , err ) {
2020-07-15 15:12:28 +02:00
assert . True ( t , cf . loggedIn )
2020-07-01 15:19:47 +02:00
assert . Equal ( t , [ ] mock . ExecCall {
2020-09-24 08:58:53 +02:00
{ Exec : "cf" , Params : [ ] string {
2020-07-01 15:19:47 +02:00
"login" ,
"-a" , "https://api.endpoint.com" ,
"-o" , "testOrg" ,
"-s" , "testSpace" ,
"-u" , "testUser" ,
"-p" , "testPassword" ,
"--skip-ssl-validation" ,
"--origin" , "ldap" ,
} } ,
} , m . Calls )
}
} )
2020-05-13 14:51:48 +02:00
}
func TestCloudFoundryLogout ( t * testing . T ) {
t . Run ( "CF Logout" , func ( t * testing . T ) {
2020-07-15 15:12:28 +02:00
cf := CFUtils { Exec : & mock . ExecMockRunner { } , loggedIn : true }
2020-07-10 16:31:17 +02:00
err := cf . Logout ( )
2020-07-15 15:12:28 +02:00
if assert . NoError ( t , err ) {
assert . False ( t , cf . loggedIn )
2020-05-13 14:51:48 +02:00
}
} )
}
func TestCloudFoundryReadServiceKeyAbapEnvironment ( t * testing . T ) {
2020-07-23 10:26:50 +02:00
t . Run ( "CF ReadServiceKey" , func ( t * testing . T ) {
//given
m := & mock . ExecMockRunner { }
defer loginMockCleanup ( m )
const testURL = "testurl.com"
const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull"
const username = "test_user"
const password = "test_password"
const serviceKey = `
cf comment test \ n \ n
{ "sap.cloud.service" : "com.sap.cloud.abap" , "url" : "` + testURL + `" , "systemid" : "H01" , "abap" : { "username" : "` + username + `" , "password" : "` + password + `" , "communication_scenario_id" : "SAP_COM_0510" , "communication_arrangement_id" : "SK_I6CBIRFZPPJDKYNATQA32W" , "communication_system_id" : "SK_I6CBIRFZPPJDKYNATQA32W" , "communication_inbound_user_id" : "CC0000000001" , "communication_inbound_user_auth_mode" : "2" } , "binding" : { "env" : "cf" , "version" : "0.0.1.1" , "type" : "basic" , "id" : "i6cBiRfZppJdKynaTqa32W" } , "preserve_host_header" : true } `
m . StdoutReturn = map [ string ] string { "cf service-key testInstance testServiceKeyName" : serviceKey }
2020-05-13 14:51:48 +02:00
cfconfig := ServiceKeyOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfSpace : "testSpace" ,
CfOrg : "testOrg" ,
CfServiceInstance : "testInstance" ,
2020-07-23 10:26:50 +02:00
CfServiceKeyName : "testServiceKeyName" ,
2020-05-13 14:51:48 +02:00
Username : "testUser" ,
Password : "testPassword" ,
}
2020-07-23 10:26:50 +02:00
//when
var err error
var abapServiceKey string
cf := CFUtils { Exec : m }
abapServiceKey , err = cf . ReadServiceKey ( cfconfig )
//then
if assert . NoError ( t , err ) {
assert . Equal ( t , [ ] mock . ExecCall {
2020-09-24 08:58:53 +02:00
{ Exec : "cf" , Params : [ ] string {
2020-07-23 10:26:50 +02:00
"login" ,
"-a" , "https://api.endpoint.com" ,
"-o" , "testOrg" ,
"-s" , "testSpace" ,
"-u" , "testUser" ,
"-p" , "testPassword" ,
} } ,
2020-09-24 08:58:53 +02:00
{ Exec : "cf" , Params : [ ] string { "service-key" , "testInstance" , "testServiceKeyName" } } ,
{ Exec : "cf" , Params : [ ] string { "logout" } } ,
2020-07-23 10:26:50 +02:00
} , m . Calls )
}
assert . Equal ( t , ` { "sap.cloud.service":"com.sap.cloud.abap","url": " ` + testURL + ` " ,"systemid":"H01","abap": { "username":" ` + username + ` ","password":" ` + password + ` ","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding": { "env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true} ` , abapServiceKey )
2020-05-13 14:51:48 +02:00
} )
}