open-consul/agent/proxycfg-glue/config_entry_test.go
Daniel Upton 497df1ca3b proxycfg: server-local config entry data sources
This is the OSS portion of enterprise PR 2056.

This commit provides server-local implementations of the proxycfg.ConfigEntry
and proxycfg.ConfigEntryList interfaces, that source data from streaming events.

It makes use of the LocalMaterializer type introduced for peering replication,
adding the necessary support for authorization.

It also adds support for "wildcard" subscriptions (within a topic) to the event
publisher, as this is needed to fetch service-resolvers for all services when
configuring mesh gateways.

Currently, events will be emitted for just the ingress-gateway, service-resolver,
and mesh config entry types, as these are the only entries required by proxycfg
— the events will be emitted on topics named IngressGateway, ServiceResolver,
and MeshConfig topics respectively.

Though these events will only be consumed "locally" for now, they can also be
consumed via the gRPC endpoint (confirmed using grpcurl) so using them from
client agents should be a case of swapping the LocalMaterializer for an
RPCMaterializer.
2022-07-04 10:48:36 +01:00

184 lines
5.4 KiB
Go

package proxycfgglue
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/proto/pbconfigentry"
"github.com/hashicorp/consul/proto/pbsubscribe"
"github.com/hashicorp/consul/sdk/testutil"
)
func TestConfigEntryView(t *testing.T) {
const index uint64 = 123
view := &configEntryView{}
testutil.RunStep(t, "initial state", func(t *testing.T) {
result := view.Result(index)
resp, ok := result.(*structs.ConfigEntryResponse)
require.Truef(t, ok, "expected ConfigEntryResponse, got: %T", result)
require.Nil(t, resp.Entry)
require.Equal(t, index, resp.QueryMeta.Index)
})
testutil.RunStep(t, "upsert event", func(t *testing.T) {
err := view.Update([]*pbsubscribe.Event{
{
Index: index,
Payload: &pbsubscribe.Event_ConfigEntry{
ConfigEntry: &pbsubscribe.ConfigEntryUpdate{
Op: pbsubscribe.ConfigEntryUpdate_Upsert,
ConfigEntry: &pbconfigentry.ConfigEntry{
Kind: pbconfigentry.Kind_KindServiceResolver,
Name: "web",
Entry: &pbconfigentry.ConfigEntry_ServiceResolver{
ServiceResolver: &pbconfigentry.ServiceResolver{},
},
},
},
},
},
})
require.NoError(t, err)
result := view.Result(index)
resp, ok := result.(*structs.ConfigEntryResponse)
require.Truef(t, ok, "expected ConfigEntryResponse, got: %T", result)
serviceResolver, ok := resp.Entry.(*structs.ServiceResolverConfigEntry)
require.Truef(t, ok, "expected ServiceResolverConfigEntry, got: %T", resp.Entry)
require.Equal(t, "web", serviceResolver.Name)
})
testutil.RunStep(t, "delete event", func(t *testing.T) {
err := view.Update([]*pbsubscribe.Event{
{
Index: index,
Payload: &pbsubscribe.Event_ConfigEntry{
ConfigEntry: &pbsubscribe.ConfigEntryUpdate{
Op: pbsubscribe.ConfigEntryUpdate_Delete,
ConfigEntry: &pbconfigentry.ConfigEntry{
Kind: pbconfigentry.Kind_KindServiceResolver,
Name: "web",
Entry: &pbconfigentry.ConfigEntry_ServiceResolver{
ServiceResolver: &pbconfigentry.ServiceResolver{},
},
},
},
},
},
})
require.NoError(t, err)
result := view.Result(index)
resp, ok := result.(*structs.ConfigEntryResponse)
require.Truef(t, ok, "expected ConfigEntryResponse, got: %T", result)
require.Nil(t, resp.Entry)
})
testutil.RunStep(t, "bogus event", func(t *testing.T) {
err := view.Update([]*pbsubscribe.Event{
{
Index: index,
Payload: &pbsubscribe.Event_ServiceHealth{},
},
})
require.NoError(t, err)
result := view.Result(index)
resp, ok := result.(*structs.ConfigEntryResponse)
require.Truef(t, ok, "expected ConfigEntryResponse, got: %T", result)
require.Nil(t, resp.Entry)
})
}
func TestConfigEntryListView(t *testing.T) {
const index uint64 = 123
view := newConfigEntryListView(structs.ServiceResolver, *acl.DefaultEnterpriseMeta())
testutil.RunStep(t, "initial state", func(t *testing.T) {
result := view.Result(index)
resp, ok := result.(*structs.IndexedConfigEntries)
require.Truef(t, ok, "expected IndexedConfigEntries, got: %T", result)
require.Empty(t, resp.Entries)
require.Equal(t, index, resp.QueryMeta.Index)
})
testutil.RunStep(t, "upsert events", func(t *testing.T) {
err := view.Update([]*pbsubscribe.Event{
{
Index: index,
Payload: &pbsubscribe.Event_ConfigEntry{
ConfigEntry: &pbsubscribe.ConfigEntryUpdate{
Op: pbsubscribe.ConfigEntryUpdate_Upsert,
ConfigEntry: &pbconfigentry.ConfigEntry{
Kind: pbconfigentry.Kind_KindServiceResolver,
Name: "web",
Entry: &pbconfigentry.ConfigEntry_ServiceResolver{
ServiceResolver: &pbconfigentry.ServiceResolver{},
},
},
},
},
},
{
Index: index,
Payload: &pbsubscribe.Event_ConfigEntry{
ConfigEntry: &pbsubscribe.ConfigEntryUpdate{
Op: pbsubscribe.ConfigEntryUpdate_Upsert,
ConfigEntry: &pbconfigentry.ConfigEntry{
Kind: pbconfigentry.Kind_KindServiceResolver,
Name: "db",
Entry: &pbconfigentry.ConfigEntry_ServiceResolver{
ServiceResolver: &pbconfigentry.ServiceResolver{},
},
},
},
},
},
})
require.NoError(t, err)
result := view.Result(index)
resp, ok := result.(*structs.IndexedConfigEntries)
require.Truef(t, ok, "expected IndexedConfigEntries, got: %T", result)
require.Len(t, resp.Entries, 2)
})
testutil.RunStep(t, "delete event", func(t *testing.T) {
err := view.Update([]*pbsubscribe.Event{
{
Index: index,
Payload: &pbsubscribe.Event_ConfigEntry{
ConfigEntry: &pbsubscribe.ConfigEntryUpdate{
Op: pbsubscribe.ConfigEntryUpdate_Delete,
ConfigEntry: &pbconfigentry.ConfigEntry{
Kind: pbconfigentry.Kind_KindServiceResolver,
Name: "web",
Entry: &pbconfigentry.ConfigEntry_ServiceResolver{
ServiceResolver: &pbconfigentry.ServiceResolver{},
},
},
},
},
},
})
require.NoError(t, err)
result := view.Result(index)
resp, ok := result.(*structs.IndexedConfigEntries)
require.Truef(t, ok, "expected IndexedConfigEntries, got: %T", result)
require.Len(t, resp.Entries, 1)
serviceResolver, ok := resp.Entries[0].(*structs.ServiceResolverConfigEntry)
require.Truef(t, ok, "expected ServiceResolverConfigEntry, got: %T", resp.Entries[0])
require.Equal(t, "db", serviceResolver.Name)
})
}