github.com/cloudfoundry/cli@v7.1.0+incompatible/cf/commands/service/delete_service_test.go (about)

     1  package service_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/errors"
     8  	"code.cloudfoundry.org/cli/cf/models"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  
    17  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    18  )
    19  
    20  var _ = Describe("delete-service command", func() {
    21  	var (
    22  		ui                  *testterm.FakeUI
    23  		requirementsFactory *requirementsfakes.FakeFactory
    24  		serviceRepo         *apifakes.FakeServiceRepository
    25  		serviceInstance     models.ServiceInstance
    26  		configRepo          coreconfig.Repository
    27  		deps                commandregistry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.UI = ui
    32  		deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo)
    33  		deps.Config = configRepo
    34  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-service").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	BeforeEach(func() {
    38  		ui = &testterm.FakeUI{
    39  			Inputs: []string{"yes"},
    40  		}
    41  
    42  		configRepo = testconfig.NewRepositoryWithDefaults()
    43  		serviceRepo = new(apifakes.FakeServiceRepository)
    44  		requirementsFactory = new(requirementsfakes.FakeFactory)
    45  	})
    46  
    47  	runCommand := func(args ...string) bool {
    48  		return testcmd.RunCLICommand("delete-service", args, requirementsFactory, updateCommandDependency, false, ui)
    49  	}
    50  
    51  	Context("when not logged in", func() {
    52  		BeforeEach(func() {
    53  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    54  		})
    55  
    56  		It("does not pass requirements", func() {
    57  			Expect(runCommand("vestigial-service")).To(BeFalse())
    58  		})
    59  	})
    60  
    61  	Context("when logged in", func() {
    62  		BeforeEach(func() {
    63  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    64  		})
    65  
    66  		When("not targeting an org or space", func() {
    67  			BeforeEach(func() {
    68  				requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "no space targeted"})
    69  			})
    70  
    71  			It("does not pass requirements", func() {
    72  				Expect(runCommand("vestigial-service")).To(BeFalse())
    73  			})
    74  		})
    75  
    76  		When("targeting an org and space", func() {
    77  			BeforeEach(func() {
    78  				requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    79  			})
    80  
    81  			It("fails with usage when not provided exactly one arg", func() {
    82  				runCommand()
    83  				Expect(ui.Outputs()).To(ContainSubstrings(
    84  					[]string{"Incorrect Usage", "Requires an argument"},
    85  				))
    86  			})
    87  
    88  			Context("when the service exists", func() {
    89  				Context("and the service deletion is asynchronous", func() {
    90  					BeforeEach(func() {
    91  						serviceInstance = models.ServiceInstance{}
    92  						serviceInstance.Name = "my-service"
    93  						serviceInstance.GUID = "my-service-guid"
    94  						serviceInstance.LastOperation.Type = "delete"
    95  						serviceInstance.LastOperation.State = "in progress"
    96  						serviceInstance.LastOperation.Description = "delete"
    97  						serviceRepo.FindInstanceByNameReturns(serviceInstance, nil)
    98  					})
    99  
   100  					Context("when the command is confirmed", func() {
   101  						It("deletes the service", func() {
   102  							runCommand("my-service")
   103  
   104  							Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service my-service"}))
   105  
   106  							Expect(ui.Outputs()).To(ContainSubstrings(
   107  								[]string{"Deleting service", "my-service", "my-org", "my-space", "my-user"},
   108  								[]string{"OK"},
   109  								[]string{"Delete in progress. Use 'cf services' or 'cf service my-service' to check operation status."},
   110  							))
   111  
   112  							Expect(serviceRepo.DeleteServiceArgsForCall(0)).To(Equal(serviceInstance))
   113  						})
   114  					})
   115  
   116  					It("skips confirmation when the -f flag is given", func() {
   117  						runCommand("-f", "foo.com")
   118  
   119  						Expect(ui.Prompts).To(BeEmpty())
   120  						Expect(ui.Outputs()).To(ContainSubstrings(
   121  							[]string{"Deleting service", "foo.com"},
   122  							[]string{"OK"},
   123  							[]string{"Delete in progress. Use 'cf services' or 'cf service foo.com' to check operation status."},
   124  						))
   125  					})
   126  				})
   127  
   128  				Context("and the service deletion is synchronous", func() {
   129  					BeforeEach(func() {
   130  						serviceInstance = models.ServiceInstance{}
   131  						serviceInstance.Name = "my-service"
   132  						serviceInstance.GUID = "my-service-guid"
   133  						serviceRepo.FindInstanceByNameReturns(serviceInstance, nil)
   134  					})
   135  
   136  					Context("when the command is confirmed", func() {
   137  						It("deletes the service", func() {
   138  							runCommand("my-service")
   139  
   140  							Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service my-service"}))
   141  
   142  							Expect(ui.Outputs()).To(ContainSubstrings(
   143  								[]string{"Deleting service", "my-service", "my-org", "my-space", "my-user"},
   144  								[]string{"OK"},
   145  							))
   146  
   147  							Expect(serviceRepo.DeleteServiceArgsForCall(0)).To(Equal(serviceInstance))
   148  						})
   149  					})
   150  
   151  					It("skips confirmation when the -f flag is given", func() {
   152  						runCommand("-f", "foo.com")
   153  
   154  						Expect(ui.Prompts).To(BeEmpty())
   155  						Expect(ui.Outputs()).To(ContainSubstrings(
   156  							[]string{"Deleting service", "foo.com"},
   157  							[]string{"OK"},
   158  						))
   159  					})
   160  				})
   161  			})
   162  
   163  			Context("when the service does not exist", func() {
   164  				BeforeEach(func() {
   165  					serviceRepo.FindInstanceByNameReturns(models.ServiceInstance{}, errors.NewModelNotFoundError("Service instance", "my-service"))
   166  				})
   167  
   168  				It("warns the user the service does not exist", func() {
   169  					runCommand("-f", "my-service")
   170  
   171  					Expect(ui.Outputs()).To(ContainSubstrings(
   172  						[]string{"Deleting service", "my-service"},
   173  						[]string{"OK"},
   174  					))
   175  
   176  					Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"my-service", "does not exist"}))
   177  				})
   178  			})
   179  		})
   180  	})
   181  })