f0c3dca49c
Copy the updated version of freeport (sdk/freeport), and tweak it for use in Nomad tests. This means staying below port 10000 to avoid conflicts with the lib/freeport that is still transitively used by the old version of consul that we vendor. Also provide implementations to find ephemeral ports of macOS and Windows environments. Ports acquired through freeport are supposed to be returned to freeport, which this change now also introduces. Many tests are modified to include calls to a cleanup function for Server objects. This should help quite a bit with some flakey tests, but not all of them. Our port problems will not go away completely until we upgrade our vendor version of consul. With Go modules, we'll probably do a 'replace' to swap out other copies of freeport with the one now in 'nomad/helper/freeport'.
96 lines
2 KiB
Go
96 lines
2 KiB
Go
package nomad
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
)
|
|
|
|
func TestStatsFetcher(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
conf := func(c *Config) {
|
|
c.Region = "region-a"
|
|
c.DevDisableBootstrap = true
|
|
c.BootstrapExpect = 3
|
|
}
|
|
|
|
s1, cleanupS1 := TestServer(t, conf)
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, conf)
|
|
defer cleanupS2()
|
|
|
|
s3, cleanupS3 := TestServer(t, conf)
|
|
defer cleanupS3()
|
|
|
|
TestJoin(t, s1, s2, s3)
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
members := s1.serf.Members()
|
|
if len(members) != 3 {
|
|
t.Fatalf("bad len: %d", len(members))
|
|
}
|
|
|
|
var servers []*serverParts
|
|
for _, member := range members {
|
|
ok, server := isNomadServer(member)
|
|
if !ok {
|
|
t.Fatalf("bad: %#v", member)
|
|
}
|
|
servers = append(servers, server)
|
|
}
|
|
|
|
// Do a normal fetch and make sure we get three responses.
|
|
func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
stats := s1.statsFetcher.Fetch(ctx, s1.Members())
|
|
if len(stats) != 3 {
|
|
t.Fatalf("bad: %#v", stats)
|
|
}
|
|
for id, stat := range stats {
|
|
switch id {
|
|
case s1.config.NodeID, s2.config.NodeID, s3.config.NodeID:
|
|
// OK
|
|
default:
|
|
t.Fatalf("bad: %s", id)
|
|
}
|
|
|
|
if stat == nil || stat.LastTerm == 0 {
|
|
t.Fatalf("bad: %#v", stat)
|
|
}
|
|
}
|
|
}()
|
|
|
|
// Fake an in-flight request to server 3 and make sure we don't fetch
|
|
// from it.
|
|
func() {
|
|
s1.statsFetcher.inflight[string(s3.config.NodeID)] = struct{}{}
|
|
defer delete(s1.statsFetcher.inflight, string(s3.config.NodeID))
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
stats := s1.statsFetcher.Fetch(ctx, s1.Members())
|
|
if len(stats) != 2 {
|
|
t.Fatalf("bad: %#v", stats)
|
|
}
|
|
for id, stat := range stats {
|
|
switch id {
|
|
case s1.config.NodeID, s2.config.NodeID:
|
|
// OK
|
|
case s3.config.NodeID:
|
|
t.Fatalf("bad")
|
|
default:
|
|
t.Fatalf("bad: %s", id)
|
|
}
|
|
|
|
if stat == nil || stat.LastTerm == 0 {
|
|
t.Fatalf("bad: %#v", stat)
|
|
}
|
|
}
|
|
}()
|
|
}
|