Missing hyphen in dhcp template path. Unit tests.

This is a single character bug fix.
Broke this out into a function for testing.
This commit is contained in:
MatthewHink
2022-01-25 18:39:42 -05:00
parent b038a8817b
commit 51798e483c
2 changed files with 40 additions and 10 deletions

View File

@@ -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)
}
}
}

View File

@@ -84,16 +84,7 @@ func Configure(show bool) error {
d.Nodes = append(d.Nodes, nodes...) d.Nodes = append(d.Nodes, nodes...)
if controller.Dhcp.Template == "" { templateFile = dhcpTemplateFile(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")
}
}
tmpl, err := template.New(path.Base(templateFile)).ParseFiles(templateFile) tmpl, err := template.New(path.Base(templateFile)).ParseFiles(templateFile)
if err != nil { if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err) wwlog.Printf(wwlog.ERROR, "%s\n", err)
@@ -137,3 +128,17 @@ func Configure(show bool) error {
return nil 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
}