github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/create_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/v3action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	. "code.cloudfoundry.org/cli/command/v6"
    12  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    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("create-isolation-segment Command", func() {
    21  	var (
    22  		cmd              CreateIsolationSegmentCommand
    23  		testUI           *ui.UI
    24  		fakeConfig       *commandfakes.FakeConfig
    25  		fakeSharedActor  *commandfakes.FakeSharedActor
    26  		fakeActor        *v6fakes.FakeCreateIsolationSegmentActor
    27  		binaryName       string
    28  		executeErr       error
    29  		isolationSegment 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.FakeCreateIsolationSegmentActor)
    37  
    38  		cmd = CreateIsolationSegmentCommand{
    39  			UI:          testUI,
    40  			Config:      fakeConfig,
    41  			SharedActor: fakeSharedActor,
    42  			Actor:       fakeActor,
    43  		}
    44  
    45  		binaryName = "faceman"
    46  		fakeConfig.BinaryNameReturns(binaryName)
    47  		isolationSegment = "segment1"
    48  	})
    49  
    50  	JustBeforeEach(func() {
    51  		executeErr = cmd.Execute(nil)
    52  	})
    53  
    54  	When("the API version is below the minimum", func() {
    55  		BeforeEach(func() {
    56  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinV3ClientVersion)
    57  		})
    58  
    59  		It("returns a MinimumAPIVersionNotMetError", func() {
    60  			Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{
    61  				CurrentVersion: ccversion.MinV3ClientVersion,
    62  				MinimumVersion: ccversion.MinVersionIsolationSegmentV3,
    63  			}))
    64  		})
    65  	})
    66  
    67  	When("checking target fails", func() {
    68  		BeforeEach(func() {
    69  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
    70  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    71  		})
    72  
    73  		It("returns an error", func() {
    74  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    75  
    76  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    77  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    78  			Expect(checkTargetedOrg).To(BeFalse())
    79  			Expect(checkTargetedSpace).To(BeFalse())
    80  		})
    81  	})
    82  
    83  	When("the user is logged in", func() {
    84  		BeforeEach(func() {
    85  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
    86  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    87  
    88  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    89  		})
    90  
    91  		When("the create is successful", func() {
    92  			BeforeEach(func() {
    93  				fakeActor.CreateIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, nil)
    94  			})
    95  
    96  			It("displays the header and ok", func() {
    97  				Expect(executeErr).ToNot(HaveOccurred())
    98  
    99  				Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana..."))
   100  				Expect(testUI.Out).To(Say("OK"))
   101  
   102  				Expect(testUI.Err).To(Say("I am a warning"))
   103  				Expect(testUI.Err).To(Say("I am also a warning"))
   104  
   105  				Expect(fakeActor.CreateIsolationSegmentByNameCallCount()).To(Equal(1))
   106  				Expect(fakeActor.CreateIsolationSegmentByNameArgsForCall(0)).To(Equal(v3action.IsolationSegment{Name: isolationSegment}))
   107  			})
   108  		})
   109  
   110  		When("the create is unsuccessful", func() {
   111  			Context("due to an unexpected error", func() {
   112  				var expectedErr error
   113  
   114  				BeforeEach(func() {
   115  					expectedErr = errors.New("I am an error")
   116  					fakeActor.CreateIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, expectedErr)
   117  				})
   118  
   119  				It("displays the header and error", func() {
   120  					Expect(executeErr).To(MatchError(expectedErr))
   121  
   122  					Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana..."))
   123  
   124  					Expect(testUI.Err).To(Say("I am a warning"))
   125  					Expect(testUI.Err).To(Say("I am also a warning"))
   126  				})
   127  			})
   128  
   129  			Context("due to an IsolationSegmentAlreadyExistsError", func() {
   130  				BeforeEach(func() {
   131  					fakeActor.CreateIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, actionerror.IsolationSegmentAlreadyExistsError{})
   132  				})
   133  
   134  				It("displays the header and ok", func() {
   135  					Expect(executeErr).ToNot(HaveOccurred())
   136  
   137  					Expect(testUI.Out).To(Say("Creating isolation segment segment1 as banana..."))
   138  					Expect(testUI.Out).To(Say("OK"))
   139  
   140  					Expect(testUI.Err).To(Say("I am a warning"))
   141  					Expect(testUI.Err).To(Say("I am also a warning"))
   142  					Expect(testUI.Err).To(Say("Isolation segment %s already exists.", isolationSegment))
   143  				})
   144  			})
   145  		})
   146  	})
   147  })