From 9b2fae9bac0e77a1cc178421b84b7a4cbac79e1a Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 30 Oct 2020 12:25:22 -0400 Subject: [PATCH] cache-type: use namespace in tests to verify that the namespace is passed through correctly to the server. --- .../streaming_health_services_test.go | 41 +++++++++++++------ agent/cache-types/streaming_test.go | 17 +++++--- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/agent/cache-types/streaming_health_services_test.go b/agent/cache-types/streaming_health_services_test.go index 5c963c7a8a..768962aa83 100644 --- a/agent/cache-types/streaming_health_services_test.go +++ b/agent/cache-types/streaming_health_services_test.go @@ -14,11 +14,13 @@ import ( "github.com/hashicorp/consul/agent/cache" "github.com/hashicorp/consul/agent/structs" + "github.com/hashicorp/consul/proto/pbcommon" "github.com/hashicorp/consul/proto/pbsubscribe" ) func TestStreamingHealthServices_EmptySnapshot(t *testing.T) { - client := NewTestStreamingClient() + namespace := pbcommon.DefaultEnterpriseMeta.Namespace + client := NewTestStreamingClient(namespace) typ := StreamingHealthServices{deps: MaterializerDeps{ Client: client, Logger: hclog.Default(), @@ -33,8 +35,9 @@ func TestStreamingHealthServices_EmptySnapshot(t *testing.T) { Timeout: time.Second, } req := &structs.ServiceSpecificRequest{ - Datacenter: "dc1", - ServiceName: "web", + Datacenter: "dc1", + ServiceName: "web", + EnterpriseMeta: structs.EnterpriseMetaInitializer(namespace), } empty := &structs.IndexedCheckServiceNodes{ Nodes: structs.CheckServiceNodes{}, @@ -215,8 +218,17 @@ func requireResultsSame(t *testing.T, want, got *structs.IndexedCheckServiceNode require.ElementsMatch(t, wantIDs, gotIDs) } +// 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 { + meta := structs.EnterpriseMetaInitializer(ns) + return meta.GetNamespace() +} + func TestStreamingHealthServices_FullSnapshot(t *testing.T) { - client := NewTestStreamingClient() + namespace := getNamespace("ns2") + client := NewTestStreamingClient(namespace) typ := StreamingHealthServices{deps: MaterializerDeps{ Client: client, Logger: hclog.Default(), @@ -238,8 +250,9 @@ func TestStreamingHealthServices_FullSnapshot(t *testing.T) { Timeout: 1 * time.Second, } req := &structs.ServiceSpecificRequest{ - Datacenter: "dc1", - ServiceName: "web", + Datacenter: "dc1", + ServiceName: "web", + EnterpriseMeta: structs.EnterpriseMetaInitializer(namespace), } gatherNodes := func(res interface{}) []string { @@ -345,7 +358,8 @@ func TestStreamingHealthServices_FullSnapshot(t *testing.T) { } func TestStreamingHealthServices_EventBatches(t *testing.T) { - client := NewTestStreamingClient() + namespace := getNamespace("ns3") + client := NewTestStreamingClient(namespace) typ := StreamingHealthServices{deps: MaterializerDeps{ Client: client, Logger: hclog.Default(), @@ -366,8 +380,9 @@ func TestStreamingHealthServices_EventBatches(t *testing.T) { Timeout: 1 * time.Second, } req := &structs.ServiceSpecificRequest{ - Datacenter: "dc1", - ServiceName: "web", + Datacenter: "dc1", + ServiceName: "web", + EnterpriseMeta: structs.EnterpriseMetaInitializer(namespace), } gatherNodes := func(res interface{}) []string { @@ -415,7 +430,8 @@ func TestStreamingHealthServices_EventBatches(t *testing.T) { } func TestStreamingHealthServices_Filtering(t *testing.T) { - client := NewTestStreamingClient() + namespace := getNamespace("ns3") + client := NewTestStreamingClient(namespace) typ := StreamingHealthServices{deps: MaterializerDeps{ Client: client, Logger: hclog.Default(), @@ -436,8 +452,9 @@ func TestStreamingHealthServices_Filtering(t *testing.T) { Timeout: 1 * time.Second, } req := &structs.ServiceSpecificRequest{ - Datacenter: "dc1", - ServiceName: "web", + Datacenter: "dc1", + ServiceName: "web", + EnterpriseMeta: structs.EnterpriseMetaInitializer(namespace), QueryOptions: structs.QueryOptions{ Filter: `Node.Node == "node2"`, }, diff --git a/agent/cache-types/streaming_test.go b/agent/cache-types/streaming_test.go index 09721817a0..b12809c3c5 100644 --- a/agent/cache-types/streaming_test.go +++ b/agent/cache-types/streaming_test.go @@ -2,6 +2,7 @@ package cachetype import ( "context" + "fmt" "google.golang.org/grpc" @@ -12,8 +13,9 @@ import ( // for queueing up custom events to a subscriber. type TestStreamingClient struct { pbsubscribe.StateChangeSubscription_SubscribeClient - events chan eventOrErr - ctx context.Context + events chan eventOrErr + ctx context.Context + expectedNamespace string } type eventOrErr struct { @@ -21,17 +23,22 @@ type eventOrErr struct { Event *pbsubscribe.Event } -func NewTestStreamingClient() *TestStreamingClient { +func NewTestStreamingClient(ns string) *TestStreamingClient { return &TestStreamingClient{ - events: make(chan eventOrErr, 32), + events: make(chan eventOrErr, 32), + expectedNamespace: ns, } } func (t *TestStreamingClient) Subscribe( ctx context.Context, - _ *pbsubscribe.SubscribeRequest, + req *pbsubscribe.SubscribeRequest, _ ...grpc.CallOption, ) (pbsubscribe.StateChangeSubscription_SubscribeClient, error) { + if req.Namespace != t.expectedNamespace { + return nil, fmt.Errorf("wrong SubscribeRequest.Namespace %v, expected %v", + req.Namespace, t.expectedNamespace) + } t.ctx = ctx return t, nil }