From 2fcc6145b41a3bb25b0b827d3f062b300654bbaa Mon Sep 17 00:00:00 2001 From: Rein Krul Date: Thu, 11 Feb 2021 06:49:44 +0100 Subject: [PATCH] f --- cmd/root.go | 2 +- cmd/root_test.go | 27 ++++++++++++++++++++++++++- core/echo.go | 4 ++-- core/echo_test.go | 12 ++++++------ core/server_config.go | 22 +++++++++++----------- 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index e9c9e63c7c..daeba248b6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -98,7 +98,7 @@ func startServer(system *core.System) error { } // init HTTP interfaces and routes - echoServer := core.NewMultiEcho(system.EchoCreator, system.Config.HTTP.HttpConfig) + echoServer := core.NewMultiEcho(system.EchoCreator, system.Config.HTTP.HTTPConfig) for httpGroup, httpConfig := range system.Config.HTTP.AltBinds { logrus.Infof("Binding /%s -> %s", httpGroup, httpConfig.Address) if err := echoServer.Bind(httpGroup, httpConfig); err != nil { diff --git a/cmd/root_test.go b/cmd/root_test.go index 0339511fad..887cbb7111 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "errors" "github.com/labstack/echo/v4" "os" "testing" @@ -80,7 +81,7 @@ func Test_serverCmd(t *testing.T) { } system.Config = core.NewServerConfig() system.Config.Datadir = io.TestDirectory(t) - system.Config.HTTP.AltBinds["internal"] = core.HttpConfig{Address: "localhost:7642"} + system.Config.HTTP.AltBinds["internal"] = core.HTTPConfig{Address: "localhost:7642"} err := startServer(system) if !assert.NoError(t, err) { return @@ -89,6 +90,30 @@ func Test_serverCmd(t *testing.T) { assert.Equal(t, system.Config.HTTP.Address, echoServers[0].address) assert.Equal(t, "localhost:7642", echoServers[1].address) }) + t.Run("unable to configure system", func(t *testing.T) { + system := core.NewSystem() + system.Config = core.NewServerConfig() + system.Config.Datadir = "root_test.go" + err := startServer(system) + assert.Error(t, err, "unable to start") + }) + t.Run("alt binds error", func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + echoServer := core.NewMockEchoServer(ctrl) + echoServer.EXPECT().Start(gomock.Any()).Return(errors.New("unable to start")) + + system := core.NewSystem() + system.EchoCreator = func() core.EchoServer { + return echoServer + } + system.Config = core.NewServerConfig() + system.Config.Datadir = io.TestDirectory(t) + system.Config.HTTP.AltBinds["internal"] = core.HTTPConfig{Address: "localhost:7642"} + err := startServer(system) + assert.EqualError(t, err, "unable to start") + }) } func Test_CreateSystem(t *testing.T) { diff --git a/core/echo.go b/core/echo.go index 938acf0b51..c8bef3039d 100644 --- a/core/echo.go +++ b/core/echo.go @@ -29,7 +29,7 @@ const defaultEchoGroup = "" // NewMultiEcho creates a new MultiEcho which uses the given function to create EchoServers. If a route is registered // for an unknown group is is bound to the given defaultInterface. -func NewMultiEcho(creatorFn func() EchoServer, defaultInterface HttpConfig) *MultiEcho { +func NewMultiEcho(creatorFn func() EchoServer, defaultInterface HTTPConfig) *MultiEcho { instance := &MultiEcho{ interfaces: map[string]EchoServer{}, groups: map[string]string{}, @@ -48,7 +48,7 @@ type MultiEcho struct { // Bind binds the given group (first part of the URL) to the given HTTP interface. Calling Bind for the same group twice // results in an error being returned. -func (c *MultiEcho) Bind(group string, interfaceConfig HttpConfig) error { +func (c *MultiEcho) Bind(group string, interfaceConfig HTTPConfig) error { normGroup := strings.ToLower(group) if _, groupExists := c.groups[normGroup]; groupExists { return fmt.Errorf("http bind group already exists: %s", group) diff --git a/core/echo_test.go b/core/echo_test.go index 3044b8402d..6c2f6856a4 100644 --- a/core/echo_test.go +++ b/core/echo_test.go @@ -11,7 +11,7 @@ func Test_MultiEcho_Bind(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - cfg := NewServerConfig().HTTP.HttpConfig + cfg := NewServerConfig().HTTP.HTTPConfig m := NewMultiEcho(func() EchoServer { return NewMockEchoServer(ctrl) }, cfg) @@ -38,7 +38,7 @@ func Test_MultiEcho_Routes(t *testing.T) { m := NewMultiEcho(func() EchoServer { return s - }, NewServerConfig().HTTP.HttpConfig) + }, NewServerConfig().HTTP.HTTPConfig) m.PATCH("patch", nil) m.GET("get", nil) m.OPTIONS("options", nil) @@ -56,7 +56,7 @@ func Test_MultiEcho(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - defaultHttpCfg := NewServerConfig().HTTP.HttpConfig + defaultHttpCfg := NewServerConfig().HTTP.HTTPConfig // Set up expected echo servers defaultServer := NewMockEchoServer(ctrl) @@ -82,15 +82,15 @@ func Test_MultiEcho(t *testing.T) { // Bind interfaces m := NewMultiEcho(createFn, defaultHttpCfg) - err := m.Bind("internal", HttpConfig{Address: "internal:8080"}) + err := m.Bind("internal", HTTPConfig{Address: "internal:8080"}) if !assert.NoError(t, err) { return } - err = m.Bind("public", HttpConfig{Address: "public:8080"}) + err = m.Bind("public", HTTPConfig{Address: "public:8080"}) if !assert.NoError(t, err) { return } - err = m.Bind("extra-public", HttpConfig{Address: "public:8080"}) + err = m.Bind("extra-public", HTTPConfig{Address: "public:8080"}) if !assert.NoError(t, err) { return } diff --git a/core/server_config.go b/core/server_config.go index c77c66cc48..e7da3f87e3 100644 --- a/core/server_config.go +++ b/core/server_config.go @@ -50,22 +50,22 @@ type ServerConfig struct { Verbosity string `koanf:"verbosity"` Strictmode bool `koanf:"strictmode"` Datadir string `koanf:"datadir"` - HTTP GlobalHttpConfig `koanf:"http"` + HTTP GlobalHTTPConfig `koanf:"http"` configMap *koanf.Koanf } -// GlobalHttpConfig is the top-level config struct for HTTP interfaces. -type GlobalHttpConfig struct { - // HttpConfig contains the config for the default HTTP interface. - HttpConfig `koanf:"default"` +// GlobalHTTPConfig is the top-level config struct for HTTP interfaces. +type GlobalHTTPConfig struct { + // HTTPConfig contains the config for the default HTTP interface. + HTTPConfig `koanf:"default"` // AltBinds contains binds for alternative HTTP interfaces. The key of the map is the first part of the path // of the URL (e.g. `/internal/some-api` -> `internal`), the value is the HTTP interface it must be bound to. - AltBinds map[string]HttpConfig `koanf:"alt"` + AltBinds map[string]HTTPConfig `koanf:"alt"` } -// HttpConfig contains configuration for an HTTP interface, e.g. address. +// HTTPConfig contains configuration for an HTTP interface, e.g. address. // It will probably contain security related properties in the future (TLS configuration, user/pwd requirements). -type HttpConfig struct { +type HTTPConfig struct { // Address holds the interface address the HTTP service must be bound to, in the format of `host:port` (e.g. localhost:5555). Address string `koanf:"address"` } @@ -77,9 +77,9 @@ func NewServerConfig() *ServerConfig { Verbosity: defaultLogLevel, Strictmode: defaultStrictMode, Datadir: defaultDatadir, - HTTP: GlobalHttpConfig{ - HttpConfig: HttpConfig{Address: defaultAddress}, - AltBinds: map[string]HttpConfig{}, + HTTP: GlobalHTTPConfig{ + HTTPConfig: HTTPConfig{Address: defaultAddress}, + AltBinds: map[string]HTTPConfig{}, }, } }