github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v7/create_isolation_segment_command_test.go (about)

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