1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/cloudFoundryDeleteSpace_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

72 lines
2.1 KiB
Go

//go:build unit
// +build unit
package cmd
import (
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/cloudfoundry"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/stretchr/testify/assert"
)
func TestCloudFoundryDeleteSpace(t *testing.T) {
m := &mock.ExecMockRunner{}
s := mock.ShellMockRunner{}
cf := cloudfoundry.CFUtils{Exec: m}
var telemetryData telemetry.CustomData
config := cloudFoundryDeleteSpaceOptions{
CfAPIEndpoint: "https://api.endpoint.com",
CfOrg: "testOrg",
CfSpace: "testSpace",
Username: "testUser",
Password: "testPassword",
}
t.Run("CF login: Success", func(t *testing.T) {
err := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s)
if assert.NoError(t, err) {
assert.Contains(t, s.Calls[0], "yes '' | cf login -a https://api.endpoint.com -u testUser -p testPassword")
}
})
t.Run("CF login: failure case", func(t *testing.T) {
errorMessage := "cf login failed"
defer func() {
s.Calls = nil
s.ShouldFailOnCommand = nil
}()
s.ShouldFailOnCommand = map[string]error{"yes '' | cf login -a https://api.endpoint.com -u testUser -p testPassword ": fmt.Errorf(errorMessage)}
e := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s)
assert.EqualError(t, e, "Error while logging in occured: "+errorMessage)
})
t.Run("CF Delete Space : success case", func(t *testing.T) {
err := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s)
if assert.NoError(t, err) {
assert.Equal(t, "cf", m.Calls[0].Exec)
assert.Equal(t, []string{"delete-space", "testSpace", "-o", "testOrg", "-f"}, m.Calls[0].Params)
}
})
t.Run("CF Delete space: failure case", func(t *testing.T) {
errorMessage := "cf space creation error"
m.ShouldFailOnCommand = map[string]error{"cf delete-space testSpace -o testOrg -f": fmt.Errorf(errorMessage)}
gotError := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s)
assert.EqualError(t, gotError, "Deletion of cf space has failed: "+errorMessage, "Wrong error message")
})
}