github.com/loafoe/cli@v7.1.0+incompatible/command/v7/set_space_isolation_segment_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/v7action"
     6  	"code.cloudfoundry.org/cli/command/commandfakes"
     7  	. "code.cloudfoundry.org/cli/command/v7"
     8  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
     9  	"code.cloudfoundry.org/cli/resources"
    10  	"code.cloudfoundry.org/cli/util/configv3"
    11  	"code.cloudfoundry.org/cli/util/ui"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/gbytes"
    15  )
    16  
    17  var _ = Describe("set-space-isolation-segment Command", func() {
    18  	var (
    19  		cmd              SetSpaceIsolationSegmentCommand
    20  		testUI           *ui.UI
    21  		fakeConfig       *commandfakes.FakeConfig
    22  		fakeSharedActor  *commandfakes.FakeSharedActor
    23  		fakeActor        *v7fakes.FakeActor
    24  		binaryName       string
    25  		executeErr       error
    26  		isolationSegment string
    27  		space            string
    28  		org              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 = SetSpaceIsolationSegmentCommand{
    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  		space = "some-space"
    49  		org = "some-org"
    50  		isolationSegment = "segment1"
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(nil)
    55  	})
    56  
    57  	When("checking target fails", func() {
    58  		BeforeEach(func() {
    59  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    60  		})
    61  
    62  		It("returns an error", func() {
    63  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    64  
    65  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    66  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    67  			Expect(checkTargetedOrg).To(BeTrue())
    68  			Expect(checkTargetedSpace).To(BeFalse())
    69  		})
    70  	})
    71  
    72  	When("the user is logged in", func() {
    73  		BeforeEach(func() {
    74  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    75  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    76  				Name: org,
    77  				GUID: "some-org-guid",
    78  			})
    79  
    80  			cmd.RequiredArgs.SpaceName = space
    81  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    82  		})
    83  
    84  		When("the space lookup is unsuccessful", func() {
    85  			BeforeEach(func() {
    86  				fakeActor.GetSpaceByNameAndOrganizationReturns(resources.Space{}, v7action.Warnings{"I am a warning", "I am also a warning"}, actionerror.SpaceNotFoundError{Name: space})
    87  			})
    88  
    89  			It("returns the warnings and error", func() {
    90  				Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: space}))
    91  				Expect(testUI.Err).To(Say("I am a warning"))
    92  				Expect(testUI.Err).To(Say("I am also a warning"))
    93  			})
    94  		})
    95  
    96  		When("the space lookup is successful", func() {
    97  			BeforeEach(func() {
    98  				fakeActor.GetSpaceByNameAndOrganizationReturns(resources.Space{
    99  					Name: space,
   100  					GUID: "some-space-guid",
   101  				}, v7action.Warnings{"I am a warning", "I am also a warning"}, nil)
   102  			})
   103  
   104  			When("the entitlement is successful", func() {
   105  				BeforeEach(func() {
   106  					fakeActor.AssignIsolationSegmentToSpaceByNameAndSpaceReturns(v7action.Warnings{"entitlement-warning", "banana"}, nil)
   107  				})
   108  
   109  				It("Displays the header and okay", func() {
   110  					Expect(executeErr).ToNot(HaveOccurred())
   111  
   112  					Expect(testUI.Out).To(Say("Updating isolation segment of space %s in org %s as banana...", space, org))
   113  					Expect(testUI.Out).To(Say("OK"))
   114  
   115  					Expect(testUI.Err).To(Say("I am a warning"))
   116  					Expect(testUI.Err).To(Say("I am also a warning"))
   117  					Expect(testUI.Err).To(Say("entitlement-warning"))
   118  					Expect(testUI.Err).To(Say("banana"))
   119  
   120  					Expect(testUI.Out).To(Say("TIP: Restart applications in this space to relocate them to this isolation segment."))
   121  
   122  					Expect(fakeActor.AssignIsolationSegmentToSpaceByNameAndSpaceCallCount()).To(Equal(1))
   123  					isolationSegmentName, spaceGUID := fakeActor.AssignIsolationSegmentToSpaceByNameAndSpaceArgsForCall(0)
   124  					Expect(isolationSegmentName).To(Equal(isolationSegment))
   125  					Expect(spaceGUID).To(Equal("some-space-guid"))
   126  				})
   127  			})
   128  
   129  			When("the entitlement errors", func() {
   130  				BeforeEach(func() {
   131  					fakeActor.AssignIsolationSegmentToSpaceByNameAndSpaceReturns(v7action.Warnings{"entitlement-warning", "banana"}, actionerror.IsolationSegmentNotFoundError{Name: "segment1"})
   132  				})
   133  
   134  				It("returns the warnings and error", func() {
   135  					Expect(testUI.Out).To(Say("Updating isolation segment of space %s in org %s as banana...", space, org))
   136  					Expect(testUI.Err).To(Say("I am a warning"))
   137  					Expect(testUI.Err).To(Say("I am also a warning"))
   138  					Expect(testUI.Err).To(Say("entitlement-warning"))
   139  					Expect(testUI.Err).To(Say("banana"))
   140  					Expect(executeErr).To(MatchError(actionerror.IsolationSegmentNotFoundError{Name: "segment1"}))
   141  				})
   142  			})
   143  		})
   144  	})
   145  })