Improve names and documentation in test code.

This commit is contained in:
Chris Hines 2015-11-20 10:56:30 -05:00 committed by Alex Dadgar
parent ec7e78680d
commit 769415dbd2

View file

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@ -15,50 +16,81 @@ import (
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
) )
// testBinary is the path to the running test binary
var testBinary = os.Args[0]
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
switch os.Getenv("TEST_MAIN") { // The tests in this package recursively execute the test binary produced
// by go test. The TEST_MAIN environment variable controls the recursive
// execution.
switch tm := os.Getenv("TEST_MAIN"); tm {
case "":
os.Exit(m.Run())
case "app": case "app":
appMain() appMain()
default: default:
os.Exit(m.Run()) fmt.Fprintf(os.Stderr, "unexpected value for TEST_MAIN, \"%s\"\n", tm)
os.Exit(1)
} }
} }
// setTestAppEnv sets the environement of cmd for a recursive call into
// TestMain.
func setTestAppEnv(cmd *exec.Cmd) {
cmd.Env = append(os.Environ(), "TEST_MAIN=app")
}
func appMain() { func appMain() {
if len(os.Args) < 2 { if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "no command provided") fmt.Fprintln(os.Stderr, "no command provided")
os.Exit(1) os.Exit(1)
} }
args := os.Args[1:] args := os.Args[1:]
shift := func() string {
// popArg removes the first argument from args and returns it.
popArg := func() string {
s := args[0] s := args[0]
args = args[1:] args = args[1:]
return s return s
} }
// execute a sequence of operations from args
for len(args) > 0 { for len(args) > 0 {
switch cmd := shift(); cmd { switch cmd := popArg(); cmd {
case "sleep": case "sleep":
// sleep <dur>: sleep for a duration indicated by the first
// argument
if len(args) < 1 { if len(args) < 1 {
fmt.Fprintln(os.Stderr, "expected arg for sleep") fmt.Fprintln(os.Stderr, "expected arg for sleep")
os.Exit(1) os.Exit(1)
} }
dur, err := time.ParseDuration(shift()) dur, err := time.ParseDuration(popArg())
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "could not parse sleep time: %v", err) fmt.Fprintf(os.Stderr, "could not parse sleep time: %v", err)
os.Exit(1) os.Exit(1)
} }
time.Sleep(dur) time.Sleep(dur)
case "echo": case "echo":
// echo <msg ...>: write the remaining arguments to stdout each
// separated by a single space and followed by a newline.
fmt.Println(strings.Join(args, " ")) fmt.Println(strings.Join(args, " "))
args = args[:0] args = args[:0]
case "write": case "write":
// write <msg> <file>: write a message to a file. The first
// argument is the msg. The second argument is the path to the
// target file.
if len(args) < 2 { if len(args) < 2 {
fmt.Fprintln(os.Stderr, "expected two args for write") fmt.Fprintln(os.Stderr, "expected two args for write")
os.Exit(1) os.Exit(1)
} }
msg := shift() msg := popArg()
file := shift() file := popArg()
ioutil.WriteFile(file, []byte(msg), 0666) ioutil.WriteFile(file, []byte(msg), 0666)
default: default:
fmt.Fprintln(os.Stderr, "unknown command:", cmd) fmt.Fprintln(os.Stderr, "unknown command:", cmd)
os.Exit(1) os.Exit(1)
@ -99,7 +131,7 @@ func testExecutor(t *testing.T, buildExecutor func() Executor, compatible func(*
command := func(name string, args ...string) Executor { command := func(name string, args ...string) Executor {
e := buildExecutor() e := buildExecutor()
SetCommand(e, name, args) SetCommand(e, name, args)
e.Command().Env = append(os.Environ(), "TEST_MAIN=app") setTestAppEnv(e.Command())
return e return e
} }
@ -132,7 +164,7 @@ func Executor_Start_Invalid(t *testing.T, command buildExecCommand) {
} }
func Executor_Start_Wait_Failure_Code(t *testing.T, command buildExecCommand) { func Executor_Start_Wait_Failure_Code(t *testing.T, command buildExecCommand) {
e := command(os.Args[0], "fail") e := command(testBinary, "fail")
if err := e.Limit(constraint); err != nil { if err := e.Limit(constraint); err != nil {
log.Panicf("Limit() failed: %v", err) log.Panicf("Limit() failed: %v", err)
@ -165,7 +197,7 @@ func Executor_Start_Wait(t *testing.T, command buildExecCommand) {
expected := "hello world" expected := "hello world"
file := filepath.Join(allocdir.TaskLocal, "output.txt") file := filepath.Join(allocdir.TaskLocal, "output.txt")
absFilePath := filepath.Join(taskDir, file) absFilePath := filepath.Join(taskDir, file)
e := command(os.Args[0], "sleep", "1s", "write", expected, file) e := command(testBinary, "sleep", "1s", "write", expected, file)
if err := e.Limit(constraint); err != nil { if err := e.Limit(constraint); err != nil {
log.Panicf("Limit() failed: %v", err) log.Panicf("Limit() failed: %v", err)
@ -204,7 +236,7 @@ func Executor_Start_Kill(t *testing.T, command buildExecCommand) {
} }
filePath := filepath.Join(taskDir, "output") filePath := filepath.Join(taskDir, "output")
e := command(os.Args[0], "sleep", "1s", "write", "failure", filePath) e := command(testBinary, "sleep", "1s", "write", "failure", filePath)
if err := e.Limit(constraint); err != nil { if err := e.Limit(constraint); err != nil {
log.Panicf("Limit() failed: %v", err) log.Panicf("Limit() failed: %v", err)
@ -242,7 +274,7 @@ func Executor_Open(t *testing.T, command buildExecCommand, newExecutor func() Ex
expected := "hello world" expected := "hello world"
file := filepath.Join(allocdir.TaskLocal, "output.txt") file := filepath.Join(allocdir.TaskLocal, "output.txt")
absFilePath := filepath.Join(taskDir, file) absFilePath := filepath.Join(taskDir, file)
e := command(os.Args[0], "sleep", "1s", "write", expected, file) e := command(testBinary, "sleep", "1s", "write", expected, file)
if err := e.Limit(constraint); err != nil { if err := e.Limit(constraint); err != nil {
log.Panicf("Limit() failed: %v", err) log.Panicf("Limit() failed: %v", err)