2021-02-25 23:11:21 +00:00
|
|
|
package health
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-11-20 15:23:35 +00:00
|
|
|
"strings"
|
2020-09-18 22:25:56 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-11-20 15:23:35 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2020-09-18 22:25:56 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-18 22:33:02 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-10-30 16:25:22 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbcommon"
|
2020-09-18 22:33:02 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbsubscribe"
|
2020-11-20 15:23:35 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2020-09-18 22:25:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStreamingHealthServices_EmptySnapshot(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:25:22 +00:00
|
|
|
namespace := pbcommon.DefaultEnterpriseMeta.Namespace
|
|
|
|
client := NewTestStreamingClient(namespace)
|
2020-10-01 06:34:43 +00:00
|
|
|
typ := StreamingHealthServices{deps: MaterializerDeps{
|
|
|
|
Client: client,
|
|
|
|
Logger: hclog.Default(),
|
|
|
|
}}
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// Initially there are no services registered. Server should send an
|
|
|
|
// EndOfSnapshot message immediately with index of 1.
|
2020-10-27 17:43:20 +00:00
|
|
|
client.QueueEvents(newEndOfSnapshotEvent(1))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
opts := cache.FetchOptions{
|
|
|
|
MinIndex: 0,
|
2020-10-01 06:36:36 +00:00
|
|
|
Timeout: time.Second,
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
req := &structs.ServiceSpecificRequest{
|
2020-10-30 16:25:22 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "web",
|
2021-02-02 23:04:01 +00:00
|
|
|
EnterpriseMeta: structs.NewEnterpriseMeta(namespace),
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
empty := &structs.IndexedCheckServiceNodes{
|
|
|
|
Nodes: structs.CheckServiceNodes{},
|
|
|
|
QueryMeta: structs.QueryMeta{
|
|
|
|
Index: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "empty snapshot returned", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
// Fetch should return an empty
|
|
|
|
// result of the right type with a non-zero index, and in the background begin
|
|
|
|
// streaming updates.
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, uint64(1), result.Index)
|
|
|
|
require.Equal(t, empty, result.Value)
|
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "blocks for timeout", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
// Subsequent fetch should block for the timeout
|
|
|
|
start := time.Now()
|
|
|
|
opts.Timeout = 200 * time.Millisecond
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
require.True(t, elapsed >= 200*time.Millisecond,
|
|
|
|
"Fetch should have blocked until timeout")
|
|
|
|
|
|
|
|
require.Equal(t, opts.MinIndex, result.Index, "result index should not have changed")
|
|
|
|
require.Equal(t, empty, result.Value, "result value should not have changed")
|
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "blocks until update", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
// Make another blocking query with a longer timeout and trigger an update
|
|
|
|
// event part way through.
|
|
|
|
start := time.Now()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
2020-10-03 00:04:45 +00:00
|
|
|
client.QueueEvents(newEventServiceHealthRegister(4, 1, "web"))
|
2020-09-18 22:25:56 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
opts.Timeout = time.Second
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
require.True(t, elapsed >= 200*time.Millisecond,
|
|
|
|
"Fetch should have blocked until the event was delivered")
|
|
|
|
require.True(t, elapsed < time.Second,
|
|
|
|
"Fetch should have returned before the timeout")
|
|
|
|
|
|
|
|
require.Equal(t, uint64(4), result.Index, "result index should not have changed")
|
|
|
|
require.Len(t, result.Value.(*structs.IndexedCheckServiceNodes).Nodes, 1,
|
|
|
|
"result value should contain the new registration")
|
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-10-03 00:04:45 +00:00
|
|
|
runStep(t, "reconnects and resumes after temporary error", func(t *testing.T) {
|
2020-10-01 06:21:50 +00:00
|
|
|
client.QueueErr(tempError("broken pipe"))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// Next fetch will continue to block until timeout and receive the same
|
|
|
|
// result.
|
|
|
|
start := time.Now()
|
|
|
|
opts.Timeout = 200 * time.Millisecond
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
require.True(t, elapsed >= 200*time.Millisecond,
|
|
|
|
"Fetch should have blocked until timeout")
|
|
|
|
|
|
|
|
require.Equal(t, opts.MinIndex, result.Index, "result index should not have changed")
|
|
|
|
require.Equal(t, opts.LastResult.Value, result.Value, "result value should not have changed")
|
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
|
|
|
|
|
|
|
// But an update should still be noticed due to reconnection
|
2020-10-03 00:04:45 +00:00
|
|
|
client.QueueEvents(newEventServiceHealthRegister(10, 2, "web"))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
start = time.Now()
|
|
|
|
opts.Timeout = time.Second
|
|
|
|
result, err = typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
elapsed = time.Since(start)
|
|
|
|
require.True(t, elapsed < time.Second,
|
|
|
|
"Fetch should have returned before the timeout")
|
|
|
|
|
|
|
|
require.Equal(t, uint64(10), result.Index, "result index should not have changed")
|
|
|
|
require.Len(t, result.Value.(*structs.IndexedCheckServiceNodes).Nodes, 2,
|
|
|
|
"result value should contain the new registration")
|
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "returns non-temporary error to watchers", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
// Wait and send the error while fetcher is waiting
|
|
|
|
go func() {
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
client.QueueErr(errors.New("invalid request"))
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Next fetch should return the error
|
|
|
|
start := time.Now()
|
|
|
|
opts.Timeout = time.Second
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
require.True(t, elapsed >= 200*time.Millisecond,
|
|
|
|
"Fetch should have blocked until error was sent")
|
|
|
|
require.True(t, elapsed < time.Second,
|
|
|
|
"Fetch should have returned before the timeout")
|
|
|
|
|
|
|
|
require.Equal(t, opts.MinIndex, result.Index, "result index should not have changed")
|
|
|
|
// We don't require instances to be returned in same order so we use
|
|
|
|
// elementsMatch which is recursive.
|
|
|
|
requireResultsSame(t,
|
|
|
|
opts.LastResult.Value.(*structs.IndexedCheckServiceNodes),
|
|
|
|
result.Value.(*structs.IndexedCheckServiceNodes),
|
|
|
|
)
|
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
|
|
|
|
|
|
|
// But an update should still be noticed due to reconnection
|
2020-10-03 00:04:45 +00:00
|
|
|
client.QueueEvents(newEventServiceHealthRegister(opts.MinIndex+5, 3, "web"))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
opts.Timeout = time.Second
|
|
|
|
result, err = typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
elapsed = time.Since(start)
|
|
|
|
require.True(t, elapsed < time.Second,
|
|
|
|
"Fetch should have returned before the timeout")
|
|
|
|
|
|
|
|
require.Equal(t, opts.MinIndex+5, result.Index, "result index should not have changed")
|
|
|
|
require.Len(t, result.Value.(*structs.IndexedCheckServiceNodes).Nodes, 3,
|
|
|
|
"result value should contain the new registration")
|
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 06:21:50 +00:00
|
|
|
type tempError string
|
|
|
|
|
|
|
|
func (e tempError) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e tempError) Temporary() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:25:56 +00:00
|
|
|
// requireResultsSame compares two IndexedCheckServiceNodes without requiring
|
|
|
|
// the same order of results (which vary due to map usage internally).
|
|
|
|
func requireResultsSame(t *testing.T, want, got *structs.IndexedCheckServiceNodes) {
|
|
|
|
require.Equal(t, want.Index, got.Index)
|
|
|
|
|
|
|
|
svcIDs := func(csns structs.CheckServiceNodes) []string {
|
|
|
|
res := make([]string, 0, len(csns))
|
|
|
|
for _, csn := range csns {
|
|
|
|
res = append(res, fmt.Sprintf("%s/%s", csn.Node.Node, csn.Service.ID))
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
gotIDs := svcIDs(got.Nodes)
|
|
|
|
wantIDs := svcIDs(want.Nodes)
|
|
|
|
|
|
|
|
require.ElementsMatch(t, wantIDs, gotIDs)
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:25:22 +00:00
|
|
|
// getNamespace returns a namespace if namespace support exists, otherwise
|
|
|
|
// returns the empty string. It allows the same tests to work in both oss and ent
|
|
|
|
// without duplicating the tests.
|
|
|
|
func getNamespace(ns string) string {
|
2021-02-02 23:04:01 +00:00
|
|
|
meta := structs.NewEnterpriseMeta(ns)
|
2021-02-22 17:50:52 +00:00
|
|
|
return meta.NamespaceOrEmpty()
|
2020-10-30 16:25:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 15:23:35 +00:00
|
|
|
func TestOrderingConsistentWithMemDb(t *testing.T) {
|
|
|
|
index := uint64(42)
|
|
|
|
buildTestNode := func(nodeName string, serviceID string) structs.CheckServiceNode {
|
|
|
|
newID, err := uuid.GenerateUUID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
ID: types.NodeID(strings.ToUpper(newID)),
|
|
|
|
Node: nodeName,
|
|
|
|
Address: nodeName,
|
|
|
|
Datacenter: "dc1",
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: serviceID,
|
|
|
|
Service: "testService",
|
|
|
|
Port: 8080,
|
|
|
|
Weights: &structs.Weights{
|
|
|
|
Passing: 1,
|
|
|
|
Warning: 1,
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: index,
|
|
|
|
ModifyIndex: index,
|
|
|
|
},
|
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
|
|
|
|
},
|
|
|
|
Checks: []*structs.HealthCheck{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zero := buildTestNode("a-zero-node", "testService:1")
|
|
|
|
one := buildTestNode("node1", "testService:1")
|
|
|
|
two := buildTestNode("node1", "testService:2")
|
|
|
|
three := buildTestNode("node2", "testService")
|
|
|
|
result := structs.IndexedCheckServiceNodes{
|
|
|
|
Nodes: structs.CheckServiceNodes{
|
|
|
|
three, two, zero, one,
|
|
|
|
},
|
|
|
|
QueryMeta: structs.QueryMeta{
|
|
|
|
Index: index,
|
|
|
|
},
|
|
|
|
}
|
2020-11-25 20:01:07 +00:00
|
|
|
sortCheckServiceNodes(&result)
|
|
|
|
expected := structs.CheckServiceNodes{zero, one, two, three}
|
|
|
|
require.Equal(t, expected, result.Nodes)
|
2020-11-20 15:23:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 22:25:56 +00:00
|
|
|
func TestStreamingHealthServices_FullSnapshot(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:25:22 +00:00
|
|
|
namespace := getNamespace("ns2")
|
|
|
|
client := NewTestStreamingClient(namespace)
|
2020-10-01 06:34:43 +00:00
|
|
|
typ := StreamingHealthServices{deps: MaterializerDeps{
|
|
|
|
Client: client,
|
|
|
|
Logger: hclog.Default(),
|
|
|
|
}}
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// Create an initial snapshot of 3 instances on different nodes
|
2020-10-03 00:04:45 +00:00
|
|
|
registerServiceWeb := func(index uint64, nodeNum int) *pbsubscribe.Event {
|
|
|
|
return newEventServiceHealthRegister(index, nodeNum, "web")
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
client.QueueEvents(
|
2020-10-03 00:04:45 +00:00
|
|
|
registerServiceWeb(5, 1),
|
|
|
|
registerServiceWeb(5, 2),
|
|
|
|
registerServiceWeb(5, 3),
|
2020-10-27 17:43:20 +00:00
|
|
|
newEndOfSnapshotEvent(5))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// This contains the view state so important we share it between calls.
|
|
|
|
opts := cache.FetchOptions{
|
|
|
|
MinIndex: 0,
|
|
|
|
Timeout: 1 * time.Second,
|
|
|
|
}
|
|
|
|
req := &structs.ServiceSpecificRequest{
|
2020-10-30 16:25:22 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "web",
|
2021-02-02 23:04:01 +00:00
|
|
|
EnterpriseMeta: structs.NewEnterpriseMeta(namespace),
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gatherNodes := func(res interface{}) []string {
|
|
|
|
nodes := make([]string, 0, 3)
|
|
|
|
r := res.(*structs.IndexedCheckServiceNodes)
|
|
|
|
for _, csn := range r.Nodes {
|
|
|
|
nodes = append(nodes, csn.Node.Node)
|
|
|
|
}
|
2020-11-20 15:23:35 +00:00
|
|
|
// Result will be sorted alphabetically the same way as memdb
|
2020-09-18 22:25:56 +00:00
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "full snapshot returned", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, uint64(5), result.Index)
|
|
|
|
require.ElementsMatch(t, []string{"node1", "node2", "node3"},
|
|
|
|
gatherNodes(result.Value))
|
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "blocks until deregistration", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
// Make another blocking query with a longer timeout and trigger an update
|
|
|
|
// event part way through.
|
|
|
|
start := time.Now()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
// Deregister instance on node1
|
2020-10-03 00:04:45 +00:00
|
|
|
client.QueueEvents(newEventServiceHealthDeregister(20, 1, "web"))
|
2020-09-18 22:25:56 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
opts.Timeout = time.Second
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
require.True(t, elapsed >= 200*time.Millisecond,
|
|
|
|
"Fetch should have blocked until the event was delivered")
|
|
|
|
require.True(t, elapsed < time.Second,
|
|
|
|
"Fetch should have returned before the timeout")
|
|
|
|
|
|
|
|
require.Equal(t, uint64(20), result.Index)
|
2020-09-29 21:42:48 +00:00
|
|
|
require.Equal(t, []string{"node2", "node3"}, gatherNodes(result.Value))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "server reload is respected", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
// Simulates the server noticing the request's ACL token privs changing. To
|
|
|
|
// detect this we'll queue up the new snapshot as a different set of nodes
|
|
|
|
// to the first.
|
2020-09-18 22:33:02 +00:00
|
|
|
client.QueueErr(status.Error(codes.Aborted, "reset by server"))
|
|
|
|
|
2020-09-18 22:25:56 +00:00
|
|
|
client.QueueEvents(
|
2020-10-03 00:04:45 +00:00
|
|
|
registerServiceWeb(50, 3), // overlap existing node
|
|
|
|
registerServiceWeb(50, 4),
|
|
|
|
registerServiceWeb(50, 5),
|
2020-10-27 17:43:20 +00:00
|
|
|
newEndOfSnapshotEvent(50))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// Make another blocking query with THE SAME index. It should immediately
|
|
|
|
// return the new snapshot.
|
|
|
|
start := time.Now()
|
|
|
|
opts.Timeout = time.Second
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
require.True(t, elapsed < time.Second,
|
|
|
|
"Fetch should have returned before the timeout")
|
|
|
|
|
|
|
|
require.Equal(t, uint64(50), result.Index)
|
2020-09-29 21:42:48 +00:00
|
|
|
require.Equal(t, []string{"node3", "node4", "node5"}, gatherNodes(result.Value))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-10-03 00:04:45 +00:00
|
|
|
|
|
|
|
runStep(t, "reconnects and receives new snapshot when server state has changed", func(t *testing.T) {
|
|
|
|
client.QueueErr(tempError("temporary connection error"))
|
|
|
|
|
|
|
|
client.QueueEvents(
|
2020-10-27 17:43:20 +00:00
|
|
|
newNewSnapshotToFollowEvent(),
|
2020-10-03 00:04:45 +00:00
|
|
|
registerServiceWeb(50, 3), // overlap existing node
|
|
|
|
registerServiceWeb(50, 4),
|
|
|
|
registerServiceWeb(50, 5),
|
2020-10-27 17:43:20 +00:00
|
|
|
newEndOfSnapshotEvent(50))
|
2020-10-03 00:04:45 +00:00
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
opts.MinIndex = 49
|
|
|
|
opts.Timeout = time.Second
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
require.True(t, elapsed < time.Second,
|
|
|
|
"Fetch should have returned before the timeout")
|
|
|
|
|
|
|
|
require.Equal(t, uint64(50), result.Index)
|
|
|
|
require.Equal(t, []string{"node3", "node4", "node5"}, gatherNodes(result.Value))
|
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStreamingHealthServices_EventBatches(t *testing.T) {
|
2020-10-30 16:25:22 +00:00
|
|
|
namespace := getNamespace("ns3")
|
|
|
|
client := NewTestStreamingClient(namespace)
|
2020-10-01 06:34:43 +00:00
|
|
|
typ := StreamingHealthServices{deps: MaterializerDeps{
|
|
|
|
Client: client,
|
|
|
|
Logger: hclog.Default(),
|
|
|
|
}}
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// Create an initial snapshot of 3 instances but in a single event batch
|
2020-09-18 22:33:02 +00:00
|
|
|
batchEv := newEventBatchWithEvents(
|
|
|
|
newEventServiceHealthRegister(5, 1, "web"),
|
|
|
|
newEventServiceHealthRegister(5, 2, "web"),
|
|
|
|
newEventServiceHealthRegister(5, 3, "web"))
|
2020-09-18 22:25:56 +00:00
|
|
|
client.QueueEvents(
|
2020-10-03 00:04:45 +00:00
|
|
|
batchEv,
|
2020-10-27 17:43:20 +00:00
|
|
|
newEndOfSnapshotEvent(5))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// This contains the view state so important we share it between calls.
|
|
|
|
opts := cache.FetchOptions{
|
|
|
|
MinIndex: 0,
|
|
|
|
Timeout: 1 * time.Second,
|
|
|
|
}
|
|
|
|
req := &structs.ServiceSpecificRequest{
|
2020-10-30 16:25:22 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "web",
|
2021-02-02 23:04:01 +00:00
|
|
|
EnterpriseMeta: structs.NewEnterpriseMeta(namespace),
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gatherNodes := func(res interface{}) []string {
|
|
|
|
nodes := make([]string, 0, 3)
|
|
|
|
r := res.(*structs.IndexedCheckServiceNodes)
|
|
|
|
for _, csn := range r.Nodes {
|
|
|
|
nodes = append(nodes, csn.Node.Node)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "full snapshot returned", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, uint64(5), result.Index)
|
|
|
|
require.ElementsMatch(t, []string{"node1", "node2", "node3"},
|
|
|
|
gatherNodes(result.Value))
|
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "batched updates work too", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
// Simulate multiple registrations happening in one Txn (so all have same
|
|
|
|
// index)
|
2020-09-18 22:33:02 +00:00
|
|
|
batchEv := newEventBatchWithEvents(
|
2020-09-18 22:25:56 +00:00
|
|
|
// Deregister an existing node
|
2020-09-18 22:33:02 +00:00
|
|
|
newEventServiceHealthDeregister(20, 1, "web"),
|
2020-09-18 22:25:56 +00:00
|
|
|
// Register another
|
2020-09-18 22:33:02 +00:00
|
|
|
newEventServiceHealthRegister(20, 4, "web"),
|
2020-09-18 22:25:56 +00:00
|
|
|
)
|
2020-10-03 00:04:45 +00:00
|
|
|
client.QueueEvents(batchEv)
|
2020-09-18 22:25:56 +00:00
|
|
|
opts.Timeout = time.Second
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, uint64(20), result.Index)
|
|
|
|
require.ElementsMatch(t, []string{"node2", "node3", "node4"},
|
|
|
|
gatherNodes(result.Value))
|
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStreamingHealthServices_Filtering(t *testing.T) {
|
2020-10-30 16:25:22 +00:00
|
|
|
namespace := getNamespace("ns3")
|
|
|
|
client := NewTestStreamingClient(namespace)
|
2020-10-01 06:34:43 +00:00
|
|
|
typ := StreamingHealthServices{deps: MaterializerDeps{
|
|
|
|
Client: client,
|
|
|
|
Logger: hclog.Default(),
|
|
|
|
}}
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// Create an initial snapshot of 3 instances but in a single event batch
|
2020-09-18 22:33:02 +00:00
|
|
|
batchEv := newEventBatchWithEvents(
|
|
|
|
newEventServiceHealthRegister(5, 1, "web"),
|
|
|
|
newEventServiceHealthRegister(5, 2, "web"),
|
|
|
|
newEventServiceHealthRegister(5, 3, "web"))
|
2020-09-18 22:25:56 +00:00
|
|
|
client.QueueEvents(
|
2020-10-03 00:04:45 +00:00
|
|
|
batchEv,
|
2020-10-27 17:43:20 +00:00
|
|
|
newEndOfSnapshotEvent(5))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// This contains the view state so important we share it between calls.
|
|
|
|
opts := cache.FetchOptions{
|
|
|
|
MinIndex: 0,
|
|
|
|
Timeout: 1 * time.Second,
|
|
|
|
}
|
|
|
|
req := &structs.ServiceSpecificRequest{
|
2020-10-30 16:25:22 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "web",
|
2021-02-02 23:04:01 +00:00
|
|
|
EnterpriseMeta: structs.NewEnterpriseMeta(namespace),
|
2020-09-18 22:25:56 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Filter: `Node.Node == "node2"`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
gatherNodes := func(res interface{}) []string {
|
|
|
|
nodes := make([]string, 0, 3)
|
|
|
|
r := res.(*structs.IndexedCheckServiceNodes)
|
|
|
|
for _, csn := range r.Nodes {
|
|
|
|
nodes = append(nodes, csn.Node.Node)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "filtered snapshot returned", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, uint64(5), result.Index)
|
2020-10-03 00:04:45 +00:00
|
|
|
require.Equal(t, []string{"node2"}, gatherNodes(result.Value))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
runStep(t, "filtered updates work too", func(t *testing.T) {
|
2020-09-18 22:25:56 +00:00
|
|
|
// Simulate multiple registrations happening in one Txn (so all have same
|
|
|
|
// index)
|
2020-09-18 22:33:02 +00:00
|
|
|
batchEv := newEventBatchWithEvents(
|
2020-09-18 22:25:56 +00:00
|
|
|
// Deregister an existing node
|
2020-09-18 22:33:02 +00:00
|
|
|
newEventServiceHealthDeregister(20, 1, "web"),
|
2020-09-18 22:25:56 +00:00
|
|
|
// Register another
|
2020-09-18 22:33:02 +00:00
|
|
|
newEventServiceHealthRegister(20, 4, "web"),
|
2020-09-18 22:25:56 +00:00
|
|
|
)
|
2020-10-03 00:04:45 +00:00
|
|
|
client.QueueEvents(batchEv)
|
2020-09-18 22:25:56 +00:00
|
|
|
opts.Timeout = time.Second
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, uint64(20), result.Index)
|
2020-10-03 00:04:45 +00:00
|
|
|
require.Equal(t, []string{"node2"}, gatherNodes(result.Value))
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
opts.MinIndex = result.Index
|
|
|
|
opts.LastResult = &result
|
2020-09-18 22:33:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func runStep(t *testing.T, name string, fn func(t *testing.T)) {
|
2020-10-06 17:52:02 +00:00
|
|
|
t.Helper()
|
2020-09-18 22:33:02 +00:00
|
|
|
if !t.Run(name, fn) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
2021-02-25 23:11:21 +00:00
|
|
|
*/
|
2021-02-08 23:54:37 +00:00
|
|
|
|
|
|
|
func TestNewFilterEvaluator(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
req structs.ServiceSpecificRequest
|
|
|
|
data structs.CheckServiceNode
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := func(t *testing.T, tc testCase) {
|
2021-02-25 21:22:30 +00:00
|
|
|
e, err := newFilterEvaluator(tc.req)
|
2021-02-08 23:54:37 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
actual, err := e.Evaluate(tc.data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCases = []testCase{
|
|
|
|
{
|
|
|
|
name: "single ServiceTags match",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
ServiceTags: []string{"match"},
|
|
|
|
TagFilter: true,
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Tags: []string{"extra", "match"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single deprecated ServiceTag match",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
ServiceTag: "match",
|
|
|
|
TagFilter: true,
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Tags: []string{"extra", "match"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single ServiceTags mismatch",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
ServiceTags: []string{"other"},
|
|
|
|
TagFilter: true,
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Tags: []string{"extra", "match"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple ServiceTags match",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
ServiceTags: []string{"match", "second"},
|
|
|
|
TagFilter: true,
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Tags: []string{"extra", "match", "second"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple ServiceTags mismatch",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
ServiceTags: []string{"match", "not"},
|
|
|
|
TagFilter: true,
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Tags: []string{"extra", "match"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single NodeMetaFilter match",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
NodeMetaFilters: map[string]string{"meta1": "match"},
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
Meta: map[string]string{
|
|
|
|
"meta1": "match",
|
|
|
|
"extra": "some",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single NodeMetaFilter mismatch",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
NodeMetaFilters: map[string]string{
|
|
|
|
"meta1": "match",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
Meta: map[string]string{
|
|
|
|
"meta1": "other",
|
|
|
|
"extra": "some",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple NodeMetaFilter match",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
NodeMetaFilters: map[string]string{"meta1": "match", "meta2": "a"},
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
Meta: map[string]string{
|
|
|
|
"meta1": "match",
|
|
|
|
"meta2": "a",
|
|
|
|
"extra": "some",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple NodeMetaFilter mismatch",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
NodeMetaFilters: map[string]string{
|
|
|
|
"meta1": "match",
|
|
|
|
"meta2": "beta",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
Meta: map[string]string{
|
|
|
|
"meta1": "other",
|
|
|
|
"meta2": "gamma",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "QueryOptions.Filter match",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Filter: `Node.Node == "node3"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{Node: "node3"},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "QueryOptions.Filter mismatch",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Filter: `Node.Node == "node2"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{Node: "node3"},
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all match",
|
|
|
|
req: structs.ServiceSpecificRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Filter: `Node.Node == "node3"`,
|
|
|
|
},
|
|
|
|
ServiceTags: []string{"tag1", "tag2"},
|
|
|
|
NodeMetaFilters: map[string]string{
|
|
|
|
"meta1": "match1",
|
|
|
|
"meta2": "match2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
Node: "node3",
|
|
|
|
Meta: map[string]string{
|
|
|
|
"meta1": "match1",
|
|
|
|
"meta2": "match2",
|
|
|
|
"extra": "other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Tags: []string{"tag1", "tag2", "extra"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
fn(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|