github.com/shaardie/u-root@v4.0.1-0.20190127173353-f24a1c26aa2e+incompatible/integration/testcmd/gotest/uinit/gotest.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  package main
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"log"
    12  	"os"
    13  	"os/exec"
    14  	"path/filepath"
    15  	"sort"
    16  	"time"
    17  
    18  	"golang.org/x/sys/unix"
    19  
    20  	"github.com/u-root/u-root/pkg/sh"
    21  )
    22  
    23  // Mount a vfat volume and run the tests within.
    24  func main() {
    25  	sh.RunOrDie("mkdir", "/testdata")
    26  	sh.RunOrDie("mount", "-r", "-t", "vfat", "/dev/sda1", "/testdata")
    27  
    28  	// Gather list of tests.
    29  	files, err := ioutil.ReadDir("/testdata/tests")
    30  	if err != nil {
    31  		log.Fatal(err)
    32  	}
    33  	tests := []string{}
    34  	for _, f := range files {
    35  		tests = append(tests, f.Name())
    36  	}
    37  
    38  	// Sort tests.
    39  	sort.Strings(tests)
    40  
    41  	// We are using TAP-style test output. See: https://testanything.org/
    42  	// One unfortunate design in TAP is "ok" is a subset of "not ok", so we
    43  	// prepend each line with "TAP: " and search for for "TAP: ok".
    44  	log.Printf("TAP: 1..%d", len(tests))
    45  
    46  	// Run tests.
    47  	for i, t := range tests {
    48  		runMsg := fmt.Sprintf("TAP: # running %d - %s", i, t)
    49  		passMsg := fmt.Sprintf("TAP: ok %d - %s", i, t)
    50  		failMsg := fmt.Sprintf("TAP: not ok %d - %s", i, t)
    51  		log.Println(runMsg)
    52  
    53  		ctx, cancel := context.WithTimeout(context.Background(), 25000*time.Millisecond)
    54  		defer cancel()
    55  		cmd := exec.CommandContext(ctx, filepath.Join("/testdata/tests", t))
    56  		cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
    57  		err := cmd.Run()
    58  
    59  		if ctx.Err() == context.DeadlineExceeded {
    60  			log.Println("TAP: # timed out")
    61  			log.Println(failMsg)
    62  		} else if err == nil {
    63  			log.Println(passMsg)
    64  		} else {
    65  			log.Println(err)
    66  			log.Println(failMsg)
    67  		}
    68  	}
    69  
    70  	unix.Reboot(unix.LINUX_REBOOT_CMD_POWER_OFF)
    71  }