diff --git a/nomad/serf_test.go b/nomad/serf_test.go index 56181e9b7..ce97b9970 100644 --- a/nomad/serf_test.go +++ b/nomad/serf_test.go @@ -6,11 +6,13 @@ import ( "os" "path" "strings" + "sync/atomic" "testing" "time" "github.com/hashicorp/nomad/testutil" "github.com/hashicorp/serf/serf" + "github.com/stretchr/testify/require" ) func TestNomad_JoinPeer(t *testing.T) { @@ -442,3 +444,41 @@ func TestNomad_BadExpect(t *testing.T) { t.Fatalf("should have 0 peers: %v", err) }) } + +// TestNomad_NonBootstraping_ShouldntBootstap asserts that if BootstrapExpect is zero, +// the server shouldn't bootstrap +func TestNomad_NonBootstraping_ShouldntBootstap(t *testing.T) { + t.Parallel() + + 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) + }) + + time.Sleep(500 * time.Millisecond) + + 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") + +}