github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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 "code.cloudfoundry.org/cli/plugin/models" 13 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 14 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 15 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 16 17 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 18 "code.cloudfoundry.org/cli/cf/models" 19 20 . "code.cloudfoundry.org/cli/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 140 serviceInstances := []models.ServiceInstance{serviceInstance, serviceInstance2, userProvidedServiceInstance} 141 142 serviceSummaryRepo.GetSummariesInCurrentSpaceInstances = serviceInstances 143 144 runCommand() 145 Expect(ui.Outputs()).To(ContainSubstrings( 146 []string{"Getting services in org", "my-org", "my-space", "my-user"}, 147 []string{"name", "service", "plan", "bound apps", "last operation"}, 148 []string{"OK"}, 149 []string{"my-service-1", "cleardb", "spark", "cli1, cli2", "create in progress"}, 150 []string{"my-service-2", "cleardb", "spark-2", "cli1", ""}, 151 []string{"my-service-provided-by-user", "user-provided", "", "", ""}, 152 )) 153 }) 154 155 It("lists no services when none are found", func() { 156 serviceInstances := []models.ServiceInstance{} 157 serviceSummaryRepo.GetSummariesInCurrentSpaceInstances = serviceInstances 158 159 runCommand() 160 161 Expect(ui.Outputs()).To(ContainSubstrings( 162 []string{"Getting services in org", "my-org", "my-space", "my-user"}, 163 []string{"OK"}, 164 []string{"No services found"}, 165 )) 166 167 Expect(ui.Outputs()).ToNot(ContainSubstrings( 168 []string{"name", "service", "plan", "bound apps"}, 169 )) 170 }) 171 172 Describe("when invoked by a plugin", func() { 173 174 var ( 175 pluginModels []plugin_models.GetServices_Model 176 ) 177 178 BeforeEach(func() { 179 180 pluginModels = []plugin_models.GetServices_Model{} 181 deps.PluginModels.Services = &pluginModels 182 plan := models.ServicePlanFields{ 183 GUID: "spark-guid", 184 Name: "spark", 185 } 186 187 plan2 := models.ServicePlanFields{ 188 GUID: "spark-guid-2", 189 Name: "spark-2", 190 } 191 192 offering := models.ServiceOfferingFields{Label: "cleardb"} 193 194 serviceInstance := models.ServiceInstance{} 195 serviceInstance.Name = "my-service-1" 196 serviceInstance.GUID = "123" 197 serviceInstance.LastOperation.Type = "create" 198 serviceInstance.LastOperation.State = "in progress" 199 serviceInstance.LastOperation.Description = "fake state description" 200 serviceInstance.ServicePlan = plan 201 serviceInstance.ApplicationNames = []string{"cli1", "cli2"} 202 serviceInstance.ServiceOffering = offering 203 204 serviceInstance2 := models.ServiceInstance{} 205 serviceInstance2.Name = "my-service-2" 206 serviceInstance2.GUID = "345" 207 serviceInstance2.LastOperation.Type = "create" 208 serviceInstance2.LastOperation.State = "" 209 serviceInstance2.LastOperation.Description = "fake state description" 210 serviceInstance2.ServicePlan = plan2 211 serviceInstance2.ApplicationNames = []string{"cli1"} 212 serviceInstance2.ServiceOffering = offering 213 214 userProvidedServiceInstance := models.ServiceInstance{} 215 userProvidedServiceInstance.Name = "my-service-provided-by-user" 216 userProvidedServiceInstance.GUID = "678" 217 218 serviceInstances := []models.ServiceInstance{serviceInstance, serviceInstance2, userProvidedServiceInstance} 219 220 serviceSummaryRepo.GetSummariesInCurrentSpaceInstances = serviceInstances 221 }) 222 223 It("populates the plugin model", func() { 224 testcmd.RunCLICommand("services", []string{}, requirementsFactory, updateCommandDependency, true, ui) 225 226 Expect(len(pluginModels)).To(Equal(3)) 227 Expect(pluginModels[0].Name).To(Equal("my-service-1")) 228 Expect(pluginModels[0].Guid).To(Equal("123")) 229 Expect(pluginModels[0].ServicePlan.Name).To(Equal("spark")) 230 Expect(pluginModels[0].ServicePlan.Guid).To(Equal("spark-guid")) 231 Expect(pluginModels[0].Service.Name).To(Equal("cleardb")) 232 Expect(pluginModels[0].ApplicationNames).To(Equal([]string{"cli1", "cli2"})) 233 Expect(pluginModels[0].LastOperation.Type).To(Equal("create")) 234 Expect(pluginModels[0].LastOperation.State).To(Equal("in progress")) 235 Expect(pluginModels[0].IsUserProvided).To(BeFalse()) 236 237 Expect(pluginModels[1].Name).To(Equal("my-service-2")) 238 Expect(pluginModels[1].Guid).To(Equal("345")) 239 Expect(pluginModels[1].ServicePlan.Name).To(Equal("spark-2")) 240 Expect(pluginModels[1].ServicePlan.Guid).To(Equal("spark-guid-2")) 241 Expect(pluginModels[1].Service.Name).To(Equal("cleardb")) 242 Expect(pluginModels[1].ApplicationNames).To(Equal([]string{"cli1"})) 243 Expect(pluginModels[1].LastOperation.Type).To(Equal("create")) 244 Expect(pluginModels[1].LastOperation.State).To(Equal("")) 245 Expect(pluginModels[1].IsUserProvided).To(BeFalse()) 246 247 Expect(pluginModels[2].Name).To(Equal("my-service-provided-by-user")) 248 Expect(pluginModels[2].Guid).To(Equal("678")) 249 Expect(pluginModels[2].ServicePlan.Name).To(Equal("")) 250 Expect(pluginModels[2].ServicePlan.Guid).To(Equal("")) 251 Expect(pluginModels[2].Service.Name).To(Equal("")) 252 Expect(pluginModels[2].ApplicationNames).To(BeNil()) 253 Expect(pluginModels[2].LastOperation.Type).To(Equal("")) 254 Expect(pluginModels[2].LastOperation.State).To(Equal("")) 255 Expect(pluginModels[2].IsUserProvided).To(BeTrue()) 256 257 }) 258 259 }) 260 })