Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Jan 11, 2016
1 parent 8cc513d commit dccbc03
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 173 deletions.
56 changes: 23 additions & 33 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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),
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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{
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
29 changes: 16 additions & 13 deletions client/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand Down Expand Up @@ -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)
}
Expand Down
53 changes: 24 additions & 29 deletions client/driver/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
15 changes: 4 additions & 11 deletions client/driver/executor/exec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
Loading

0 comments on commit dccbc03

Please sign in to comment.