github.com/xgoffin/jenkins-library@v1.154.0/cmd/cloudFoundryDeleteSpace_test.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/SAP/jenkins-library/pkg/cloudfoundry" 8 "github.com/SAP/jenkins-library/pkg/mock" 9 "github.com/SAP/jenkins-library/pkg/telemetry" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestCloudFoundryDeleteSpace(t *testing.T) { 14 m := &mock.ExecMockRunner{} 15 s := mock.ShellMockRunner{} 16 cf := cloudfoundry.CFUtils{Exec: m} 17 var telemetryData telemetry.CustomData 18 19 config := cloudFoundryDeleteSpaceOptions{ 20 CfAPIEndpoint: "https://api.endpoint.com", 21 CfOrg: "testOrg", 22 CfSpace: "testSpace", 23 Username: "testUser", 24 Password: "testPassword", 25 } 26 27 t.Run("CF login: Success", func(t *testing.T) { 28 29 err := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s) 30 if assert.NoError(t, err) { 31 assert.Contains(t, s.Calls[0], "yes '' | cf login -a https://api.endpoint.com -u testUser -p testPassword") 32 } 33 }) 34 35 t.Run("CF login: failure case", func(t *testing.T) { 36 37 errorMessage := "cf login failed" 38 39 defer func() { 40 s.Calls = nil 41 s.ShouldFailOnCommand = nil 42 }() 43 44 s.ShouldFailOnCommand = map[string]error{"yes '' | cf login -a https://api.endpoint.com -u testUser -p testPassword ": fmt.Errorf(errorMessage)} 45 46 e := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s) 47 assert.EqualError(t, e, "Error while logging in occured: "+errorMessage) 48 }) 49 50 t.Run("CF Delete Space : success case", func(t *testing.T) { 51 52 err := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s) 53 if assert.NoError(t, err) { 54 assert.Equal(t, "cf", m.Calls[0].Exec) 55 assert.Equal(t, []string{"delete-space", "testSpace", "-o", "testOrg", "-f"}, m.Calls[0].Params) 56 } 57 }) 58 59 t.Run("CF Delete space: failure case", func(t *testing.T) { 60 61 errorMessage := "cf space creation error" 62 63 m.ShouldFailOnCommand = map[string]error{"cf delete-space testSpace -o testOrg -f": fmt.Errorf(errorMessage)} 64 65 gotError := runCloudFoundryDeleteSpace(&config, &telemetryData, cf, &s) 66 assert.EqualError(t, gotError, "Deletion of cf space has failed: "+errorMessage, "Wrong error message") 67 }) 68 }