github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/integration/generic-tests/uefiboot_test.go (about)

     1  // Copyright 2021 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 amd64 && !race
     6  // +build amd64,!race
     7  
     8  package integration
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  	"path/filepath"
    14  	"testing"
    15  	"time"
    16  
    17  	expect "github.com/google/goexpect"
    18  	"github.com/mvdan/u-root-coreutils/pkg/qemu"
    19  	"github.com/mvdan/u-root-coreutils/pkg/vmtest"
    20  )
    21  
    22  // TestUefiboot tests uefiboot commmands to boot to uefishell.
    23  func TestUefiBoot(t *testing.T) {
    24  	if vmtest.TestArch() != "amd64" {
    25  		t.Skipf("test not supported on %s", vmtest.TestArch())
    26  	}
    27  
    28  	payload := "UEFIPAYLOAD.fd"
    29  	src := fmt.Sprintf("/home/circleci/%v", payload)
    30  	if tk := os.Getenv("UROOT_TEST_UEFIPAYLOAD_DIR"); len(tk) > 0 {
    31  		src = filepath.Join(tk, payload)
    32  	}
    33  
    34  	if _, err := os.Stat(src); err != nil && os.IsNotExist(err) {
    35  		t.Skipf("UEFI payload image is not found: %s\n Usage: uefiboot <payload>", src)
    36  	}
    37  
    38  	// Create the CPIO and start QEMU.
    39  	q, cleanup := vmtest.QEMUTest(t, &vmtest.Options{
    40  		TestCmds: []string{"uefiboot /dev/sda"},
    41  		QEMUOpts: qemu.Options{
    42  			Devices: []qemu.Device{
    43  				qemu.IDEBlockDevice{File: src},
    44  				qemu.ArbitraryArgs{"-machine", "q35"},
    45  				qemu.ArbitraryArgs{"-m", "4096"},
    46  			},
    47  		},
    48  	})
    49  	defer cleanup()
    50  
    51  	// Edk2 debug mode will print PROGRESS CODE. We will want to make sure
    52  	// payload is booting to uefi shell correctly.
    53  	if out, err := q.ExpectBatch([]expect.Batcher{
    54  		// Finish booting.
    55  		&expect.BExp{R: "PROGRESS CODE: V02070003"},
    56  		// Last code before booting to UEFI Shell
    57  		&expect.BExp{R: "PROGRESS CODE: V03058001"},
    58  	}, 50*time.Second); err != nil {
    59  		t.Fatalf("VM output did not match expectations: %v", out)
    60  	}
    61  }