From 5398ef5efe268d157a9d48cffe3a712498ee73f1 Mon Sep 17 00:00:00 2001 From: DerekStrickland Date: Fri, 11 Feb 2022 11:14:35 -0500 Subject: [PATCH] Added test fixtures and removed the IsEmpty check --- client/config/config.go | 17 ------ command/agent/config_parse.go | 4 -- command/agent/config_parse_test.go | 53 +++++++++++++++++++ .../client_with_empty_template.hcl | 6 +++ .../client_with_function_denylist.hcl | 7 +++ .../client_with_function_denylist_empty.hcl | 8 +++ ...nt_with_function_denylist_empty_string.hcl | 8 +++ .../client_with_function_denylist_nil.hcl | 7 +++ 8 files changed, 89 insertions(+), 21 deletions(-) diff --git a/client/config/config.go b/client/config/config.go index 9ec256eabb6d..7591e91da986 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -445,23 +445,6 @@ func (c *ClientTemplateConfig) Merge(b *ClientTemplateConfig) *ClientTemplateCon return &result } -func (c *ClientTemplateConfig) IsEmpty() bool { - if c == nil { - return true - } - - return !c.DisableSandbox && - len(c.FunctionDenylist) == 0 && - len(c.FunctionBlacklist) == 0 && - c.BlockQueryWaitTime == nil && - c.BlockQueryWaitTimeHCL == "" && - c.MaxStale == nil && - c.MaxStaleHCL == "" && - c.Wait.IsEmpty() && - c.ConsulRetry.IsEmpty() && - c.VaultRetry.IsEmpty() -} - // WaitConfig is mirrored from templateconfig.WaitConfig because we need to handle // the HCL conversion which happens in agent.ParseConfigFile // NOTE: Since Consul Template requires pointers, this type uses pointers to fields diff --git a/command/agent/config_parse.go b/command/agent/config_parse.go index 363db9e1e331..589265ecec56 100644 --- a/command/agent/config_parse.go +++ b/command/agent/config_parse.go @@ -259,8 +259,4 @@ func finalizeClientTemplateConfig(config *Config) { if config.Client.TemplateConfig.VaultRetry.IsEmpty() { config.Client.TemplateConfig.VaultRetry = nil } - - if config.Client.TemplateConfig.IsEmpty() { - config.Client.TemplateConfig = nil - } } diff --git a/command/agent/config_parse_test.go b/command/agent/config_parse_test.go index 5ee1305012f9..10fd9d9061aa 100644 --- a/command/agent/config_parse_test.go +++ b/command/agent/config_parse_test.go @@ -860,3 +860,56 @@ func permutations(arr []string) [][]string { helper(arr, len(arr)) return res } + +func TestConfig_Parse_Client_TemplateConfig_FunctionDenylist(t *testing.T) { + t.Parallel() + + defaultFunctionDenyList := DefaultConfig().Client.TemplateConfig.FunctionDenylist + + cases := []struct { + File string + Expected []string + }{ + { + "client_with_function_denylist.hcl", + []string{"foo"}, + }, + { + "client_with_function_denylist_empty.hcl", + nil, + }, + { + "client_with_function_denylist_empty_string.hcl", + nil, + }, + { + "client_with_function_denylist_nil.hcl", + defaultFunctionDenyList, + }, + { + "client_with_empty_template.hcl", + defaultFunctionDenyList, + }, + { + "minimal_client.hcl", + defaultFunctionDenyList, + }, + } + + for _, tc := range cases { + t.Run(tc.File, func(t *testing.T) { + path, err := filepath.Abs(filepath.Join("./test-resources", tc.File)) + require.NoError(t, err) + + parsed, err := ParseConfigFile(path) + require.NoError(t, err) + + var actual []string + if tc.Expected != nil { + actual = parsed.Client.TemplateConfig.FunctionDenylist + } + + require.EqualValues(t, tc.Expected, actual) + }) + } +} diff --git a/command/agent/test-resources/client_with_empty_template.hcl b/command/agent/test-resources/client_with_empty_template.hcl index e69de29bb2d1..7d0eeec11297 100644 --- a/command/agent/test-resources/client_with_empty_template.hcl +++ b/command/agent/test-resources/client_with_empty_template.hcl @@ -0,0 +1,6 @@ +client { + enabled = true + + template { + } +} diff --git a/command/agent/test-resources/client_with_function_denylist.hcl b/command/agent/test-resources/client_with_function_denylist.hcl index e69de29bb2d1..3efa76b542f4 100644 --- a/command/agent/test-resources/client_with_function_denylist.hcl +++ b/command/agent/test-resources/client_with_function_denylist.hcl @@ -0,0 +1,7 @@ +client { + enabled = true + + template { + function_denylist = ["foo"] + } +} diff --git a/command/agent/test-resources/client_with_function_denylist_empty.hcl b/command/agent/test-resources/client_with_function_denylist_empty.hcl index e69de29bb2d1..b8a529f2e634 100644 --- a/command/agent/test-resources/client_with_function_denylist_empty.hcl +++ b/command/agent/test-resources/client_with_function_denylist_empty.hcl @@ -0,0 +1,8 @@ +client { + enabled = true + + template { + disable_file_sandbox = true + function_denylist = [] + } +} diff --git a/command/agent/test-resources/client_with_function_denylist_empty_string.hcl b/command/agent/test-resources/client_with_function_denylist_empty_string.hcl index e69de29bb2d1..91f3b3910d5f 100644 --- a/command/agent/test-resources/client_with_function_denylist_empty_string.hcl +++ b/command/agent/test-resources/client_with_function_denylist_empty_string.hcl @@ -0,0 +1,8 @@ +client { + enabled = true + + template { + disable_file_sandbox = true + function_denylist = [""] + } +} diff --git a/command/agent/test-resources/client_with_function_denylist_nil.hcl b/command/agent/test-resources/client_with_function_denylist_nil.hcl index e69de29bb2d1..15f090bb7a55 100644 --- a/command/agent/test-resources/client_with_function_denylist_nil.hcl +++ b/command/agent/test-resources/client_with_function_denylist_nil.hcl @@ -0,0 +1,7 @@ +client { + enabled = true + + template { + disable_file_sandbox = true + } +}