A couple testing helper updates (#7694)

This commit is contained in:
Matt Keeler 2020-04-27 12:17:38 -04:00 committed by GitHub
parent 58a597693b
commit a1648c61ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 124 additions and 1 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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")
}