2015-09-20 01:47:04 +00:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2015-09-20 01:47:04 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-09-25 23:49:14 +00:00
|
|
|
|
|
|
|
ctestutil "github.com/hashicorp/nomad/client/testutil"
|
2015-09-20 01:47:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
constraint = &structs.Resources{
|
2015-09-24 21:44:41 +00:00
|
|
|
CPU: 250,
|
2015-09-20 01:47:04 +00:00
|
|
|
MemoryMB: 256,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
|
|
|
MBits: 50,
|
2015-09-22 20:33:16 +00:00
|
|
|
DynamicPorts: []string{"http"},
|
2015-09-20 01:47:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
func mockAllocDir(t *testing.T) (string, *allocdir.AllocDir) {
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
|
|
|
|
allocDir := allocdir.NewAllocDir(filepath.Join(os.TempDir(), alloc.ID))
|
|
|
|
if err := allocDir.Build([]*structs.Task{task}); err != nil {
|
|
|
|
t.Fatalf("allocDir.Build() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return task.Name, allocDir
|
|
|
|
}
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
func TestExecutorLinux_Start_Invalid(t *testing.T) {
|
2015-09-25 23:49:14 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
2015-09-20 01:47:04 +00:00
|
|
|
invalid := "/bin/foobar"
|
|
|
|
e := Command(invalid, "1")
|
|
|
|
|
|
|
|
if err := e.Limit(constraint); err != nil {
|
|
|
|
t.Fatalf("Limit() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
task, alloc := mockAllocDir(t)
|
|
|
|
defer alloc.Destroy()
|
|
|
|
if err := e.ConfigureTaskDir(task, alloc); err != nil {
|
|
|
|
t.Fatalf("ConfigureTaskDir(%v, %v) failed: %v", task, alloc, err)
|
|
|
|
}
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
if err := e.Start(); err == nil {
|
|
|
|
t.Fatalf("Start(%v) should have failed", invalid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecutorLinux_Start_Wait_Failure_Code(t *testing.T) {
|
2015-09-25 23:49:14 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
2015-09-20 01:47:04 +00:00
|
|
|
e := Command("/bin/date", "-invalid")
|
|
|
|
|
|
|
|
if err := e.Limit(constraint); err != nil {
|
|
|
|
t.Fatalf("Limit() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
task, alloc := mockAllocDir(t)
|
|
|
|
defer alloc.Destroy()
|
|
|
|
if err := e.ConfigureTaskDir(task, alloc); err != nil {
|
|
|
|
t.Fatalf("ConfigureTaskDir(%v, %v) failed: %v", task, alloc, err)
|
|
|
|
}
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
if err := e.Start(); err != nil {
|
|
|
|
t.Fatalf("Start() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := e.Wait(); err == nil {
|
|
|
|
t.Fatalf("Wait() should have failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecutorLinux_Start_Wait(t *testing.T) {
|
2015-09-25 23:49:14 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
|
|
|
task, alloc := mockAllocDir(t)
|
|
|
|
defer alloc.Destroy()
|
2015-09-20 01:47:04 +00:00
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
taskDir, ok := alloc.TaskDirs[task]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("No task directory found for task %v", task)
|
|
|
|
}
|
2015-09-20 01:47:04 +00:00
|
|
|
|
|
|
|
expected := "hello world"
|
2015-09-25 23:49:14 +00:00
|
|
|
file := filepath.Join(allocdir.TaskLocal, "output.txt")
|
|
|
|
absFilePath := filepath.Join(taskDir, file)
|
2015-09-28 00:41:43 +00:00
|
|
|
cmd := fmt.Sprintf(`"%v \"%v\" > %v"`, "/bin/sleep 1 ; echo -n", expected, file)
|
2015-09-20 01:47:04 +00:00
|
|
|
e := Command("/bin/bash", "-c", cmd)
|
|
|
|
|
|
|
|
if err := e.Limit(constraint); err != nil {
|
|
|
|
t.Fatalf("Limit() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
if err := e.ConfigureTaskDir(task, alloc); err != nil {
|
|
|
|
t.Fatalf("ConfigureTaskDir(%v, %v) failed: %v", task, alloc, err)
|
|
|
|
}
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
if err := e.Start(); err != nil {
|
|
|
|
t.Fatalf("Start() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := e.Wait(); err != nil {
|
|
|
|
t.Fatalf("Wait() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
output, err := ioutil.ReadFile(absFilePath)
|
2015-09-20 01:47:04 +00:00
|
|
|
if err != nil {
|
2015-09-25 23:49:14 +00:00
|
|
|
t.Fatalf("Couldn't read file %v", absFilePath)
|
2015-09-20 01:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
act := string(output)
|
|
|
|
if act != expected {
|
|
|
|
t.Fatalf("Command output incorrectly: want %v; got %v", expected, act)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecutorLinux_Start_Kill(t *testing.T) {
|
2015-09-25 23:49:14 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
|
|
|
task, alloc := mockAllocDir(t)
|
|
|
|
defer alloc.Destroy()
|
2015-09-20 01:47:04 +00:00
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
taskDir, ok := alloc.TaskDirs[task]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("No task directory found for task %v", task)
|
|
|
|
}
|
2015-09-20 01:47:04 +00:00
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
filePath := filepath.Join(taskDir, "output")
|
2015-09-20 01:47:04 +00:00
|
|
|
e := Command("/bin/bash", "-c", "sleep 1 ; echo \"failure\" > "+filePath)
|
|
|
|
|
|
|
|
if err := e.Limit(constraint); err != nil {
|
|
|
|
t.Fatalf("Limit() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
if err := e.ConfigureTaskDir(task, alloc); err != nil {
|
|
|
|
t.Fatalf("ConfigureTaskDir(%v, %v) failed: %v", task, alloc, err)
|
|
|
|
}
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
if err := e.Start(); err != nil {
|
|
|
|
t.Fatalf("Start() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := e.Shutdown(); err != nil {
|
|
|
|
t.Fatalf("Shutdown() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1500 * time.Millisecond)
|
|
|
|
|
|
|
|
// Check that the file doesn't exist.
|
|
|
|
if _, err := os.Stat(filePath); err == nil {
|
|
|
|
t.Fatalf("Stat(%v) should have failed: task not killed", filePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecutorLinux_Open(t *testing.T) {
|
2015-09-25 23:49:14 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
|
|
|
task, alloc := mockAllocDir(t)
|
|
|
|
defer alloc.Destroy()
|
2015-09-20 01:47:04 +00:00
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
taskDir, ok := alloc.TaskDirs[task]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("No task directory found for task %v", task)
|
|
|
|
}
|
2015-09-20 01:47:04 +00:00
|
|
|
|
2015-10-29 22:26:35 +00:00
|
|
|
expected := "hello world"
|
|
|
|
file := filepath.Join(allocdir.TaskLocal, "output.txt")
|
|
|
|
absFilePath := filepath.Join(taskDir, file)
|
|
|
|
cmd := fmt.Sprintf(`"%v \"%v\" > %v"`, "/bin/sleep 1 ; echo -n", expected, file)
|
|
|
|
e := Command("/bin/bash", "-c", cmd)
|
2015-09-20 01:47:04 +00:00
|
|
|
|
|
|
|
if err := e.Limit(constraint); err != nil {
|
|
|
|
t.Fatalf("Limit() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
if err := e.ConfigureTaskDir(task, alloc); err != nil {
|
|
|
|
t.Fatalf("ConfigureTaskDir(%v, %v) failed: %v", task, alloc, err)
|
|
|
|
}
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
if err := e.Start(); err != nil {
|
|
|
|
t.Fatalf("Start() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := e.ID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ID() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-29 22:26:35 +00:00
|
|
|
e2 := NewExecutor()
|
|
|
|
if err := e2.Open(id); err != nil {
|
|
|
|
t.Fatalf("Open(%v) failed: %v", id, err)
|
2015-09-20 01:47:04 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 22:26:35 +00:00
|
|
|
if err := e2.Wait(); err != nil {
|
|
|
|
t.Fatalf("Wait() failed: %v", err)
|
|
|
|
}
|
2015-09-20 01:47:04 +00:00
|
|
|
|
2015-10-29 22:26:35 +00:00
|
|
|
output, err := ioutil.ReadFile(absFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't read file %v", absFilePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
act := string(output)
|
|
|
|
if act != expected {
|
|
|
|
t.Fatalf("Command output incorrectly: want %v; got %v", expected, act)
|
2015-09-20 01:47:04 +00:00
|
|
|
}
|
|
|
|
}
|