github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/purge_service_offering_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    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("purge-service-offering command", func() {
    21  	var (
    22  		cmd                   PurgeServiceOfferingCommand
    23  		testUI                *ui.UI
    24  		fakeConfig            *commandfakes.FakeConfig
    25  		fakeSharedActor       *commandfakes.FakeSharedActor
    26  		fakePurgeServiceActor *v6fakes.FakePurgeServiceOfferingActor
    27  		input                 *Buffer
    28  		binaryName            string
    29  		executeErr            error
    30  		extraArgs             []string
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		input = NewBuffer()
    35  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakePurgeServiceActor = new(v6fakes.FakePurgeServiceOfferingActor)
    39  		extraArgs = nil
    40  
    41  		cmd = PurgeServiceOfferingCommand{
    42  			UI:          testUI,
    43  			Config:      fakeConfig,
    44  			SharedActor: fakeSharedActor,
    45  			Actor:       fakePurgeServiceActor,
    46  			RequiredArgs: flag.Service{
    47  				Service: "some-service",
    48  			},
    49  		}
    50  		fakeConfig.ExperimentalReturns(true)
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(extraArgs)
    55  	})
    56  
    57  	When("the user provides extra arguments", func() {
    58  		BeforeEach(func() {
    59  			extraArgs = []string{"some-extra-arg"}
    60  		})
    61  
    62  		It("fails with a TooManyArgumentsError", func() {
    63  			Expect(executeErr).To(MatchError(translatableerror.TooManyArgumentsError{
    64  				ExtraArgument: "some-extra-arg",
    65  			}))
    66  		})
    67  	})
    68  
    69  	When("cloud controller API endpoint is set", func() {
    70  		BeforeEach(func() {
    71  			fakeConfig.TargetReturns("some-url")
    72  		})
    73  
    74  		When("checking target fails", func() {
    75  			BeforeEach(func() {
    76  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    77  			})
    78  
    79  			It("returns a not logged in error", func() {
    80  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    81  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    82  			})
    83  		})
    84  
    85  		When("the user is logged in", func() {
    86  			BeforeEach(func() {
    87  				fakeConfig.CurrentUserReturns(
    88  					configv3.User{Name: "admin"},
    89  					nil)
    90  			})
    91  
    92  			When("no flags are passed", func() {
    93  				It("prints the warning text", func() {
    94  					Expect(testUI.Out).To(Say("WARNING: This operation assumes that the service broker responsible for this service offering is no longer available, and all service instances have been deleted, leaving orphan records in Cloud Foundry's database\\. All knowledge of the service will be removed from Cloud Foundry, including service instances and service bindings\\. No attempt will be made to contact the service broker; running this command without destroying the service broker will cause orphan service instances\\. After running this command you may want to run either delete-service-auth-token or delete-service-broker to complete the cleanup\\."))
    95  					Expect(testUI.Out).To(Say("Really purge service offering some-service from Cloud Foundry?"))
    96  				})
    97  
    98  				When("the user chooses the default", func() {
    99  					BeforeEach(func() {
   100  						input.Write([]byte("\n"))
   101  					})
   102  
   103  					It("does not purge the service offering", func() {
   104  						Expect(executeErr).ToNot(HaveOccurred())
   105  
   106  						Expect(testUI.Out).To(Say("Purge service offering cancelled"))
   107  
   108  						Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(0))
   109  					})
   110  				})
   111  
   112  				When("the user inputs no", func() {
   113  					BeforeEach(func() {
   114  						input.Write([]byte("n\n"))
   115  					})
   116  
   117  					It("does not purge the service offering", func() {
   118  						Expect(executeErr).ToNot(HaveOccurred())
   119  
   120  						Expect(testUI.Out).To(Say("Purge service offering cancelled"))
   121  
   122  						Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(0))
   123  					})
   124  				})
   125  
   126  				When("the user inputs yes", func() {
   127  					BeforeEach(func() {
   128  						input.Write([]byte("y\n"))
   129  						fakePurgeServiceActor.GetServiceByNameAndBrokerNameReturns(v2action.Service{
   130  							Label: "some-service",
   131  							GUID:  "some-service-guid",
   132  						}, v2action.Warnings{"get-service-warning"}, nil)
   133  						fakePurgeServiceActor.PurgeServiceOfferingReturns(v2action.Warnings{"warning-1"}, nil)
   134  					})
   135  
   136  					It("purges the service offering and prints all warnings", func() {
   137  						Expect(executeErr).NotTo(HaveOccurred())
   138  
   139  						Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(1))
   140  
   141  						service := fakePurgeServiceActor.PurgeServiceOfferingArgsForCall(0)
   142  						Expect(service.Label).To(Equal("some-service"))
   143  						Expect(service.GUID).To(Equal("some-service-guid"))
   144  
   145  						Expect(testUI.Err).To(Say("get-service-warning"))
   146  						Expect(testUI.Err).To(Say("warning-1"))
   147  						Expect(testUI.Out).To(Say("OK"))
   148  					})
   149  
   150  					When("purge service offering fails", func() {
   151  						When("the service offering does not exist", func() {
   152  							BeforeEach(func() {
   153  								fakePurgeServiceActor.GetServiceByNameAndBrokerNameReturns(v2action.Service{}, v2action.Warnings{"get-service-warning"}, actionerror.ServiceNotFoundError{Name: "some-service"})
   154  							})
   155  
   156  							It("returns a PurgeServiceOfferingNotFound error and prints the warnings, and OK", func() {
   157  								Expect(fakePurgeServiceActor.GetServiceByNameAndBrokerNameCallCount()).To(Equal(1))
   158  
   159  								serviceName, brokerName := fakePurgeServiceActor.GetServiceByNameAndBrokerNameArgsForCall(0)
   160  								Expect(serviceName).To(Equal("some-service"))
   161  								Expect(brokerName).To(Equal(""))
   162  
   163  								Eventually(testUI.Out).Should(Say(`Service offering 'some-service' not found`))
   164  								Eventually(testUI.Out).Should(Say(`OK`))
   165  								Expect(testUI.Err).To(Say("get-service-warning"))
   166  							})
   167  						})
   168  
   169  						When("an unknown error occurs", func() {
   170  							BeforeEach(func() {
   171  								fakePurgeServiceActor.PurgeServiceOfferingReturns(v2action.Warnings{"warning-1"}, fmt.Errorf("it broke!"))
   172  							})
   173  
   174  							It("returns the error and prints the warnings", func() {
   175  								Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(1))
   176  
   177  								Expect(executeErr).To(MatchError(fmt.Errorf("it broke!")))
   178  								Expect(testUI.Err).To(Say("get-service-warning"))
   179  								Expect(testUI.Err).To(Say("warning-1"))
   180  							})
   181  						})
   182  					})
   183  				})
   184  
   185  				When("the user input is invalid", func() {
   186  					BeforeEach(func() {
   187  						input.Write([]byte("e\n\n"))
   188  					})
   189  
   190  					It("asks the user again", func() {
   191  						Expect(executeErr).NotTo(HaveOccurred())
   192  
   193  						Expect(testUI.Out).To(Say(`Really purge service offering some-service from Cloud Foundry\? \[yN\]:`))
   194  						Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`))
   195  						Expect(testUI.Out).To(Say(`Really purge service offering some-service from Cloud Foundry\? \[yN\]:`))
   196  
   197  						Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(0))
   198  					})
   199  				})
   200  			})
   201  
   202  			When("the -f flag is passed", func() {
   203  				BeforeEach(func() {
   204  					cmd.Force = true
   205  
   206  					fakePurgeServiceActor.GetServiceByNameAndBrokerNameReturns(v2action.Service{
   207  						Label: "some-service",
   208  						GUID:  "some-service-guid",
   209  					}, v2action.Warnings{"get-service-warning"}, nil)
   210  					fakePurgeServiceActor.PurgeServiceOfferingReturns(v2action.Warnings{"warning-1"}, nil)
   211  				})
   212  
   213  				It("purges the service offering without asking for confirmation", func() {
   214  					Expect(executeErr).NotTo(HaveOccurred())
   215  					Expect(testUI.Out).NotTo(Say(`Really purge service offering some-service from Cloud Foundry\? \[yN\]:`))
   216  
   217  					Expect(testUI.Out).To(Say(`Purging service some-service\.\.\.`))
   218  
   219  					Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(1))
   220  					service := fakePurgeServiceActor.PurgeServiceOfferingArgsForCall(0)
   221  					Expect(service.Label).To(Equal("some-service"))
   222  					Expect(service.GUID).To(Equal("some-service-guid"))
   223  
   224  					Expect(testUI.Err).To(Say("warning-1"))
   225  					Expect(testUI.Out).To(Say("OK"))
   226  				})
   227  			})
   228  
   229  			When("the -p flag is passed", func() {
   230  				BeforeEach(func() {
   231  					cmd.Provider = "dave"
   232  					input.Write([]byte("y\n"))
   233  				})
   234  
   235  				It("returns an error that this flag is no longer supported", func() {
   236  					Expect(executeErr).To(MatchError(translatableerror.FlagNoLongerSupportedError{Flag: "-p"}))
   237  				})
   238  			})
   239  		})
   240  	})
   241  })