github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/cloudFoundryCreateSpace_test.go (about)

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