github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(extraArgs)
    54  	})
    55  
    56  	When("the user provides extra arguments", func() {
    57  		BeforeEach(func() {
    58  			extraArgs = []string{"some-extra-arg"}
    59  		})
    60  
    61  		It("fails with a TooManyArgumentsError", func() {
    62  			Expect(executeErr).To(MatchError(translatableerror.TooManyArgumentsError{
    63  				ExtraArgument: "some-extra-arg",
    64  			}))
    65  		})
    66  	})
    67  
    68  	When("cloud controller API endpoint is set", func() {
    69  		BeforeEach(func() {
    70  			fakeConfig.TargetReturns("some-url")
    71  		})
    72  
    73  		When("checking target fails", func() {
    74  			BeforeEach(func() {
    75  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    76  			})
    77  
    78  			It("returns a not logged in error", func() {
    79  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    80  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    81  			})
    82  		})
    83  
    84  		When("the user is logged in", func() {
    85  			BeforeEach(func() {
    86  				fakeConfig.CurrentUserReturns(
    87  					configv3.User{Name: "admin"},
    88  					nil)
    89  			})
    90  
    91  			When("no flags are passed", func() {
    92  				It("prints the warning text", func() {
    93  					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\\."))
    94  					Expect(testUI.Out).To(Say("Really purge service offering some-service from Cloud Foundry?"))
    95  				})
    96  
    97  				When("the user chooses the default", func() {
    98  					BeforeEach(func() {
    99  						input.Write([]byte("\n"))
   100  					})
   101  
   102  					It("does not purge the service offering", func() {
   103  						Expect(executeErr).ToNot(HaveOccurred())
   104  
   105  						Expect(testUI.Out).To(Say("Purge service offering cancelled"))
   106  
   107  						Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(0))
   108  					})
   109  				})
   110  
   111  				When("the user inputs no", func() {
   112  					BeforeEach(func() {
   113  						input.Write([]byte("n\n"))
   114  					})
   115  
   116  					It("does not purge the service offering", func() {
   117  						Expect(executeErr).ToNot(HaveOccurred())
   118  
   119  						Expect(testUI.Out).To(Say("Purge service offering cancelled"))
   120  
   121  						Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(0))
   122  					})
   123  				})
   124  
   125  				When("the user inputs yes", func() {
   126  					BeforeEach(func() {
   127  						input.Write([]byte("y\n"))
   128  						fakePurgeServiceActor.GetServiceByNameAndBrokerNameReturns(v2action.Service{
   129  							Label: "some-service",
   130  							GUID:  "some-service-guid",
   131  						}, v2action.Warnings{"get-service-warning"}, nil)
   132  						fakePurgeServiceActor.PurgeServiceOfferingReturns(v2action.Warnings{"warning-1"}, nil)
   133  					})
   134  
   135  					It("purges the service offering and prints all warnings", func() {
   136  						Expect(executeErr).NotTo(HaveOccurred())
   137  
   138  						Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(1))
   139  
   140  						service := fakePurgeServiceActor.PurgeServiceOfferingArgsForCall(0)
   141  						Expect(service.Label).To(Equal("some-service"))
   142  						Expect(service.GUID).To(Equal("some-service-guid"))
   143  
   144  						Expect(testUI.Err).To(Say("get-service-warning"))
   145  						Expect(testUI.Err).To(Say("warning-1"))
   146  						Expect(testUI.Out).To(Say("OK"))
   147  					})
   148  
   149  					When("purge service offering fails", func() {
   150  						When("the service offering does not exist", func() {
   151  							BeforeEach(func() {
   152  								fakePurgeServiceActor.GetServiceByNameAndBrokerNameReturns(v2action.Service{}, v2action.Warnings{"get-service-warning"}, actionerror.ServiceNotFoundError{Name: "some-service"})
   153  							})
   154  
   155  							It("returns a PurgeServiceOfferingNotFound error and prints the warnings, and OK", func() {
   156  								Expect(fakePurgeServiceActor.GetServiceByNameAndBrokerNameCallCount()).To(Equal(1))
   157  
   158  								serviceName, brokerName := fakePurgeServiceActor.GetServiceByNameAndBrokerNameArgsForCall(0)
   159  								Expect(serviceName).To(Equal("some-service"))
   160  								Expect(brokerName).To(Equal(""))
   161  
   162  								Eventually(testUI.Out).Should(Say(`Service offering 'some-service' not found`))
   163  								Eventually(testUI.Out).Should(Say(`OK`))
   164  								Expect(testUI.Err).To(Say("get-service-warning"))
   165  							})
   166  						})
   167  
   168  						When("an unknown error occurs", func() {
   169  							BeforeEach(func() {
   170  								fakePurgeServiceActor.PurgeServiceOfferingReturns(v2action.Warnings{"warning-1"}, fmt.Errorf("it broke!"))
   171  							})
   172  
   173  							It("returns the error and prints the warnings", func() {
   174  								Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(1))
   175  
   176  								Expect(executeErr).To(MatchError(fmt.Errorf("it broke!")))
   177  								Expect(testUI.Err).To(Say("get-service-warning"))
   178  								Expect(testUI.Err).To(Say("warning-1"))
   179  							})
   180  						})
   181  					})
   182  				})
   183  
   184  				When("the user input is invalid", func() {
   185  					BeforeEach(func() {
   186  						input.Write([]byte("e\n\n"))
   187  					})
   188  
   189  					It("asks the user again", func() {
   190  						Expect(executeErr).NotTo(HaveOccurred())
   191  
   192  						Expect(testUI.Out).To(Say(`Really purge service offering some-service from Cloud Foundry\? \[yN\]:`))
   193  						Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`))
   194  						Expect(testUI.Out).To(Say(`Really purge service offering some-service from Cloud Foundry\? \[yN\]:`))
   195  
   196  						Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(0))
   197  					})
   198  				})
   199  			})
   200  
   201  			When("the -f flag is passed", func() {
   202  				BeforeEach(func() {
   203  					cmd.Force = true
   204  
   205  					fakePurgeServiceActor.GetServiceByNameAndBrokerNameReturns(v2action.Service{
   206  						Label: "some-service",
   207  						GUID:  "some-service-guid",
   208  					}, v2action.Warnings{"get-service-warning"}, nil)
   209  					fakePurgeServiceActor.PurgeServiceOfferingReturns(v2action.Warnings{"warning-1"}, nil)
   210  				})
   211  
   212  				It("purges the service offering without asking for confirmation", func() {
   213  					Expect(executeErr).NotTo(HaveOccurred())
   214  					Expect(testUI.Out).NotTo(Say(`Really purge service offering some-service from Cloud Foundry\? \[yN\]:`))
   215  
   216  					Expect(testUI.Out).To(Say(`Purging service some-service\.\.\.`))
   217  
   218  					Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(1))
   219  					service := fakePurgeServiceActor.PurgeServiceOfferingArgsForCall(0)
   220  					Expect(service.Label).To(Equal("some-service"))
   221  					Expect(service.GUID).To(Equal("some-service-guid"))
   222  
   223  					Expect(testUI.Err).To(Say("warning-1"))
   224  					Expect(testUI.Out).To(Say("OK"))
   225  				})
   226  			})
   227  
   228  			When("the -p flag is passed", func() {
   229  				BeforeEach(func() {
   230  					cmd.Provider = "dave"
   231  					input.Write([]byte("y\n"))
   232  				})
   233  
   234  				It("returns an error that this flag is no longer supported", func() {
   235  					Expect(executeErr).To(MatchError(translatableerror.FlagNoLongerSupportedError{Flag: "-p"}))
   236  				})
   237  			})
   238  
   239  			When("the -b flag is passed", func() {
   240  				BeforeEach(func() {
   241  					cmd.ServiceBroker = "some-broker"
   242  
   243  					fakePurgeServiceActor.GetServiceByNameAndBrokerNameReturns(v2action.Service{
   244  						Label: "some-service",
   245  						GUID:  "some-service-guid",
   246  					}, v2action.Warnings{"get-service-warning"}, nil)
   247  					fakePurgeServiceActor.PurgeServiceOfferingReturns(v2action.Warnings{"warning-1"}, nil)
   248  					input.Write([]byte("y\n"))
   249  				})
   250  
   251  				It("purges the service offering for the specified broker", func() {
   252  					Expect(executeErr).NotTo(HaveOccurred())
   253  					Expect(testUI.Out).To(Say(`Really purge service offering some-service from broker some-broker from Cloud Foundry\? \[yN\]:`))
   254  					Expect(testUI.Out).To(Say(`Purging service some-service\.\.\.`))
   255  
   256  					serviceName, brokerName := fakePurgeServiceActor.GetServiceByNameAndBrokerNameArgsForCall(0)
   257  					Expect(serviceName).To(Equal("some-service"))
   258  					Expect(brokerName).To(Equal("some-broker"))
   259  
   260  					Expect(fakePurgeServiceActor.PurgeServiceOfferingCallCount()).To(Equal(1))
   261  					service := fakePurgeServiceActor.PurgeServiceOfferingArgsForCall(0)
   262  					Expect(service.Label).To(Equal("some-service"))
   263  					Expect(service.GUID).To(Equal("some-service-guid"))
   264  
   265  					Expect(testUI.Err).To(Say("warning-1"))
   266  					Expect(testUI.Out).To(Say("OK"))
   267  				})
   268  			})
   269  		})
   270  	})
   271  })