github.com/niteshexa/cloudfoundry_cli@v7.1.0+incompatible/command/v7/delete_isolation_segment_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    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        *v7fakes.FakeActor
    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(v7fakes.FakeActor)
    37  
    38  		cmd = DeleteIsolationSegmentCommand{
    39  			BaseCommand: BaseCommand{
    40  				UI:          testUI,
    41  				Config:      fakeConfig,
    42  				SharedActor: fakeSharedActor,
    43  				Actor:       fakeActor,
    44  			},
    45  		}
    46  
    47  		binaryName = "faceman"
    48  		fakeConfig.BinaryNameReturns(binaryName)
    49  		isolationSegment = "segment1"
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	When("checking target fails", func() {
    57  		BeforeEach(func() {
    58  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    59  		})
    60  
    61  		It("returns an error", func() {
    62  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    63  
    64  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    65  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    66  			Expect(checkTargetedOrg).To(BeFalse())
    67  			Expect(checkTargetedSpace).To(BeFalse())
    68  		})
    69  	})
    70  
    71  	When("the user is logged in", func() {
    72  		BeforeEach(func() {
    73  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    74  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    75  		})
    76  
    77  		When("the -f flag is provided", func() {
    78  			BeforeEach(func() {
    79  				cmd.Force = true
    80  			})
    81  
    82  			When("the iso segment exists", func() {
    83  				When("the delete is successful", func() {
    84  					BeforeEach(func() {
    85  						fakeActor.DeleteIsolationSegmentByNameReturns(v7action.Warnings{"I am a warning", "I am also a warning"}, nil)
    86  					})
    87  
    88  					It("displays the header and ok", func() {
    89  						Expect(executeErr).ToNot(HaveOccurred())
    90  
    91  						Expect(testUI.Out).To(Say("Deleting isolation segment segment1 as banana..."))
    92  						Expect(testUI.Out).To(Say("OK"))
    93  
    94  						Expect(testUI.Err).To(Say("I am a warning"))
    95  						Expect(testUI.Err).To(Say("I am also a warning"))
    96  
    97  						Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(1))
    98  						Expect(fakeActor.DeleteIsolationSegmentByNameArgsForCall(0)).To(Equal("segment1"))
    99  					})
   100  				})
   101  
   102  				When("the delete is unsuccessful", func() {
   103  					var expectedErr error
   104  
   105  					BeforeEach(func() {
   106  						expectedErr = errors.New("I am an error")
   107  						fakeActor.DeleteIsolationSegmentByNameReturns(v7action.Warnings{"I am a warning", "I am also a warning"}, expectedErr)
   108  					})
   109  
   110  					It("displays the header and error", func() {
   111  						Expect(executeErr).To(MatchError(expectedErr))
   112  
   113  						Expect(testUI.Out).To(Say("Deleting isolation segment segment1 as banana..."))
   114  
   115  						Expect(testUI.Err).To(Say("I am a warning"))
   116  						Expect(testUI.Err).To(Say("I am also a warning"))
   117  					})
   118  				})
   119  			})
   120  
   121  			When("the iso segment does not exist", func() {
   122  				BeforeEach(func() {
   123  					fakeActor.DeleteIsolationSegmentByNameReturns(v7action.Warnings{"I am a warning", "I am also a warning"}, actionerror.IsolationSegmentNotFoundError{})
   124  				})
   125  
   126  				It("displays does not exist warning", func() {
   127  					Expect(testUI.Out).To(Say("OK"))
   128  					Expect(executeErr).NotTo(HaveOccurred())
   129  					Expect(testUI.Err).To(Say("Isolation segment %s does not exist.", isolationSegment))
   130  				})
   131  			})
   132  		})
   133  
   134  		When("the -f flag is not provided", func() {
   135  			When("the user chooses the default", func() {
   136  				BeforeEach(func() {
   137  					_, err := input.Write([]byte("\n"))
   138  					Expect(err).ToNot(HaveOccurred())
   139  				})
   140  
   141  				It("cancels the deletion", func() {
   142  					Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment))
   143  					Expect(testUI.Out).To(Say("Delete cancelled"))
   144  					Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(0))
   145  				})
   146  			})
   147  
   148  			When("the user inputs yes", func() {
   149  				BeforeEach(func() {
   150  					_, err := input.Write([]byte("yes\n"))
   151  					Expect(err).ToNot(HaveOccurred())
   152  				})
   153  
   154  				It("deletes the isolation segment", func() {
   155  					Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment))
   156  					Expect(testUI.Out).To(Say("OK"))
   157  					Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(1))
   158  				})
   159  			})
   160  
   161  			When("the user inputs no", func() {
   162  				BeforeEach(func() {
   163  					_, err := input.Write([]byte("no\n"))
   164  					Expect(err).ToNot(HaveOccurred())
   165  				})
   166  
   167  				It("cancels the deletion", func() {
   168  					Expect(testUI.Out).To(Say("Really delete the isolation segment %s?", isolationSegment))
   169  					Expect(testUI.Out).To(Say("Delete cancelled"))
   170  					Expect(fakeActor.DeleteIsolationSegmentByNameCallCount()).To(Equal(0))
   171  				})
   172  			})
   173  		})
   174  	})
   175  })