github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/delete_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/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v6"
    10  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("delete-isolation-segment Command", func() {
    19  	var (
    20  		cmd              DeleteIsolationSegmentCommand
    21  		input            *Buffer
    22  		testUI           *ui.UI
    23  		fakeConfig       *commandfakes.FakeConfig
    24  		fakeSharedActor  *commandfakes.FakeSharedActor
    25  		fakeActor        *v6fakes.FakeDeleteIsolationSegmentActor
    26  		binaryName       string
    27  		executeErr       error
    28  		isolationSegment string
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		input = NewBuffer()
    33  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v6fakes.FakeDeleteIsolationSegmentActor)
    37  
    38  		cmd = DeleteIsolationSegmentCommand{
    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("checking target fails", func() {
    55  		BeforeEach(func() {
    56  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    57  		})
    58  
    59  		It("returns an error", func() {
    60  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    61  
    62  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    63  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    64  			Expect(checkTargetedOrg).To(BeFalse())
    65  			Expect(checkTargetedSpace).To(BeFalse())
    66  		})
    67  	})
    68  
    69  	When("the user is logged in", func() {
    70  		BeforeEach(func() {
    71  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    72  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    73  		})
    74  
    75  		When("the -f flag is provided", func() {
    76  			BeforeEach(func() {
    77  				cmd.Force = true
    78  			})
    79  
    80  			When("the iso segment exists", func() {
    81  				When("the delete is successful", func() {
    82  					BeforeEach(func() {
    83  						fakeActor.DeleteIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, nil)
    84  					})
    85  
    86  					It("displays the header and ok", func() {
    87  						Expect(executeErr).ToNot(HaveOccurred())
    88  
    89  						Expect(testUI.Out).To(Say("Deleting isolation segment segment1 as banana..."))
    90  						Expect(testUI.Out).To(Say("OK"))
    91  
    92  						Expect(testUI.Err).To(Say("I am a warning"))
    93  						Expect(testUI.Err).To(Say("I am also a warning"))
    94  
    95  						Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(1))
    96  						Expect(fakeActor.DeleteIsolationSegmentByNameArgsForCall(0)).To(Equal("segment1"))
    97  					})
    98  				})
    99  
   100  				When("the delete is unsuccessful", func() {
   101  					var expectedErr error
   102  
   103  					BeforeEach(func() {
   104  						expectedErr = errors.New("I am an error")
   105  						fakeActor.DeleteIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, expectedErr)
   106  					})
   107  
   108  					It("displays the header and error", func() {
   109  						Expect(executeErr).To(MatchError(expectedErr))
   110  
   111  						Expect(testUI.Out).To(Say("Deleting isolation segment segment1 as banana..."))
   112  
   113  						Expect(testUI.Err).To(Say("I am a warning"))
   114  						Expect(testUI.Err).To(Say("I am also a warning"))
   115  					})
   116  				})
   117  			})
   118  
   119  			When("the iso segment does not exist", func() {
   120  				BeforeEach(func() {
   121  					fakeActor.DeleteIsolationSegmentByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, actionerror.IsolationSegmentNotFoundError{})
   122  				})
   123  
   124  				It("displays does not exist warning", func() {
   125  					Expect(testUI.Out).To(Say("OK"))
   126  					Expect(executeErr).NotTo(HaveOccurred())
   127  					Expect(testUI.Err).To(Say("Isolation segment %s does not exist.", isolationSegment))
   128  				})
   129  			})
   130  		})
   131  
   132  		When("the -f flag is not provided", func() {
   133  			When("the user chooses the default", func() {
   134  				BeforeEach(func() {
   135  					input.Write([]byte("\n"))
   136  				})
   137  
   138  				It("cancels the deletion", func() {
   139  					Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment))
   140  					Expect(testUI.Out).To(Say("Delete cancelled"))
   141  					Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(0))
   142  				})
   143  			})
   144  
   145  			When("the user inputs yes", func() {
   146  				BeforeEach(func() {
   147  					input.Write([]byte("yes\n"))
   148  				})
   149  
   150  				It("deletes the isolation segment", func() {
   151  					Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment))
   152  					Expect(testUI.Out).To(Say("OK"))
   153  					Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(1))
   154  				})
   155  			})
   156  
   157  			When("the user inputs no", func() {
   158  				BeforeEach(func() {
   159  					input.Write([]byte("no\n"))
   160  				})
   161  
   162  				It("cancels the deletion", func() {
   163  					Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment))
   164  					Expect(testUI.Out).To(Say("Delete cancelled"))
   165  					Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(0))
   166  				})
   167  			})
   168  		})
   169  	})
   170  })