Use package testtask and httptest.Server to make client/driver tests OS independent.

This commit is contained in:
Chris Hines
2015-11-24 15:11:26 -05:00
parent 551104cafe
commit d707adc3ed
5 changed files with 58 additions and 41 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/helper/testtask"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -30,6 +31,12 @@ func init() {
rand.Seed(49875)
}
func TestMain(m *testing.M) {
if !testtask.Run() {
os.Exit(m.Run())
}
}
func testLogger() *log.Logger {
return log.New(os.Stderr, "", log.LstdFlags)
}

View File

@@ -53,7 +53,7 @@ func testExecutor(t *testing.T, buildExecutor func() Executor, compatible func(*
command := func(name string, args ...string) Executor {
e := buildExecutor()
SetCommand(e, name, args)
testtask.SetEnv(e.Command())
testtask.SetCmdEnv(e.Command())
return e
}

View File

@@ -3,14 +3,16 @@ package driver
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"
"reflect"
"runtime"
"testing"
"time"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/environment"
"github.com/hashicorp/nomad/helper/testtask"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -52,11 +54,12 @@ func TestRawExecDriver_StartOpen_Wait(t *testing.T) {
task := &structs.Task{
Name: "sleep",
Config: map[string]interface{}{
"command": "/bin/sleep",
"args": []string{"1"},
"command": testtask.Path(),
"args": []string{"sleep", "1s"},
},
Resources: basicResources,
}
testtask.SetTaskEnv(task)
driverCtx := testDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
defer ctx.AllocDir.Destroy()
@@ -88,25 +91,22 @@ func TestRawExecDriver_StartOpen_Wait(t *testing.T) {
}
func TestRawExecDriver_Start_Artifact_basic(t *testing.T) {
var file, checksum string
switch runtime.GOOS {
case "darwin":
file = "hi_darwin_amd64"
checksum = "md5:d7f2fdb13b36dcb7407721d78926b335"
default:
file = "hi_linux_amd64"
checksum = "md5:a9b14903a8942748e4f8474e11f795d3"
}
path := testtask.Path()
ts := httptest.NewServer(http.FileServer(http.Dir(filepath.Dir(path))))
defer ts.Close()
file := filepath.Base(path)
task := &structs.Task{
Name: "sleep",
Config: map[string]interface{}{
"artifact_source": fmt.Sprintf("https://dl.dropboxusercontent.com/u/47675/jar_thing/%s", file),
"artifact_source": fmt.Sprintf("%s/%s", ts.URL, file),
"command": filepath.Join("$NOMAD_TASK_DIR", file),
"checksum": checksum,
"args": []string{"sleep", "1s"},
},
Resources: basicResources,
}
testtask.SetTaskEnv(task)
driverCtx := testDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
defer ctx.AllocDir.Destroy()
@@ -138,26 +138,22 @@ func TestRawExecDriver_Start_Artifact_basic(t *testing.T) {
}
func TestRawExecDriver_Start_Artifact_expanded(t *testing.T) {
var file string
switch runtime.GOOS {
case "darwin":
file = "hi_darwin_amd64"
default:
file = "hi_linux_amd64"
}
path := testtask.Path()
ts := httptest.NewServer(http.FileServer(http.Dir(filepath.Dir(path))))
defer ts.Close()
file := filepath.Base(path)
task := &structs.Task{
Name: "sleep",
Config: map[string]interface{}{
"artifact_source": fmt.Sprintf("https://dl.dropboxusercontent.com/u/47675/jar_thing/%s", file),
"command": "/bin/bash",
"args": []string{
"-c",
fmt.Sprintf(`'/bin/sleep 1 && %s'`, filepath.Join("$NOMAD_TASK_DIR", file)),
},
"artifact_source": fmt.Sprintf("%s/%s", ts.URL, file),
"command": filepath.Join("$NOMAD_TASK_DIR", file),
"args": []string{"sleep", "1s"},
},
Resources: basicResources,
}
testtask.SetTaskEnv(task)
driverCtx := testDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
defer ctx.AllocDir.Destroy()
@@ -192,11 +188,12 @@ func TestRawExecDriver_Start_Wait(t *testing.T) {
task := &structs.Task{
Name: "sleep",
Config: map[string]interface{}{
"command": "/bin/sleep",
"args": []string{"1"},
"command": testtask.Path(),
"args": []string{"sleep", "1s"},
},
Resources: basicResources,
}
testtask.SetTaskEnv(task)
driverCtx := testDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
@@ -231,17 +228,19 @@ func TestRawExecDriver_Start_Wait(t *testing.T) {
func TestRawExecDriver_Start_Wait_AllocDir(t *testing.T) {
exp := []byte{'w', 'i', 'n'}
file := "output.txt"
outPath := fmt.Sprintf(`$%s/%s`, environment.AllocDir, file)
task := &structs.Task{
Name: "sleep",
Config: map[string]interface{}{
"command": "/bin/bash",
"command": testtask.Path(),
"args": []string{
"-c",
fmt.Sprintf(`sleep 1; echo -n %s > $%s/%s`, string(exp), environment.AllocDir, file),
"sleep", "1s",
"write", string(exp), outPath,
},
},
Resources: basicResources,
}
testtask.SetTaskEnv(task)
driverCtx := testDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
@@ -282,11 +281,12 @@ func TestRawExecDriver_Start_Kill_Wait(t *testing.T) {
task := &structs.Task{
Name: "sleep",
Config: map[string]interface{}{
"command": "/bin/sleep",
"args": []string{"1"},
"command": testtask.Path(),
"args": []string{"sleep", "1s"},
},
Resources: basicResources,
}
testtask.SetTaskEnv(task)
driverCtx := testDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)

View File

@@ -330,6 +330,6 @@ func tempFileName(t *testing.T) string {
func testCommand(args ...string) *exec.Cmd {
cmd := exec.Command(testtask.Path(), args...)
testtask.SetEnv(cmd)
testtask.SetCmdEnv(cmd)
return cmd
}

View File

@@ -9,6 +9,7 @@ import (
"os/exec"
"time"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/kardianos/osext"
)
@@ -21,15 +22,24 @@ func Path() string {
return path
}
// SetEnv configures the environment of cmd so that Run executes a testtask
// script when called from within cmd when executed.
func SetEnv(cmd *exec.Cmd) {
// SetCmdEnv configures the environment of cmd so that Run executes a testtask
// script when called from within cmd.
func SetCmdEnv(cmd *exec.Cmd) {
cmd.Env = append(os.Environ(), "TEST_TASK=execute")
}
// SetTaskEnv configures the environment of t so that Run executes a testtask
// script when called from within t.
func SetTaskEnv(t *structs.Task) {
if t.Env == nil {
t.Env = map[string]string{}
}
t.Env["TEST_TASK"] = "execute"
}
// Run interprets os.Args as a testtask script if the current program was
// launched with an environment configured by SetEnv. It returns false if
// the environment was not set by this package.
// launched with an environment configured by SetCmdEnv or SetTaskEnv. It
// returns false if the environment was not set by this package.
func Run() bool {
switch tm := os.Getenv("TEST_TASK"); tm {
case "":