From c8c85523e182537eafbc073146409885f471c869 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 4 May 2021 17:14:21 -0400 Subject: [PATCH] config-entries: add a test for the API client Also fixes a bug with listing kind=mesh config entries. ValidateConfigEntryKind was only being used by the List endpoint, and was yet another place where we have to enumerate all the kinds. This commit removes ValidateConfigEntryKind and uses MakeConfigEntry instead. This change removes the need to maintain two separate functions at the cost of creating an instance of the config entry which will be thrown away immediately. --- agent/consul/config_endpoint.go | 6 ++-- agent/structs/config_entry.go | 15 --------- api/config_entry_test.go | 58 +++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 17 deletions(-) diff --git a/agent/consul/config_endpoint.go b/agent/consul/config_endpoint.go index 9704b30b11..51a919675d 100644 --- a/agent/consul/config_endpoint.go +++ b/agent/consul/config_endpoint.go @@ -162,8 +162,10 @@ func (c *ConfigEntry) List(args *structs.ConfigEntryQuery, reply *structs.Indexe return err } - if args.Kind != "" && !structs.ValidateConfigEntryKind(args.Kind) { - return fmt.Errorf("invalid config entry kind: %s", args.Kind) + if args.Kind != "" { + if _, err := structs.MakeConfigEntry(args.Kind, ""); err != nil { + return fmt.Errorf("invalid config entry kind: %s", args.Kind) + } } return c.srv.blockingQuery( diff --git a/agent/structs/config_entry.go b/agent/structs/config_entry.go index e05e131efa..3f7145ee2b 100644 --- a/agent/structs/config_entry.go +++ b/agent/structs/config_entry.go @@ -535,21 +535,6 @@ func MakeConfigEntry(kind, name string) (ConfigEntry, error) { } } -func ValidateConfigEntryKind(kind string) bool { - switch kind { - case ServiceDefaults, ProxyDefaults: - return true - case ServiceRouter, ServiceSplitter, ServiceResolver: - return true - case IngressGateway, TerminatingGateway: - return true - case ServiceIntentions: - return true - default: - return false - } -} - // ConfigEntryQuery is used when requesting info about a config entry. type ConfigEntryQuery struct { Kind string diff --git a/api/config_entry_test.go b/api/config_entry_test.go index 74f92afe88..85ee5c4364 100644 --- a/api/config_entry_test.go +++ b/api/config_entry_test.go @@ -196,6 +196,64 @@ func TestAPI_ConfigEntries(t *testing.T) { _, _, err = config_entries.Get(ServiceDefaults, "foo", nil) require.Error(t, err) }) + + t.Run("Mesh", func(t *testing.T) { + mesh := &MeshConfigEntry{ + TransparentProxy: TransparentProxyMeshConfig{CatalogDestinationsOnly: true}, + Meta: map[string]string{ + "foo": "bar", + "gir": "zim", + }, + } + ce := c.ConfigEntries() + + runStep(t, "set and get", func(t *testing.T) { + _, wm, err := ce.Set(mesh, nil) + require.NoError(t, err) + require.NotNil(t, wm) + require.NotEqual(t, 0, wm.RequestTime) + + entry, qm, err := ce.Get(MeshConfig, MeshConfigMesh, nil) + require.NoError(t, err) + require.NotNil(t, qm) + require.NotEqual(t, 0, qm.RequestTime) + + result, ok := entry.(*MeshConfigEntry) + require.True(t, ok) + + // ignore indexes + result.CreateIndex = 0 + result.ModifyIndex = 0 + require.Equal(t, mesh, result) + }) + + runStep(t, "list", func(t *testing.T) { + entries, qm, err := ce.List(MeshConfig, nil) + require.NoError(t, err) + require.NotNil(t, qm) + require.NotEqual(t, 0, qm.RequestTime) + require.Len(t, entries, 1) + }) + + runStep(t, "delete", func(t *testing.T) { + wm, err := ce.Delete(MeshConfig, MeshConfigMesh, nil) + require.NoError(t, err) + require.NotNil(t, wm) + require.NotEqual(t, 0, wm.RequestTime) + + // verify deletion + _, _, err = ce.Get(MeshConfig, MeshConfigMesh, nil) + require.Error(t, err) + }) + }) + +} + +func runStep(t *testing.T, name string, fn func(t *testing.T)) { + t.Helper() + if !t.Run(name, fn) { + t.FailNow() + } } func TestDecodeConfigEntry(t *testing.T) {