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