github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/integration/generic-tests/pxeboot_test.go (about) 1 // Copyright 2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build !race 6 // +build !race 7 8 package integration 9 10 import ( 11 "testing" 12 "time" 13 14 "github.com/mvdan/u-root-coreutils/pkg/qemu" 15 "github.com/mvdan/u-root-coreutils/pkg/testutil" 16 "github.com/mvdan/u-root-coreutils/pkg/uroot" 17 "github.com/mvdan/u-root-coreutils/pkg/vmtest" 18 ) 19 20 // TestPxeboot runs a server and client to test pxebooting a node. 21 func TestPxeboot4(t *testing.T) { 22 // TODO: support arm 23 if vmtest.TestArch() != "amd64" && vmtest.TestArch() != "arm64" { 24 t.Skipf("test not supported on %s", vmtest.TestArch()) 25 } 26 27 network := qemu.NewNetwork() 28 dhcpServer, scleanup := vmtest.QEMUTest(t, &vmtest.Options{ 29 Name: "TestPxeboot_Server", 30 BuildOpts: uroot.Opts{ 31 ExtraFiles: []string{ 32 "./testdata/pxe:pxeroot", 33 }, 34 }, 35 TestCmds: []string{ 36 "ip addr add 192.168.0.1/24 dev eth0", 37 "ip link set eth0 up", 38 "ip route add 0.0.0.0/0 dev eth0", 39 "ls -l /pxeroot", 40 "pxeserver -tftp-dir=/pxeroot", 41 }, 42 QEMUOpts: qemu.Options{ 43 SerialOutput: vmtest.TestLineWriter(t, "server"), 44 Timeout: 30 * time.Second, 45 Devices: []qemu.Device{ 46 network.NewVM(), 47 }, 48 }, 49 }) 50 defer scleanup() 51 52 dhcpClient, ccleanup := vmtest.QEMUTest(t, &vmtest.Options{ 53 Name: "TestPxeboot_Client", 54 BuildOpts: uroot.Opts{ 55 Commands: uroot.BusyBoxCmds( 56 "github.com/mvdan/u-root-coreutils/cmds/core/init", 57 "github.com/mvdan/u-root-coreutils/cmds/core/elvish", 58 "github.com/mvdan/u-root-coreutils/cmds/core/ip", 59 "github.com/mvdan/u-root-coreutils/cmds/core/shutdown", 60 "github.com/mvdan/u-root-coreutils/cmds/core/sleep", 61 "github.com/mvdan/u-root-coreutils/cmds/boot/pxeboot", 62 ), 63 }, 64 TestCmds: []string{ 65 "pxeboot --no-exec -v", 66 // Sleep so serial console output gets flushed. The expect library is racy. 67 "sleep 5", 68 "shutdown -h", 69 }, 70 QEMUOpts: qemu.Options{ 71 SerialOutput: vmtest.TestLineWriter(t, "client"), 72 Timeout: 30 * time.Second, 73 Devices: []qemu.Device{ 74 network.NewVM(), 75 }, 76 }, 77 }) 78 defer ccleanup() 79 80 if err := dhcpServer.Expect("starting file server"); err != nil { 81 t.Errorf("%s File server: %v", testutil.NowLog(), err) 82 } 83 if err := dhcpClient.Expect("Got DHCPv4 lease on eth0:"); err != nil { 84 t.Errorf("%s Lease %v:", testutil.NowLog(), err) 85 } 86 if err := dhcpClient.Expect("Boot URI: tftp://192.168.0.1/pxelinux.0"); err != nil { 87 t.Errorf("%s Boot: %v", testutil.NowLog(), err) 88 } 89 90 // Boot menu should show the label from the pxelinux file. 91 if err := dhcpClient.Expect("01. some-random-kernel"); err != nil { 92 t.Errorf("%s Boot Menu: %v", testutil.NowLog(), err) 93 } 94 if err := dhcpClient.Expect("Attempting to boot"); err != nil { 95 t.Errorf("%s Boot Menu: %v", testutil.NowLog(), err) 96 } 97 if err := dhcpClient.Expect("Kernel: tftp://192.168.0.1/kernel"); err != nil { 98 t.Errorf("%s parsed kernel: %v", testutil.NowLog(), err) 99 } 100 }