github.com/cloudfoundry/cli@v7.1.0+incompatible/plugin/rpc/cli_rpc_server_test.go (about) 1 package rpc_test 2 3 import ( 4 "errors" 5 "net" 6 "net/rpc" 7 "os" 8 "time" 9 10 "code.cloudfoundry.org/cli/cf/api" 11 "code.cloudfoundry.org/cli/cf/api/authentication/authenticationfakes" 12 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 13 "code.cloudfoundry.org/cli/cf/models" 14 "code.cloudfoundry.org/cli/cf/terminal" 15 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 16 "code.cloudfoundry.org/cli/plugin" 17 plugin_models "code.cloudfoundry.org/cli/plugin/models" 18 . "code.cloudfoundry.org/cli/plugin/rpc" 19 cmdRunner "code.cloudfoundry.org/cli/plugin/rpc" 20 . "code.cloudfoundry.org/cli/plugin/rpc/fakecommand" 21 "code.cloudfoundry.org/cli/plugin/rpc/rpcfakes" 22 . "github.com/onsi/ginkgo" 23 . "github.com/onsi/gomega" 24 ) 25 26 var _ = Describe("Server", func() { 27 28 _ = FakeCommand1{} //make sure fake_command is imported and self-registered with init() 29 30 var ( 31 err error 32 client *rpc.Client 33 rpcService *CliRpcService 34 ) 35 36 AfterEach(func() { 37 if client != nil { 38 client.Close() 39 } 40 }) 41 42 BeforeEach(func() { 43 rpc.DefaultServer = rpc.NewServer() 44 }) 45 46 Describe(".NewRpcService", func() { 47 BeforeEach(func() { 48 rpcService, err = NewRpcService(nil, nil, nil, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 49 Expect(err).ToNot(HaveOccurred()) 50 }) 51 52 It("returns an err of another Rpc process is already registered", func() { 53 _, err := NewRpcService(nil, nil, nil, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 54 Expect(err).To(HaveOccurred()) 55 }) 56 }) 57 58 Describe(".Stop", func() { 59 BeforeEach(func() { 60 rpcService, err = NewRpcService(nil, nil, nil, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 61 Expect(err).ToNot(HaveOccurred()) 62 63 err := rpcService.Start() 64 Expect(err).ToNot(HaveOccurred()) 65 66 pingCli(rpcService.Port()) 67 }) 68 69 It("shuts down the rpc server", func() { 70 rpcService.Stop() 71 72 //give time for server to stop 73 time.Sleep(50 * time.Millisecond) 74 75 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 76 Expect(err).To(HaveOccurred()) 77 }) 78 }) 79 80 Describe(".Start", func() { 81 BeforeEach(func() { 82 rpcService, err = NewRpcService(nil, nil, nil, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 83 Expect(err).ToNot(HaveOccurred()) 84 85 err := rpcService.Start() 86 Expect(err).ToNot(HaveOccurred()) 87 88 pingCli(rpcService.Port()) 89 }) 90 91 AfterEach(func() { 92 rpcService.Stop() 93 94 //give time for server to stop 95 time.Sleep(50 * time.Millisecond) 96 }) 97 98 It("Start an Rpc server for communication", func() { 99 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 100 Expect(err).ToNot(HaveOccurred()) 101 }) 102 }) 103 104 // Describe(".IsMinCliVersion()", func() { 105 // BeforeEach(func() { 106 // rpcService, err = NewRpcService(nil, nil, nil, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 107 // Expect(err).ToNot(HaveOccurred()) 108 109 // err := rpcService.Start() 110 // Expect(err).ToNot(HaveOccurred()) 111 112 // pingCli(rpcService.Port()) 113 114 // client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 115 // Expect(err).ToNot(HaveOccurred()) 116 // }) 117 118 // AfterEach(func() { 119 // rpcService.Stop() 120 121 // //give time for server to stop 122 // time.Sleep(50 * time.Millisecond) 123 // }) 124 125 // It("returns true if cli version is greater than the required version", func() { 126 // version.BinaryVersion = "1.2.3+abc123" 127 128 // var result bool 129 // err = client.Call("CliRpcCmd.IsMinCliVersion", "1.2.2", &result) 130 // Expect(err).ToNot(HaveOccurred()) 131 132 // Expect(result).To(BeTrue()) 133 // }) 134 135 // It("returns true if cli version is equal to the required version", func() { 136 // version.BinaryVersion = "1.2.3+abc123" 137 138 // var result bool 139 // err = client.Call("CliRpcCmd.IsMinCliVersion", "1.2.3", &result) 140 // Expect(err).ToNot(HaveOccurred()) 141 142 // Expect(result).To(BeTrue()) 143 // }) 144 145 // It("returns false if cli version is less than the required version", func() { 146 // version.BinaryVersion = "1.2.3+abc123" 147 148 // var result bool 149 // err = client.Call("CliRpcCmd.IsMinCliVersion", "1.2.4", &result) 150 // Expect(err).ToNot(HaveOccurred()) 151 152 // Expect(result).To(BeFalse()) 153 // }) 154 155 // It("returns true if cli version is 'BUILT_FROM_SOURCE'", func() { 156 // version.BinaryVersion = "BUILT_FROM_SOURCE" 157 158 // var result bool 159 // err = client.Call("CliRpcCmd.IsMinCliVersion", "12.0.6", &result) 160 // Expect(err).ToNot(HaveOccurred()) 161 162 // Expect(result).To(BeTrue()) 163 // }) 164 // }) 165 166 Describe(".SetPluginMetadata", func() { 167 var ( 168 metadata *plugin.PluginMetadata 169 ) 170 171 BeforeEach(func() { 172 rpcService, err = NewRpcService(nil, nil, nil, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 173 Expect(err).ToNot(HaveOccurred()) 174 175 err := rpcService.Start() 176 Expect(err).ToNot(HaveOccurred()) 177 178 pingCli(rpcService.Port()) 179 180 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 181 Expect(err).ToNot(HaveOccurred()) 182 183 metadata = &plugin.PluginMetadata{ 184 Name: "foo", 185 Commands: []plugin.Command{ 186 {Name: "cmd_1", HelpText: "cm 1 help text"}, 187 {Name: "cmd_2", HelpText: "cmd 2 help text"}, 188 }, 189 } 190 }) 191 192 AfterEach(func() { 193 rpcService.Stop() 194 195 //give time for server to stop 196 time.Sleep(50 * time.Millisecond) 197 }) 198 199 It("set the rpc command's Return Data", func() { 200 var success bool 201 err = client.Call("CliRpcCmd.SetPluginMetadata", metadata, &success) 202 203 Expect(err).ToNot(HaveOccurred()) 204 Expect(success).To(BeTrue()) 205 Expect(rpcService.RpcCmd.PluginMetadata).To(Equal(metadata)) 206 }) 207 }) 208 209 Describe(".GetOutputAndReset", func() { 210 Context("success", func() { 211 BeforeEach(func() { 212 outputCapture := terminal.NewTeePrinter(os.Stdout) 213 rpcService, err = NewRpcService(outputCapture, nil, nil, api.RepositoryLocator{}, cmdRunner.NewCommandRunner(), nil, nil, rpc.DefaultServer) 214 Expect(err).ToNot(HaveOccurred()) 215 216 err := rpcService.Start() 217 Expect(err).ToNot(HaveOccurred()) 218 219 pingCli(rpcService.Port()) 220 }) 221 222 AfterEach(func() { 223 rpcService.Stop() 224 225 //give time for server to stop 226 time.Sleep(50 * time.Millisecond) 227 }) 228 229 It("should return the logs from the output capture", func() { 230 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 231 Expect(err).ToNot(HaveOccurred()) 232 233 success := false 234 235 oldStd := os.Stdout 236 os.Stdout = nil 237 client.Call("CliRpcCmd.CallCoreCommand", []string{"fake-command"}, &success) 238 Expect(success).To(BeTrue()) 239 os.Stdout = oldStd 240 241 var output []string 242 client.Call("CliRpcCmd.GetOutputAndReset", false, &output) 243 244 Expect(output).To(Equal([]string{"Requirement executed", "Command Executed"})) 245 }) 246 }) 247 }) 248 249 Describe("disabling terminal output", func() { 250 var terminalOutputSwitch *rpcfakes.FakeTerminalOutputSwitch 251 252 BeforeEach(func() { 253 terminalOutputSwitch = new(rpcfakes.FakeTerminalOutputSwitch) 254 rpcService, err = NewRpcService(nil, terminalOutputSwitch, nil, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 255 Expect(err).ToNot(HaveOccurred()) 256 257 err := rpcService.Start() 258 Expect(err).ToNot(HaveOccurred()) 259 260 pingCli(rpcService.Port()) 261 }) 262 263 It("should disable the terminal output switch", func() { 264 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 265 Expect(err).ToNot(HaveOccurred()) 266 267 var success bool 268 err = client.Call("CliRpcCmd.DisableTerminalOutput", true, &success) 269 270 Expect(err).ToNot(HaveOccurred()) 271 Expect(success).To(BeTrue()) 272 Expect(terminalOutputSwitch.DisableTerminalOutputCallCount()).To(Equal(1)) 273 Expect(terminalOutputSwitch.DisableTerminalOutputArgsForCall(0)).To(Equal(true)) 274 }) 275 }) 276 277 Describe("Plugin API", func() { 278 279 var runner *rpcfakes.FakeCommandRunner 280 281 BeforeEach(func() { 282 outputCapture := terminal.NewTeePrinter(os.Stdout) 283 terminalOutputSwitch := terminal.NewTeePrinter(os.Stdout) 284 285 runner = new(rpcfakes.FakeCommandRunner) 286 rpcService, err = NewRpcService(outputCapture, terminalOutputSwitch, nil, api.RepositoryLocator{}, runner, nil, nil, rpc.DefaultServer) 287 Expect(err).ToNot(HaveOccurred()) 288 289 err := rpcService.Start() 290 Expect(err).ToNot(HaveOccurred()) 291 292 pingCli(rpcService.Port()) 293 294 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 295 Expect(err).ToNot(HaveOccurred()) 296 }) 297 298 AfterEach(func() { 299 rpcService.Stop() 300 301 //give time for server to stop 302 time.Sleep(50 * time.Millisecond) 303 }) 304 305 It("calls GetApp() with 'app' as argument", func() { 306 result := plugin_models.GetAppModel{} 307 err = client.Call("CliRpcCmd.GetApp", "fake-app", &result) 308 309 Expect(err).ToNot(HaveOccurred()) 310 Expect(runner.CommandCallCount()).To(Equal(1)) 311 arg1, _, pluginApiCall := runner.CommandArgsForCall(0) 312 Expect(arg1[0]).To(Equal("app")) 313 Expect(arg1[1]).To(Equal("fake-app")) 314 Expect(pluginApiCall).To(BeTrue()) 315 }) 316 317 It("calls GetOrg() with 'my-org' as argument", func() { 318 result := plugin_models.GetOrg_Model{} 319 err = client.Call("CliRpcCmd.GetOrg", "my-org", &result) 320 321 Expect(err).ToNot(HaveOccurred()) 322 Expect(runner.CommandCallCount()).To(Equal(1)) 323 arg1, _, pluginApiCall := runner.CommandArgsForCall(0) 324 Expect(arg1[0]).To(Equal("org")) 325 Expect(arg1[1]).To(Equal("my-org")) 326 Expect(pluginApiCall).To(BeTrue()) 327 }) 328 329 It("calls GetSpace() with 'my-space' as argument", func() { 330 result := plugin_models.GetSpace_Model{} 331 err = client.Call("CliRpcCmd.GetSpace", "my-space", &result) 332 333 Expect(err).ToNot(HaveOccurred()) 334 Expect(runner.CommandCallCount()).To(Equal(1)) 335 arg1, _, pluginApiCall := runner.CommandArgsForCall(0) 336 Expect(arg1[0]).To(Equal("space")) 337 Expect(arg1[1]).To(Equal("my-space")) 338 Expect(pluginApiCall).To(BeTrue()) 339 }) 340 341 It("calls GetApps() ", func() { 342 result := []plugin_models.GetAppsModel{} 343 err = client.Call("CliRpcCmd.GetApps", "", &result) 344 345 Expect(err).ToNot(HaveOccurred()) 346 Expect(runner.CommandCallCount()).To(Equal(1)) 347 arg1, _, pluginApiCall := runner.CommandArgsForCall(0) 348 Expect(arg1[0]).To(Equal("apps")) 349 Expect(pluginApiCall).To(BeTrue()) 350 }) 351 352 It("calls GetOrgs() ", func() { 353 result := []plugin_models.GetOrgs_Model{} 354 err = client.Call("CliRpcCmd.GetOrgs", "", &result) 355 356 Expect(err).ToNot(HaveOccurred()) 357 Expect(runner.CommandCallCount()).To(Equal(1)) 358 arg1, _, pluginApiCall := runner.CommandArgsForCall(0) 359 Expect(arg1[0]).To(Equal("orgs")) 360 Expect(pluginApiCall).To(BeTrue()) 361 }) 362 363 It("calls GetServices() ", func() { 364 result := []plugin_models.GetServices_Model{} 365 err = client.Call("CliRpcCmd.GetServices", "", &result) 366 367 Expect(err).ToNot(HaveOccurred()) 368 Expect(runner.CommandCallCount()).To(Equal(1)) 369 arg1, _, pluginApiCall := runner.CommandArgsForCall(0) 370 Expect(arg1[0]).To(Equal("services")) 371 Expect(pluginApiCall).To(BeTrue()) 372 }) 373 374 It("calls GetSpaces() ", func() { 375 result := []plugin_models.GetSpaces_Model{} 376 err = client.Call("CliRpcCmd.GetSpaces", "", &result) 377 378 Expect(err).ToNot(HaveOccurred()) 379 Expect(runner.CommandCallCount()).To(Equal(1)) 380 arg1, _, pluginApiCall := runner.CommandArgsForCall(0) 381 Expect(arg1[0]).To(Equal("spaces")) 382 Expect(pluginApiCall).To(BeTrue()) 383 }) 384 385 It("calls GetOrgUsers() ", func() { 386 result := []plugin_models.GetOrgUsers_Model{} 387 args := []string{"orgName1", "-a"} 388 err = client.Call("CliRpcCmd.GetOrgUsers", args, &result) 389 390 Expect(err).ToNot(HaveOccurred()) 391 Expect(runner.CommandCallCount()).To(Equal(1)) 392 arg1, _, pluginApiCall := runner.CommandArgsForCall(0) 393 Expect(arg1[0]).To(Equal("org-users")) 394 Expect(pluginApiCall).To(BeTrue()) 395 }) 396 397 It("calls GetSpaceUsers() ", func() { 398 result := []plugin_models.GetSpaceUsers_Model{} 399 args := []string{"orgName1", "spaceName1"} 400 err = client.Call("CliRpcCmd.GetSpaceUsers", args, &result) 401 402 Expect(err).ToNot(HaveOccurred()) 403 Expect(runner.CommandCallCount()).To(Equal(1)) 404 arg1, _, pluginApiCall := runner.CommandArgsForCall(0) 405 Expect(arg1[0]).To(Equal("space-users")) 406 Expect(pluginApiCall).To(BeTrue()) 407 }) 408 409 It("calls GetService() with 'serviceInstance' as argument", func() { 410 result := plugin_models.GetService_Model{} 411 err = client.Call("CliRpcCmd.GetService", "fake-service-instance", &result) 412 413 Expect(err).ToNot(HaveOccurred()) 414 Expect(runner.CommandCallCount()).To(Equal(1)) 415 arg1, _, pluginApiCall := runner.CommandArgsForCall(0) 416 Expect(arg1[0]).To(Equal("service")) 417 Expect(arg1[1]).To(Equal("fake-service-instance")) 418 Expect(pluginApiCall).To(BeTrue()) 419 }) 420 421 }) 422 423 Describe(".CallCoreCommand", func() { 424 var runner *rpcfakes.FakeCommandRunner 425 426 Context("success", func() { 427 BeforeEach(func() { 428 429 outputCapture := terminal.NewTeePrinter(os.Stdout) 430 runner = new(rpcfakes.FakeCommandRunner) 431 432 rpcService, err = NewRpcService(outputCapture, nil, nil, api.RepositoryLocator{}, runner, nil, nil, rpc.DefaultServer) 433 Expect(err).ToNot(HaveOccurred()) 434 435 err := rpcService.Start() 436 Expect(err).ToNot(HaveOccurred()) 437 438 pingCli(rpcService.Port()) 439 }) 440 441 AfterEach(func() { 442 rpcService.Stop() 443 444 //give time for server to stop 445 time.Sleep(50 * time.Millisecond) 446 }) 447 448 It("is able to call a command", func() { 449 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 450 Expect(err).ToNot(HaveOccurred()) 451 452 var success bool 453 err = client.Call("CliRpcCmd.CallCoreCommand", []string{"fake-command3"}, &success) 454 455 Expect(err).ToNot(HaveOccurred()) 456 Expect(runner.CommandCallCount()).To(Equal(1)) 457 458 _, _, pluginApiCall := runner.CommandArgsForCall(0) 459 Expect(pluginApiCall).To(BeFalse()) 460 }) 461 }) 462 463 Describe("CLI Config object methods", func() { 464 var ( 465 config coreconfig.Repository 466 ) 467 468 BeforeEach(func() { 469 config = testconfig.NewRepositoryWithDefaults() 470 }) 471 472 AfterEach(func() { 473 rpcService.Stop() 474 475 //give time for server to stop 476 time.Sleep(50 * time.Millisecond) 477 }) 478 479 Context(".GetCurrentOrg", func() { 480 BeforeEach(func() { 481 config.SetOrganizationFields(models.OrganizationFields{ 482 GUID: "test-guid", 483 Name: "test-org", 484 QuotaDefinition: models.QuotaFields{ 485 GUID: "guid123", 486 Name: "quota123", 487 MemoryLimit: 128, 488 InstanceMemoryLimit: 16, 489 RoutesLimit: 5, 490 ServicesLimit: 6, 491 NonBasicServicesAllowed: true, 492 }, 493 }) 494 495 rpcService, err = NewRpcService(nil, nil, config, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 496 err := rpcService.Start() 497 Expect(err).ToNot(HaveOccurred()) 498 499 pingCli(rpcService.Port()) 500 }) 501 502 It("populates the plugin Organization object with the current org settings in config", func() { 503 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 504 Expect(err).ToNot(HaveOccurred()) 505 506 var org plugin_models.Organization 507 err = client.Call("CliRpcCmd.GetCurrentOrg", "", &org) 508 509 Expect(err).ToNot(HaveOccurred()) 510 Expect(org.Name).To(Equal("test-org")) 511 Expect(org.Guid).To(Equal("test-guid")) 512 }) 513 }) 514 515 Context(".GetCurrentSpace", func() { 516 BeforeEach(func() { 517 config.SetSpaceFields(models.SpaceFields{ 518 GUID: "space-guid", 519 Name: "space-name", 520 }) 521 522 rpcService, err = NewRpcService(nil, nil, config, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 523 err := rpcService.Start() 524 Expect(err).ToNot(HaveOccurred()) 525 526 pingCli(rpcService.Port()) 527 }) 528 529 It("populates the plugin Space object with the current space settings in config", func() { 530 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 531 Expect(err).ToNot(HaveOccurred()) 532 533 var space plugin_models.Space 534 err = client.Call("CliRpcCmd.GetCurrentSpace", "", &space) 535 536 Expect(err).ToNot(HaveOccurred()) 537 Expect(space.Name).To(Equal("space-name")) 538 Expect(space.Guid).To(Equal("space-guid")) 539 }) 540 }) 541 542 Context(".Username, .UserGuid, .UserEmail", func() { 543 BeforeEach(func() { 544 rpcService, err = NewRpcService(nil, nil, config, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 545 err := rpcService.Start() 546 Expect(err).ToNot(HaveOccurred()) 547 548 pingCli(rpcService.Port()) 549 }) 550 551 It("returns username, user guid and user email", func() { 552 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 553 Expect(err).ToNot(HaveOccurred()) 554 555 var result string 556 err = client.Call("CliRpcCmd.Username", "", &result) 557 Expect(err).ToNot(HaveOccurred()) 558 Expect(result).To(Equal("my-user")) 559 560 err = client.Call("CliRpcCmd.UserGuid", "", &result) 561 Expect(err).ToNot(HaveOccurred()) 562 Expect(result).To(Equal("my-user-guid")) 563 564 err = client.Call("CliRpcCmd.UserEmail", "", &result) 565 Expect(err).ToNot(HaveOccurred()) 566 Expect(result).To(Equal("my-user-email")) 567 }) 568 }) 569 570 Context(".IsSSLDisabled", func() { 571 BeforeEach(func() { 572 rpcService, err = NewRpcService(nil, nil, config, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 573 err := rpcService.Start() 574 Expect(err).ToNot(HaveOccurred()) 575 576 pingCli(rpcService.Port()) 577 }) 578 579 It("returns the IsSSLDisabled setting in config", func() { 580 config.SetSSLDisabled(true) 581 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 582 Expect(err).ToNot(HaveOccurred()) 583 584 var result bool 585 err = client.Call("CliRpcCmd.IsSSLDisabled", "", &result) 586 Expect(err).ToNot(HaveOccurred()) 587 Expect(result).To(BeTrue()) 588 }) 589 }) 590 591 Context(".IsLoggedIn", func() { 592 BeforeEach(func() { 593 rpcService, err = NewRpcService(nil, nil, config, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 594 err := rpcService.Start() 595 Expect(err).ToNot(HaveOccurred()) 596 597 pingCli(rpcService.Port()) 598 }) 599 600 It("returns the IsLoggedIn setting in config", func() { 601 config.SetAccessToken("Logged-In-Token") 602 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 603 Expect(err).ToNot(HaveOccurred()) 604 605 var result bool 606 err = client.Call("CliRpcCmd.IsLoggedIn", "", &result) 607 Expect(err).ToNot(HaveOccurred()) 608 Expect(result).To(BeTrue()) 609 }) 610 }) 611 612 Context(".HasOrganization and .HasSpace ", func() { 613 BeforeEach(func() { 614 rpcService, err = NewRpcService(nil, nil, config, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 615 err := rpcService.Start() 616 Expect(err).ToNot(HaveOccurred()) 617 618 pingCli(rpcService.Port()) 619 }) 620 621 It("returns the HasOrganization() and HasSpace() setting in config", func() { 622 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 623 Expect(err).ToNot(HaveOccurred()) 624 625 var result bool 626 err = client.Call("CliRpcCmd.HasOrganization", "", &result) 627 Expect(err).ToNot(HaveOccurred()) 628 Expect(result).To(BeTrue()) 629 630 err = client.Call("CliRpcCmd.HasSpace", "", &result) 631 Expect(err).ToNot(HaveOccurred()) 632 Expect(result).To(BeTrue()) 633 }) 634 }) 635 636 Context(".LoggregatorEndpoint and .DopplerEndpoint ", func() { 637 BeforeEach(func() { 638 rpcService, err = NewRpcService(nil, nil, config, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 639 err := rpcService.Start() 640 Expect(err).ToNot(HaveOccurred()) 641 642 pingCli(rpcService.Port()) 643 }) 644 645 It("returns the LoggregatorEndpoint() and DopplerEndpoint() setting in config", func() { 646 config.SetDopplerEndpoint("doppler-endpoint-sample") 647 648 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 649 Expect(err).ToNot(HaveOccurred()) 650 651 var result string 652 err = client.Call("CliRpcCmd.LoggregatorEndpoint", "", &result) 653 Expect(err).ToNot(HaveOccurred()) 654 Expect(result).To(Equal("")) 655 656 err = client.Call("CliRpcCmd.DopplerEndpoint", "", &result) 657 Expect(err).ToNot(HaveOccurred()) 658 Expect(result).To(Equal("doppler-endpoint-sample")) 659 }) 660 }) 661 662 Context(".ApiEndpoint, .ApiVersion and .HasAPIEndpoint", func() { 663 BeforeEach(func() { 664 rpcService, err = NewRpcService(nil, nil, config, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) 665 err := rpcService.Start() 666 Expect(err).ToNot(HaveOccurred()) 667 668 pingCli(rpcService.Port()) 669 }) 670 671 It("returns the ApiEndpoint(), ApiVersion() and HasAPIEndpoint() setting in config", func() { 672 config.SetAPIVersion("v1.1.1") 673 config.SetAPIEndpoint("www.fake-domain.com") 674 675 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 676 Expect(err).ToNot(HaveOccurred()) 677 678 var result string 679 err = client.Call("CliRpcCmd.ApiEndpoint", "", &result) 680 Expect(err).ToNot(HaveOccurred()) 681 Expect(result).To(Equal("www.fake-domain.com")) 682 683 err = client.Call("CliRpcCmd.ApiVersion", "", &result) 684 Expect(err).ToNot(HaveOccurred()) 685 Expect(result).To(Equal("v1.1.1")) 686 687 var exists bool 688 err = client.Call("CliRpcCmd.HasAPIEndpoint", "", &exists) 689 Expect(err).ToNot(HaveOccurred()) 690 Expect(exists).To(BeTrue()) 691 692 }) 693 }) 694 695 Context(".AccessToken", func() { 696 var authRepo *authenticationfakes.FakeRepository 697 698 BeforeEach(func() { 699 authRepo = new(authenticationfakes.FakeRepository) 700 locator := api.RepositoryLocator{} 701 locator = locator.SetAuthenticationRepository(authRepo) 702 703 rpcService, err = NewRpcService(nil, nil, config, locator, nil, nil, nil, rpc.DefaultServer) 704 err := rpcService.Start() 705 Expect(err).ToNot(HaveOccurred()) 706 707 pingCli(rpcService.Port()) 708 }) 709 710 It("refreshes the token", func() { 711 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 712 Expect(err).ToNot(HaveOccurred()) 713 714 var result string 715 err = client.Call("CliRpcCmd.AccessToken", "", &result) 716 Expect(err).ToNot(HaveOccurred()) 717 718 Expect(authRepo.RefreshAuthTokenCallCount()).To(Equal(1)) 719 }) 720 721 It("returns the access token", func() { 722 authRepo.RefreshAuthTokenReturns("fake-access-token", nil) 723 724 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 725 Expect(err).ToNot(HaveOccurred()) 726 727 var result string 728 err = client.Call("CliRpcCmd.AccessToken", "", &result) 729 Expect(err).ToNot(HaveOccurred()) 730 Expect(result).To(Equal("fake-access-token")) 731 }) 732 733 It("returns the error from refreshing the access token", func() { 734 authRepo.RefreshAuthTokenReturns("", errors.New("refresh error")) 735 736 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 737 Expect(err).ToNot(HaveOccurred()) 738 739 var result string 740 err = client.Call("CliRpcCmd.AccessToken", "", &result) 741 Expect(err.Error()).To(Equal("refresh error")) 742 }) 743 }) 744 745 }) 746 747 Context("fail", func() { 748 BeforeEach(func() { 749 outputCapture := terminal.NewTeePrinter(os.Stdout) 750 rpcService, err = NewRpcService(outputCapture, nil, nil, api.RepositoryLocator{}, cmdRunner.NewCommandRunner(), nil, nil, rpc.DefaultServer) 751 Expect(err).ToNot(HaveOccurred()) 752 753 err := rpcService.Start() 754 Expect(err).ToNot(HaveOccurred()) 755 756 pingCli(rpcService.Port()) 757 }) 758 759 It("returns false in success if the command cannot be found", func() { 760 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 761 Expect(err).ToNot(HaveOccurred()) 762 763 var success bool 764 err = client.Call("CliRpcCmd.CallCoreCommand", []string{"not_a_cmd"}, &success) 765 Expect(success).To(BeFalse()) 766 Expect(err).ToNot(HaveOccurred()) 767 }) 768 769 It("returns an error if a command cannot parse provided flags", func() { 770 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 771 Expect(err).ToNot(HaveOccurred()) 772 773 var success bool 774 err = client.Call("CliRpcCmd.CallCoreCommand", []string{"fake-command", "-invalid_flag"}, &success) 775 776 Expect(err).To(HaveOccurred()) 777 Expect(success).To(BeFalse()) 778 }) 779 780 It("recovers from a panic from any core command", func() { 781 client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) 782 Expect(err).ToNot(HaveOccurred()) 783 784 var success bool 785 err = client.Call("CliRpcCmd.CallCoreCommand", []string{"fake-command3"}, &success) 786 787 Expect(success).To(BeFalse()) 788 }) 789 }) 790 }) 791 }) 792 793 func pingCli(port string) { 794 var connErr error 795 var conn net.Conn 796 for i := 0; i < 5; i++ { 797 conn, connErr = net.Dial("tcp", "127.0.0.1:"+port) 798 if connErr != nil { 799 time.Sleep(200 * time.Millisecond) 800 } else { 801 conn.Close() 802 break 803 } 804 } 805 Expect(connErr).ToNot(HaveOccurred()) 806 }