github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/v3apps_test.go (about) 1 package commands_test 2 3 import ( 4 "errors" 5 6 "github.com/cloudfoundry/cli/cf/commandregistry" 7 "github.com/cloudfoundry/cli/cf/commands" 8 "github.com/cloudfoundry/cli/cf/configuration/coreconfig" 9 "github.com/cloudfoundry/cli/cf/flags" 10 "github.com/cloudfoundry/cli/cf/requirements" 11 "github.com/cloudfoundry/cli/cf/requirements/requirementsfakes" 12 "github.com/cloudfoundry/cli/cf/terminal" 13 14 "github.com/cloudfoundry/cli/cf/api/apifakes" 15 "github.com/cloudfoundry/cli/cf/v3/models" 16 "github.com/cloudfoundry/cli/cf/v3/repository/repositoryfakes" 17 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 18 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 19 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 20 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 ) 24 25 var _ = Describe("V3Apps", func() { 26 var ( 27 ui *testterm.FakeUI 28 routeRepo *apifakes.FakeRouteRepository 29 configRepo coreconfig.Repository 30 repository *repositoryfakes.FakeRepository 31 32 cmd commandregistry.Command 33 deps commandregistry.Dependency 34 factory *requirementsfakes.FakeFactory 35 flagContext flags.FlagContext 36 37 loginRequirement requirements.Requirement 38 targetedSpaceRequirement requirements.Requirement 39 ) 40 41 BeforeEach(func() { 42 ui = &testterm.FakeUI{} 43 44 routeRepo = new(apifakes.FakeRouteRepository) 45 repoLocator := deps.RepoLocator.SetRouteRepository(routeRepo) 46 repository = new(repositoryfakes.FakeRepository) 47 repoLocator = repoLocator.SetV3Repository(repository) 48 49 configRepo = testconfig.NewRepositoryWithDefaults() 50 deps = commandregistry.Dependency{ 51 UI: ui, 52 Config: configRepo, 53 RepoLocator: repoLocator, 54 } 55 56 cmd = &commands.V3Apps{} 57 cmd.SetDependency(deps, false) 58 59 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 60 61 factory = new(requirementsfakes.FakeFactory) 62 63 loginRequirement = &passingRequirement{} 64 factory.NewLoginRequirementReturns(loginRequirement) 65 66 targetedSpaceRequirement = &passingRequirement{} 67 factory.NewTargetedSpaceRequirementReturns(targetedSpaceRequirement) 68 }) 69 70 Describe("Requirements", func() { 71 It("returns a LoginRequirement", func() { 72 actualRequirements, err := cmd.Requirements(factory, flagContext) 73 Expect(err).NotTo(HaveOccurred()) 74 Expect(factory.NewLoginRequirementCallCount()).To(Equal(1)) 75 76 Expect(actualRequirements).To(ContainElement(loginRequirement)) 77 }) 78 79 It("returns a TargetedSpaceRequirement", func() { 80 actualRequirements, err := cmd.Requirements(factory, flagContext) 81 Expect(err).NotTo(HaveOccurred()) 82 Expect(factory.NewTargetedSpaceRequirementCallCount()).To(Equal(1)) 83 84 Expect(actualRequirements).To(ContainElement(targetedSpaceRequirement)) 85 }) 86 87 It("should fail with usage", func() { 88 flagContext.Parse("blahblah") 89 90 reqs, err := cmd.Requirements(factory, flagContext) 91 Expect(err).NotTo(HaveOccurred()) 92 93 err = testcmd.RunRequirements(reqs) 94 Expect(err).To(HaveOccurred()) 95 Expect(err.Error()).To(ContainSubstring("Incorrect Usage")) 96 Expect(err.Error()).To(ContainSubstring("No argument required")) 97 }) 98 }) 99 100 Describe("Execute", func() { 101 var runCLIErr error 102 103 BeforeEach(func() { 104 cmd.Requirements(factory, flagContext) 105 repository.GetProcessesReturns([]models.V3Process{{Type: "web"}, {Type: "web"}}, nil) 106 repository.GetRoutesReturns([]models.V3Route{{}, {}}, nil) 107 }) 108 109 JustBeforeEach(func() { 110 runCLIErr = cmd.Execute(flagContext) 111 }) 112 113 It("attemps to get applications for the targeted space", func() { 114 Expect(runCLIErr).NotTo(HaveOccurred()) 115 Expect(repository.GetApplicationsCallCount()).To(Equal(1)) 116 }) 117 118 Context("when getting the applications succeeds", func() { 119 BeforeEach(func() { 120 repository.GetApplicationsReturns([]models.V3Application{ 121 { 122 Name: "app-1-name", 123 DesiredState: "STOPPED", 124 TotalDesiredInstances: 1, 125 Links: models.Links{ 126 Processes: models.Link{ 127 Href: "/v3/apps/app-1-guid/processes", 128 }, 129 Routes: models.Link{ 130 Href: "/v3/apps/app-1-guid/routes", 131 }, 132 }, 133 }, 134 { 135 Name: "app-2-name", 136 DesiredState: "RUNNING", 137 TotalDesiredInstances: 2, 138 Links: models.Links{ 139 Processes: models.Link{ 140 Href: "/v3/apps/app-2-guid/processes", 141 }, 142 Routes: models.Link{ 143 Href: "/v3/apps/app-2-guid/routes", 144 }, 145 }, 146 }, 147 }, nil) 148 }) 149 150 It("tries to get the processes for each application", func() { 151 Expect(runCLIErr).NotTo(HaveOccurred()) 152 Expect(repository.GetProcessesCallCount()).To(Equal(2)) 153 calls := make([]string, repository.GetProcessesCallCount()) 154 for i := range calls { 155 calls[i] = repository.GetProcessesArgsForCall(i) 156 } 157 Expect(calls).To(ContainElement("/v3/apps/app-1-guid/processes")) 158 Expect(calls).To(ContainElement("/v3/apps/app-2-guid/processes")) 159 }) 160 161 Context("when getting all processes succeeds", func() { 162 BeforeEach(func() { 163 repository.GetProcessesStub = func(path string) ([]models.V3Process, error) { 164 if repository.GetProcessesCallCount() == 1 { 165 return []models.V3Process{ 166 { 167 Type: "web", 168 Instances: 1, 169 MemoryInMB: 1024, 170 DiskInMB: 2048, 171 }, 172 }, nil 173 } 174 175 return []models.V3Process{ 176 { 177 Type: "web", 178 Instances: 2, 179 MemoryInMB: 512, 180 DiskInMB: 1024, 181 }, 182 }, nil 183 } 184 }) 185 186 It("tries to get the routes for each application", func() { 187 Expect(runCLIErr).NotTo(HaveOccurred()) 188 Expect(repository.GetRoutesCallCount()).To(Equal(2)) 189 calls := make([]string, repository.GetRoutesCallCount()) 190 for i := range calls { 191 calls[i] = repository.GetRoutesArgsForCall(i) 192 } 193 Expect(calls).To(ContainElement("/v3/apps/app-1-guid/routes")) 194 Expect(calls).To(ContainElement("/v3/apps/app-2-guid/routes")) 195 }) 196 197 Context("when getting the routes succeeds", func() { 198 BeforeEach(func() { 199 repository.GetRoutesStub = func(path string) ([]models.V3Route, error) { 200 if repository.GetRoutesCallCount() == 1 { 201 return []models.V3Route{ 202 { 203 Host: "route-1-host", 204 Path: "/route-1-path", 205 }, 206 { 207 Host: "route-1-host-2", 208 Path: "", 209 }, 210 }, nil 211 } 212 213 return []models.V3Route{ 214 { 215 Host: "route-2-host", 216 Path: "", 217 }, 218 }, nil 219 } 220 }) 221 222 It("prints a table of the results", func() { 223 Expect(runCLIErr).NotTo(HaveOccurred()) 224 outputs := make([]string, len(ui.Outputs())) 225 for i := range ui.Outputs() { 226 outputs[i] = terminal.Decolorize(ui.Outputs()[i]) 227 } 228 Expect(outputs).To(ConsistOf( 229 MatchRegexp(`name.*requested state.*instances.*memory.*disk.*urls`), 230 MatchRegexp("app-1-name.*stopped.*1.*1G.*2G.*route-1-host/route-1-path, route-1-host-2"), 231 MatchRegexp("app-2-name.*running.*2.*512M.*1G.*route-2-host"), 232 )) 233 }) 234 }) 235 236 Context("when getting the routes fails", func() { 237 BeforeEach(func() { 238 repository.GetRoutesReturns([]models.V3Route{}, errors.New("get-routes-err")) 239 }) 240 241 It("fails with error", func() { 242 Expect(runCLIErr).To(HaveOccurred()) 243 Expect(runCLIErr.Error()).To(Equal("get-routes-err")) 244 }) 245 }) 246 }) 247 248 Context("when getting any process fails", func() { 249 BeforeEach(func() { 250 repository.GetProcessesStub = func(path string) ([]models.V3Process, error) { 251 if repository.GetProcessesCallCount() == 0 { 252 return []models.V3Process{ 253 { 254 Type: "web", 255 Instances: 1, 256 MemoryInMB: 1024, 257 DiskInMB: 1024, 258 }, 259 }, nil 260 } 261 262 return []models.V3Process{}, errors.New("get-processes-err") 263 } 264 }) 265 266 It("fails with error", func() { 267 Expect(runCLIErr).To(HaveOccurred()) 268 Expect(runCLIErr.Error()).To(Equal("get-processes-err")) 269 }) 270 }) 271 }) 272 273 Context("when getting the applications fails", func() { 274 BeforeEach(func() { 275 repository.GetApplicationsReturns([]models.V3Application{}, errors.New("get-applications-err")) 276 }) 277 278 It("fails with error", func() { 279 Expect(runCLIErr).To(HaveOccurred()) 280 Expect(runCLIErr.Error()).To(Equal("get-applications-err")) 281 }) 282 }) 283 }) 284 })