From dccbc0320b290b1d0da8c83faea06c583a79497b Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 5 Jan 2016 18:02:11 -0800 Subject: [PATCH] Fix tests --- client/driver/docker_test.go | 56 +++++++---------- client/driver/driver_test.go | 29 +++++---- client/driver/exec_test.go | 53 +++++++--------- client/driver/executor/exec_linux.go | 15 ++--- client/driver/executor/test_harness_test.go | 18 ++++-- client/driver/java_test.go | 26 ++++---- client/driver/qemu_test.go | 19 +++--- client/driver/raw_exec_test.go | 70 ++++++++++----------- client/driver/rkt_test.go | 35 +++++------ 9 files changed, 148 insertions(+), 173 deletions(-) diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index 8adcc60920bd..7f58a349f845 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -11,17 +11,11 @@ import ( docker "github.com/fsouza/go-dockerclient" "github.com/hashicorp/nomad/client/config" - "github.com/hashicorp/nomad/client/driver/environment" + "github.com/hashicorp/nomad/client/driver/env" cstructs "github.com/hashicorp/nomad/client/driver/structs" "github.com/hashicorp/nomad/nomad/structs" ) -func testDockerDriverContext(task string) *DriverContext { - cfg := testConfig() - cfg.DevMode = true - return NewDriverContext(task, cfg, cfg.Node, testLogger()) -} - // dockerIsConnected checks to see if a docker daemon is available (local or remote) func dockerIsConnected(t *testing.T) bool { client, err := docker.NewClientFromEnv() @@ -96,23 +90,22 @@ func dockerSetup(t *testing.T, task *structs.Task) (*docker.Client, DriverHandle t.Fatalf("Failed to initialize client: %s\nStack\n%s", err, debug.Stack()) } - driverCtx := testDockerDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) + driverCtx, execCtx := testDriverContexts(task) driver := NewDockerDriver(driverCtx) - handle, err := driver.Start(ctx, task) + handle, err := driver.Start(execCtx, task) if err != nil { - ctx.AllocDir.Destroy() + execCtx.AllocDir.Destroy() t.Fatalf("Failed to start driver: %s\nStack\n%s", err, debug.Stack()) } if handle == nil { - ctx.AllocDir.Destroy() + execCtx.AllocDir.Destroy() t.Fatalf("handle is nil\nStack\n%s", debug.Stack()) } cleanup := func() { handle.Kill() - ctx.AllocDir.Destroy() + execCtx.AllocDir.Destroy() } return client, handle, cleanup @@ -138,7 +131,8 @@ func TestDockerDriver_Handle(t *testing.T) { // This test should always pass, even if docker daemon is not available func TestDockerDriver_Fingerprint(t *testing.T) { t.Parallel() - d := NewDockerDriver(testDockerDriverContext("")) + driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"}) + d := NewDockerDriver(driverCtx) node := &structs.Node{ Attributes: make(map[string]string), } @@ -169,12 +163,11 @@ func TestDockerDriver_StartOpen_Wait(t *testing.T) { Resources: basicResources, } - driverCtx := testDockerDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewDockerDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -184,7 +177,7 @@ func TestDockerDriver_StartOpen_Wait(t *testing.T) { defer handle.Kill() // Attempt to open - handle2, err := d.Open(ctx, handle.ID()) + handle2, err := d.Open(execCtx, handle.ID()) if err != nil { t.Fatalf("err: %v", err) } @@ -246,7 +239,7 @@ func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) { "args": []string{ "-c", fmt.Sprintf(`sleep 1; echo -n %s > $%s/%s`, - string(exp), environment.AllocDir, file), + string(exp), env.AllocDir, file), }, }, Resources: &structs.Resources{ @@ -255,12 +248,11 @@ func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) { }, } - driverCtx := testDockerDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewDockerDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -279,7 +271,7 @@ func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) { } // Check that data was written to the shared alloc directory. - outputFile := filepath.Join(ctx.AllocDir.SharedDir, file) + outputFile := filepath.Join(execCtx.AllocDir.SharedDir, file) act, err := ioutil.ReadFile(outputFile) if err != nil { t.Fatalf("Couldn't read expected output: %v", err) @@ -350,12 +342,11 @@ func TestDocker_StartN(t *testing.T) { // Let's spin up a bunch of things var err error for idx, task := range taskList { - driverCtx := testDockerDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewDockerDriver(driverCtx) - handles[idx], err = d.Start(ctx, task) + handles[idx], err = d.Start(execCtx, task) if err != nil { t.Errorf("Failed starting task #%d: %s", idx+1, err) } @@ -408,12 +399,11 @@ func TestDocker_StartNVersions(t *testing.T) { // Let's spin up a bunch of things var err error for idx, task := range taskList { - driverCtx := testDockerDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewDockerDriver(driverCtx) - handles[idx], err = d.Start(ctx, task) + handles[idx], err = d.Start(execCtx, task) if err != nil { t.Errorf("Failed starting task #%d: %s", idx+1, err) } diff --git a/client/driver/driver_test.go b/client/driver/driver_test.go index b5df412ca69b..02b74f835744 100644 --- a/client/driver/driver_test.go +++ b/client/driver/driver_test.go @@ -49,23 +49,26 @@ func testConfig() *config.Config { return conf } -func testDriverContext(task string) *DriverContext { +func testDriverContexts(task *structs.Task) (*DriverContext, *ExecContext) { cfg := testConfig() - return NewDriverContext(task, cfg, cfg.Node, testLogger()) -} - -func testDriverExecContext(task *structs.Task, driverCtx *DriverContext) *ExecContext { - allocDir := allocdir.NewAllocDir(filepath.Join(driverCtx.config.AllocDir, structs.GenerateUUID())) + allocDir := allocdir.NewAllocDir(filepath.Join(cfg.AllocDir, structs.GenerateUUID())) allocDir.Build([]*structs.Task{task}) - ctx := NewExecContext(allocDir, fmt.Sprintf("alloc-id-%d", int(rand.Int31()))) - return ctx + execCtx := NewExecContext(allocDir, fmt.Sprintf("alloc-id-%d", int(rand.Int31()))) + + taskEnv, err := GetTaskEnv(allocDir, cfg.Node, task) + if err != nil { + return nil, nil + } + + driverCtx := NewDriverContext(task.Name, cfg, cfg.Node, testLogger(), taskEnv) + return driverCtx, execCtx } func TestDriver_KillTimeout(t *testing.T) { - ctx := testDriverContext("foo") - ctx.config.MaxKillTimeout = 10 * time.Second expected := 1 * time.Second - task := &structs.Task{KillTimeout: expected} + task := &structs.Task{Name: "foo", KillTimeout: expected} + ctx, _ := testDriverContexts(task) + ctx.config.MaxKillTimeout = 10 * time.Second if actual := ctx.KillTimeout(task); expected != actual { t.Fatalf("KillTimeout(%v) returned %v; want %v", task, actual, expected) @@ -79,7 +82,7 @@ func TestDriver_KillTimeout(t *testing.T) { } } -func TestDriver_TaskEnvironmentVariables(t *testing.T) { +func TestDriver_GetTaskEnv(t *testing.T) { t.Parallel() task := &structs.Task{ Env: map[string]string{ @@ -123,7 +126,7 @@ func TestDriver_TaskEnvironmentVariables(t *testing.T) { "lorem": "ipsum", } - act := env.Map() + act := env.EnvMap() if !reflect.DeepEqual(act, exp) { t.Fatalf("GetTaskEnv() returned %#v; want %#v", act, exp) } diff --git a/client/driver/exec_test.go b/client/driver/exec_test.go index 66795781fee1..5b5c1de2def7 100644 --- a/client/driver/exec_test.go +++ b/client/driver/exec_test.go @@ -9,7 +9,7 @@ import ( "time" "github.com/hashicorp/nomad/client/config" - "github.com/hashicorp/nomad/client/driver/environment" + "github.com/hashicorp/nomad/client/driver/env" "github.com/hashicorp/nomad/nomad/structs" ctestutils "github.com/hashicorp/nomad/client/testutil" @@ -18,7 +18,8 @@ import ( func TestExecDriver_Fingerprint(t *testing.T) { t.Parallel() ctestutils.ExecCompatible(t) - d := NewExecDriver(testDriverContext("")) + driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"}) + d := NewExecDriver(driverCtx) node := &structs.Node{ Attributes: make(map[string]string), } @@ -46,12 +47,11 @@ func TestExecDriver_StartOpen_Wait(t *testing.T) { Resources: basicResources, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -60,7 +60,7 @@ func TestExecDriver_StartOpen_Wait(t *testing.T) { } // Attempt to open - handle2, err := d.Open(ctx, handle.ID()) + handle2, err := d.Open(execCtx, handle.ID()) if err != nil { t.Fatalf("err: %v", err) } @@ -81,12 +81,11 @@ func TestExecDriver_Start_Wait(t *testing.T) { Resources: basicResources, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -126,12 +125,11 @@ func TestExecDriver_Start_Artifact_basic(t *testing.T) { Resources: basicResources, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -174,12 +172,11 @@ func TestExecDriver_Start_Artifact_expanded(t *testing.T) { Resources: basicResources, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -215,18 +212,17 @@ func TestExecDriver_Start_Wait_AllocDir(t *testing.T) { "command": "/bin/bash", "args": []string{ "-c", - fmt.Sprintf(`sleep 1; echo -n %s > $%s/%s`, string(exp), environment.AllocDir, file), + fmt.Sprintf(`sleep 1; echo -n %s > $%s/%s`, string(exp), env.AllocDir, file), }, }, Resources: basicResources, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -245,7 +241,7 @@ func TestExecDriver_Start_Wait_AllocDir(t *testing.T) { } // Check that data was written to the shared alloc directory. - outputFile := filepath.Join(ctx.AllocDir.SharedDir, file) + outputFile := filepath.Join(execCtx.AllocDir.SharedDir, file) act, err := ioutil.ReadFile(outputFile) if err != nil { t.Fatalf("Couldn't read expected output: %v", err) @@ -268,12 +264,11 @@ func TestExecDriver_Start_Kill_Wait(t *testing.T) { Resources: basicResources, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } diff --git a/client/driver/executor/exec_linux.go b/client/driver/executor/exec_linux.go index e9ceca710cd2..43a7730a4a11 100644 --- a/client/driver/executor/exec_linux.go +++ b/client/driver/executor/exec_linux.go @@ -20,7 +20,6 @@ import ( cgroupConfig "github.com/opencontainers/runc/libcontainer/configs" "github.com/hashicorp/nomad/client/allocdir" - "github.com/hashicorp/nomad/client/driver/environment" "github.com/hashicorp/nomad/client/driver/spawn" cstructs "github.com/hashicorp/nomad/client/driver/structs" "github.com/hashicorp/nomad/nomad/structs" @@ -161,8 +160,9 @@ func (e *LinuxExecutor) Start() error { // Parse the commands arguments and replace instances of Nomad environment // variables. - e.cmd.Path = e.taskEnv.ReplaceEnv(e.cmd.Path, envVars.Map()) - e.cmd.Args = e.taskEnv.ParseAndReplace(e.cmd.Args, envVars.Map()) + e.cmd.Path = e.taskEnv.ReplaceEnv(e.cmd.Path) + e.cmd.Args = e.taskEnv.ParseAndReplace(e.cmd.Args) + e.cmd.Env = e.taskEnv.EnvList() spawnState := filepath.Join(e.allocDir, fmt.Sprintf("%s_%s", e.taskName, "exit_status")) e.spawn = spawn.NewSpawner(spawnState) @@ -283,14 +283,7 @@ func (e *LinuxExecutor) ConfigureTaskDir(taskName string, alloc *allocdir.AllocD } // Set the tasks AllocDir environment variable. - env, err := environment.ParseFromList(e.cmd.Env) - if err != nil { - return err - } - env.SetAllocDir(filepath.Join("/", allocdir.SharedAllocName)) - env.SetTaskLocalDir(filepath.Join("/", allocdir.TaskLocal)) - e.cmd.Env = env.List() - + e.taskEnv.SetAllocDir(filepath.Join("/", allocdir.SharedAllocName)).SetTaskLocalDir(filepath.Join("/", allocdir.TaskLocal)).Build() return nil } diff --git a/client/driver/executor/test_harness_test.go b/client/driver/executor/test_harness_test.go index 8e156543ddc1..e31cc422c33b 100644 --- a/client/driver/executor/test_harness_test.go +++ b/client/driver/executor/test_harness_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/hashicorp/nomad/client/allocdir" + "github.com/hashicorp/nomad/client/driver/env" "github.com/hashicorp/nomad/helper/testtask" "github.com/hashicorp/nomad/nomad/mock" "github.com/hashicorp/nomad/nomad/structs" @@ -45,13 +46,18 @@ func mockAllocDir(t *testing.T) (string, *allocdir.AllocDir) { return task.Name, allocDir } -func testExecutor(t *testing.T, buildExecutor func() Executor, compatible func(*testing.T)) { +func testExecutorContext() *ExecutorContext { + taskEnv := env.NewTaskEnvironment(mock.Node()) + return &ExecutorContext{taskEnv: taskEnv} +} + +func testExecutor(t *testing.T, buildExecutor func(*ExecutorContext) Executor, compatible func(*testing.T)) { if compatible != nil { compatible(t) } command := func(name string, args ...string) Executor { - e := buildExecutor() + e := buildExecutor(testExecutorContext()) SetCommand(e, name, args) testtask.SetCmdEnv(e.Command()) return e @@ -185,7 +191,7 @@ func Executor_Start_Kill(t *testing.T, command buildExecCommand) { } } -func Executor_Open(t *testing.T, command buildExecCommand, newExecutor func() Executor) { +func Executor_Open(t *testing.T, command buildExecCommand, newExecutor func(*ExecutorContext) Executor) { task, alloc := mockAllocDir(t) defer alloc.Destroy() @@ -216,7 +222,7 @@ func Executor_Open(t *testing.T, command buildExecCommand, newExecutor func() Ex log.Panicf("ID() failed: %v", err) } - e2 := newExecutor() + e2 := newExecutor(testExecutorContext()) if err := e2.Open(id); err != nil { log.Panicf("Open(%v) failed: %v", id, err) } @@ -236,7 +242,7 @@ func Executor_Open(t *testing.T, command buildExecCommand, newExecutor func() Ex } } -func Executor_Open_Invalid(t *testing.T, command buildExecCommand, newExecutor func() Executor) { +func Executor_Open_Invalid(t *testing.T, command buildExecCommand, newExecutor func(*ExecutorContext) Executor) { task, alloc := mockAllocDir(t) e := command(testtask.Path(), "echo", "foo") @@ -271,7 +277,7 @@ func Executor_Open_Invalid(t *testing.T, command buildExecCommand, newExecutor f log.Panicf("alloc.Destroy() failed: %v", err) } - e2 := newExecutor() + e2 := newExecutor(testExecutorContext()) if err := e2.Open(id); err == nil { log.Panicf("Open(%v) should have failed", id) } diff --git a/client/driver/java_test.go b/client/driver/java_test.go index 5fa47320b4bd..b06aaa56164b 100644 --- a/client/driver/java_test.go +++ b/client/driver/java_test.go @@ -21,7 +21,8 @@ func javaLocated() bool { func TestJavaDriver_Fingerprint(t *testing.T) { t.Parallel() ctestutils.JavaCompatible(t) - d := NewJavaDriver(testDriverContext("")) + driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"}) + d := NewJavaDriver(driverCtx) node := &structs.Node{ Attributes: make(map[string]string), } @@ -59,12 +60,11 @@ func TestJavaDriver_StartOpen_Wait(t *testing.T) { Resources: basicResources, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewJavaDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -73,7 +73,7 @@ func TestJavaDriver_StartOpen_Wait(t *testing.T) { } // Attempt to open - handle2, err := d.Open(ctx, handle.ID()) + handle2, err := d.Open(execCtx, handle.ID()) if err != nil { t.Fatalf("err: %v", err) } @@ -105,12 +105,11 @@ func TestJavaDriver_Start_Wait(t *testing.T) { Resources: basicResources, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewJavaDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -151,12 +150,11 @@ func TestJavaDriver_Start_Kill_Wait(t *testing.T) { Resources: basicResources, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewJavaDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } diff --git a/client/driver/qemu_test.go b/client/driver/qemu_test.go index 65846203bc6a..151fcf7f9555 100644 --- a/client/driver/qemu_test.go +++ b/client/driver/qemu_test.go @@ -14,7 +14,8 @@ import ( func TestQemuDriver_Fingerprint(t *testing.T) { t.Parallel() ctestutils.QemuCompatible(t) - d := NewQemuDriver(testDriverContext("")) + driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"}) + d := NewQemuDriver(driverCtx) node := &structs.Node{ Attributes: make(map[string]string), } @@ -59,12 +60,11 @@ func TestQemuDriver_StartOpen_Wait(t *testing.T) { }, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewQemuDriver(driverCtx) - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -73,7 +73,7 @@ func TestQemuDriver_StartOpen_Wait(t *testing.T) { } // Attempt to open - handle2, err := d.Open(ctx, handle.ID()) + handle2, err := d.Open(execCtx, handle.ID()) if err != nil { t.Fatalf("err: %v", err) } @@ -103,12 +103,11 @@ func TestQemuDriver_RequiresMemory(t *testing.T) { }, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewQemuDriver(driverCtx) - _, err := d.Start(ctx, task) + _, err := d.Start(execCtx, task) if err == nil { t.Fatalf("Expected error when not specifying memory") } diff --git a/client/driver/raw_exec_test.go b/client/driver/raw_exec_test.go index 8d656c9b085c..a2d3a86345d7 100644 --- a/client/driver/raw_exec_test.go +++ b/client/driver/raw_exec_test.go @@ -11,14 +11,15 @@ import ( "time" "github.com/hashicorp/nomad/client/config" - "github.com/hashicorp/nomad/client/driver/environment" + "github.com/hashicorp/nomad/client/driver/env" "github.com/hashicorp/nomad/helper/testtask" "github.com/hashicorp/nomad/nomad/structs" ) func TestRawExecDriver_Fingerprint(t *testing.T) { t.Parallel() - d := NewRawExecDriver(testDriverContext("")) + driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"}) + d := NewRawExecDriver(driverCtx) node := &structs.Node{ Attributes: make(map[string]string), } @@ -62,12 +63,11 @@ func TestRawExecDriver_StartOpen_Wait(t *testing.T) { Resources: basicResources, } testtask.SetTaskEnv(task) - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() - + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewRawExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -76,7 +76,7 @@ func TestRawExecDriver_StartOpen_Wait(t *testing.T) { } // Attempt to open - handle2, err := d.Open(ctx, handle.ID()) + handle2, err := d.Open(execCtx, handle.ID()) if err != nil { t.Fatalf("err: %v", err) } @@ -110,12 +110,11 @@ func TestRawExecDriver_Start_Artifact_basic(t *testing.T) { } testtask.SetTaskEnv(task) - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() - + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewRawExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -124,7 +123,7 @@ func TestRawExecDriver_Start_Artifact_basic(t *testing.T) { } // Attempt to open - handle2, err := d.Open(ctx, handle.ID()) + handle2, err := d.Open(execCtx, handle.ID()) if err != nil { t.Fatalf("err: %v", err) } @@ -158,12 +157,11 @@ func TestRawExecDriver_Start_Artifact_expanded(t *testing.T) { } testtask.SetTaskEnv(task) - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() - + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewRawExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -172,7 +170,7 @@ func TestRawExecDriver_Start_Artifact_expanded(t *testing.T) { } // Attempt to open - handle2, err := d.Open(ctx, handle.ID()) + handle2, err := d.Open(execCtx, handle.ID()) if err != nil { t.Fatalf("err: %v", err) } @@ -199,13 +197,11 @@ func TestRawExecDriver_Start_Wait(t *testing.T) { Resources: basicResources, } testtask.SetTaskEnv(task) - - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() - + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewRawExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -234,7 +230,7 @@ func TestRawExecDriver_Start_Wait_AllocDir(t *testing.T) { t.Parallel() exp := []byte{'w', 'i', 'n'} file := "output.txt" - outPath := fmt.Sprintf(`$%s/%s`, environment.AllocDir, file) + outPath := fmt.Sprintf(`$%s/%s`, env.AllocDir, file) task := &structs.Task{ Name: "sleep", Config: map[string]interface{}{ @@ -248,12 +244,11 @@ func TestRawExecDriver_Start_Wait_AllocDir(t *testing.T) { } testtask.SetTaskEnv(task) - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() - + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewRawExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -272,7 +267,7 @@ func TestRawExecDriver_Start_Wait_AllocDir(t *testing.T) { } // Check that data was written to the shared alloc directory. - outputFile := filepath.Join(ctx.AllocDir.SharedDir, file) + outputFile := filepath.Join(execCtx.AllocDir.SharedDir, file) act, err := ioutil.ReadFile(outputFile) if err != nil { t.Fatalf("Couldn't read expected output: %v", err) @@ -295,12 +290,11 @@ func TestRawExecDriver_Start_Kill_Wait(t *testing.T) { } testtask.SetTaskEnv(task) - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) - defer ctx.AllocDir.Destroy() - + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewRawExecDriver(driverCtx) - handle, err := d.Start(ctx, task) + + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } diff --git a/client/driver/rkt_test.go b/client/driver/rkt_test.go index 13429c3cbca6..95a8e5f71322 100644 --- a/client/driver/rkt_test.go +++ b/client/driver/rkt_test.go @@ -50,7 +50,8 @@ func TestRktDriver_Handle(t *testing.T) { // The fingerprinter test should always pass, even if rkt is not installed. func TestRktDriver_Fingerprint(t *testing.T) { ctestutils.RktCompatible(t) - d := NewRktDriver(testDriverContext("")) + driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"}) + d := NewRktDriver(driverCtx) node := &structs.Node{ Attributes: make(map[string]string), } @@ -88,12 +89,11 @@ func TestRktDriver_Start(t *testing.T) { }, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewRktDriver(driverCtx) - defer ctx.AllocDir.Destroy() - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -102,7 +102,7 @@ func TestRktDriver_Start(t *testing.T) { } // Attempt to open - handle2, err := d.Open(ctx, handle.ID()) + handle2, err := d.Open(execCtx, handle.ID()) if err != nil { t.Fatalf("err: %v", err) } @@ -132,12 +132,11 @@ func TestRktDriver_Start_Wait(t *testing.T) { }, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewRktDriver(driverCtx) - defer ctx.AllocDir.Destroy() - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -177,12 +176,11 @@ func TestRktDriver_Start_Wait_Skip_Trust(t *testing.T) { }, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewRktDriver(driverCtx) - defer ctx.AllocDir.Destroy() - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -223,12 +221,11 @@ func TestRktDriver_Start_Wait_Logs(t *testing.T) { }, } - driverCtx := testDriverContext(task.Name) - ctx := testDriverExecContext(task, driverCtx) + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() d := NewRktDriver(driverCtx) - defer ctx.AllocDir.Destroy() - handle, err := d.Start(ctx, task) + handle, err := d.Start(execCtx, task) if err != nil { t.Fatalf("err: %v", err) } @@ -246,7 +243,7 @@ func TestRktDriver_Start_Wait_Logs(t *testing.T) { t.Fatalf("timeout") } - taskDir, ok := ctx.AllocDir.TaskDirs[task.Name] + taskDir, ok := execCtx.AllocDir.TaskDirs[task.Name] if !ok { t.Fatalf("Could not find task directory for task: %v", task) }