Skip to content

Commit

Permalink
job: Change setPXEFilename -> getPXEFilename
Browse files Browse the repository at this point in the history
This way its easier to test since its side effect free. I was getting a panic
when trying to rework the not-our-ipxe case for an unknown machine because
it would try to call j.Error(). This is just better code.

Signed-off-by: Manuel Mendez <mmendez@equinix.com>
  • Loading branch information
mmlb committed Apr 26, 2021
1 parent edf43e9 commit d2a3c64
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
18 changes: 7 additions & 11 deletions job/dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (j Job) configureDHCP(rep, req *dhcp4.Packet) bool {
ipxe.Setup(rep)
}

j.setPXEFilename(rep, isOuriPXE, isARM, isUEFI)
if filename := j.getPXEFilename(isOuriPXE, isARM, isUEFI); filename != "" {
dhcp.SetFilename(rep, filename, conf.PublicIPv4)
}
}
return true
}
Expand All @@ -97,15 +99,15 @@ func (j Job) areWeProvisioner() bool {
return j.hardware.HardwareProvisioner() == j.ProvisionerEngineName()
}

func (j Job) setPXEFilename(rep *dhcp4.Packet, isOuriPXE, isARM, isUEFI bool) {
func (j Job) getPXEFilename(isOuriPXE, isARM, isUEFI bool) string {
if !j.isPXEAllowed() {
if j.instance != nil && j.instance.State == "active" {
// We set a filename because if a machine is actually trying to PXE and nothing is sent it may hang for
// a while waiting for any possible ProxyDHCP packets and it would delay booting from disks.
// This short cuts all that when we know we want to be booting from disk.
dhcp.SetFilename(rep, "/pxe-is-not-allowed", conf.PublicIPv4)
return "/pxe-is-not-allowed"
}
return
return ""
}

var filename string
Expand All @@ -119,14 +121,8 @@ func (j Job) setPXEFilename(rep *dhcp4.Packet, isOuriPXE, isARM, isUEFI bool) {
} else {
filename = "undionly.kpxe"
}

if filename == "" {
j.Error(errors.New("unable to figure out iPXE to serve"))
return
}
} else {
filename = "http://" + conf.PublicFQDN + "/auto.ipxe"
}

dhcp.SetFilename(rep, filename, conf.PublicIPv4)
return filename
}
13 changes: 4 additions & 9 deletions job/dhcp_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package job

import (
"bytes"
"fmt"
"testing"

"github.com/google/uuid"
dhcp4 "github.com/packethost/dhcp4-go"
assert "github.com/stretchr/testify/require"
"github.com/tinkerbell/boots/conf"
"github.com/tinkerbell/boots/packet"
)

func TestSetPXEFilename(t *testing.T) {
func TestGetPXEFilename(t *testing.T) {
conf.PublicFQDN = "boots-testing.packet.net"

var setPXEFilenameTests = []struct {
var getPXEFilenameTests = []struct {
name string
iState string
plan string
Expand Down Expand Up @@ -53,7 +51,7 @@ func TestSetPXEFilename(t *testing.T) {
filename: "ipxe.efi"},
}

for i, tt := range setPXEFilenameTests {
for i, tt := range getPXEFilenameTests {
t.Run(tt.name, func(t *testing.T) {
t.Logf("index=%d iState=%q plan=%q allowPXE=%v ouriPXE=%v arm=%v uefi=%v filename=%q",
i, tt.iState, tt.plan, tt.allowPXE, tt.ouriPXE, tt.arm, tt.uefi, tt.filename)
Expand All @@ -76,10 +74,7 @@ func TestSetPXEFilename(t *testing.T) {
},
instance: instance,
}
rep := dhcp4.NewPacket(dhcp4.BootReply)
j.setPXEFilename(&rep, tt.ouriPXE, tt.arm, tt.uefi)
filename := string(bytes.TrimRight(rep.File(), "\x00"))

filename := j.getPXEFilename(tt.ouriPXE, tt.arm, tt.uefi)
if tt.filename != filename {
t.Fatalf("unexpected filename want:%q, got:%q", tt.filename, filename)
}
Expand Down

0 comments on commit d2a3c64

Please sign in to comment.