github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/service/services_test.go (about) 1 package service_test 2 3 import ( 4 "os" 5 6 "code.cloudfoundry.org/cli/cf/api/apifakes" 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/flags" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 11 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 12 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 13 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 14 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 15 "code.cloudfoundry.org/cli/plugin/models" 16 17 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 18 "code.cloudfoundry.org/cli/cf/models" 19 20 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 21 22 "code.cloudfoundry.org/cli/cf/commands/service" 23 . "github.com/onsi/ginkgo" 24 . "github.com/onsi/gomega" 25 ) 26 27 var _ = Describe("services", func() { 28 var ( 29 ui *testterm.FakeUI 30 configRepo coreconfig.Repository 31 requirementsFactory *requirementsfakes.FakeFactory 32 serviceSummaryRepo *apifakes.OldFakeServiceSummaryRepo 33 deps commandregistry.Dependency 34 ) 35 36 updateCommandDependency := func(pluginCall bool) { 37 deps.UI = ui 38 deps.Config = configRepo 39 deps.RepoLocator = deps.RepoLocator.SetServiceSummaryRepository(serviceSummaryRepo) 40 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("services").SetDependency(deps, pluginCall)) 41 } 42 43 runCommand := func(args ...string) bool { 44 return testcmd.RunCLICommand("services", args, requirementsFactory, updateCommandDependency, false, ui) 45 } 46 47 BeforeEach(func() { 48 ui = &testterm.FakeUI{} 49 configRepo = testconfig.NewRepositoryWithDefaults() 50 serviceSummaryRepo = new(apifakes.OldFakeServiceSummaryRepo) 51 targetedOrgRequirement := new(requirementsfakes.FakeTargetedOrgRequirement) 52 requirementsFactory = new(requirementsfakes.FakeFactory) 53 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 54 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) 55 requirementsFactory.NewTargetedOrgRequirementReturns(targetedOrgRequirement) 56 57 deps = commandregistry.NewDependency(os.Stdout, new(tracefakes.FakePrinter), "") 58 }) 59 60 Describe("services requirements", func() { 61 62 Context("when not logged in", func() { 63 BeforeEach(func() { 64 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 65 }) 66 67 It("fails requirements", func() { 68 Expect(runCommand()).To(BeFalse()) 69 }) 70 }) 71 72 Context("when no space is targeted", func() { 73 BeforeEach(func() { 74 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"}) 75 }) 76 77 It("fails requirements", func() { 78 Expect(runCommand()).To(BeFalse()) 79 }) 80 }) 81 82 Context("when arguments are provided", func() { 83 var cmd commandregistry.Command 84 var flagContext flags.FlagContext 85 86 BeforeEach(func() { 87 cmd = &service.ListServices{} 88 cmd.SetDependency(deps, false) 89 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 90 }) 91 92 It("should fail with usage", func() { 93 flagContext.Parse("blahblah") 94 95 reqs, err := cmd.Requirements(requirementsFactory, flagContext) 96 Expect(err).NotTo(HaveOccurred()) 97 98 err = testcmd.RunRequirements(reqs) 99 Expect(err).To(HaveOccurred()) 100 Expect(err.Error()).To(ContainSubstring("Incorrect Usage")) 101 Expect(err.Error()).To(ContainSubstring("No argument required")) 102 }) 103 }) 104 }) 105 106 It("lists available services", func() { 107 plan := models.ServicePlanFields{ 108 GUID: "spark-guid", 109 Name: "spark", 110 } 111 112 plan2 := models.ServicePlanFields{ 113 GUID: "spark-guid-2", 114 Name: "spark-2", 115 } 116 117 offering := models.ServiceOfferingFields{Label: "cleardb"} 118 119 serviceInstance := models.ServiceInstance{} 120 serviceInstance.Name = "my-service-1" 121 serviceInstance.LastOperation.Type = "create" 122 serviceInstance.LastOperation.State = "in progress" 123 serviceInstance.LastOperation.Description = "fake state description" 124 serviceInstance.ServicePlan = plan 125 serviceInstance.ApplicationNames = []string{"cli1", "cli2"} 126 serviceInstance.ServiceOffering = offering 127 128 serviceInstance2 := models.ServiceInstance{} 129 serviceInstance2.Name = "my-service-2" 130 serviceInstance2.LastOperation.Type = "create" 131 serviceInstance2.LastOperation.State = "" 132 serviceInstance2.LastOperation.Description = "fake state description" 133 serviceInstance2.ServicePlan = plan2 134 serviceInstance2.ApplicationNames = []string{"cli1"} 135 serviceInstance2.ServiceOffering = offering 136 137 userProvidedServiceInstance := models.ServiceInstance{} 138 userProvidedServiceInstance.Name = "my-service-provided-by-user" 139 userProvidedServiceInstance.Type = "user_provided_service_instance" 140 141 serviceInstances := []models.ServiceInstance{serviceInstance, serviceInstance2, userProvidedServiceInstance} 142 143 serviceSummaryRepo.GetSummariesInCurrentSpaceInstances = serviceInstances 144 145 runCommand() 146 Expect(ui.Outputs()).To(ContainSubstrings( 147 []string{"Getting services in org", "my-org", "my-space", "my-user"}, 148 []string{"name", "service", "plan", "bound apps", "last operation"}, 149 []string{"OK"}, 150 []string{"my-service-1", "cleardb", "spark", "cli1, cli2", "create in progress"}, 151 []string{"my-service-2", "cleardb", "spark-2", "cli1", ""}, 152 []string{"my-service-provided-by-user", "user-provided", "", "", ""}, 153 )) 154 }) 155 156 It("lists no services when none are found", func() { 157 serviceInstances := []models.ServiceInstance{} 158 serviceSummaryRepo.GetSummariesInCurrentSpaceInstances = serviceInstances 159 160 runCommand() 161 162 Expect(ui.Outputs()).To(ContainSubstrings( 163 []string{"Getting services in org", "my-org", "my-space", "my-user"}, 164 []string{"OK"}, 165 []string{"No services found"}, 166 )) 167 168 Expect(ui.Outputs()).ToNot(ContainSubstrings( 169 []string{"name", "service", "plan", "bound apps"}, 170 )) 171 }) 172 173 Describe("when invoked by a plugin", func() { 174 175 var ( 176 pluginModels []plugin_models.GetServices_Model 177 ) 178 179 BeforeEach(func() { 180 181 pluginModels = []plugin_models.GetServices_Model{} 182 deps.PluginModels.Services = &pluginModels 183 plan := models.ServicePlanFields{ 184 GUID: "spark-guid", 185 Name: "spark", 186 } 187 188 plan2 := models.ServicePlanFields{ 189 GUID: "spark-guid-2", 190 Name: "spark-2", 191 } 192 193 offering := models.ServiceOfferingFields{Label: "cleardb"} 194 195 serviceInstance := models.ServiceInstance{} 196 serviceInstance.Name = "my-service-1" 197 serviceInstance.GUID = "123" 198 serviceInstance.LastOperation.Type = "create" 199 serviceInstance.LastOperation.State = "in progress" 200 serviceInstance.LastOperation.Description = "fake state description" 201 serviceInstance.ServicePlan = plan 202 serviceInstance.ApplicationNames = []string{"cli1", "cli2"} 203 serviceInstance.ServiceOffering = offering 204 205 serviceInstance2 := models.ServiceInstance{} 206 serviceInstance2.Name = "my-service-2" 207 serviceInstance2.GUID = "345" 208 serviceInstance2.LastOperation.Type = "create" 209 serviceInstance2.LastOperation.State = "" 210 serviceInstance2.LastOperation.Description = "fake state description" 211 serviceInstance2.ServicePlan = plan2 212 serviceInstance2.ApplicationNames = []string{"cli1"} 213 serviceInstance2.ServiceOffering = offering 214 215 userProvidedServiceInstance := models.ServiceInstance{} 216 userProvidedServiceInstance.Name = "my-service-provided-by-user" 217 userProvidedServiceInstance.GUID = "678" 218 userProvidedServiceInstance.Type = "user_provided_service_instance" 219 220 serviceInstances := []models.ServiceInstance{serviceInstance, serviceInstance2, userProvidedServiceInstance} 221 222 serviceSummaryRepo.GetSummariesInCurrentSpaceInstances = serviceInstances 223 }) 224 225 It("populates the plugin model", func() { 226 testcmd.RunCLICommand("services", []string{}, requirementsFactory, updateCommandDependency, true, ui) 227 228 Expect(len(pluginModels)).To(Equal(3)) 229 Expect(pluginModels[0].Name).To(Equal("my-service-1")) 230 Expect(pluginModels[0].Guid).To(Equal("123")) 231 Expect(pluginModels[0].ServicePlan.Name).To(Equal("spark")) 232 Expect(pluginModels[0].ServicePlan.Guid).To(Equal("spark-guid")) 233 Expect(pluginModels[0].Service.Name).To(Equal("cleardb")) 234 Expect(pluginModels[0].ApplicationNames).To(Equal([]string{"cli1", "cli2"})) 235 Expect(pluginModels[0].LastOperation.Type).To(Equal("create")) 236 Expect(pluginModels[0].LastOperation.State).To(Equal("in progress")) 237 Expect(pluginModels[0].IsUserProvided).To(BeFalse()) 238 239 Expect(pluginModels[1].Name).To(Equal("my-service-2")) 240 Expect(pluginModels[1].Guid).To(Equal("345")) 241 Expect(pluginModels[1].ServicePlan.Name).To(Equal("spark-2")) 242 Expect(pluginModels[1].ServicePlan.Guid).To(Equal("spark-guid-2")) 243 Expect(pluginModels[1].Service.Name).To(Equal("cleardb")) 244 Expect(pluginModels[1].ApplicationNames).To(Equal([]string{"cli1"})) 245 Expect(pluginModels[1].LastOperation.Type).To(Equal("create")) 246 Expect(pluginModels[1].LastOperation.State).To(Equal("")) 247 Expect(pluginModels[1].IsUserProvided).To(BeFalse()) 248 249 Expect(pluginModels[2].Name).To(Equal("my-service-provided-by-user")) 250 Expect(pluginModels[2].Guid).To(Equal("678")) 251 Expect(pluginModels[2].ServicePlan.Name).To(Equal("")) 252 Expect(pluginModels[2].ServicePlan.Guid).To(Equal("")) 253 Expect(pluginModels[2].Service.Name).To(Equal("")) 254 Expect(pluginModels[2].ApplicationNames).To(BeNil()) 255 Expect(pluginModels[2].LastOperation.Type).To(Equal("")) 256 Expect(pluginModels[2].LastOperation.State).To(Equal("")) 257 Expect(pluginModels[2].IsUserProvided).To(BeTrue()) 258 259 }) 260 261 }) 262 })