diff --git a/internal/pkg/container/grub.go b/internal/pkg/container/grub.go index ab6ba5c3..443ae995 100644 --- a/internal/pkg/container/grub.go +++ b/internal/pkg/container/grub.go @@ -30,8 +30,8 @@ func grubNames() []string { find a grub.efi in the used container */ func GrubFind(container string) string { - wwlog.Debug("Finding grub") container_path := RootFsDir(container) + wwlog.Debug("Finding grub under paths: %s", container_path) if container_path == "" { return "" } diff --git a/internal/pkg/container/shim.go b/internal/pkg/container/shim.go index 9517d2b9..6d1efe5e 100644 --- a/internal/pkg/container/shim.go +++ b/internal/pkg/container/shim.go @@ -28,8 +28,8 @@ func shimNames() []string { find the path of the shim binary */ func ShimFind(container string) string { - wwlog.Debug("Finding shim for container %s", container) container_path := RootFsDir(container) + wwlog.Debug("Finding shim under path: %s", container_path) if container_path == "" { return "" } diff --git a/internal/pkg/warewulfd/parser.go b/internal/pkg/warewulfd/parser.go index 5923bf25..063d10b4 100644 --- a/internal/pkg/warewulfd/parser.go +++ b/internal/pkg/warewulfd/parser.go @@ -87,7 +87,7 @@ func parseReq(req *http.Request) (parserInfo, error) { } if ret.hwaddr == "" { ret.hwaddr = ArpFind(ret.ipaddr) - wwlog.Verbose("node mac encoded, arp cache got %s for %s", ret.hwaddr, ret.ipaddr) + wwlog.Verbose("node mac not encoded, arp cache got %s for %s", ret.hwaddr, ret.ipaddr) if ret.hwaddr == "" { return ret, errors.New("no hwaddr encoded in GET") } diff --git a/internal/pkg/warewulfd/provision_test.go b/internal/pkg/warewulfd/provision_test.go index 870e1de0..5ec8d72e 100644 --- a/internal/pkg/warewulfd/provision_test.go +++ b/internal/pkg/warewulfd/provision_test.go @@ -1,16 +1,18 @@ package warewulfd import ( - "github.com/stretchr/testify/assert" - "io/ioutil" + "io" "net/http" "net/http/httptest" "os" "path" "testing" + "github.com/stretchr/testify/assert" + warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/node" + "github.com/hpcng/warewulf/internal/pkg/wwlog" ) var provisionSendTests = []struct { @@ -18,37 +20,80 @@ var provisionSendTests = []struct { url string body string status int + ip string }{ - {"system overlay", "/overlay-system/00:00:00:ff:ff:ff", "system overlay", 200}, - {"runtime overlay", "/overlay-runtime/00:00:00:ff:ff:ff", "runtime overlay", 200}, - {"fake overlay", "/overlay-system/00:00:00:ff:ff:ff?overlay=fake", "", 404}, - {"specific overlay", "/overlay-system/00:00:00:ff:ff:ff?overlay=o1", "specific overlay", 200}, + {"system overlay", "/overlay-system/00:00:00:ff:ff:ff", "system overlay", 200, "10.10.10.10:9873"}, + {"runtime overlay", "/overlay-runtime/00:00:00:ff:ff:ff", "runtime overlay", 200, "10.10.10.10:9873"}, + {"fake overlay", "/overlay-system/00:00:00:ff:ff:ff?overlay=fake", "", 404, "10.10.10.10:9873:9873"}, + {"specific overlay", "/overlay-system/00:00:00:ff:ff:ff?overlay=o1", "specific overlay", 200, "10.10.10.10:9873"}, + {"find shim", "/efiboot/shim.efi", "", 200, "10.10.10.10:9873"}, + {"find shim", "/efiboot/shim.efi", "", 404, "10.10.10.11:9873"}, + {"find grub", "/efiboot/grub.efi", "", 200, "10.10.10.10:9873"}, + {"find grub", "/efiboot/grub.efi", "", 404, "10.10.10.11:9873"}, } func Test_ProvisionSend(t *testing.T) { - file, err := os.CreateTemp(os.TempDir(), "ww-test-nodes.conf-*") + conf_file, err := os.CreateTemp(os.TempDir(), "ww-test-nodes.conf-*") assert.NoError(t, err) - defer file.Close() + defer conf_file.Close() { - _, err := file.WriteString(`WW_INTERNAL: 43 + _, err := conf_file.WriteString(`WW_INTERNAL: 43 +nodeprofiles: + default: + container name: suse nodes: n1: network devices: default: - hwaddr: 00:00:00:ff:ff:ff`) + hwaddr: 00:00:00:ff:ff:ff + n2: + network devices: + default: + hwaddr: 00:00:00:00:ff:ff + container name: none`) assert.NoError(t, err) } - assert.NoError(t, file.Sync()) - node.ConfigFile = file.Name() + assert.NoError(t, conf_file.Sync()) + node.ConfigFile = conf_file.Name() + + // create a arp file as for grub we look up the ip address through the arp cache + arp_file, err := os.CreateTemp(os.TempDir(), "ww-arp") + assert.NoError(t, err) + defer arp_file.Close() + { + _, err := arp_file.WriteString(`IP address HW type Flags HW address Mask Device +10.10.10.10 0x1 0x2 00:00:00:ff:ff:ff * dummy +10.10.10.11 0x1 0x2 00:00:00:00:ff:ff * dummy`) + assert.NoError(t, err) + } + assert.NoError(t, arp_file.Sync()) + SetArpFile(arp_file.Name()) + + conf := warewulfconf.Get() + containerDir, imageDirErr := os.MkdirTemp(os.TempDir(), "ww-test-container-*") + assert.NoError(t, imageDirErr) + defer os.RemoveAll(containerDir) + conf.Paths.WWChrootdir = containerDir + assert.NoError(t, os.MkdirAll(path.Join(containerDir, "suse/rootfs/usr/lib64/efi"), 0700)) + { + _, err := os.Create(path.Join(containerDir, "suse/rootfs/usr/lib64/efi", "shim.efi")) + assert.NoError(t, err) + } + assert.NoError(t, os.MkdirAll(path.Join(containerDir, "suse/rootfs/usr/share/efi/x86_64/"), 0700)) + { + _, err := os.Create(path.Join(containerDir, "suse/rootfs/usr/share/efi/x86_64/", "grub.efi")) + assert.NoError(t, err) + } + dbErr := LoadNodeDB() assert.NoError(t, dbErr) provisionDir, provisionDirErr := os.MkdirTemp(os.TempDir(), "ww-test-provision-*") assert.NoError(t, provisionDirErr) defer os.RemoveAll(provisionDir) - conf := warewulfconf.Get() conf.Paths.WWProvisiondir = provisionDir conf.Warewulf.Secure = false + wwlog.SetLogLevel(wwlog.DEBUG) assert.NoError(t, os.MkdirAll(path.Join(provisionDir, "overlays", "n1"), 0700)) assert.NoError(t, os.WriteFile(path.Join(provisionDir, "overlays", "n1", "__SYSTEM__.img"), []byte("system overlay"), 0600)) assert.NoError(t, os.WriteFile(path.Join(provisionDir, "overlays", "n1", "__RUNTIME__.img"), []byte("runtime overlay"), 0600)) @@ -57,12 +102,13 @@ nodes: for _, tt := range provisionSendTests { t.Run(tt.description, func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, tt.url, nil) + req.RemoteAddr = tt.ip w := httptest.NewRecorder() ProvisionSend(w, req) res := w.Result() defer res.Body.Close() - data, readErr := ioutil.ReadAll(res.Body) + data, readErr := io.ReadAll(res.Body) assert.NoError(t, readErr) assert.Equal(t, tt.body, string(data)) assert.Equal(t, tt.status, res.StatusCode) diff --git a/internal/pkg/warewulfd/util.go b/internal/pkg/warewulfd/util.go index a3f7439d..caaf22ae 100644 --- a/internal/pkg/warewulfd/util.go +++ b/internal/pkg/warewulfd/util.go @@ -73,12 +73,21 @@ func getOverlayFile( return } +var arpFile string + +func init() { + arpFile = "/proc/net/arp" +} + +func SetArpFile(newName string) { + arpFile = newName +} + /* returns the mac address if it has an entry in the arp cache */ - func ArpFind(ip string) (mac string) { - arpCache, err := os.Open("/proc/net/arp") + arpCache, err := os.Open(arpFile) if err != nil { return }