github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/plugin/rpc/cli_rpc_server_test.go (about)

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