2017-03-20 03:48:42 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2017-03-20 03:48:42 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatsFetcher(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2017-03-20 03:48:42 +00:00
|
|
|
dir1, s1 := testServerDCExpect(t, "dc1", 3)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, s2 := testServerDCExpect(t, "dc1", 3)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
dir3, s3 := testServerDCExpect(t, "dc1", 3)
|
|
|
|
defer os.RemoveAll(dir3)
|
|
|
|
defer s3.Shutdown()
|
|
|
|
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, s2, s1)
|
|
|
|
joinLAN(t, s3, s1)
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
|
|
|
testrpc.WaitForTestAgent(t, s2.RPC, "dc1")
|
|
|
|
testrpc.WaitForTestAgent(t, s3.RPC, "dc1")
|
2017-03-20 03:48:42 +00:00
|
|
|
|
|
|
|
members := s1.serfLAN.Members()
|
|
|
|
if len(members) != 3 {
|
|
|
|
t.Fatalf("bad len: %d", len(members))
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
var servers []*metadata.Server
|
2017-03-20 03:48:42 +00:00
|
|
|
for _, member := range members {
|
2017-07-06 10:48:37 +00:00
|
|
|
ok, server := metadata.IsConsulServer(member)
|
2017-03-20 03:48:42 +00:00
|
|
|
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()
|
2017-12-13 01:45:03 +00:00
|
|
|
stats := s1.statsFetcher.Fetch(ctx, s1.LANMembers())
|
2017-03-20 03:48:42 +00:00
|
|
|
if len(stats) != 3 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
for id, stat := range stats {
|
|
|
|
switch types.NodeID(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()
|
2017-12-13 01:45:03 +00:00
|
|
|
stats := s1.statsFetcher.Fetch(ctx, s1.LANMembers())
|
2017-03-20 03:48:42 +00:00
|
|
|
if len(stats) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
for id, stat := range stats {
|
|
|
|
switch types.NodeID(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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|