github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/service/delete_service_test.go (about) 1 package service_test 2 3 import ( 4 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 5 "github.com/cloudfoundry/cli/cf/command_registry" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 "github.com/cloudfoundry/cli/cf/models" 8 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 9 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 10 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 11 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 15 . "github.com/cloudfoundry/cli/testhelpers/matchers" 16 ) 17 18 var _ = Describe("delete-service command", func() { 19 var ( 20 ui *testterm.FakeUI 21 requirementsFactory *testreq.FakeReqFactory 22 serviceRepo *testapi.FakeServiceRepo 23 serviceInstance models.ServiceInstance 24 configRepo core_config.Repository 25 deps command_registry.Dependency 26 ) 27 28 updateCommandDependency := func(pluginCall bool) { 29 deps.Ui = ui 30 deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo) 31 deps.Config = configRepo 32 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("delete-service").SetDependency(deps, pluginCall)) 33 } 34 35 BeforeEach(func() { 36 ui = &testterm.FakeUI{ 37 Inputs: []string{"yes"}, 38 } 39 40 configRepo = testconfig.NewRepositoryWithDefaults() 41 serviceRepo = &testapi.FakeServiceRepo{} 42 requirementsFactory = &testreq.FakeReqFactory{ 43 LoginSuccess: true, 44 } 45 }) 46 47 runCommand := func(args ...string) bool { 48 return testcmd.RunCliCommand("delete-service", args, requirementsFactory, updateCommandDependency, false) 49 } 50 51 Context("when not logged in", func() { 52 BeforeEach(func() { 53 requirementsFactory.LoginSuccess = false 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.LoginSuccess = true 64 }) 65 66 It("fails with usage when not provided exactly one arg", func() { 67 runCommand() 68 Expect(ui.Outputs).To(ContainSubstrings( 69 []string{"Incorrect Usage", "Requires an argument"}, 70 )) 71 }) 72 73 Context("when the service exists", func() { 74 Context("and the service deletion is asynchronous", func() { 75 BeforeEach(func() { 76 serviceInstance = models.ServiceInstance{} 77 serviceInstance.Name = "my-service" 78 serviceInstance.Guid = "my-service-guid" 79 serviceInstance.LastOperation.Type = "delete" 80 serviceInstance.LastOperation.State = "in progress" 81 serviceInstance.LastOperation.Description = "delete" 82 serviceRepo.FindInstanceByNameServiceInstance = serviceInstance 83 }) 84 85 Context("when the command is confirmed", func() { 86 It("deletes the service", func() { 87 runCommand("my-service") 88 89 Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service my-service"})) 90 91 Expect(ui.Outputs).To(ContainSubstrings( 92 []string{"Deleting service", "my-service", "my-org", "my-space", "my-user"}, 93 []string{"OK"}, 94 []string{"Delete in progress. Use 'cf services' or 'cf service my-service' to check operation status."}, 95 )) 96 97 Expect(serviceRepo.DeleteServiceServiceInstance).To(Equal(serviceInstance)) 98 }) 99 }) 100 101 It("skips confirmation when the -f flag is given", func() { 102 runCommand("-f", "foo.com") 103 104 Expect(ui.Prompts).To(BeEmpty()) 105 Expect(ui.Outputs).To(ContainSubstrings( 106 []string{"Deleting service", "foo.com"}, 107 []string{"OK"}, 108 []string{"Delete in progress. Use 'cf services' or 'cf service foo.com' to check operation status."}, 109 )) 110 }) 111 }) 112 113 Context("and the service deletion is synchronous", func() { 114 BeforeEach(func() { 115 serviceInstance = models.ServiceInstance{} 116 serviceInstance.Name = "my-service" 117 serviceInstance.Guid = "my-service-guid" 118 serviceRepo.FindInstanceByNameServiceInstance = serviceInstance 119 }) 120 121 Context("when the command is confirmed", func() { 122 It("deletes the service", func() { 123 runCommand("my-service") 124 125 Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service my-service"})) 126 127 Expect(ui.Outputs).To(ContainSubstrings( 128 []string{"Deleting service", "my-service", "my-org", "my-space", "my-user"}, 129 []string{"OK"}, 130 )) 131 132 Expect(serviceRepo.DeleteServiceServiceInstance).To(Equal(serviceInstance)) 133 }) 134 }) 135 136 It("skips confirmation when the -f flag is given", func() { 137 runCommand("-f", "foo.com") 138 139 Expect(ui.Prompts).To(BeEmpty()) 140 Expect(ui.Outputs).To(ContainSubstrings( 141 []string{"Deleting service", "foo.com"}, 142 []string{"OK"}, 143 )) 144 }) 145 }) 146 }) 147 148 Context("when the service does not exist", func() { 149 BeforeEach(func() { 150 serviceRepo.FindInstanceByNameNotFound = true 151 }) 152 153 It("warns the user the service does not exist", func() { 154 runCommand("-f", "my-service") 155 156 Expect(ui.Outputs).To(ContainSubstrings( 157 []string{"Deleting service", "my-service"}, 158 []string{"OK"}, 159 )) 160 161 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"my-service", "does not exist"})) 162 }) 163 }) 164 }) 165 })