github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/set_space_isolation_segment_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/v2action"
     6  	"code.cloudfoundry.org/cli/actor/v3action"
     7  	"code.cloudfoundry.org/cli/command/commandfakes"
     8  	. "code.cloudfoundry.org/cli/command/v6"
     9  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    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        *v6fakes.FakeSetSpaceIsolationSegmentActor
    24  		fakeActorV2      *v6fakes.FakeSetSpaceIsolationSegmentActorV2
    25  		binaryName       string
    26  		executeErr       error
    27  		isolationSegment string
    28  		space            string
    29  		org              string
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v6fakes.FakeSetSpaceIsolationSegmentActor)
    37  		fakeActorV2 = new(v6fakes.FakeSetSpaceIsolationSegmentActorV2)
    38  
    39  		cmd = SetSpaceIsolationSegmentCommand{
    40  			UI:          testUI,
    41  			Config:      fakeConfig,
    42  			SharedActor: fakeSharedActor,
    43  			Actor:       fakeActor,
    44  			ActorV2:     fakeActorV2,
    45  		}
    46  
    47  		binaryName = "faceman"
    48  		fakeConfig.BinaryNameReturns(binaryName)
    49  		space = "some-space"
    50  		org = "some-org"
    51  		isolationSegment = "segment1"
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(nil)
    56  	})
    57  
    58  	When("checking target fails", func() {
    59  		BeforeEach(func() {
    60  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    61  		})
    62  
    63  		It("returns an error", func() {
    64  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    65  
    66  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    67  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    68  			Expect(checkTargetedOrg).To(BeTrue())
    69  			Expect(checkTargetedSpace).To(BeFalse())
    70  		})
    71  	})
    72  
    73  	When("the user is logged in", func() {
    74  		BeforeEach(func() {
    75  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    76  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    77  				Name: org,
    78  				GUID: "some-org-guid",
    79  			})
    80  
    81  			cmd.RequiredArgs.SpaceName = space
    82  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    83  		})
    84  
    85  		When("the space lookup is unsuccessful", func() {
    86  			BeforeEach(func() {
    87  				fakeActorV2.GetSpaceByOrganizationAndNameReturns(v2action.Space{}, v2action.Warnings{"I am a warning", "I am also a warning"}, actionerror.SpaceNotFoundError{Name: space})
    88  			})
    89  
    90  			It("returns the warnings and error", func() {
    91  				Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: space}))
    92  				Expect(testUI.Err).To(Say("I am a warning"))
    93  				Expect(testUI.Err).To(Say("I am also a warning"))
    94  			})
    95  		})
    96  
    97  		When("the space lookup is successful", func() {
    98  			BeforeEach(func() {
    99  				fakeActorV2.GetSpaceByOrganizationAndNameReturns(v2action.Space{
   100  					Name: space,
   101  					GUID: "some-space-guid",
   102  				}, v2action.Warnings{"I am a warning", "I am also a warning"}, nil)
   103  			})
   104  
   105  			When("the entitlement is successful", func() {
   106  				BeforeEach(func() {
   107  					fakeActor.AssignIsolationSegmentToSpaceByNameAndSpaceReturns(v3action.Warnings{"entitlement-warning", "banana"}, nil)
   108  				})
   109  
   110  				It("Displays the header and okay", func() {
   111  					Expect(executeErr).ToNot(HaveOccurred())
   112  
   113  					Expect(testUI.Out).To(Say("Updating isolation segment of space %s in org %s as banana...", space, org))
   114  					Expect(testUI.Out).To(Say("OK"))
   115  
   116  					Expect(testUI.Err).To(Say("I am a warning"))
   117  					Expect(testUI.Err).To(Say("I am also a warning"))
   118  					Expect(testUI.Err).To(Say("entitlement-warning"))
   119  					Expect(testUI.Err).To(Say("banana"))
   120  
   121  					Expect(testUI.Out).To(Say("In order to move running applications to this isolation segment, they must be restarted."))
   122  
   123  					Expect(fakeActor.AssignIsolationSegmentToSpaceByNameAndSpaceCallCount()).To(Equal(1))
   124  					isolationSegmentName, spaceGUID := fakeActor.AssignIsolationSegmentToSpaceByNameAndSpaceArgsForCall(0)
   125  					Expect(isolationSegmentName).To(Equal(isolationSegment))
   126  					Expect(spaceGUID).To(Equal("some-space-guid"))
   127  				})
   128  			})
   129  
   130  			When("the entitlement errors", func() {
   131  				BeforeEach(func() {
   132  					fakeActor.AssignIsolationSegmentToSpaceByNameAndSpaceReturns(v3action.Warnings{"entitlement-warning", "banana"}, actionerror.IsolationSegmentNotFoundError{Name: "segment1"})
   133  				})
   134  
   135  				It("returns the warnings and error", func() {
   136  					Expect(testUI.Out).To(Say("Updating isolation segment of space %s in org %s as banana...", space, org))
   137  					Expect(testUI.Err).To(Say("I am a warning"))
   138  					Expect(testUI.Err).To(Say("I am also a warning"))
   139  					Expect(testUI.Err).To(Say("entitlement-warning"))
   140  					Expect(testUI.Err).To(Say("banana"))
   141  					Expect(executeErr).To(MatchError(actionerror.IsolationSegmentNotFoundError{Name: "segment1"}))
   142  				})
   143  			})
   144  		})
   145  	})
   146  })