Skip to content

Commit

Permalink
Added test fixtures and removed the IsEmpty check
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekStrickland committed Feb 11, 2022
1 parent 4bc254d commit 5398ef5
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 21 deletions.
17 changes: 0 additions & 17 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions command/agent/config_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
53 changes: 53 additions & 0 deletions command/agent/config_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
6 changes: 6 additions & 0 deletions command/agent/test-resources/client_with_empty_template.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
client {
enabled = true

template {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
client {
enabled = true

template {
function_denylist = ["foo"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
client {
enabled = true

template {
disable_file_sandbox = true
function_denylist = []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
client {
enabled = true

template {
disable_file_sandbox = true
function_denylist = [""]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
client {
enabled = true

template {
disable_file_sandbox = true
}
}

0 comments on commit 5398ef5

Please sign in to comment.