Skip to content

Commit

Permalink
integration: Add sh package to simplify os/exec
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan O'Leary <ryanoleary@google.com>
  • Loading branch information
rjoleary committed Oct 4, 2018
1 parent ba09ee7 commit c8b7e51
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
16 changes: 4 additions & 12 deletions integration/testdata/gotest/uinit/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,14 @@ import (
"os/exec"
"path/filepath"
"sort"
)

func sh(arg0 string, args ...string) {
cmd := exec.Command(arg0, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
"github.com/u-root/u-root/pkg/sh"
)

// Mount a vfat volume and run the tests within.
func main() {
sh("mkdir", "/testdata")
sh("mount", "-r", "-t", "vfat", "/dev/sda1", "/testdata")
sh.RunOrDie("mkdir", "/testdata")
sh.RunOrDie("mount", "-r", "-t", "vfat", "/dev/sda1", "/testdata")

// Gather list of tests.
files, err := ioutil.ReadDir("/testdata/tests")
Expand Down
19 changes: 4 additions & 15 deletions integration/testdata/kexec/uinit/kexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,15 @@ package main
import (
"fmt"
"log"
"os"
"os/exec"

"github.com/u-root/u-root/pkg/cmdline"
"github.com/u-root/u-root/pkg/sh"
)

func sh(arg0 string, args ...string) {
cmd := exec.Command(arg0, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}

// Mount a vfat volume and kexec the kernel within.
func main() {
sh("mkdir", "/testdata")
sh("mount", "-r", "-t", "vfat", "/dev/sda1", "/testdata")
sh.RunOrDie("mkdir", "/testdata")
sh.RunOrDie("mount", "-r", "-t", "vfat", "/dev/sda1", "/testdata")

// Get and increment the counter.
kExecCounter, ok := cmdline.Flag("kexeccounter")
Expand All @@ -38,7 +27,7 @@ func main() {
if kExecCounter == "0" {
cmdLine := cmdline.FullCmdLine() + " kexeccounter=1"
log.Print("cmdline: ", cmdLine)
sh("kexec",
sh.RunOrDie("kexec",
"-i", "/testdata/initramfs.cpio",
"-c", cmdLine,
"/testdata/bzImage")
Expand Down
21 changes: 21 additions & 0 deletions pkg/sh/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sh

import (
"log"
"os"
"os/exec"
)

func RunOrDie(arg0 string, args ...string) {
cmd := exec.Command(arg0, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}

0 comments on commit c8b7e51

Please sign in to comment.