From 51798e483c098643e38c5f92e089165d8330f084 Mon Sep 17 00:00:00 2001 From: MatthewHink Date: Tue, 25 Jan 2022 18:39:42 -0500 Subject: [PATCH] Missing hyphen in dhcp template path. Unit tests. This is a single character bug fix. Broke this out into a function for testing. --- .../wwctl/configure/dhcp/dhcp_main_test.go | 25 +++++++++++++++++++ internal/app/wwctl/configure/dhcp/main.go | 25 +++++++++++-------- 2 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 internal/app/wwctl/configure/dhcp/dhcp_main_test.go diff --git a/internal/app/wwctl/configure/dhcp/dhcp_main_test.go b/internal/app/wwctl/configure/dhcp/dhcp_main_test.go new file mode 100644 index 00000000..9aa96fa3 --- /dev/null +++ b/internal/app/wwctl/configure/dhcp/dhcp_main_test.go @@ -0,0 +1,25 @@ +package dhcp + +import ( + "testing" +) + +func TestDhcpTemplateFile(t *testing.T) { + tests := []struct{ + parameter string + expected string + }{ + {"", "/usr/local/etc/warewulf/dhcp/default-dhcpd.conf"}, + {"default", "/usr/local/etc/warewulf/dhcp/default-dhcpd.conf"}, + {"static", "/usr/local/etc/warewulf/dhcp/static-dhcpd.conf"}, + {"/test/absolute/path.conf", "/test/absolute/path.conf"}, + } + for _, tt := range tests { + actual := dhcpTemplateFile(tt.parameter) + if actual != tt.expected { + t.Errorf("dhcpTemplateFile(%v) expected: %v, actual: %v", + tt.parameter, tt.expected, actual) + } + } +} + diff --git a/internal/app/wwctl/configure/dhcp/main.go b/internal/app/wwctl/configure/dhcp/main.go index 8931c669..f9863584 100644 --- a/internal/app/wwctl/configure/dhcp/main.go +++ b/internal/app/wwctl/configure/dhcp/main.go @@ -84,16 +84,7 @@ func Configure(show bool) error { d.Nodes = append(d.Nodes, nodes...) - if controller.Dhcp.Template == "" { - templateFile = path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/default-dhcpd.conf") - } else { - if strings.HasPrefix(controller.Dhcp.Template, "/") { - templateFile = controller.Dhcp.Template - } else { - templateFile = path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/"+controller.Dhcp.Template+"dhcpd.conf") - } - } - + templateFile = dhcpTemplateFile(controller.Dhcp.Template) tmpl, err := template.New(path.Base(templateFile)).ParseFiles(templateFile) if err != nil { wwlog.Printf(wwlog.ERROR, "%s\n", err) @@ -137,3 +128,17 @@ func Configure(show bool) error { return nil } + +// dhcpTemplateFile returns the path of the warewulf dhcp template given controller.Dhcp.Template. +func dhcpTemplateFile(controllerDhcpTemplate string) (templateFile string) { + if controllerDhcpTemplate == "" { + templateFile = path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/default-dhcpd.conf") + } else { + if strings.HasPrefix(controllerDhcpTemplate, "/") { + templateFile = controllerDhcpTemplate + } else { + templateFile = path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/"+controllerDhcpTemplate+"-dhcpd.conf") + } + } + return +}