github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  
    17  	. "code.cloudfoundry.org/cli/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  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    46  	})
    47  
    48  	runCommand := func(args ...string) bool {
    49  		return testcmd.RunCLICommand("delete-service", args, requirementsFactory, updateCommandDependency, false, ui)
    50  	}
    51  
    52  	Context("when not logged in", func() {
    53  		BeforeEach(func() {
    54  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    55  		})
    56  
    57  		It("does not pass requirements", func() {
    58  			Expect(runCommand("vestigial-service")).To(BeFalse())
    59  		})
    60  	})
    61  
    62  	Context("when logged in", func() {
    63  		BeforeEach(func() {
    64  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    65  		})
    66  
    67  		It("fails with usage when not provided exactly one arg", func() {
    68  			runCommand()
    69  			Expect(ui.Outputs()).To(ContainSubstrings(
    70  				[]string{"Incorrect Usage", "Requires an argument"},
    71  			))
    72  		})
    73  
    74  		Context("when the service exists", func() {
    75  			Context("and the service deletion is asynchronous", func() {
    76  				BeforeEach(func() {
    77  					serviceInstance = models.ServiceInstance{}
    78  					serviceInstance.Name = "my-service"
    79  					serviceInstance.GUID = "my-service-guid"
    80  					serviceInstance.LastOperation.Type = "delete"
    81  					serviceInstance.LastOperation.State = "in progress"
    82  					serviceInstance.LastOperation.Description = "delete"
    83  					serviceRepo.FindInstanceByNameReturns(serviceInstance, nil)
    84  				})
    85  
    86  				Context("when the command is confirmed", func() {
    87  					It("deletes the service", func() {
    88  						runCommand("my-service")
    89  
    90  						Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service my-service"}))
    91  
    92  						Expect(ui.Outputs()).To(ContainSubstrings(
    93  							[]string{"Deleting service", "my-service", "my-org", "my-space", "my-user"},
    94  							[]string{"OK"},
    95  							[]string{"Delete in progress. Use 'cf services' or 'cf service my-service' to check operation status."},
    96  						))
    97  
    98  						Expect(serviceRepo.DeleteServiceArgsForCall(0)).To(Equal(serviceInstance))
    99  					})
   100  				})
   101  
   102  				It("skips confirmation when the -f flag is given", func() {
   103  					runCommand("-f", "foo.com")
   104  
   105  					Expect(ui.Prompts).To(BeEmpty())
   106  					Expect(ui.Outputs()).To(ContainSubstrings(
   107  						[]string{"Deleting service", "foo.com"},
   108  						[]string{"OK"},
   109  						[]string{"Delete in progress. Use 'cf services' or 'cf service foo.com' to check operation status."},
   110  					))
   111  				})
   112  			})
   113  
   114  			Context("and the service deletion is synchronous", func() {
   115  				BeforeEach(func() {
   116  					serviceInstance = models.ServiceInstance{}
   117  					serviceInstance.Name = "my-service"
   118  					serviceInstance.GUID = "my-service-guid"
   119  					serviceRepo.FindInstanceByNameReturns(serviceInstance, nil)
   120  				})
   121  
   122  				Context("when the command is confirmed", func() {
   123  					It("deletes the service", func() {
   124  						runCommand("my-service")
   125  
   126  						Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service my-service"}))
   127  
   128  						Expect(ui.Outputs()).To(ContainSubstrings(
   129  							[]string{"Deleting service", "my-service", "my-org", "my-space", "my-user"},
   130  							[]string{"OK"},
   131  						))
   132  
   133  						Expect(serviceRepo.DeleteServiceArgsForCall(0)).To(Equal(serviceInstance))
   134  					})
   135  				})
   136  
   137  				It("skips confirmation when the -f flag is given", func() {
   138  					runCommand("-f", "foo.com")
   139  
   140  					Expect(ui.Prompts).To(BeEmpty())
   141  					Expect(ui.Outputs()).To(ContainSubstrings(
   142  						[]string{"Deleting service", "foo.com"},
   143  						[]string{"OK"},
   144  					))
   145  				})
   146  			})
   147  		})
   148  
   149  		Context("when the service does not exist", func() {
   150  			BeforeEach(func() {
   151  				serviceRepo.FindInstanceByNameReturns(models.ServiceInstance{}, errors.NewModelNotFoundError("Service instance", "my-service"))
   152  			})
   153  
   154  			It("warns the user the service does not exist", func() {
   155  				runCommand("-f", "my-service")
   156  
   157  				Expect(ui.Outputs()).To(ContainSubstrings(
   158  					[]string{"Deleting service", "my-service"},
   159  					[]string{"OK"},
   160  				))
   161  
   162  				Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"my-service", "does not exist"}))
   163  			})
   164  		})
   165  	})
   166  })