1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

Improve tests for cf steps (#1875)

* Improve Tests for cf steps:
CreateServiceKey and Delete Service

* Fix CodeClimate issues

* Adapt descriptions

* Defer logout
This commit is contained in:
Daniel Mieg 2020-08-10 11:08:34 +02:00 committed by GitHub
parent b80581f98c
commit 7ea5b09555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 222 additions and 171 deletions

View File

@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"github.com/SAP/jenkins-library/pkg/cloudfoundry"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
@ -15,58 +16,50 @@ func cloudFoundryCreateServiceKey(options cloudFoundryCreateServiceKeyOptions, t
c.Stdout(log.Writer())
c.Stderr(log.Writer())
config := cloudFoundryDeleteServiceOptions{
CfAPIEndpoint: options.CfAPIEndpoint,
CfOrg: options.CfOrg,
CfSpace: options.CfSpace,
Username: options.Username,
Password: options.Password,
cfUtils := cloudfoundry.CFUtils{
Exec: &c,
}
var err error
err = cloudFoundryLogin(config, &c)
if err == nil {
err = runCloudFoundryCreateServiceKey(&options, telemetryData, &c)
}
var logoutErr error
if err == nil {
logoutErr = cloudFoundryLogout(&c)
if logoutErr != nil {
log.Entry().
WithError(logoutErr).
Fatal("Error while logging out occured.")
}
} else if err != nil {
logoutErr = cloudFoundryLogout(&c)
if logoutErr != nil {
log.Entry().
WithError(logoutErr).
Fatal("Error while logging out occured.")
}
err := runCloudFoundryCreateServiceKey(&options, telemetryData, &c, &cfUtils)
if err != nil {
log.Entry().
WithError(err).
Fatal("Error occured during step.")
}
}
func runCloudFoundryCreateServiceKey(config *cloudFoundryCreateServiceKeyOptions, telemetryData *telemetry.CustomData, c command.ExecRunner) error {
func runCloudFoundryCreateServiceKey(options *cloudFoundryCreateServiceKeyOptions, telemetryData *telemetry.CustomData, c command.ExecRunner, cfUtils cloudfoundry.AuthenticationUtils) (returnedError error) {
// Login via cf cli
config := cloudfoundry.LoginOptions{
CfAPIEndpoint: options.CfAPIEndpoint,
CfOrg: options.CfOrg,
CfSpace: options.CfSpace,
Username: options.Username,
Password: options.Password,
}
loginErr := cfUtils.Login(config)
if loginErr != nil {
return fmt.Errorf("Error while logging in occured: %w", loginErr)
}
defer func() {
logoutErr := cfUtils.Logout()
if logoutErr != nil && returnedError == nil {
returnedError = fmt.Errorf("Error while logging out occured: %w", logoutErr)
}
}()
log.Entry().Info("Creating Service Key")
var cfCreateServiceKeyScript []string
if config.CfServiceKeyConfig == "" {
cfCreateServiceKeyScript = []string{"create-service-key", config.CfServiceInstance, config.CfServiceKeyName}
if options.CfServiceKeyConfig == "" {
cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName}
} else {
cfCreateServiceKeyScript = []string{"create-service-key", config.CfServiceInstance, config.CfServiceKeyName, "-c", config.CfServiceKeyConfig}
cfCreateServiceKeyScript = []string{"create-service-key", options.CfServiceInstance, options.CfServiceKeyName, "-c", options.CfServiceKeyConfig}
}
err := c.RunExecutable("cf", cfCreateServiceKeyScript...)
if err != nil {
return fmt.Errorf("Failed to Create Service Key: %w", err)
}
return nil
return returnedError
}

View File

@ -3,28 +3,15 @@ package cmd
import (
"testing"
"github.com/SAP/jenkins-library/pkg/cloudfoundry"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestCloudFoundryCreateServiceKey(t *testing.T) {
execRunner := mock.ExecMockRunner{}
var telemetryData telemetry.CustomData
t.Run("CF Login: success case", func(t *testing.T) {
loginconfig := cloudFoundryDeleteServiceOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
}
error := cloudFoundryLogin(loginconfig, &execRunner)
if error == nil {
assert.Equal(t, "cf", execRunner.Calls[0].Exec)
assert.Equal(t, []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}, execRunner.Calls[0].Params)
}
})
t.Run("CF Create Service Key: Success case", func(t *testing.T) {
config := cloudFoundryCreateServiceKeyOptions{
CfAPIEndpoint: "https://api.endpoint.com",
@ -35,10 +22,14 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) {
CfServiceInstance: "testInstance",
CfServiceKeyName: "testKey",
}
error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner)
execRunner := mock.ExecMockRunner{}
cfUtilsMock := cloudfoundry.CfUtilsMock{}
defer cfUtilsMock.Cleanup()
error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock)
if error == nil {
assert.Equal(t, "cf", execRunner.Calls[1].Exec)
assert.Equal(t, []string{"create-service-key", "testInstance", "testKey"}, execRunner.Calls[1].Params)
assert.Equal(t, "cf", execRunner.Calls[0].Exec)
assert.Equal(t, []string{"create-service-key", "testInstance", "testKey"}, execRunner.Calls[0].Params)
}
})
t.Run("CF Create Service Key with service Key config: Success case", func(t *testing.T) {
@ -52,10 +43,14 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) {
CfServiceKeyName: "testKey",
CfServiceKeyConfig: "testconfig.yml",
}
error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner)
execRunner := mock.ExecMockRunner{}
cfUtilsMock := cloudfoundry.CfUtilsMock{}
defer cfUtilsMock.Cleanup()
error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock)
if error == nil {
assert.Equal(t, "cf", execRunner.Calls[2].Exec)
assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", "-c", "testconfig.yml"}, execRunner.Calls[2].Params)
assert.Equal(t, "cf", execRunner.Calls[0].Exec)
assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", "-c", "testconfig.yml"}, execRunner.Calls[0].Params)
}
})
t.Run("CF Create Service Key with service Key config: Success case", func(t *testing.T) {
@ -69,17 +64,86 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) {
CfServiceKeyName: "testKey",
CfServiceKeyConfig: "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}",
}
error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner)
execRunner := mock.ExecMockRunner{}
cfUtilsMock := cloudfoundry.CfUtilsMock{}
defer cfUtilsMock.Cleanup()
error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock)
if error == nil {
assert.Equal(t, "cf", execRunner.Calls[3].Exec)
assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", "-c", "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}"}, execRunner.Calls[3].Params)
}
})
t.Run("CF Logout: Success case", func(t *testing.T) {
error := cloudFoundryLogout(&execRunner)
if error == nil {
assert.Equal(t, "cf", execRunner.Calls[4].Exec)
assert.Equal(t, "logout", execRunner.Calls[4].Params[0])
assert.Equal(t, "cf", execRunner.Calls[0].Exec)
assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", "-c", "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}"}, execRunner.Calls[0].Params)
}
})
}
func TestCloudFoundryCreateServiceKeyErrorMessages(t *testing.T) {
errorMessage := "errorMessage"
var telemetryData telemetry.CustomData
t.Run("CF Login Error", func(t *testing.T) {
config := cloudFoundryCreateServiceKeyOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfServiceInstance: "testInstance",
CfServiceKeyName: "testKey",
CfServiceKeyConfig: "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}",
}
execRunner := mock.ExecMockRunner{}
cfUtilsMock := cloudfoundry.CfUtilsMock{
LoginError: errors.New(errorMessage),
}
defer cfUtilsMock.Cleanup()
error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock)
assert.Equal(t, error.Error(), "Error while logging in occured: "+errorMessage, "Wrong error message")
})
t.Run("CF Logout Error", func(t *testing.T) {
config := cloudFoundryCreateServiceKeyOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfServiceInstance: "testInstance",
CfServiceKeyName: "testKey",
CfServiceKeyConfig: "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}",
}
execRunner := mock.ExecMockRunner{}
cfUtilsMock := cloudfoundry.CfUtilsMock{
LogoutError: errors.New(errorMessage),
}
defer cfUtilsMock.Cleanup()
err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock)
assert.Equal(t, err.Error(), "Error while logging out occured: "+errorMessage, "Wrong error message")
})
t.Run("CF Create Service Key Error", func(t *testing.T) {
errorMessage := "errorMessage"
config := cloudFoundryCreateServiceKeyOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfServiceInstance: "testInstance",
CfServiceKeyName: "testKey",
CfServiceKeyConfig: "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}",
}
m := make(map[string]error)
m["cf create-service-key testInstance testKey -c {\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}"] = errors.New(errorMessage)
execRunner := mock.ExecMockRunner{
ShouldFailOnCommand: m,
}
cfUtilsMock := cloudfoundry.CfUtilsMock{
LogoutError: errors.New(errorMessage),
}
defer cfUtilsMock.Cleanup()
error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock)
assert.Equal(t, error.Error(), "Failed to Create Service Key: "+errorMessage, "Wrong error message")
})
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/SAP/jenkins-library/pkg/cloudfoundry"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
@ -18,40 +19,53 @@ func cloudFoundryDeleteService(options cloudFoundryDeleteServiceOptions, telemet
c.Stdout(log.Writer())
c.Stderr(log.Writer())
var err error
err = cloudFoundryLogin(options, &c)
if err == nil && options.CfDeleteServiceKeys == true {
err = cloudFoundryDeleteServiceKeys(options, &c)
cfUtils := cloudfoundry.CFUtils{
Exec: &c,
}
if err == nil {
err = cloudFoundryDeleteServiceFunction(options.CfServiceInstance, &c)
}
var logoutErr error
if err == nil {
logoutErr = cloudFoundryLogout(&c)
if logoutErr != nil {
log.Entry().
WithError(logoutErr).
Fatal("Error while logging out occured.")
}
} else if err != nil {
logoutErr = cloudFoundryLogout(&c)
if logoutErr != nil {
log.Entry().
WithError(logoutErr).
Fatal("Error while logging out occured.")
}
err := runCloudFoundryDeleteService(options, &c, &cfUtils)
if err != nil {
log.Entry().
WithError(err).
Fatal("Error occured.")
Fatal("Error occured during step.")
}
}
func runCloudFoundryDeleteService(options cloudFoundryDeleteServiceOptions, c command.ExecRunner, cfUtils cloudfoundry.AuthenticationUtils) (returnedError error) {
config := cloudfoundry.LoginOptions{
CfAPIEndpoint: options.CfAPIEndpoint,
CfOrg: options.CfOrg,
CfSpace: options.CfSpace,
Username: options.Username,
Password: options.Password,
}
loginErr := cfUtils.Login(config)
if loginErr != nil {
return fmt.Errorf("Error while logging in occured: %w", loginErr)
}
defer func() {
logoutErr := cfUtils.Logout()
if logoutErr != nil && returnedError == nil {
returnedError = fmt.Errorf("Error while logging out occured: %w", logoutErr)
}
}()
if options.CfDeleteServiceKeys == true {
err := cloudFoundryDeleteServiceKeys(options, c)
if err != nil {
return err
}
}
err := cloudFoundryDeleteServiceFunction(options.CfServiceInstance, c)
if err != nil {
return err
}
return returnedError
}
func cloudFoundryDeleteServiceKeys(options cloudFoundryDeleteServiceOptions, c command.ExecRunner) error {
log.Entry().Info("Deleting inherent Service Keys")
@ -92,20 +106,6 @@ func cloudFoundryDeleteServiceKeys(options cloudFoundryDeleteServiceOptions, c c
return nil
}
func cloudFoundryLogin(options cloudFoundryDeleteServiceOptions, c command.ExecRunner) error {
var cfLoginScript = []string{"login", "-a", options.CfAPIEndpoint, "-o", options.CfOrg, "-s", options.CfSpace, "-u", options.Username, "-p", options.Password}
log.Entry().WithField("cfAPI:", options.CfAPIEndpoint).WithField("cfOrg", options.CfOrg).WithField("space", options.CfSpace).Info("Logging into Cloud Foundry..")
err := c.RunExecutable("cf", cfLoginScript...)
if err != nil {
return fmt.Errorf("Failed to login to Cloud Foundry: %w", err)
}
log.Entry().Info("Logged in successfully to Cloud Foundry..")
return nil
}
func cloudFoundryDeleteServiceFunction(service string, c command.ExecRunner) error {
var cfdeleteServiceScript = []string{"delete-service", service, "-f"}
@ -119,16 +119,3 @@ func cloudFoundryDeleteServiceFunction(service string, c command.ExecRunner) err
log.Entry().Info("Deletion of Service is finished or the Service has never existed")
return nil
}
func cloudFoundryLogout(c command.ExecRunner) error {
var cfLogoutScript = "logout"
log.Entry().Info("Logging out of Cloud Foundry")
err := c.RunExecutable("cf", cfLogoutScript)
if err != nil {
return fmt.Errorf("Failed to Logout of Cloud Foundry: %w", err)
}
log.Entry().Info("Logged out successfully")
return nil
}

View File

@ -1,68 +1,47 @@
package cmd
import (
"testing"
"github.com/SAP/jenkins-library/pkg/cloudfoundry"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
"testing"
)
func TestCloudFoundryDeleteService(t *testing.T) {
execRunner := mock.ExecMockRunner{}
t.Run("CF Login: success case", func(t *testing.T) {
t.Run("CF Delete Service : success case", func(t *testing.T) {
config := cloudFoundryDeleteServiceOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfServiceInstance: "testInstance",
CfDeleteServiceKeys: true,
}
error := cloudFoundryLogin(config, &execRunner)
if error == nil {
m := make(map[string]string)
m["cf service-keys testInstance"] = `line1
line2
line3
myServiceKey1
myServiceKey2
`
execRunner := mock.ExecMockRunner{
StdoutReturn: m,
}
cfUtils := cloudfoundry.CfUtilsMock{}
err := runCloudFoundryDeleteService(config, &execRunner, &cfUtils)
if assert.NoError(t, err) {
assert.Equal(t, "cf", execRunner.Calls[0].Exec)
assert.Equal(t, "login", execRunner.Calls[0].Params[0])
assert.Equal(t, "-a", execRunner.Calls[0].Params[1])
assert.Equal(t, "https://api.endpoint.com", execRunner.Calls[0].Params[2])
assert.Equal(t, "-o", execRunner.Calls[0].Params[3])
assert.Equal(t, "testOrg", execRunner.Calls[0].Params[4])
assert.Equal(t, "-s", execRunner.Calls[0].Params[5])
assert.Equal(t, "testSpace", execRunner.Calls[0].Params[6])
assert.Equal(t, "-u", execRunner.Calls[0].Params[7])
assert.Equal(t, "testUser", execRunner.Calls[0].Params[8])
assert.Equal(t, "-p", execRunner.Calls[0].Params[9])
assert.Equal(t, "testPassword", execRunner.Calls[0].Params[10])
}
})
t.Run("CF Delete Service: Success case", func(t *testing.T) {
ServiceName := "testInstance"
error := cloudFoundryDeleteServiceFunction(ServiceName, &execRunner)
if error == nil {
assert.Equal(t, "cf", execRunner.Calls[1].Exec)
assert.Equal(t, "delete-service", execRunner.Calls[1].Params[0])
assert.Equal(t, "testInstance", execRunner.Calls[1].Params[1])
assert.Equal(t, "-f", execRunner.Calls[1].Params[2])
}
})
t.Run("CF Logout: Success case", func(t *testing.T) {
error := cloudFoundryLogout(&execRunner)
if error == nil {
assert.Equal(t, "cf", execRunner.Calls[2].Exec)
assert.Equal(t, "logout", execRunner.Calls[2].Params[0])
}
})
t.Run("CF Delete Service Keys: success case", func(t *testing.T) {
config := cloudFoundryDeleteServiceOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
CfServiceInstance: "testInstance",
}
error := cloudFoundryDeleteServiceKeys(config, &execRunner)
if error == nil {
assert.Equal(t, "cf", execRunner.Calls[3].Exec)
assert.Equal(t, []string{"service-keys", "testInstance"}, execRunner.Calls[3].Params)
assert.Equal(t, []string{"service-keys", "testInstance"}, execRunner.Calls[0].Params)
assert.Equal(t, []string{"delete-service-key", "testInstance", "myServiceKey1", "-f"}, execRunner.Calls[1].Params)
assert.Equal(t, []string{"delete-service-key", "testInstance", "myServiceKey2", "-f"}, execRunner.Calls[2].Params)
assert.Equal(t, []string{"delete-service", "testInstance", "-f"}, execRunner.Calls[3].Params)
}
})
}

View File

@ -105,3 +105,31 @@ type CFUtils struct {
Exec command.ExecRunner
loggedIn bool
}
// AuthenticationUtils - interface for cloud foundry login and logout
type AuthenticationUtils interface {
Login(options LoginOptions) error
Logout() error
}
// CfUtilsMock - mock for CfUtils
type CfUtilsMock struct {
LoginError error
LogoutError error
}
// Login mock implementation
func (cf *CfUtilsMock) Login(options LoginOptions) error {
return cf.LoginError
}
// Logout mock implementation
func (cf *CfUtilsMock) Logout() error {
return cf.LogoutError
}
// Cleanup for CfUtilsMock
func (cf *CfUtilsMock) Cleanup() {
cf.LoginError = nil
cf.LogoutError = nil
}