gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/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  // +build !race
     6  
     7  package integration
     8  
     9  import (
    10  	"os"
    11  	"os/exec"
    12  	"strings"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/u-root/u-root/pkg/qemu"
    17  	"github.com/u-root/u-root/pkg/vmtest"
    18  )
    19  
    20  // testPkgs returns a slice of tests to run.
    21  func testPkgs(t *testing.T) []string {
    22  	// Packages which do not contain tests (or do not contain tests for the
    23  	// build target) will still compile a test binary which vacuously pass.
    24  	cmd := exec.Command("go", "list",
    25  		"github.com/u-root/u-root/cmds/boot/...",
    26  		"github.com/u-root/u-root/cmds/core/...",
    27  		"github.com/u-root/u-root/cmds/exp/...",
    28  		"github.com/u-root/u-root/pkg/...",
    29  	)
    30  	cmd.Env = append(os.Environ(), "GOARCH="+vmtest.TestArch())
    31  	out, err := cmd.CombinedOutput()
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	pkgs := strings.Fields(strings.TrimSpace(string(out)))
    36  
    37  	// TODO: Some tests do not run properly in QEMU at the moment. They are
    38  	// blocklisted. These tests fail for mostly two reasons:
    39  	// 1. either it requires networking (not enabled in the kernel)
    40  	// 2. or it depends on some test files (for example /bin/sleep)
    41  	blocklist := []string{
    42  		"github.com/u-root/u-root/cmds/core/cmp",
    43  		"github.com/u-root/u-root/cmds/core/dd",
    44  		"github.com/u-root/u-root/cmds/core/elvish/eval",
    45  		"github.com/u-root/u-root/cmds/core/elvish/edit/tty",
    46  		"github.com/u-root/u-root/cmds/core/fusermount",
    47  		"github.com/u-root/u-root/cmds/core/wget",
    48  		"github.com/u-root/u-root/cmds/core/which",
    49  		"github.com/u-root/u-root/cmds/exp/rush",
    50  		"github.com/u-root/u-root/cmds/exp/pox",
    51  		"github.com/u-root/u-root/pkg/crypto",
    52  		"github.com/u-root/u-root/pkg/tarutil",
    53  		"github.com/u-root/u-root/pkg/ldd",
    54  
    55  		// These have special configuration.
    56  		"github.com/u-root/u-root/pkg/gpio",
    57  		"github.com/u-root/u-root/pkg/mount",
    58  		"github.com/u-root/u-root/pkg/mount/loop",
    59  
    60  		// Missing xzcat in VM.
    61  		"github.com/u-root/u-root/cmds/exp/bzimage",
    62  		"github.com/u-root/u-root/pkg/boot/bzimage",
    63  
    64  		// Missing /dev/mem and /sys/firmware/efi
    65  		"github.com/u-root/u-root/pkg/boot/acpi",
    66  
    67  		// No Go compiler in VM.
    68  		"github.com/u-root/u-root/pkg/bb",
    69  		"github.com/u-root/u-root/pkg/uroot",
    70  		"github.com/u-root/u-root/pkg/uroot/builder",
    71  	}
    72  	if vmtest.TestArch() == "arm64" {
    73  		blocklist = append(
    74  			blocklist,
    75  			"github.com/u-root/u-root/pkg/strace",
    76  		)
    77  	}
    78  	for i := 0; i < len(pkgs); i++ {
    79  		for _, b := range blocklist {
    80  			if pkgs[i] == b {
    81  				pkgs = append(pkgs[:i], pkgs[i+1:]...)
    82  			}
    83  		}
    84  	}
    85  
    86  	return pkgs
    87  }
    88  
    89  // TestGoTest effectively runs "go test ./..." inside a QEMU instance. The
    90  // tests run as root and can do all sorts of things not possible otherwise.
    91  func TestGoTest(t *testing.T) {
    92  	pkgs := testPkgs(t)
    93  
    94  	o := &vmtest.Options{
    95  		QEMUOpts: qemu.Options{
    96  			Timeout: 120 * time.Second,
    97  		},
    98  	}
    99  	vmtest.GolangTest(t, pkgs, o)
   100  }