From b95b14e168b29da9b031dfc48f50b3e3bb943294 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 28 Oct 2020 19:25:15 -0400 Subject: [PATCH] state: test EventPayloadCheckServiceNode.FilterByKey Also fix a bug in that function when only one of key or namespace were the empty string. --- agent/consul/state/catalog_events.go | 3 +- agent/consul/state/catalog_events_test.go | 97 +++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/agent/consul/state/catalog_events.go b/agent/consul/state/catalog_events.go index d3ee638063..54b67ef2ee 100644 --- a/agent/consul/state/catalog_events.go +++ b/agent/consul/state/catalog_events.go @@ -32,7 +32,8 @@ func (e EventPayloadCheckServiceNode) FilterByKey(key, namespace string) bool { if e.key != "" { name = e.key } - return key == name && namespace == e.Value.Service.EnterpriseMeta.GetNamespace() + ns := e.Value.Service.EnterpriseMeta.GetNamespace() + return (key == "" || key == name) && (namespace == "" || namespace == ns) } // serviceHealthSnapshot returns a stream.SnapshotFunc that provides a snapshot diff --git a/agent/consul/state/catalog_events_test.go b/agent/consul/state/catalog_events_test.go index ac4a07d67e..1efead52ad 100644 --- a/agent/consul/state/catalog_events_test.go +++ b/agent/consul/state/catalog_events_test.go @@ -8,6 +8,8 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/stretchr/testify/require" + "github.com/hashicorp/consul/proto/pbcommon" + "github.com/hashicorp/consul/agent/consul/stream" "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/api" @@ -1459,3 +1461,98 @@ func newTestEventServiceHealthDeregister(index uint64, nodeNum int, svc string) }, } } + +func TestEventPayloadCheckServiceNode_FilterByKey(t *testing.T) { + type testCase struct { + name string + payload EventPayloadCheckServiceNode + key string + namespace string + expected bool + } + + fn := func(t *testing.T, tc testCase) { + if tc.namespace != "" && pbcommon.DefaultEnterpriseMeta.Namespace == "" { + t.Skip("cant test namespace matching without namespace support") + } + + require.Equal(t, tc.expected, tc.payload.FilterByKey(tc.key, tc.namespace)) + } + + var testCases = []testCase{ + { + name: "no key or namespace", + payload: newPayloadCheckServiceNode("srv1", "ns1"), + expected: true, + }, + { + name: "no key, with namespace match", + payload: newPayloadCheckServiceNode("srv1", "ns1"), + namespace: "ns1", + expected: true, + }, + { + name: "no namespace, with key match", + payload: newPayloadCheckServiceNode("srv1", "ns1"), + key: "srv1", + expected: true, + }, + { + name: "key match, namespace mismatch", + payload: newPayloadCheckServiceNode("srv1", "ns1"), + key: "srv1", + namespace: "ns2", + expected: false, + }, + { + name: "key mismatch, namespace match", + payload: newPayloadCheckServiceNode("srv1", "ns1"), + key: "srv2", + namespace: "ns1", + expected: false, + }, + { + name: "override key match", + payload: newPayloadCheckServiceNodeWithKey("proxy", "ns1", "srv1"), + key: "srv1", + namespace: "ns1", + expected: true, + }, + { + name: "override key match", + payload: newPayloadCheckServiceNodeWithKey("proxy", "ns1", "srv2"), + key: "proxy", + namespace: "ns1", + expected: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + fn(t, tc) + }) + } +} + +func newPayloadCheckServiceNode(service, namespace string) EventPayloadCheckServiceNode { + return EventPayloadCheckServiceNode{ + Value: &structs.CheckServiceNode{ + Service: &structs.NodeService{ + Service: service, + EnterpriseMeta: structs.EnterpriseMetaInitializer(namespace), + }, + }, + } +} + +func newPayloadCheckServiceNodeWithKey(service, namespace, key string) EventPayloadCheckServiceNode { + return EventPayloadCheckServiceNode{ + Value: &structs.CheckServiceNode{ + Service: &structs.NodeService{ + Service: service, + EnterpriseMeta: structs.EnterpriseMetaInitializer(namespace), + }, + }, + key: key, + } +}