github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/set_org_default_isolation_segment_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     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/v6"
    13  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    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("set-org-default-isolation-segment Command", func() {
    22  	var (
    23  		cmd              SetOrgDefaultIsolationSegmentCommand
    24  		testUI           *ui.UI
    25  		fakeConfig       *commandfakes.FakeConfig
    26  		fakeSharedActor  *commandfakes.FakeSharedActor
    27  		fakeActor        *v6fakes.FakeSetOrgDefaultIsolationSegmentActor
    28  		fakeActorV2      *v6fakes.FakeSetOrgDefaultIsolationSegmentActorV2
    29  		binaryName       string
    30  		executeErr       error
    31  		isolationSegment 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(v6fakes.FakeSetOrgDefaultIsolationSegmentActor)
    40  		fakeActorV2 = new(v6fakes.FakeSetOrgDefaultIsolationSegmentActorV2)
    41  
    42  		cmd = SetOrgDefaultIsolationSegmentCommand{
    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  		org = "some-org"
    53  		isolationSegment = "segment1"
    54  	})
    55  
    56  	JustBeforeEach(func() {
    57  		executeErr = cmd.Execute(nil)
    58  	})
    59  
    60  	When("the API version is below the minimum", func() {
    61  		BeforeEach(func() {
    62  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinV3ClientVersion)
    63  		})
    64  
    65  		It("returns a MinimumAPIVersionNotMetError", func() {
    66  			Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{
    67  				CurrentVersion: ccversion.MinV3ClientVersion,
    68  				MinimumVersion: ccversion.MinVersionIsolationSegmentV3,
    69  			}))
    70  		})
    71  	})
    72  
    73  	When("checking target fails", func() {
    74  		BeforeEach(func() {
    75  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
    76  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    77  		})
    78  
    79  		It("returns an error", func() {
    80  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    81  
    82  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    83  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    84  			Expect(checkTargetedOrg).To(BeFalse())
    85  			Expect(checkTargetedSpace).To(BeFalse())
    86  		})
    87  	})
    88  
    89  	When("fetching the user fails", func() {
    90  		BeforeEach(func() {
    91  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
    92  			fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error"))
    93  		})
    94  
    95  		It("returns an error", func() {
    96  			Expect(executeErr).To(MatchError("some-error"))
    97  		})
    98  	})
    99  
   100  	When("the user is logged in", func() {
   101  		BeforeEach(func() {
   102  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
   103  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
   104  
   105  			cmd.RequiredArgs.OrganizationName = org
   106  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
   107  		})
   108  
   109  		When("the org lookup is unsuccessful", func() {
   110  			BeforeEach(func() {
   111  				fakeActorV2.GetOrganizationByNameReturns(v2action.Organization{}, v2action.Warnings{"I am a warning", "I am also a warning"}, actionerror.OrganizationNotFoundError{Name: org})
   112  			})
   113  
   114  			It("returns the warnings and error", func() {
   115  				Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: org}))
   116  				Expect(testUI.Err).To(Say("I am a warning"))
   117  				Expect(testUI.Err).To(Say("I am also a warning"))
   118  			})
   119  		})
   120  
   121  		When("the org lookup is successful", func() {
   122  			BeforeEach(func() {
   123  				fakeActorV2.GetOrganizationByNameReturns(v2action.Organization{
   124  					Name: org,
   125  					GUID: "some-org-guid",
   126  				}, v2action.Warnings{"org-warning-1", "org-warning-2"}, nil)
   127  			})
   128  
   129  			When("the isolation segment lookup is unsuccessful", func() {
   130  				BeforeEach(func() {
   131  					fakeActor.GetIsolationSegmentByNameReturns(v3action.IsolationSegment{}, v3action.Warnings{"iso-seg-warning-1", "iso-seg-warning-2"}, actionerror.IsolationSegmentNotFoundError{Name: isolationSegment})
   132  				})
   133  
   134  				It("returns the warnings and error", func() {
   135  					Expect(executeErr).To(MatchError(actionerror.IsolationSegmentNotFoundError{Name: isolationSegment}))
   136  					Expect(testUI.Err).To(Say("org-warning-1"))
   137  					Expect(testUI.Err).To(Say("org-warning-2"))
   138  					Expect(testUI.Err).To(Say("iso-seg-warning-1"))
   139  					Expect(testUI.Err).To(Say("iso-seg-warning-2"))
   140  				})
   141  			})
   142  
   143  			When("the entitlement is successful", func() {
   144  				BeforeEach(func() {
   145  					fakeActor.GetIsolationSegmentByNameReturns(v3action.IsolationSegment{GUID: "some-iso-guid"}, v3action.Warnings{"iso-seg-warning-1", "iso-seg-warning-2"}, nil)
   146  					fakeActor.SetOrganizationDefaultIsolationSegmentReturns(v3action.Warnings{"entitlement-warning", "banana"}, nil)
   147  				})
   148  
   149  				It("Displays the header and okay", func() {
   150  					Expect(executeErr).ToNot(HaveOccurred())
   151  
   152  					Expect(testUI.Out).To(Say(`Setting isolation segment %s to default on org %s as banana\.\.\.`, isolationSegment, org))
   153  					Expect(testUI.Out).To(Say("OK"))
   154  
   155  					Expect(testUI.Err).To(Say("org-warning-1"))
   156  					Expect(testUI.Err).To(Say("org-warning-2"))
   157  					Expect(testUI.Err).To(Say("iso-seg-warning-1"))
   158  					Expect(testUI.Err).To(Say("iso-seg-warning-2"))
   159  					Expect(testUI.Err).To(Say("entitlement-warning"))
   160  					Expect(testUI.Err).To(Say("banana"))
   161  
   162  					Expect(testUI.Out).To(Say(`In order to move running applications to this isolation segment, they must be restarted\.`))
   163  
   164  					Expect(fakeActor.SetOrganizationDefaultIsolationSegmentCallCount()).To(Equal(1))
   165  					orgGUID, isoSegGUID := fakeActor.SetOrganizationDefaultIsolationSegmentArgsForCall(0)
   166  					Expect(orgGUID).To(Equal("some-org-guid"))
   167  					Expect(isoSegGUID).To(Equal("some-iso-guid"))
   168  				})
   169  
   170  				When("the entitlement errors", func() {
   171  					BeforeEach(func() {
   172  						fakeActor.SetOrganizationDefaultIsolationSegmentReturns(v3action.Warnings{"entitlement-warning", "banana"}, actionerror.IsolationSegmentNotFoundError{Name: isolationSegment})
   173  					})
   174  
   175  					It("returns the warnings and error", func() {
   176  						Expect(testUI.Out).To(Say(`Setting isolation segment %s to default on org %s as banana\.\.\.`, isolationSegment, org))
   177  						Expect(testUI.Err).To(Say("org-warning-1"))
   178  						Expect(testUI.Err).To(Say("org-warning-2"))
   179  						Expect(testUI.Err).To(Say("iso-seg-warning-1"))
   180  						Expect(testUI.Err).To(Say("iso-seg-warning-2"))
   181  						Expect(testUI.Err).To(Say("entitlement-warning"))
   182  						Expect(testUI.Err).To(Say("banana"))
   183  						Expect(executeErr).To(MatchError(actionerror.IsolationSegmentNotFoundError{Name: isolationSegment}))
   184  					})
   185  				})
   186  			})
   187  		})
   188  	})
   189  })