Skip to content

Commit

Permalink
handle instance path with no data gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemrm committed Aug 11, 2021
1 parent daaeacb commit 48fdcab
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
14 changes: 9 additions & 5 deletions installers/custom_ipxe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
79 changes: 76 additions & 3 deletions installers/custom_ipxe/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions job/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 48fdcab

Please sign in to comment.