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"
|
2019-05-23 23:51:31 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
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"
|
2020-09-25 17:46:38 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2017-03-20 03:48:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatsFetcher(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, member := range members {
|
2020-05-14 21:02:52 +00:00
|
|
|
ok, _ := metadata.IsConsulServer(member)
|
2017-03-20 03:48:42 +00:00
|
|
|
if !ok {
|
2020-05-14 21:02:52 +00:00
|
|
|
t.Fatalf("expected member to be a server: %#v", member)
|
2017-03-20 03:48:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a normal fetch and make sure we get three responses.
|
|
|
|
func() {
|
2019-05-23 23:51:31 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
2020-09-25 17:46:38 +00:00
|
|
|
stats := s1.statsFetcher.Fetch(ctx, s1.autopilotServers())
|
2019-05-23 23:51:31 +00:00
|
|
|
if len(stats) != 3 {
|
2019-07-12 15:52:26 +00:00
|
|
|
r.Fatalf("bad: %#v", stats)
|
2017-03-20 03:48:42 +00:00
|
|
|
}
|
2019-05-23 23:51:31 +00:00
|
|
|
for id, stat := range stats {
|
|
|
|
switch types.NodeID(id) {
|
|
|
|
case s1.config.NodeID, s2.config.NodeID, s3.config.NodeID:
|
|
|
|
// OK
|
|
|
|
default:
|
2019-07-12 15:52:26 +00:00
|
|
|
r.Fatalf("bad: %s", id)
|
2019-05-23 23:51:31 +00:00
|
|
|
}
|
2017-03-20 03:48:42 +00:00
|
|
|
|
2019-05-23 23:51:31 +00:00
|
|
|
if stat == nil || stat.LastTerm == 0 {
|
2019-07-12 15:52:26 +00:00
|
|
|
r.Fatalf("bad: %#v", stat)
|
2019-05-23 23:51:31 +00:00
|
|
|
}
|
2017-03-20 03:48:42 +00:00
|
|
|
}
|
2019-05-23 23:51:31 +00:00
|
|
|
})
|
2017-03-20 03:48:42 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Fake an in-flight request to server 3 and make sure we don't fetch
|
|
|
|
// from it.
|
|
|
|
func() {
|
2019-05-23 23:51:31 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2020-09-25 17:46:38 +00:00
|
|
|
s1.statsFetcher.inflight[raft.ServerID(s3.config.NodeID)] = struct{}{}
|
|
|
|
defer delete(s1.statsFetcher.inflight, raft.ServerID(s3.config.NodeID))
|
2017-03-20 03:48:42 +00:00
|
|
|
|
2019-05-23 23:51:31 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
2020-09-25 17:46:38 +00:00
|
|
|
stats := s1.statsFetcher.Fetch(ctx, s1.autopilotServers())
|
2019-05-23 23:51:31 +00:00
|
|
|
if len(stats) != 2 {
|
2019-07-12 15:52:26 +00:00
|
|
|
r.Fatalf("bad: %#v", stats)
|
2017-03-20 03:48:42 +00:00
|
|
|
}
|
2019-05-23 23:51:31 +00:00
|
|
|
for id, stat := range stats {
|
|
|
|
switch types.NodeID(id) {
|
|
|
|
case s1.config.NodeID, s2.config.NodeID:
|
|
|
|
// OK
|
|
|
|
case s3.config.NodeID:
|
2019-07-12 15:52:26 +00:00
|
|
|
r.Fatalf("bad")
|
2019-05-23 23:51:31 +00:00
|
|
|
default:
|
2019-07-12 15:52:26 +00:00
|
|
|
r.Fatalf("bad: %s", id)
|
2019-05-23 23:51:31 +00:00
|
|
|
}
|
2017-03-20 03:48:42 +00:00
|
|
|
|
2019-05-23 23:51:31 +00:00
|
|
|
if stat == nil || stat.LastTerm == 0 {
|
2019-07-12 15:52:26 +00:00
|
|
|
r.Fatalf("bad: %#v", stat)
|
2019-05-23 23:51:31 +00:00
|
|
|
}
|
2017-03-20 03:48:42 +00:00
|
|
|
}
|
2019-05-23 23:51:31 +00:00
|
|
|
})
|
2017-03-20 03:48:42 +00:00
|
|
|
}()
|
|
|
|
}
|