// NewNode creates a new raft node to use for tests func NewNode(t *testing.T, clockSource *fakeclock.FakeClock, tc *cautils.TestCA, opts ...raft.NewNodeOptions) *TestNode { l, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err, "can't bind to raft service port") wrappedListener := NewWrappedListener(l) securityConfig, err := tc.NewNodeConfig(ca.ManagerRole) require.NoError(t, err) serverOpts := []grpc.ServerOption{grpc.Creds(securityConfig.ServerTLSCreds)} s := grpc.NewServer(serverOpts...) cfg := raft.DefaultNodeConfig() stateDir, err := ioutil.TempDir("", "test-raft") require.NoError(t, err, "can't create temporary state directory") newNodeOpts := raft.NewNodeOptions{ ID: securityConfig.ClientTLSCreds.NodeID(), Addr: l.Addr().String(), Config: cfg, StateDir: stateDir, ClockSource: clockSource, SendTimeout: 10 * time.Second, TLSCredentials: securityConfig.ClientTLSCreds, } if len(opts) > 1 { panic("more than one optional argument provided") } if len(opts) == 1 { newNodeOpts.JoinAddr = opts[0].JoinAddr if opts[0].Addr != "" { newNodeOpts.Addr = opts[0].Addr } } n := raft.NewNode(context.Background(), newNodeOpts) n.Server = s healthServer := health.NewHealthServer() api.RegisterHealthServer(s, healthServer) raft.Register(s, n) go func() { // After stopping, we should receive an error from Serve assert.Error(t, s.Serve(wrappedListener)) }() healthServer.SetServingStatus("Raft", api.HealthCheckResponse_SERVING) return &TestNode{Node: n, Listener: wrappedListener, SecurityConfig: securityConfig} }
// NewJoinNode creates a new raft node joining an existing cluster func NewJoinNode(t *testing.T, clockSource *fakeclock.FakeClock, join string, tc *cautils.TestCA, opts ...raft.NewNodeOptions) *TestNode { var derivedOpts raft.NewNodeOptions if len(opts) == 1 { derivedOpts = opts[0] } derivedOpts.JoinAddr = join n := NewNode(t, clockSource, tc, derivedOpts) err := n.Node.JoinAndStart() require.NoError(t, err, "can't join cluster") go n.Run(context.Background()) return n }