From 769415dbd20b66f9b575c4d80c26961eab414790 Mon Sep 17 00:00:00 2001 From: Chris Hines Date: Fri, 20 Nov 2015 10:56:30 -0500 Subject: [PATCH] Improve names and documentation in test code. --- client/driver/executor/test_harness_test.go | 56 ++++++++++++++++----- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/client/driver/executor/test_harness_test.go b/client/driver/executor/test_harness_test.go index 391073b63..7bfa42dfb 100644 --- a/client/driver/executor/test_harness_test.go +++ b/client/driver/executor/test_harness_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "log" "os" + "os/exec" "path/filepath" "strings" "testing" @@ -15,50 +16,81 @@ import ( "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) { - 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": appMain() 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() { if len(os.Args) < 2 { fmt.Fprintln(os.Stderr, "no command provided") os.Exit(1) } + args := os.Args[1:] - shift := func() string { + + // popArg removes the first argument from args and returns it. + popArg := func() string { s := args[0] args = args[1:] return s } + + // execute a sequence of operations from args for len(args) > 0 { - switch cmd := shift(); cmd { + switch cmd := popArg(); cmd { + case "sleep": + // sleep : sleep for a duration indicated by the first + // argument if len(args) < 1 { fmt.Fprintln(os.Stderr, "expected arg for sleep") os.Exit(1) } - dur, err := time.ParseDuration(shift()) + dur, err := time.ParseDuration(popArg()) if err != nil { fmt.Fprintf(os.Stderr, "could not parse sleep time: %v", err) os.Exit(1) } time.Sleep(dur) + case "echo": + // echo : write the remaining arguments to stdout each + // separated by a single space and followed by a newline. fmt.Println(strings.Join(args, " ")) args = args[:0] + case "write": + // write : 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 { fmt.Fprintln(os.Stderr, "expected two args for write") os.Exit(1) } - msg := shift() - file := shift() + msg := popArg() + file := popArg() ioutil.WriteFile(file, []byte(msg), 0666) + default: fmt.Fprintln(os.Stderr, "unknown command:", cmd) os.Exit(1) @@ -99,7 +131,7 @@ func testExecutor(t *testing.T, buildExecutor func() Executor, compatible func(* command := func(name string, args ...string) Executor { e := buildExecutor() SetCommand(e, name, args) - e.Command().Env = append(os.Environ(), "TEST_MAIN=app") + setTestAppEnv(e.Command()) 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) { - e := command(os.Args[0], "fail") + e := command(testBinary, "fail") if err := e.Limit(constraint); err != nil { log.Panicf("Limit() failed: %v", err) @@ -165,7 +197,7 @@ func Executor_Start_Wait(t *testing.T, command buildExecCommand) { expected := "hello world" file := filepath.Join(allocdir.TaskLocal, "output.txt") 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 { log.Panicf("Limit() failed: %v", err) @@ -204,7 +236,7 @@ func Executor_Start_Kill(t *testing.T, command buildExecCommand) { } 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 { log.Panicf("Limit() failed: %v", err) @@ -242,7 +274,7 @@ func Executor_Open(t *testing.T, command buildExecCommand, newExecutor func() Ex expected := "hello world" file := filepath.Join(allocdir.TaskLocal, "output.txt") 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 { log.Panicf("Limit() failed: %v", err)