github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/integration/testcmd/generic/uinit/generic.go (about)

     1  // Copyright 2019 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  	"log"
     9  	"os"
    10  	"os/exec"
    11  	"path/filepath"
    12  
    13  	"github.com/u-root/u-root/pkg/mount"
    14  	"golang.org/x/sys/unix"
    15  )
    16  
    17  func main() {
    18  	if err := os.MkdirAll("/testdata", 0755); err != nil {
    19  		log.Fatalf("Couldn't create testdata: %v", err)
    20  	}
    21  
    22  	// Mount a vfat volume and run the tests within.
    23  	var (
    24  		mp  *mount.MountPoint
    25  		err error
    26  	)
    27  	if os.Getenv("UROOT_USE_9P") == "1" {
    28  		mp, err = mount.Mount("tmpdir", "/testdata", "9p", "", 0)
    29  	} else {
    30  		mp, err = mount.Mount("/dev/sda1", "/testdata", "vfat", "", unix.MS_RDONLY)
    31  	}
    32  	if err != nil {
    33  		log.Fatalf("Failed to mount test directory: %v", err)
    34  	}
    35  	defer mp.Unmount(0) //nolint:errcheck
    36  
    37  	// Run the test script test.elv
    38  	test := filepath.Join("/testdata", "test.elv")
    39  	if _, err := os.Stat(test); os.IsNotExist(err) {
    40  		log.Fatalf("Could not find any test script to run.")
    41  	}
    42  
    43  	cmd := exec.Command("elvish", test)
    44  	cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
    45  
    46  	if err := cmd.Run(); err != nil {
    47  		log.Fatalf("test.elv ran unsuccessfully: %v", err)
    48  	}
    49  
    50  	if err := unix.Reboot(unix.LINUX_REBOOT_CMD_POWER_OFF); err != nil {
    51  		log.Fatalf("Failed to reboot: %v", err)
    52  	}
    53  }