Browse Source

Remove LogOutput from Server

pull/8404/head
Daniel Nephin 4 years ago
parent
commit
5acf01ceeb
  1. 3
      agent/agent.go
  2. 6
      agent/consul/client_test.go
  3. 5
      agent/consul/config.go
  4. 50
      agent/consul/leader_test.go
  5. 37
      agent/consul/server.go
  6. 33
      agent/consul/server_test.go

3
agent/agent.go

@ -1517,9 +1517,6 @@ func (a *Agent) consulConfig() (*consul.Config, error) {
} }
} }
// Setup the loggers
base.LogOutput = a.LogOutput
// This will set up the LAN keyring, as well as the WAN and any segments // This will set up the LAN keyring, as well as the WAN and any segments
// for servers. // for servers.
if err := a.setupKeyrings(base); err != nil { if err := a.setupKeyrings(base); err != nil {

6
agent/consul/client_test.go

@ -438,7 +438,7 @@ func TestClient_RPC_TLS(t *testing.T) {
conf1.VerifyIncoming = true conf1.VerifyIncoming = true
conf1.VerifyOutgoing = true conf1.VerifyOutgoing = true
configureTLS(conf1) configureTLS(conf1)
s1, err := NewServer(conf1) s1, err := newServer(t, conf1)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -494,7 +494,7 @@ func newClient(t *testing.T, config *Config) (*Client, error) {
func TestClient_RPC_RateLimit(t *testing.T) { func TestClient_RPC_RateLimit(t *testing.T) {
t.Parallel() t.Parallel()
dir1, conf1 := testServerConfig(t) dir1, conf1 := testServerConfig(t)
s1, err := NewServer(conf1) s1, err := newServer(t, conf1)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -606,7 +606,7 @@ func TestClient_SnapshotRPC_TLS(t *testing.T) {
conf1.VerifyIncoming = true conf1.VerifyIncoming = true
conf1.VerifyOutgoing = true conf1.VerifyOutgoing = true
configureTLS(conf1) configureTLS(conf1)
s1, err := NewServer(conf1) s1, err := newServer(t, conf1)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }

5
agent/consul/config.go

@ -2,7 +2,6 @@ package consul
import ( import (
"fmt" "fmt"
"io"
"net" "net"
"os" "os"
"time" "time"
@ -161,10 +160,6 @@ type Config struct {
// leader election. // leader election.
ReconcileInterval time.Duration ReconcileInterval time.Duration
// LogOutput is the location to write logs to. If this is not set,
// logs will go to stderr.
LogOutput io.Writer
// ProtocolVersion is the protocol version to speak. This must be between // ProtocolVersion is the protocol version to speak. This must be between
// ProtocolVersionMin and ProtocolVersionMax. // ProtocolVersionMin and ProtocolVersionMax.
ProtocolVersion uint8 ProtocolVersion uint8

50
agent/consul/leader_test.go

@ -10,10 +10,13 @@ import (
"time" "time"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/agent/token"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/consul/sdk/testutil/retry" "github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/testrpc" "github.com/hashicorp/consul/testrpc"
"github.com/hashicorp/consul/tlsutil"
"github.com/hashicorp/go-hclog"
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc" msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -1283,24 +1286,39 @@ func TestLeader_ConfigEntryBootstrap_Fail(t *testing.T) {
} }
}() }()
dir1, s1 := testServerWithConfig(t, func(c *Config) { dir, config := testServerConfig(t)
c.LogOutput = io.MultiWriter(pw, testutil.NewLogBuffer(t)) defer os.RemoveAll(dir)
c.Build = "1.6.0" config.Build = "1.6.0"
c.ConfigEntryBootstrap = []structs.ConfigEntry{ config.ConfigEntryBootstrap = []structs.ConfigEntry{
&structs.ServiceSplitterConfigEntry{ &structs.ServiceSplitterConfigEntry{
Kind: structs.ServiceSplitter, Kind: structs.ServiceSplitter,
Name: "web", Name: "web",
Splits: []structs.ServiceSplit{ Splits: []structs.ServiceSplit{
{Weight: 100, Service: "web"}, {Weight: 100, Service: "web"},
},
}, },
} },
}) }
defer os.RemoveAll(dir1)
defer s1.Shutdown()
result := <-ch logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
require.Empty(t, result) Name: config.NodeName,
Level: hclog.Debug,
Output: io.MultiWriter(pw, testutil.NewLogBuffer(t)),
})
tlsConf, err := tlsutil.NewConfigurator(config.ToTLSUtilConfig(), logger)
require.NoError(t, err)
srv, err := NewServerWithOptions(config,
WithLogger(logger),
WithTokenStore(new(token.Store)),
WithTLSConfigurator(tlsConf))
require.NoError(t, err)
defer srv.Shutdown()
select {
case result := <-ch:
require.Empty(t, result)
case <-time.After(time.Second):
t.Fatal("timeout waiting for a result from tailing logs")
}
} }
func TestLeader_ACLLegacyReplication(t *testing.T) { func TestLeader_ACLLegacyReplication(t *testing.T) {

37
agent/consul/server.go

@ -313,25 +313,6 @@ type Server struct {
EnterpriseServer EnterpriseServer
} }
// NewServer is only used to help setting up a server for testing. Normal code
// exercises NewServerLogger.
func NewServer(config *Config) (*Server, error) {
c, err := tlsutil.NewConfigurator(config.ToTLSUtilConfig(), nil)
if err != nil {
return nil, err
}
return NewServerLogger(config, nil, new(token.Store), c)
}
// NewServerLogger is used to construct a new Consul server from the
// configuration, potentially returning an error
func NewServerLogger(config *Config, logger hclog.InterceptLogger, tokens *token.Store, tlsConfigurator *tlsutil.Configurator) (*Server, error) {
return NewServerWithOptions(config,
WithLogger(logger),
WithTokenStore(tokens),
WithTLSConfigurator(tlsConfigurator))
}
// NewServerWithOptions is used to construct a new Consul server from the configuration // NewServerWithOptions is used to construct a new Consul server from the configuration
// and extra options, potentially returning an error // and extra options, potentially returning an error
func NewServerWithOptions(config *Config, options ...ConsulOption) (*Server, error) { func NewServerWithOptions(config *Config, options ...ConsulOption) (*Server, error) {
@ -342,31 +323,17 @@ func NewServerWithOptions(config *Config, options ...ConsulOption) (*Server, err
tlsConfigurator := flat.tlsConfigurator tlsConfigurator := flat.tlsConfigurator
connPool := flat.connPool connPool := flat.connPool
// Check the protocol version.
if err := config.CheckProtocolVersion(); err != nil { if err := config.CheckProtocolVersion(); err != nil {
return nil, err return nil, err
} }
// Check for a data directory.
if config.DataDir == "" && !config.DevMode { if config.DataDir == "" && !config.DevMode {
return nil, fmt.Errorf("Config must provide a DataDir") return nil, fmt.Errorf("Config must provide a DataDir")
} }
// Sanity check the ACLs.
if err := config.CheckACL(); err != nil { if err := config.CheckACL(); err != nil {
return nil, err return nil, err
} }
// Ensure we have a log output and create a logger.
if config.LogOutput == nil {
config.LogOutput = os.Stderr
}
if logger == nil { if logger == nil {
logger = hclog.NewInterceptLogger(&hclog.LoggerOptions{ return nil, fmt.Errorf("logger is required")
Level: hclog.Debug,
Output: config.LogOutput,
})
} }
// Check if TLS is enabled // Check if TLS is enabled
@ -735,7 +702,7 @@ func (s *Server) setupRaft() error {
log = cacheStore log = cacheStore
// Create the snapshot store. // Create the snapshot store.
snapshots, err := raft.NewFileSnapshotStore(path, snapshotsRetained, s.config.LogOutput) snapshots, err := raft.NewFileSnapshotStoreWithLogger(path, snapshotsRetained, s.logger.Named("snapshot"))
if err != nil { if err != nil {
return err return err
} }

33
agent/consul/server_test.go

@ -146,7 +146,6 @@ func testServerConfig(t *testing.T) (string, *Config) {
config.Bootstrap = true config.Bootstrap = true
config.Datacenter = "dc1" config.Datacenter = "dc1"
config.DataDir = dir config.DataDir = dir
config.LogOutput = testutil.NewLogBuffer(t)
// bind the rpc server to a random port. config.RPCAdvertise will be // bind the rpc server to a random port. config.RPCAdvertise will be
// set to the listen address unless it was set in the configuration. // set to the listen address unless it was set in the configuration.
@ -258,18 +257,18 @@ func testServerDCExpectNonVoter(t *testing.T, dc string, expect int) (string, *S
func testServerWithConfig(t *testing.T, cb func(*Config)) (string, *Server) { func testServerWithConfig(t *testing.T, cb func(*Config)) (string, *Server) {
var dir string var dir string
var config *Config
var srv *Server var srv *Server
var err error
// Retry added to avoid cases where bind addr is already in use // Retry added to avoid cases where bind addr is already in use
retry.RunWith(retry.ThreeTimes(), t, func(r *retry.R) { retry.RunWith(retry.ThreeTimes(), t, func(r *retry.R) {
var config *Config
dir, config = testServerConfig(t) dir, config = testServerConfig(t)
if cb != nil { if cb != nil {
cb(config) cb(config)
} }
srv, err = newServer(config) var err error
srv, err = newServer(t, config)
if err != nil { if err != nil {
config.NotifyShutdown() config.NotifyShutdown()
os.RemoveAll(dir) os.RemoveAll(dir)
@ -295,7 +294,7 @@ func testACLServerWithConfig(t *testing.T, cb func(*Config), initReplicationToke
return dir, srv, codec return dir, srv, codec
} }
func newServer(c *Config) (*Server, error) { func newServer(t *testing.T, c *Config) (*Server, error) {
// chain server up notification // chain server up notification
oldNotify := c.NotifyListen oldNotify := c.NotifyListen
up := make(chan struct{}) up := make(chan struct{})
@ -306,21 +305,19 @@ func newServer(c *Config) (*Server, error) {
} }
} }
// start server
w := c.LogOutput
if w == nil {
w = os.Stderr
}
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{ logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: c.NodeName, Name: c.NodeName,
Level: hclog.Debug, Level: hclog.Debug,
Output: w, Output: testutil.NewLogBuffer(t),
}) })
tlsConf, err := tlsutil.NewConfigurator(c.ToTLSUtilConfig(), logger) tlsConf, err := tlsutil.NewConfigurator(c.ToTLSUtilConfig(), logger)
if err != nil { if err != nil {
return nil, err return nil, err
} }
srv, err := NewServerLogger(c, logger, new(token.Store), tlsConf) srv, err := NewServer(c,
WithLogger(logger),
WithTokenStore(new(token.Store)),
WithTLSConfigurator(tlsConf))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1088,7 +1085,7 @@ func TestServer_JoinLAN_TLS(t *testing.T) {
conf1.VerifyIncoming = true conf1.VerifyIncoming = true
conf1.VerifyOutgoing = true conf1.VerifyOutgoing = true
configureTLS(conf1) configureTLS(conf1)
s1, err := newServer(conf1) s1, err := newServer(t, conf1)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1101,7 +1098,7 @@ func TestServer_JoinLAN_TLS(t *testing.T) {
conf2.VerifyIncoming = true conf2.VerifyIncoming = true
conf2.VerifyOutgoing = true conf2.VerifyOutgoing = true
configureTLS(conf2) configureTLS(conf2)
s2, err := newServer(conf2) s2, err := newServer(t, conf2)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1486,7 +1483,7 @@ func TestServer_RPC_RateLimit(t *testing.T) {
dir1, conf1 := testServerConfig(t) dir1, conf1 := testServerConfig(t)
conf1.RPCRate = 2 conf1.RPCRate = 2
conf1.RPCMaxBurst = 2 conf1.RPCMaxBurst = 2
s1, err := NewServer(conf1) s1, err := newServer(t, conf1)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1512,7 +1509,11 @@ func TestServer_CALogging(t *testing.T) {
c, err := tlsutil.NewConfigurator(conf1.ToTLSUtilConfig(), logger) c, err := tlsutil.NewConfigurator(conf1.ToTLSUtilConfig(), logger)
require.NoError(t, err) require.NoError(t, err)
s1, err := NewServerLogger(conf1, logger, new(token.Store), c)
s1, err := NewServer(conf1,
WithLogger(logger),
WithTokenStore(new(token.Store)),
WithTLSConfigurator(c))
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }

Loading…
Cancel
Save