From b362a71c317861ee7bbaf5086f736ab59e463df4 Mon Sep 17 00:00:00 2001 From: Joseph Phillips Date: Wed, 26 Jun 2024 15:50:45 +0200 Subject: [PATCH] chore: adds test for crosscontroller info. --- .../crosscontroller/crosscontroller_test.go | 44 +++++++++++++++++++ .../controller/crosscontroller/register.go | 13 +++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/apiserver/facades/controller/crosscontroller/crosscontroller_test.go b/apiserver/facades/controller/crosscontroller/crosscontroller_test.go index aef990cd458a..fc6def88ec6c 100644 --- a/apiserver/facades/controller/crosscontroller/crosscontroller_test.go +++ b/apiserver/facades/controller/crosscontroller/crosscontroller_test.go @@ -11,6 +11,8 @@ import ( gc "gopkg.in/check.v1" "github.com/juju/juju/apiserver/common" + "github.com/juju/juju/controller" + "github.com/juju/juju/core/network" "github.com/juju/juju/rpc/params" "github.com/juju/juju/state" ) @@ -112,3 +114,45 @@ func (s *CrossControllerSuite) TestWatchControllerInfoError(c *gc.C) { }) c.Assert(s.resources.Get("1"), gc.IsNil) } + +type stubControllerInfoGetter struct{} + +func (stubControllerInfoGetter) APIHostPortsForClients() ([]network.SpaceHostPorts, error) { + return []network.SpaceHostPorts{{ + network.SpaceHostPort{ + SpaceAddress: network.SpaceAddress{ + MachineAddress: network.MachineAddress{ + Value: "10.1.2.3", + Scope: network.ScopeCloudLocal, + }, + SpaceID: "0", + }, + NetPort: 50000, + }, + network.SpaceHostPort{ + SpaceAddress: network.SpaceAddress{ + MachineAddress: network.MachineAddress{ + Value: "host-name", + Scope: network.ScopePublic, + }, + SpaceID: "0", + }, + NetPort: 50000, + }, + }}, nil +} + +func (stubControllerInfoGetter) ControllerConfig() (controller.Config, error) { + return map[string]interface{}{ + "ca-cert": "ca-cert", + }, nil +} + +func (s *CrossControllerSuite) TestGetControllerInfo(c *gc.C) { + addrs, cert, err := controllerInfo(stubControllerInfoGetter{}) + c.Assert(err, jc.ErrorIsNil) + + // Public address is sorted first. + c.Check(addrs, jc.DeepEquals, []string{"host-name:50000", "10.1.2.3:50000"}) + c.Check(cert, gc.Equals, "ca-cert") +} diff --git a/apiserver/facades/controller/crosscontroller/register.go b/apiserver/facades/controller/crosscontroller/register.go index c46f094ff3db..f2883b24e07b 100644 --- a/apiserver/facades/controller/crosscontroller/register.go +++ b/apiserver/facades/controller/crosscontroller/register.go @@ -4,13 +4,13 @@ package crosscontroller import ( + jujucontroller "github.com/juju/juju/controller" "reflect" "github.com/juju/errors" "github.com/juju/juju/apiserver/facade" "github.com/juju/juju/core/network" - "github.com/juju/juju/state" ) // Register is called to expose a package of facades onto a given registry. @@ -40,7 +40,16 @@ func newStateCrossControllerAPI(ctx facade.Context) (*CrossControllerAPI, error) ) } -func controllerInfo(st *state.State) ([]string, string, error) { +// controllerInfoGetter indirects state for retrieving information +// required for cross-controller communication. +type controllerInfoGetter interface { + APIHostPortsForClients() ([]network.SpaceHostPorts, error) + ControllerConfig() (jujucontroller.Config, error) +} + +// controllerInfo retrieves information required to communicate +// with this controller - API addresses and the CA cert. +func controllerInfo(st controllerInfoGetter) ([]string, string, error) { apiHostPorts, err := st.APIHostPortsForClients() if err != nil { return nil, "", errors.Trace(err)