2015-06-04 10:33:12 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-04 11:11:35 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2017-02-13 22:22:54 +00:00
|
|
|
"strings"
|
2020-12-17 20:52:40 +00:00
|
|
|
"sync/atomic"
|
2015-06-04 10:33:12 +00:00
|
|
|
"testing"
|
2018-09-20 00:13:37 +00:00
|
|
|
"time"
|
2015-06-04 11:02:39 +00:00
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2015-06-04 11:02:39 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2021-06-09 02:27:26 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2017-02-13 22:22:54 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2022-02-24 22:34:56 +00:00
|
|
|
"github.com/hashicorp/serf/testutil/retry"
|
2020-12-17 20:52:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-06-04 10:33:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNomad_JoinPeer(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, nil)
|
|
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2015-06-04 11:02:39 +00:00
|
|
|
c.Region = "region2"
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
2018-01-12 01:00:30 +00:00
|
|
|
TestJoin(t, s1, s2)
|
2015-06-04 10:33:12 +00:00
|
|
|
|
2015-06-04 11:02:39 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if members := s1.Members(); len(members) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", members)
|
|
|
|
}
|
|
|
|
if members := s2.Members(); len(members) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", members)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if len(s1.peers) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", s1.peers)
|
|
|
|
}
|
|
|
|
if len(s2.peers) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", s2.peers)
|
|
|
|
}
|
2015-06-07 18:37:59 +00:00
|
|
|
if len(s1.localPeers) != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", s1.localPeers)
|
|
|
|
}
|
|
|
|
if len(s2.localPeers) != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", s2.localPeers)
|
|
|
|
}
|
2015-06-04 11:02:39 +00:00
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNomad_RemovePeer(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, nil)
|
|
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2019-05-02 20:00:21 +00:00
|
|
|
c.Region = "global"
|
2015-06-04 11:02:39 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
2018-01-12 01:00:30 +00:00
|
|
|
TestJoin(t, s1, s2)
|
2015-06-04 11:02:39 +00:00
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if members := s1.Members(); len(members) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", members)
|
|
|
|
}
|
|
|
|
if members := s2.Members(); len(members) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", members)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Leave immediately
|
|
|
|
s2.Leave()
|
|
|
|
s2.Shutdown()
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if len(s1.peers) != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", s1.peers)
|
|
|
|
}
|
|
|
|
if len(s2.peers) != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", s2.peers)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
2015-06-04 10:33:12 +00:00
|
|
|
}
|
2015-06-04 11:11:35 +00:00
|
|
|
|
2017-02-13 22:22:54 +00:00
|
|
|
func TestNomad_ReapPeer(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
2017-02-13 22:22:54 +00:00
|
|
|
dir := tmpDir(t)
|
|
|
|
defer os.RemoveAll(dir)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
2017-10-19 21:16:27 +00:00
|
|
|
c.NodeName = "node1"
|
2017-02-13 22:22:54 +00:00
|
|
|
c.BootstrapExpect = 3
|
|
|
|
c.DevMode = false
|
2021-06-09 02:27:26 +00:00
|
|
|
c.RaftConfig = raft.DefaultConfig()
|
2017-02-13 22:22:54 +00:00
|
|
|
c.DataDir = path.Join(dir, "node1")
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2017-10-19 21:16:27 +00:00
|
|
|
c.NodeName = "node2"
|
2017-02-13 22:22:54 +00:00
|
|
|
c.BootstrapExpect = 3
|
|
|
|
c.DevMode = false
|
2021-06-09 02:27:26 +00:00
|
|
|
c.RaftConfig = raft.DefaultConfig()
|
2017-02-13 22:22:54 +00:00
|
|
|
c.DataDir = path.Join(dir, "node2")
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
|
|
|
s3, cleanupS3 := TestServer(t, func(c *Config) {
|
2017-10-19 21:16:27 +00:00
|
|
|
c.NodeName = "node3"
|
2017-02-13 22:22:54 +00:00
|
|
|
c.BootstrapExpect = 3
|
|
|
|
c.DevMode = false
|
2021-06-09 02:27:26 +00:00
|
|
|
c.RaftConfig = raft.DefaultConfig()
|
2017-02-13 22:22:54 +00:00
|
|
|
c.DataDir = path.Join(dir, "node3")
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS3()
|
2018-01-12 01:00:30 +00:00
|
|
|
TestJoin(t, s1, s2, s3)
|
2017-02-13 22:22:54 +00:00
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2017-10-19 21:16:27 +00:00
|
|
|
// Retry the join to decrease flakiness
|
2018-01-12 01:00:30 +00:00
|
|
|
TestJoin(t, s1, s2, s3)
|
2017-02-13 22:22:54 +00:00
|
|
|
if members := s1.Members(); len(members) != 3 {
|
2017-10-19 21:16:27 +00:00
|
|
|
return false, fmt.Errorf("bad s1: %#v", members)
|
2017-02-13 22:22:54 +00:00
|
|
|
}
|
|
|
|
if members := s2.Members(); len(members) != 3 {
|
2017-10-19 21:16:27 +00:00
|
|
|
return false, fmt.Errorf("bad s2: %#v", members)
|
2017-02-13 22:22:54 +00:00
|
|
|
}
|
|
|
|
if members := s3.Members(); len(members) != 3 {
|
2017-10-19 21:16:27 +00:00
|
|
|
return false, fmt.Errorf("bad s3: %#v", members)
|
2017-02-13 22:22:54 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Simulate a reap
|
|
|
|
mems := s1.Members()
|
|
|
|
var s2mem serf.Member
|
|
|
|
for _, m := range mems {
|
|
|
|
if strings.Contains(m.Name, s2.config.NodeName) {
|
|
|
|
s2mem = m
|
|
|
|
s2mem.Status = StatusReap
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown and then send the reap
|
|
|
|
s2.Shutdown()
|
|
|
|
s1.reconcileCh <- s2mem
|
|
|
|
s2.reconcileCh <- s2mem
|
|
|
|
s3.reconcileCh <- s2mem
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if len(s1.peers["global"]) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", s1.peers["global"])
|
|
|
|
}
|
|
|
|
peers, err := s1.numPeers()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("numPeers() failed: %v", err)
|
|
|
|
}
|
|
|
|
if peers != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", peers)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s3.peers["global"]) != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", s1.peers["global"])
|
|
|
|
}
|
|
|
|
peers, err = s3.numPeers()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("numPeers() failed: %v", err)
|
|
|
|
}
|
|
|
|
if peers != 2 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", peers)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:11:35 +00:00
|
|
|
func TestNomad_BootstrapExpect(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
2015-06-04 11:11:35 +00:00
|
|
|
dir := tmpDir(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
2017-02-08 22:50:19 +00:00
|
|
|
c.BootstrapExpect = 3
|
2015-06-04 11:11:35 +00:00
|
|
|
c.DevMode = false
|
2021-06-09 02:27:26 +00:00
|
|
|
c.RaftConfig = raft.DefaultConfig()
|
2015-06-04 11:11:35 +00:00
|
|
|
c.DataDir = path.Join(dir, "node1")
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2017-02-08 22:50:19 +00:00
|
|
|
c.BootstrapExpect = 3
|
2015-06-04 11:11:35 +00:00
|
|
|
c.DevMode = false
|
2021-06-09 02:27:26 +00:00
|
|
|
c.RaftConfig = raft.DefaultConfig()
|
2015-06-04 11:11:35 +00:00
|
|
|
c.DataDir = path.Join(dir, "node2")
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
|
|
|
s3, cleanupS3 := TestServer(t, func(c *Config) {
|
2017-02-08 22:50:19 +00:00
|
|
|
c.BootstrapExpect = 3
|
|
|
|
c.DevMode = false
|
2021-06-09 02:27:26 +00:00
|
|
|
c.RaftConfig = raft.DefaultConfig()
|
2017-02-08 22:50:19 +00:00
|
|
|
c.DataDir = path.Join(dir, "node3")
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS3()
|
2018-01-12 01:00:30 +00:00
|
|
|
TestJoin(t, s1, s2, s3)
|
2015-06-04 11:11:35 +00:00
|
|
|
|
2017-02-08 22:50:19 +00:00
|
|
|
// Join a fourth server after quorum has already been formed and ensure
|
|
|
|
// there is no election
|
2019-12-04 00:15:11 +00:00
|
|
|
s4, cleanupS4 := TestServer(t, func(c *Config) {
|
2017-02-08 22:50:19 +00:00
|
|
|
c.BootstrapExpect = 3
|
|
|
|
c.DevMode = false
|
2021-06-09 02:27:26 +00:00
|
|
|
c.RaftConfig = raft.DefaultConfig()
|
2017-02-08 22:50:19 +00:00
|
|
|
c.DataDir = path.Join(dir, "node4")
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS4()
|
2017-02-08 22:50:19 +00:00
|
|
|
|
|
|
|
// Make sure a leader is elected, grab the current term and then add in
|
|
|
|
// the fourth server.
|
2021-06-09 02:27:26 +00:00
|
|
|
t.Logf("waiting for stable leadership and up to date leadership")
|
|
|
|
leader := waitForStableLeadership(t, []*Server{s1, s2, s3})
|
|
|
|
require.NoError(t, leader.raft.Barrier(10*time.Second).Error())
|
|
|
|
|
|
|
|
termBefore := leader.raft.Stats()["last_log_term"]
|
|
|
|
t.Logf("got term: %v\n%#+v", termBefore, leader.raft.Stats())
|
2017-10-19 21:16:27 +00:00
|
|
|
|
|
|
|
var addresses []string
|
|
|
|
for _, s := range []*Server{s1, s2, s3} {
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", s.config.SerfConfig.MemberlistConfig.BindPort)
|
|
|
|
addresses = append(addresses, addr)
|
|
|
|
}
|
2017-02-08 22:50:19 +00:00
|
|
|
|
|
|
|
// Wait for the new server to see itself added to the cluster.
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2017-10-19 21:16:27 +00:00
|
|
|
// Retry join to reduce flakiness
|
|
|
|
if _, err := s4.Join(addresses); err != nil {
|
2021-06-09 02:27:26 +00:00
|
|
|
return false, fmt.Errorf("failed to to join addresses: %v", err)
|
2017-10-19 21:16:27 +00:00
|
|
|
}
|
2021-06-09 02:27:26 +00:00
|
|
|
p4, _ := s4.numPeers()
|
|
|
|
if p4 != 4 {
|
|
|
|
return false, fmt.Errorf("expected %d peers found %d", 4, p4)
|
|
|
|
}
|
|
|
|
return true, nil
|
2017-02-08 22:50:19 +00:00
|
|
|
}, func(err error) {
|
2021-06-09 02:27:26 +00:00
|
|
|
require.NoError(t, err)
|
2017-02-08 22:50:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Make sure there's still a leader and that the term didn't change,
|
|
|
|
// so we know an election didn't occur.
|
2021-06-09 02:27:26 +00:00
|
|
|
leader = waitForStableLeadership(t, []*Server{s1, s2, s3, s4})
|
|
|
|
require.NoError(t, leader.raft.Barrier(10*time.Second).Error())
|
|
|
|
termAfter := leader.raft.Stats()["last_log_term"]
|
|
|
|
require.Equal(t, termBefore, termAfter, "expected no election")
|
2015-06-04 11:11:35 +00:00
|
|
|
}
|
2015-06-05 22:11:16 +00:00
|
|
|
|
2018-09-20 00:13:37 +00:00
|
|
|
func TestNomad_BootstrapExpect_NonVoter(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
2022-02-24 22:34:56 +00:00
|
|
|
dir := t.TempDir()
|
2018-09-20 00:13:37 +00:00
|
|
|
|
2022-02-24 22:34:56 +00:00
|
|
|
// Create first server, non-voter
|
2019-12-04 00:15:11 +00:00
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
2018-09-20 00:13:37 +00:00
|
|
|
c.BootstrapExpect = 2
|
|
|
|
c.DevMode = false
|
|
|
|
c.DataDir = path.Join(dir, "node1")
|
2022-02-24 22:34:56 +00:00
|
|
|
c.NumSchedulers = 0
|
2018-09-20 00:13:37 +00:00
|
|
|
c.NonVoter = true
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
2022-02-24 22:34:56 +00:00
|
|
|
|
|
|
|
// Create second server, non-voter
|
2019-12-04 00:15:11 +00:00
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2018-09-20 00:13:37 +00:00
|
|
|
c.BootstrapExpect = 2
|
|
|
|
c.DevMode = false
|
|
|
|
c.DataDir = path.Join(dir, "node2")
|
2022-02-24 22:34:56 +00:00
|
|
|
c.NumSchedulers = 0
|
2018-09-20 00:13:37 +00:00
|
|
|
c.NonVoter = true
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
2022-02-24 22:34:56 +00:00
|
|
|
|
|
|
|
// Create third server, non-voter
|
2019-12-04 00:15:11 +00:00
|
|
|
s3, cleanupS3 := TestServer(t, func(c *Config) {
|
2018-09-20 00:13:37 +00:00
|
|
|
c.BootstrapExpect = 2
|
|
|
|
c.DevMode = false
|
|
|
|
c.DataDir = path.Join(dir, "node3")
|
2022-02-24 22:34:56 +00:00
|
|
|
c.NumSchedulers = 0
|
|
|
|
c.NonVoter = false
|
2018-09-20 00:13:37 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS3()
|
2022-02-24 22:34:56 +00:00
|
|
|
|
|
|
|
// Join the three servers we have so far
|
2018-09-20 00:13:37 +00:00
|
|
|
TestJoin(t, s1, s2, s3)
|
|
|
|
|
2022-02-24 22:34:56 +00:00
|
|
|
// Assert that we do not bootstrap, because BE=2, but only 1 voter
|
2018-09-20 00:13:37 +00:00
|
|
|
testutil.AssertUntil(testutil.Timeout(time.Second), func() (bool, error) {
|
|
|
|
_, p := s1.getLeader()
|
|
|
|
if p != nil {
|
|
|
|
return false, fmt.Errorf("leader %v", p)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should not have leader: %v", err)
|
|
|
|
})
|
|
|
|
|
2022-02-24 22:34:56 +00:00
|
|
|
// Create fourth server that is a voter
|
2019-12-04 00:15:11 +00:00
|
|
|
s4, cleanupS4 := TestServer(t, func(c *Config) {
|
2018-09-20 00:13:37 +00:00
|
|
|
c.BootstrapExpect = 2
|
|
|
|
c.DevMode = false
|
|
|
|
c.DataDir = path.Join(dir, "node4")
|
2022-02-24 22:34:56 +00:00
|
|
|
c.NumSchedulers = 0
|
|
|
|
c.NonVoter = false
|
2018-09-20 00:13:37 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS4()
|
2018-09-20 00:13:37 +00:00
|
|
|
|
2022-02-24 22:34:56 +00:00
|
|
|
// Join with fourth server (now have quorum)
|
|
|
|
// Start with 4th server for higher chance of success
|
|
|
|
TestJoin(t, s4, s3, s2, s1)
|
2018-09-20 00:13:37 +00:00
|
|
|
|
2022-02-24 22:34:56 +00:00
|
|
|
// Assert leadership with 4 peers
|
|
|
|
servers := []*Server{s1, s2, s3, s4}
|
|
|
|
for _, s := range servers {
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 4)) })
|
|
|
|
}
|
2018-09-20 00:13:37 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 22:11:16 +00:00
|
|
|
func TestNomad_BadExpect(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
2015-06-05 22:11:16 +00:00
|
|
|
c.BootstrapExpect = 2
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2015-06-05 22:11:16 +00:00
|
|
|
c.BootstrapExpect = 3
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
2015-06-05 22:11:16 +00:00
|
|
|
servers := []*Server{s1, s2}
|
2018-01-12 01:00:30 +00:00
|
|
|
TestJoin(t, s1, s2)
|
2015-06-05 22:11:16 +00:00
|
|
|
|
|
|
|
// Serf members should update
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
for _, s := range servers {
|
|
|
|
members := s.Members()
|
|
|
|
if len(members) != 2 {
|
|
|
|
return false, fmt.Errorf("%d", len(members))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have 2 peers: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// should still have no peers (because s2 is in expect=2 mode)
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
for _, s := range servers {
|
2017-02-02 21:52:31 +00:00
|
|
|
p, _ := s.numPeers()
|
2020-03-02 15:29:24 +00:00
|
|
|
if p != 0 {
|
2017-02-03 01:50:06 +00:00
|
|
|
return false, fmt.Errorf("%d", p)
|
2015-06-05 22:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have 0 peers: %v", err)
|
|
|
|
})
|
|
|
|
}
|
2020-12-17 20:52:40 +00:00
|
|
|
|
|
|
|
// TestNomad_NonBootstraping_ShouldntBootstap asserts that if BootstrapExpect is zero,
|
|
|
|
// the server shouldn't bootstrap
|
|
|
|
func TestNomad_NonBootstraping_ShouldntBootstap(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-12-17 20:52:40 +00:00
|
|
|
|
|
|
|
dir := tmpDir(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
|
|
|
c.BootstrapExpect = 0
|
|
|
|
c.DevMode = false
|
|
|
|
c.DataDir = path.Join(dir, "node")
|
|
|
|
})
|
|
|
|
defer cleanupS1()
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
s1.peerLock.Lock()
|
|
|
|
p := len(s1.localPeers)
|
|
|
|
s1.peerLock.Unlock()
|
|
|
|
if p != 1 {
|
|
|
|
return false, fmt.Errorf("%d", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("expected 1 local peer: %v", err)
|
|
|
|
})
|
|
|
|
|
2021-01-04 14:00:40 +00:00
|
|
|
// as non-bootstrap mode is the initial state, we must wait long enough to assert that
|
|
|
|
// we don't bootstrap even if enough time has elapsed. Also, explicitly attempt bootstrap.
|
|
|
|
s1.maybeBootstrap()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-12-17 20:52:40 +00:00
|
|
|
|
|
|
|
bootstrapped := atomic.LoadInt32(&s1.config.Bootstrapped)
|
|
|
|
require.Zero(t, bootstrapped, "expecting non-bootstrapped servers")
|
|
|
|
|
|
|
|
p, _ := s1.numPeers()
|
|
|
|
require.Zero(t, p, "number of peers in Raft")
|
|
|
|
|
|
|
|
}
|