open-consul/agent/consul/cluster_test.go
FFMMM 1f8fb17be7
Vendor in rpc mono repo for net/rpc fork, go-msgpack, msgpackrpc. (#12311)
This commit syncs ENT changes to the OSS repo.

Original commit details in ENT:

```
commit 569d25f7f4578981c3801e6e067295668210f748
Author: FFMMM <FFMMM@users.noreply.github.com>
Date:   Thu Feb 10 10:23:33 2022 -0800

    Vendor fork net rpc (#1538)

    * replace net/rpc w consul-net-rpc/net/rpc

    Signed-off-by: FFMMM <FFMMM@users.noreply.github.com>

    * replace msgpackrpc and go-msgpack with fork from mono repo

    Signed-off-by: FFMMM <FFMMM@users.noreply.github.com>

    * gofmt all files touched

    Signed-off-by: FFMMM <FFMMM@users.noreply.github.com>
```

Signed-off-by: FFMMM <FFMMM@users.noreply.github.com>
2022-02-14 09:45:45 -08:00

101 lines
2.1 KiB
Go

package consul
import (
"os"
"testing"
"github.com/hashicorp/consul-net-rpc/net/rpc"
"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
}