github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+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  	// blacklisted. 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  	blacklist := []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/loop",
    57  		"github.com/u-root/u-root/pkg/gpio",
    58  		"github.com/u-root/u-root/pkg/mount",
    59  
    60  		// Missing xzcat in VM.
    61  		"github.com/u-root/u-root/cmds/exp/bzimage",
    62  		"github.com/u-root/u-root/pkg/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  	for i := 0; i < len(pkgs); i++ {
    73  		for _, b := range blacklist {
    74  			if pkgs[i] == b {
    75  				pkgs = append(pkgs[:i], pkgs[i+1:]...)
    76  			}
    77  		}
    78  	}
    79  
    80  	return pkgs
    81  }
    82  
    83  // TestGoTest effectively runs "go test ./..." inside a QEMU instance. The
    84  // tests run as root and can do all sorts of things not possible otherwise.
    85  func TestGoTest(t *testing.T) {
    86  	pkgs := testPkgs(t)
    87  
    88  	o := &vmtest.Options{
    89  		QEMUOpts: qemu.Options{
    90  			Timeout: 120 * time.Second,
    91  		},
    92  	}
    93  	vmtest.GolangTest(t, pkgs, o)
    94  }