github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/integration/gotests/gotest_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  	"os"
    12  	"os/exec"
    13  	"strings"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/mvdan/u-root-coreutils/pkg/qemu"
    18  	"github.com/mvdan/u-root-coreutils/pkg/uroot"
    19  	"github.com/mvdan/u-root-coreutils/pkg/vmtest"
    20  )
    21  
    22  // testPkgs returns a slice of tests to run.
    23  func testPkgs(t *testing.T) []string {
    24  	// Packages which do not contain tests (or do not contain tests for the
    25  	// build target) will still compile a test binary which vacuously pass.
    26  	cmd := exec.Command("go", "list",
    27  		"github.com/mvdan/u-root-coreutils/cmds/boot/...",
    28  		"github.com/mvdan/u-root-coreutils/cmds/core/...",
    29  		"github.com/mvdan/u-root-coreutils/cmds/exp/...",
    30  		"github.com/mvdan/u-root-coreutils/pkg/...",
    31  	)
    32  	cmd.Env = append(os.Environ(), "GOARCH="+vmtest.TestArch())
    33  	out, err := cmd.CombinedOutput()
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	pkgs := strings.Fields(strings.TrimSpace(string(out)))
    38  
    39  	// TODO: Some tests do not run properly in QEMU at the moment. They are
    40  	// blocklisted. These tests fail for mostly two reasons:
    41  	// 1. either it requires networking (not enabled in the kernel)
    42  	// 2. or it depends on some test files (for example /bin/sleep)
    43  	blocklist := []string{
    44  		"github.com/mvdan/u-root-coreutils/cmds/core/cmp",
    45  		"github.com/mvdan/u-root-coreutils/cmds/core/dd",
    46  		"github.com/mvdan/u-root-coreutils/cmds/core/fusermount",
    47  		"github.com/mvdan/u-root-coreutils/cmds/core/gosh",
    48  		"github.com/mvdan/u-root-coreutils/cmds/core/wget",
    49  		"github.com/mvdan/u-root-coreutils/cmds/core/which",
    50  		// Some of TestEdCommands do not exit properly and end up left running. No idea how to fix this yet.
    51  		"github.com/mvdan/u-root-coreutils/cmds/exp/ed",
    52  		"github.com/mvdan/u-root-coreutils/cmds/exp/pox",
    53  		"github.com/mvdan/u-root-coreutils/pkg/crypto",
    54  		"github.com/mvdan/u-root-coreutils/pkg/tarutil",
    55  		"github.com/mvdan/u-root-coreutils/pkg/ldd",
    56  
    57  		// These have special configuration.
    58  		"github.com/mvdan/u-root-coreutils/pkg/gpio",
    59  		"github.com/mvdan/u-root-coreutils/pkg/mount",
    60  		"github.com/mvdan/u-root-coreutils/pkg/mount/block",
    61  		"github.com/mvdan/u-root-coreutils/pkg/mount/loop",
    62  		"github.com/mvdan/u-root-coreutils/pkg/ipmi",
    63  		"github.com/mvdan/u-root-coreutils/pkg/smbios",
    64  
    65  		// Missing xzcat in VM.
    66  		"github.com/mvdan/u-root-coreutils/cmds/exp/bzimage",
    67  		"github.com/mvdan/u-root-coreutils/pkg/boot/bzimage",
    68  
    69  		// No Go compiler in VM.
    70  		"github.com/mvdan/u-root-coreutils/pkg/uroot",
    71  		"github.com/mvdan/u-root-coreutils/pkg/uroot/builder",
    72  
    73  		// ??
    74  		"github.com/mvdan/u-root-coreutils/pkg/tss",
    75  		"github.com/mvdan/u-root-coreutils/pkg/syscallfilter",
    76  	}
    77  	if vmtest.TestArch() == "arm64" {
    78  		blocklist = append(
    79  			blocklist,
    80  			"github.com/mvdan/u-root-coreutils/pkg/strace",
    81  
    82  			// These tests run in 1-2 seconds on x86, but run
    83  			// beyond their huge timeout under arm64 in the VM. Not
    84  			// sure why. Slow emulation?
    85  			"github.com/mvdan/u-root-coreutils/cmds/core/pci",
    86  			"github.com/mvdan/u-root-coreutils/cmds/exp/cbmem",
    87  			"github.com/mvdan/u-root-coreutils/pkg/vfile",
    88  		)
    89  	}
    90  	for i := 0; i < len(pkgs); i++ {
    91  		for _, b := range blocklist {
    92  			if pkgs[i] == b {
    93  				pkgs = append(pkgs[:i], pkgs[i+1:]...)
    94  			}
    95  		}
    96  	}
    97  
    98  	return pkgs
    99  }
   100  
   101  // TestGoTest effectively runs "go test ./..." inside a QEMU instance. The
   102  // tests run as root and can do all sorts of things not possible otherwise.
   103  func TestGoTest(t *testing.T) {
   104  	pkgs := testPkgs(t)
   105  
   106  	o := &vmtest.Options{
   107  		QEMUOpts: qemu.Options{
   108  			Timeout: 120 * time.Second,
   109  			Devices: []qemu.Device{
   110  				// Bump this up so that some unit tests can happily
   111  				// and questionably pre-claim large bytes slices.
   112  				//
   113  				// e.g. pkg/mount/gpt/gpt_test.go need to claim 4.29G
   114  				//
   115  				//     disk = make([]byte, 0x100000000)
   116  				qemu.ArbitraryArgs{"-m", "6G"},
   117  
   118  				// aarch64 VMs start at 1970-01-01 without RTC explicitly set.
   119  				qemu.ArbitraryArgs{"-rtc", "base=localtime,clock=vm"},
   120  			},
   121  		},
   122  		BuildOpts: uroot.Opts{
   123  			ExtraFiles: []string{
   124  				"/etc/group",
   125  				"/etc/passwd",
   126  			},
   127  		},
   128  	}
   129  	vmtest.GolangTest(t, pkgs, o)
   130  }