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