github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/create_isolation_segment_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes"
     9  	. "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7"
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes"
    11  	"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
    12  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
    13  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("create-isolation-segment Command", func() {
    20  	var (
    21  		cmd              CreateIsolationSegmentCommand
    22  		testUI           *ui.UI
    23  		fakeConfig       *commandfakes.FakeConfig
    24  		fakeSharedActor  *commandfakes.FakeSharedActor
    25  		fakeActor        *v7fakes.FakeActor
    26  		binaryName       string
    27  		executeErr       error
    28  		isolationSegment string
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v7fakes.FakeActor)
    36  
    37  		cmd = CreateIsolationSegmentCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				SharedActor: fakeSharedActor,
    42  				Actor:       fakeActor,
    43  			},
    44  		}
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  		isolationSegment = "segment1"
    49  	})
    50  
    51  	JustBeforeEach(func() {
    52  		executeErr = cmd.Execute(nil)
    53  	})
    54  
    55  	When("checking target fails", func() {
    56  		BeforeEach(func() {
    57  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    58  		})
    59  
    60  		It("returns an error", func() {
    61  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    62  
    63  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    64  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    65  			Expect(checkTargetedOrg).To(BeFalse())
    66  			Expect(checkTargetedSpace).To(BeFalse())
    67  		})
    68  	})
    69  
    70  	When("the user is logged in", func() {
    71  		BeforeEach(func() {
    72  			fakeActor.GetCurrentUserReturns(configv3.User{Name: "banana"}, nil)
    73  
    74  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    75  		})
    76  
    77  		When("the create is successful", func() {
    78  			BeforeEach(func() {
    79  				fakeActor.CreateIsolationSegmentByNameReturns(v7action.Warnings{"I am a warning", "I am also a warning"}, nil)
    80  			})
    81  
    82  			It("displays the header and ok", func() {
    83  				Expect(executeErr).ToNot(HaveOccurred())
    84  
    85  				Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana..."))
    86  				Expect(testUI.Out).To(Say("OK"))
    87  
    88  				Expect(testUI.Err).To(Say("I am a warning"))
    89  				Expect(testUI.Err).To(Say("I am also a warning"))
    90  
    91  				Expect(fakeActor.CreateIsolationSegmentByNameCallCount()).To(Equal(1))
    92  				Expect(fakeActor.CreateIsolationSegmentByNameArgsForCall(0)).To(Equal(resources.IsolationSegment{Name: isolationSegment}))
    93  			})
    94  		})
    95  
    96  		When("the create is unsuccessful", func() {
    97  			Context("due to an unexpected error", func() {
    98  				var expectedErr error
    99  
   100  				BeforeEach(func() {
   101  					expectedErr = errors.New("I am an error")
   102  					fakeActor.CreateIsolationSegmentByNameReturns(v7action.Warnings{"I am a warning", "I am also a warning"}, expectedErr)
   103  				})
   104  
   105  				It("displays the header and error", func() {
   106  					Expect(executeErr).To(MatchError(expectedErr))
   107  
   108  					Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana..."))
   109  
   110  					Expect(testUI.Err).To(Say("I am a warning"))
   111  					Expect(testUI.Err).To(Say("I am also a warning"))
   112  				})
   113  			})
   114  
   115  			Context("due to an IsolationSegmentAlreadyExistsError", func() {
   116  				BeforeEach(func() {
   117  					fakeActor.CreateIsolationSegmentByNameReturns(v7action.Warnings{"I am a warning", "I am also a warning"}, actionerror.IsolationSegmentAlreadyExistsError{})
   118  				})
   119  
   120  				It("displays the header and ok", func() {
   121  					Expect(executeErr).ToNot(HaveOccurred())
   122  
   123  					Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana..."))
   124  					Expect(testUI.Out).To(Say("OK"))
   125  
   126  					Expect(testUI.Err).To(Say("I am a warning"))
   127  					Expect(testUI.Err).To(Say("I am also a warning"))
   128  					Expect(testUI.Err).To(Say("Isolation segment '%s' already exists.", isolationSegment))
   129  				})
   130  			})
   131  		})
   132  	})
   133  })