code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/reset_space_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  	v7 "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/resources"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("reset-space-isolation-segment Command", func() {
    20  	var (
    21  		cmd             v7.ResetSpaceIsolationSegmentCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeActor
    26  		binaryName      string
    27  		executeErr      error
    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(v7fakes.FakeActor)
    37  
    38  		cmd = v7.ResetSpaceIsolationSegmentCommand{
    39  			BaseCommand: v7.BaseCommand{
    40  				UI:          testUI,
    41  				Config:      fakeConfig,
    42  				SharedActor: fakeSharedActor,
    43  				Actor:       fakeActor,
    44  			},
    45  		}
    46  
    47  		binaryName = "faceman"
    48  		fakeConfig.BinaryNameReturns(binaryName)
    49  		space = "some-space"
    50  		org = "some-org"
    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  		})
    82  
    83  		When("the space lookup is unsuccessful", func() {
    84  			BeforeEach(func() {
    85  				fakeActor.GetSpaceByNameAndOrganizationReturns(resources.Space{}, v7action.Warnings{"warning-1", "warning-2"}, actionerror.SpaceNotFoundError{Name: space})
    86  			})
    87  
    88  			It("returns the warnings and error", func() {
    89  				Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: space}))
    90  				Expect(testUI.Err).To(Say("warning-1"))
    91  				Expect(testUI.Err).To(Say("warning-2"))
    92  			})
    93  		})
    94  
    95  		When("the space lookup is successful", func() {
    96  			BeforeEach(func() {
    97  				fakeActor.GetSpaceByNameAndOrganizationReturns(resources.Space{
    98  					Name: space,
    99  					GUID: "some-space-guid",
   100  				}, v7action.Warnings{"warning-1", "warning-2"}, nil)
   101  			})
   102  
   103  			When("the reset changes the isolation segment to platform default", func() {
   104  				BeforeEach(func() {
   105  					fakeActor.ResetSpaceIsolationSegmentReturns("", v7action.Warnings{"warning-3", "warning-4"}, nil)
   106  				})
   107  
   108  				It("Displays the header and okay", func() {
   109  					Expect(executeErr).ToNot(HaveOccurred())
   110  
   111  					Expect(testUI.Out).To(Say("Resetting isolation segment assignment of space %s in org %s as banana...", space, org))
   112  
   113  					Expect(testUI.Out).To(Say("OK\n\n"))
   114  
   115  					Expect(testUI.Err).To(Say("warning-1"))
   116  					Expect(testUI.Err).To(Say("warning-2"))
   117  					Expect(testUI.Err).To(Say("warning-3"))
   118  					Expect(testUI.Err).To(Say("warning-4"))
   119  
   120  					Expect(testUI.Out).To(Say("TIP: Restart applications in this space to relocate them to the platform default."))
   121  
   122  					Expect(fakeActor.ResetSpaceIsolationSegmentCallCount()).To(Equal(1))
   123  					orgGUID, spaceGUID := fakeActor.ResetSpaceIsolationSegmentArgsForCall(0)
   124  					Expect(orgGUID).To(Equal("some-org-guid"))
   125  					Expect(spaceGUID).To(Equal("some-space-guid"))
   126  				})
   127  			})
   128  
   129  			When("the reset changes the isolation segment to the org's default", func() {
   130  				BeforeEach(func() {
   131  					fakeActor.ResetSpaceIsolationSegmentReturns("some-org-iso-seg-name", v7action.Warnings{"warning-3", "warning-4"}, nil)
   132  				})
   133  
   134  				It("Displays the header and okay", func() {
   135  					Expect(executeErr).ToNot(HaveOccurred())
   136  
   137  					Expect(testUI.Out).To(Say("Resetting isolation segment assignment of space %s in org %s as banana...", space, org))
   138  
   139  					Expect(testUI.Out).To(Say("OK\n\n"))
   140  
   141  					Expect(testUI.Err).To(Say("warning-1"))
   142  					Expect(testUI.Err).To(Say("warning-2"))
   143  					Expect(testUI.Err).To(Say("warning-3"))
   144  					Expect(testUI.Err).To(Say("warning-4"))
   145  
   146  					Expect(testUI.Out).To(Say("TIP: Restart applications in this space to relocate them to this organization's default isolation segment."))
   147  
   148  					Expect(fakeActor.ResetSpaceIsolationSegmentCallCount()).To(Equal(1))
   149  					orgGUID, spaceGUID := fakeActor.ResetSpaceIsolationSegmentArgsForCall(0)
   150  					Expect(orgGUID).To(Equal("some-org-guid"))
   151  					Expect(spaceGUID).To(Equal("some-space-guid"))
   152  				})
   153  			})
   154  
   155  			When("the reset errors", func() {
   156  				var expectedErr error
   157  				BeforeEach(func() {
   158  					expectedErr = errors.New("some error")
   159  					fakeActor.ResetSpaceIsolationSegmentReturns("some-org-iso-seg", v7action.Warnings{"warning-3", "warning-4"}, expectedErr)
   160  				})
   161  
   162  				It("returns the warnings and error", func() {
   163  					Expect(executeErr).To(MatchError(expectedErr))
   164  
   165  					Expect(testUI.Out).To(Say("Resetting isolation segment assignment of space %s in org %s as banana...", space, org))
   166  					Expect(testUI.Err).To(Say("warning-1"))
   167  					Expect(testUI.Err).To(Say("warning-2"))
   168  					Expect(testUI.Err).To(Say("warning-3"))
   169  					Expect(testUI.Err).To(Say("warning-4"))
   170  				})
   171  			})
   172  		})
   173  	})
   174  })