github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/reset_space_isolation_segment_command_test.go (about)

     1  package v3_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v3action"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    11  	"code.cloudfoundry.org/cli/command/translatableerror"
    12  	"code.cloudfoundry.org/cli/command/v3"
    13  	"code.cloudfoundry.org/cli/command/v3/v3fakes"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("reset-space-isolation-segment Command", func() {
    22  	var (
    23  		cmd             v3.ResetSpaceIsolationSegmentCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v3fakes.FakeResetSpaceIsolationSegmentActor
    28  		fakeActorV2     *v3fakes.FakeResetSpaceIsolationSegmentActorV2
    29  		binaryName      string
    30  		executeErr      error
    31  		space           string
    32  		org             string
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    37  		fakeConfig = new(commandfakes.FakeConfig)
    38  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    39  		fakeActor = new(v3fakes.FakeResetSpaceIsolationSegmentActor)
    40  		fakeActorV2 = new(v3fakes.FakeResetSpaceIsolationSegmentActorV2)
    41  
    42  		cmd = v3.ResetSpaceIsolationSegmentCommand{
    43  			UI:          testUI,
    44  			Config:      fakeConfig,
    45  			SharedActor: fakeSharedActor,
    46  			Actor:       fakeActor,
    47  			ActorV2:     fakeActorV2,
    48  		}
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  		space = "some-space"
    53  		org = "some-org"
    54  
    55  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
    56  	})
    57  
    58  	JustBeforeEach(func() {
    59  		executeErr = cmd.Execute(nil)
    60  	})
    61  
    62  	Context("when the API version is below the minimum", func() {
    63  		BeforeEach(func() {
    64  			fakeActor.CloudControllerAPIVersionReturns("0.0.0")
    65  		})
    66  
    67  		It("returns a MinimumAPIVersionNotMetError", func() {
    68  			Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{
    69  				CurrentVersion: "0.0.0",
    70  				MinimumVersion: ccversion.MinVersionIsolationSegmentV3,
    71  			}))
    72  		})
    73  	})
    74  
    75  	Context("when checking target fails", func() {
    76  		BeforeEach(func() {
    77  			fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName})
    78  		})
    79  
    80  		It("returns an error", func() {
    81  			Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: binaryName}))
    82  
    83  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    84  			_, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    85  			Expect(checkTargetedOrg).To(BeTrue())
    86  			Expect(checkTargetedSpace).To(BeFalse())
    87  		})
    88  	})
    89  
    90  	Context("when the user is logged in", func() {
    91  		BeforeEach(func() {
    92  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    93  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    94  				Name: org,
    95  				GUID: "some-org-guid",
    96  			})
    97  
    98  			cmd.RequiredArgs.SpaceName = space
    99  		})
   100  
   101  		Context("when the space lookup is unsuccessful", func() {
   102  			BeforeEach(func() {
   103  				fakeActorV2.GetSpaceByOrganizationAndNameReturns(v2action.Space{}, v2action.Warnings{"warning-1", "warning-2"}, v2action.SpaceNotFoundError{Name: space})
   104  			})
   105  
   106  			It("returns the warnings and error", func() {
   107  				Expect(executeErr).To(MatchError(translatableerror.SpaceNotFoundError{Name: space}))
   108  				Expect(testUI.Err).To(Say("warning-1"))
   109  				Expect(testUI.Err).To(Say("warning-2"))
   110  			})
   111  		})
   112  
   113  		Context("when the space lookup is successful", func() {
   114  			BeforeEach(func() {
   115  				fakeActorV2.GetSpaceByOrganizationAndNameReturns(v2action.Space{
   116  					Name: space,
   117  					GUID: "some-space-guid",
   118  				}, v2action.Warnings{"warning-1", "warning-2"}, nil)
   119  			})
   120  
   121  			Context("when the reset changes the isolation segment to platform default", func() {
   122  				BeforeEach(func() {
   123  					fakeActor.ResetSpaceIsolationSegmentReturns("", v3action.Warnings{"warning-3", "warning-4"}, nil)
   124  				})
   125  
   126  				It("Displays the header and okay", func() {
   127  					Expect(executeErr).ToNot(HaveOccurred())
   128  
   129  					Expect(testUI.Out).To(Say("Resetting isolation segment assignment of space %s in org %s as banana...", space, org))
   130  
   131  					Expect(testUI.Out).To(Say("OK\n\n"))
   132  
   133  					Expect(testUI.Err).To(Say("warning-1"))
   134  					Expect(testUI.Err).To(Say("warning-2"))
   135  					Expect(testUI.Err).To(Say("warning-3"))
   136  					Expect(testUI.Err).To(Say("warning-4"))
   137  
   138  					Expect(testUI.Out).To(Say("Applications in this space will be placed in the platform default isolation segment."))
   139  					Expect(testUI.Out).To(Say("Running applications need a restart to be moved there."))
   140  
   141  					Expect(fakeActor.ResetSpaceIsolationSegmentCallCount()).To(Equal(1))
   142  					orgGUID, spaceGUID := fakeActor.ResetSpaceIsolationSegmentArgsForCall(0)
   143  					Expect(orgGUID).To(Equal("some-org-guid"))
   144  					Expect(spaceGUID).To(Equal("some-space-guid"))
   145  				})
   146  			})
   147  
   148  			Context("when the reset changes the isolation segment to the org's default", func() {
   149  				BeforeEach(func() {
   150  					fakeActor.ResetSpaceIsolationSegmentReturns("some-org-iso-seg-name", v3action.Warnings{"warning-3", "warning-4"}, nil)
   151  				})
   152  
   153  				It("Displays the header and okay", func() {
   154  					Expect(executeErr).ToNot(HaveOccurred())
   155  
   156  					Expect(testUI.Out).To(Say("Resetting isolation segment assignment of space %s in org %s as banana...", space, org))
   157  
   158  					Expect(testUI.Out).To(Say("OK\n\n"))
   159  
   160  					Expect(testUI.Err).To(Say("warning-1"))
   161  					Expect(testUI.Err).To(Say("warning-2"))
   162  					Expect(testUI.Err).To(Say("warning-3"))
   163  					Expect(testUI.Err).To(Say("warning-4"))
   164  
   165  					Expect(testUI.Out).To(Say("Applications in this space will be placed in isolation segment some-org-iso-seg-name."))
   166  					Expect(testUI.Out).To(Say("Running applications need a restart to be moved there."))
   167  
   168  					Expect(fakeActor.ResetSpaceIsolationSegmentCallCount()).To(Equal(1))
   169  					orgGUID, spaceGUID := fakeActor.ResetSpaceIsolationSegmentArgsForCall(0)
   170  					Expect(orgGUID).To(Equal("some-org-guid"))
   171  					Expect(spaceGUID).To(Equal("some-space-guid"))
   172  				})
   173  			})
   174  
   175  			Context("when the reset errors", func() {
   176  				var expectedErr error
   177  				BeforeEach(func() {
   178  					expectedErr = errors.New("some error")
   179  					fakeActor.ResetSpaceIsolationSegmentReturns("some-org-iso-seg", v3action.Warnings{"warning-3", "warning-4"}, expectedErr)
   180  				})
   181  
   182  				It("returns the warnings and error", func() {
   183  					Expect(executeErr).To(MatchError(expectedErr))
   184  
   185  					Expect(testUI.Out).To(Say("Resetting isolation segment assignment of space %s in org %s as banana...", space, org))
   186  					Expect(testUI.Err).To(Say("warning-1"))
   187  					Expect(testUI.Err).To(Say("warning-2"))
   188  					Expect(testUI.Err).To(Say("warning-3"))
   189  					Expect(testUI.Err).To(Say("warning-4"))
   190  				})
   191  			})
   192  		})
   193  	})
   194  })