From 48fdcab47764b7618987e3bb34a1edca6d8705ce Mon Sep 17 00:00:00 2001 From: Mike Mason Date: Wed, 11 Aug 2021 16:32:55 -0500 Subject: [PATCH] handle instance path with no data gracefully --- installers/custom_ipxe/main.go | 14 +++-- installers/custom_ipxe/main_test.go | 79 +++++++++++++++++++++++++++-- job/mock.go | 4 ++ 3 files changed, 89 insertions(+), 8 deletions(-) diff --git a/installers/custom_ipxe/main.go b/installers/custom_ipxe/main.go index 72d2d61b6..d5cd40f73 100644 --- a/installers/custom_ipxe/main.go +++ b/installers/custom_ipxe/main.go @@ -28,12 +28,16 @@ func ipxeScript(j job.Job, s *ipxe.Script) { return } + } else if strings.HasPrefix(j.UserData(), "#!ipxe") { + cfg = &packet.InstallerData{Script: j.UserData()} + } else if j.IPXEScriptURL() != "" { + cfg = &packet.InstallerData{Chain: j.IPXEScriptURL()} } else { - if strings.HasPrefix(j.UserData(), "#!ipxe") { - cfg = &packet.InstallerData{Script: j.UserData()} - } else { - cfg = &packet.InstallerData{Chain: j.IPXEScriptURL()} - } + s.Echo("Unknown ipxe configuration") + s.Shell() + logger.Error(ErrEmptyIpxeConfig, "unknown ipxe configuration") + + return } ipxeScriptFromConfig(logger, cfg, j, s) diff --git a/installers/custom_ipxe/main_test.go b/installers/custom_ipxe/main_test.go index bc1cabc5e..7369aa6c2 100644 --- a/installers/custom_ipxe/main_test.go +++ b/installers/custom_ipxe/main_test.go @@ -33,11 +33,13 @@ func TestMain(m *testing.M) { func TestIpxeScript(t *testing.T) { var testCases = []struct { name string + installer string installerData *packet.InstallerData want string }{ { - "invalid config", + "installer: invalid config", + "custom_ipxe", nil, `#!ipxe @@ -47,6 +49,53 @@ func TestIpxeScript(t *testing.T) { }, { "valid config", + "custom_ipxe", + &packet.InstallerData{Chain: "http://url/path.ipxe"}, + `#!ipxe + + + params + param body Device connected to DHCP system + param type provisioning.104.01 + imgfetch ${tinkerbell}/phone-home##params + imgfree + + set packet_facility test.facility + set packet_plan test.slug + chain --autofree http://url/path.ipxe + `, + }, + { + "installer: valid config", + "", + &packet.InstallerData{Chain: "http://url/path.ipxe"}, + `#!ipxe + + + params + param body Device connected to DHCP system + param type provisioning.104.01 + imgfetch ${tinkerbell}/phone-home##params + imgfree + + set packet_facility test.facility + set packet_plan test.slug + chain --autofree http://url/path.ipxe + `, + }, + { + "instance: no config", + "", + nil, + `#!ipxe + + echo Unknown ipxe configuration + shell + `, + }, + { + "instance: ipxe script url", + "", &packet.InstallerData{Chain: "http://url/path.ipxe"}, `#!ipxe @@ -62,6 +111,25 @@ func TestIpxeScript(t *testing.T) { chain --autofree http://url/path.ipxe `, }, + { + "instance: userdata script", + "", + &packet.InstallerData{Script: "#!ipxe\necho userdata script"}, + `#!ipxe + + + params + param body Device connected to DHCP system + param type provisioning.104.01 + imgfetch ${tinkerbell}/phone-home##params + imgfree + + set packet_facility test.facility + set packet_plan test.slug + + echo userdata script + `, + }, } for _, tc := range testCases { @@ -70,8 +138,13 @@ func TestIpxeScript(t *testing.T) { mockJob := job.NewMock(t, "test.slug", "test.facility") script := ipxe.NewScript() - mockJob.SetOSInstaller("custom_ipxe") - mockJob.SetOSInstallerData(tc.installerData) + if tc.installer == "custom_ipxe" { + mockJob.SetOSInstaller("custom_ipxe") + mockJob.SetOSInstallerData(tc.installerData) + } else if tc.installerData != nil { + mockJob.SetIPXEScriptURL(tc.installerData.Chain) + mockJob.SetUserData(tc.installerData.Script) + } ipxeScript(mockJob.Job(), script) diff --git a/job/mock.go b/job/mock.go index 718f29efc..7650477ae 100644 --- a/job/mock.go +++ b/job/mock.go @@ -80,6 +80,10 @@ func (m *Mock) SetIPXEScriptURL(url string) { m.instance.IPXEScriptURL = url } +func (m *Mock) SetUserData(userdata string) { + m.instance.UserData = userdata +} + func (m *Mock) SetMAC(mac string) { _m, err := net.ParseMAC(mac) if err != nil {