github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/set_org_default_isolation_segment_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  	. "code.cloudfoundry.org/cli/command/v7"
     8  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
     9  	"code.cloudfoundry.org/cli/resources"
    10  
    11  	"code.cloudfoundry.org/cli/actor/actionerror"
    12  	"code.cloudfoundry.org/cli/command/commandfakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("set-org-default-isolation-segment Command", func() {
    21  	var (
    22  		cmd              SetOrgDefaultIsolationSegmentCommand
    23  		testUI           *ui.UI
    24  		fakeConfig       *commandfakes.FakeConfig
    25  		fakeSharedActor  *commandfakes.FakeSharedActor
    26  		fakeActor        *v7fakes.FakeActor
    27  		binaryName       string
    28  		executeErr       error
    29  		isolationSegment string
    30  		org              string
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    35  		fakeConfig = new(commandfakes.FakeConfig)
    36  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    37  		fakeActor = new(v7fakes.FakeActor)
    38  
    39  		cmd = SetOrgDefaultIsolationSegmentCommand{
    40  			BaseCommand: BaseCommand{
    41  				UI:          testUI,
    42  				Config:      fakeConfig,
    43  				SharedActor: fakeSharedActor,
    44  				Actor:       fakeActor,
    45  			},
    46  		}
    47  
    48  		binaryName = "faceman"
    49  		fakeConfig.BinaryNameReturns(binaryName)
    50  		org = "some-org"
    51  		isolationSegment = "segment1"
    52  
    53  		fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    54  
    55  		cmd.RequiredArgs.OrganizationName = org
    56  		cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    57  
    58  		fakeActor.GetOrganizationByNameReturns(resources.Organization{
    59  			Name: org,
    60  			GUID: "some-org-guid",
    61  		}, v7action.Warnings{"org-warning-1", "org-warning-2"}, nil)
    62  		fakeActor.GetIsolationSegmentByNameReturns(v7action.IsolationSegment{GUID: "some-iso-guid"}, v7action.Warnings{"iso-seg-warning-1", "iso-seg-warning-2"}, nil)
    63  		fakeActor.SetOrganizationDefaultIsolationSegmentReturns(v7action.Warnings{"isolation-set-warning-1", "isolation-set-warning-2"}, nil)
    64  	})
    65  
    66  	JustBeforeEach(func() {
    67  		executeErr = cmd.Execute(nil)
    68  	})
    69  
    70  	When("checking target fails", func() {
    71  		BeforeEach(func() {
    72  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    73  		})
    74  
    75  		It("returns an error", func() {
    76  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    77  
    78  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    79  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    80  			Expect(checkTargetedOrg).To(BeFalse())
    81  			Expect(checkTargetedSpace).To(BeFalse())
    82  		})
    83  	})
    84  
    85  	When("fetching the user fails", func() {
    86  		BeforeEach(func() {
    87  			fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error"))
    88  		})
    89  
    90  		It("returns an error", func() {
    91  			Expect(executeErr).To(MatchError("some-error"))
    92  		})
    93  	})
    94  
    95  	It("prints out its intentions", func() {
    96  		Expect(testUI.Out).To(Say(`Setting isolation segment %s to default on org %s as banana\.\.\.`, isolationSegment, org))
    97  	})
    98  
    99  	When("the org lookup is unsuccessful", func() {
   100  		BeforeEach(func() {
   101  			fakeActor.GetOrganizationByNameReturns(resources.Organization{}, v7action.Warnings{"org-warning-1", "org-warning-2"}, actionerror.OrganizationNotFoundError{Name: org})
   102  		})
   103  
   104  		It("returns the warnings and error", func() {
   105  			Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: org}))
   106  			Expect(testUI.Err).To(Say("org-warning-1"))
   107  			Expect(testUI.Err).To(Say("org-warning-2"))
   108  		})
   109  	})
   110  
   111  	It("prints the org fetch warnings", func() {
   112  		Expect(testUI.Err).To(Say("org-warning-1"))
   113  		Expect(testUI.Err).To(Say("org-warning-2"))
   114  	})
   115  
   116  	When("the isolation segment lookup is unsuccessful", func() {
   117  		BeforeEach(func() {
   118  			fakeActor.GetIsolationSegmentByNameReturns(v7action.IsolationSegment{}, v7action.Warnings{"iso-seg-warning-1", "iso-seg-warning-2"}, actionerror.IsolationSegmentNotFoundError{Name: isolationSegment})
   119  		})
   120  
   121  		It("returns the warnings and error", func() {
   122  			Expect(executeErr).To(MatchError(actionerror.IsolationSegmentNotFoundError{Name: isolationSegment}))
   123  			Expect(testUI.Err).To(Say("iso-seg-warning-1"))
   124  			Expect(testUI.Err).To(Say("iso-seg-warning-2"))
   125  		})
   126  	})
   127  
   128  	It("prints the iso segment fetch warnings", func() {
   129  		Expect(testUI.Err).To(Say("iso-seg-warning-1"))
   130  		Expect(testUI.Err).To(Say("iso-seg-warning-2"))
   131  	})
   132  
   133  	When("setting the default isolation to an org errors", func() {
   134  		BeforeEach(func() {
   135  			fakeActor.SetOrganizationDefaultIsolationSegmentReturns(v7action.Warnings{"isolation-set-warning-1", "isolation-set-warning-2"}, actionerror.IsolationSegmentNotFoundError{Name: isolationSegment})
   136  		})
   137  
   138  		It("returns the warnings and error", func() {
   139  			Expect(testUI.Err).To(Say("isolation-set-warning-1"))
   140  			Expect(testUI.Err).To(Say("isolation-set-warning-2"))
   141  			Expect(executeErr).To(MatchError(actionerror.IsolationSegmentNotFoundError{Name: isolationSegment}))
   142  		})
   143  	})
   144  
   145  	It("prints the iso segment set warnings", func() {
   146  		Expect(testUI.Err).To(Say("isolation-set-warning-1"))
   147  		Expect(testUI.Err).To(Say("isolation-set-warning-2"))
   148  	})
   149  
   150  	It("Displays the header and okay", func() {
   151  		Expect(executeErr).ToNot(HaveOccurred())
   152  
   153  		Expect(testUI.Out).To(Say("OK"))
   154  		Expect(testUI.Out).To(Say("TIP: Restart applications in this organization to relocate them to this isolation segment."))
   155  
   156  		Expect(fakeActor.SetOrganizationDefaultIsolationSegmentCallCount()).To(Equal(1))
   157  		orgGUID, isoSegGUID := fakeActor.SetOrganizationDefaultIsolationSegmentArgsForCall(0)
   158  		Expect(orgGUID).To(Equal("some-org-guid"))
   159  		Expect(isoSegGUID).To(Equal("some-iso-guid"))
   160  	})
   161  })