From a1648c61ae0aa5c07fa8f17fbc67dd1d8db619ff Mon Sep 17 00:00:00 2001 From: Matt Keeler Date: Mon, 27 Apr 2020 12:17:38 -0400 Subject: [PATCH] A couple testing helper updates (#7694) --- agent/consul/client_test.go | 21 +++++++- agent/consul/cluster_test.go | 100 +++++++++++++++++++++++++++++++++++ agent/consul/helper_test.go | 4 ++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 agent/consul/cluster_test.go diff --git a/agent/consul/client_test.go b/agent/consul/client_test.go index 5a218e247f..852f989be2 100644 --- a/agent/consul/client_test.go +++ b/agent/consul/client_test.go @@ -13,6 +13,8 @@ import ( "github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil/retry" "github.com/hashicorp/consul/testrpc" + "github.com/hashicorp/consul/tlsutil" + "github.com/hashicorp/go-hclog" msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc" "github.com/hashicorp/serf/serf" "github.com/stretchr/testify/require" @@ -46,6 +48,7 @@ func testClientConfig(t *testing.T) (string, *Config) { config.SerfLANConfig.MemberlistConfig.ProbeTimeout = 200 * time.Millisecond config.SerfLANConfig.MemberlistConfig.ProbeInterval = time.Second config.SerfLANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond + config.LogOutput = testutil.TestWriter(t) return dir, config } @@ -69,7 +72,23 @@ func testClientWithConfig(t *testing.T, cb func(c *Config)) (string, *Client) { if cb != nil { cb(config) } - client, err := NewClient(config) + w := config.LogOutput + if w == nil { + w = os.Stderr + } + + logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{ + Name: config.NodeName, + Level: hclog.Debug, + Output: w, + }) + + tlsConf, err := tlsutil.NewConfigurator(config.ToTLSUtilConfig(), logger) + if err != nil { + t.Fatalf("err: %v", err) + } + + client, err := NewClientLogger(config, logger, tlsConf) if err != nil { config.NotifyShutdown() t.Fatalf("err: %v", err) diff --git a/agent/consul/cluster_test.go b/agent/consul/cluster_test.go new file mode 100644 index 0000000000..29799a39ce --- /dev/null +++ b/agent/consul/cluster_test.go @@ -0,0 +1,100 @@ +package consul + +import ( + "net/rpc" + "os" + "testing" + + "github.com/hashicorp/consul/testrpc" + "github.com/stretchr/testify/require" +) + +type testClusterConfig struct { + Datacenter string + Servers int + Clients int + ServerConf func(*Config) + ClientConf func(*Config) + + ServerWait func(*testing.T, *Server) + ClientWait func(*testing.T, *Client) +} + +type testCluster struct { + Servers []*Server + ServerCodecs []rpc.ClientCodec + Clients []*Client +} + +func newTestCluster(t *testing.T, conf *testClusterConfig) *testCluster { + t.Helper() + + require.NotNil(t, conf) + cluster := testCluster{} + + // create the servers + for i := 0; i < conf.Servers; i++ { + dir, srv := testServerWithConfig(t, func(c *Config) { + if conf.Datacenter != "" { + c.Datacenter = conf.Datacenter + } + c.Bootstrap = false + c.BootstrapExpect = conf.Servers + + if conf.ServerConf != nil { + conf.ServerConf(c) + } + }) + t.Cleanup(func() { os.RemoveAll(dir) }) + t.Cleanup(func() { srv.Shutdown() }) + + cluster.Servers = append(cluster.Servers, srv) + + codec := rpcClient(t, srv) + + cluster.ServerCodecs = append(cluster.ServerCodecs, codec) + t.Cleanup(func() { codec.Close() }) + + if i > 0 { + joinLAN(t, srv, cluster.Servers[0]) + } + } + + waitForLeaderEstablishment(t, cluster.Servers...) + if conf.ServerWait != nil { + for _, srv := range cluster.Servers { + conf.ServerWait(t, srv) + } + } + + // create the clients + for i := 0; i < conf.Clients; i++ { + dir, client := testClientWithConfig(t, func(c *Config) { + if conf.Datacenter != "" { + c.Datacenter = conf.Datacenter + } + if conf.ClientConf != nil { + conf.ClientConf(c) + } + }) + + t.Cleanup(func() { os.RemoveAll(dir) }) + t.Cleanup(func() { client.Shutdown() }) + + if len(cluster.Servers) > 0 { + joinLAN(t, client, cluster.Servers[0]) + } + + cluster.Clients = append(cluster.Clients, client) + } + + for _, client := range cluster.Clients { + if conf.ClientWait != nil { + conf.ClientWait(t, client) + } else { + testrpc.WaitForTestAgent(t, client.RPC, client.config.Datacenter) + } + } + + return &cluster +} diff --git a/agent/consul/helper_test.go b/agent/consul/helper_test.go index 7192102988..7373234369 100644 --- a/agent/consul/helper_test.go +++ b/agent/consul/helper_test.go @@ -115,6 +115,8 @@ type clientOrServer interface { // // member.JoinLAN("127.0.0.1:"+leader.config.SerfLANConfig.MemberlistConfig.BindPort) func joinLAN(t *testing.T, member clientOrServer, leader *Server) { + t.Helper() + if member == nil || leader == nil { panic("no server") } @@ -143,6 +145,8 @@ func joinLAN(t *testing.T, member clientOrServer, leader *Server) { // // member.JoinWAN("127.0.0.1:"+leader.config.SerfWANConfig.MemberlistConfig.BindPort) func joinWAN(t *testing.T, member, leader *Server) { + t.Helper() + if member == nil || leader == nil { panic("no server") }