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