2016-02-05 00:03:17 +00:00
|
|
|
package executor
|
2016-02-04 23:39:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
|
|
|
"github.com/hashicorp/nomad/client/driver/env"
|
2016-02-05 19:07:16 +00:00
|
|
|
"github.com/hashicorp/nomad/client/testutil"
|
2016-02-04 23:39:29 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-02-05 19:07:16 +00:00
|
|
|
tu "github.com/hashicorp/nomad/testutil"
|
2016-02-04 23:39:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
constraint = &structs.Resources{
|
|
|
|
CPU: 250,
|
|
|
|
MemoryMB: 256,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
|
|
|
MBits: 50,
|
|
|
|
DynamicPorts: []structs.Port{{Label: "http"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Panicf("allocDir.Build() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return task.Name, allocDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func testExecutorContext(t *testing.T) *ExecutorContext {
|
|
|
|
taskEnv := env.NewTaskEnvironment(mock.Node())
|
|
|
|
taskName, allocDir := mockAllocDir(t)
|
|
|
|
ctx := &ExecutorContext{
|
|
|
|
TaskEnv: taskEnv,
|
|
|
|
TaskName: taskName,
|
|
|
|
AllocDir: allocDir,
|
|
|
|
TaskResources: constraint,
|
2016-02-11 18:58:18 +00:00
|
|
|
LogConfig: structs.DefaultLogConfig(),
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecutor_Start_Invalid(t *testing.T) {
|
|
|
|
invalid := "/bin/foobar"
|
|
|
|
execCmd := ExecCommand{Cmd: invalid, Args: []string{"1"}}
|
|
|
|
ctx := testExecutorContext(t)
|
2016-02-05 18:49:54 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
2016-02-04 23:39:29 +00:00
|
|
|
executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
|
|
|
|
_, err := executor.LaunchCmd(&execCmd, ctx)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecutor_Start_Wait_Failure_Code(t *testing.T) {
|
|
|
|
execCmd := ExecCommand{Cmd: "/bin/sleep", Args: []string{"fail"}}
|
|
|
|
ctx := testExecutorContext(t)
|
2016-02-05 18:49:54 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
2016-02-04 23:39:29 +00:00
|
|
|
executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
|
|
|
|
ps, _ := executor.LaunchCmd(&execCmd, ctx)
|
|
|
|
if ps.Pid == 0 {
|
|
|
|
t.Fatalf("expected process to start and have non zero pid")
|
|
|
|
}
|
|
|
|
ps, _ = executor.Wait()
|
|
|
|
if ps.ExitCode < 1 {
|
|
|
|
t.Fatalf("expected exit code to be non zero, actual: %v", ps.ExitCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecutor_Start_Wait(t *testing.T) {
|
|
|
|
execCmd := ExecCommand{Cmd: "/bin/echo", Args: []string{"hello world"}}
|
|
|
|
ctx := testExecutorContext(t)
|
2016-02-23 21:32:17 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
2016-02-04 23:39:29 +00:00
|
|
|
executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
|
|
|
|
ps, err := executor.LaunchCmd(&execCmd, ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in launching command: %v", err)
|
|
|
|
}
|
|
|
|
if ps.Pid == 0 {
|
|
|
|
t.Fatalf("expected process to start and have non zero pid")
|
|
|
|
}
|
|
|
|
ps, err = executor.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in waiting for command: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "hello world"
|
2016-02-25 04:06:43 +00:00
|
|
|
file := filepath.Join(ctx.AllocDir.LogDir(), "web.stdout.0")
|
|
|
|
output, err := ioutil.ReadFile(file)
|
2016-02-04 23:39:29 +00:00
|
|
|
if err != nil {
|
2016-02-25 04:06:43 +00:00
|
|
|
t.Fatalf("Couldn't read file %v", file)
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
act := strings.TrimSpace(string(output))
|
|
|
|
if act != expected {
|
|
|
|
t.Fatalf("Command output incorrectly: want %v; got %v", expected, act)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 19:07:16 +00:00
|
|
|
func TestExecutor_IsolationAndConstraints(t *testing.T) {
|
|
|
|
testutil.ExecCompatible(t)
|
|
|
|
|
|
|
|
execCmd := ExecCommand{Cmd: "/bin/echo", Args: []string{"hello world"}}
|
|
|
|
ctx := testExecutorContext(t)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
|
|
|
|
ctx.FSIsolation = true
|
|
|
|
ctx.ResourceLimits = true
|
|
|
|
ctx.UnprivilegedUser = true
|
|
|
|
|
|
|
|
executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
|
|
|
|
ps, err := executor.LaunchCmd(&execCmd, ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in launching command: %v", err)
|
|
|
|
}
|
|
|
|
if ps.Pid == 0 {
|
|
|
|
t.Fatalf("expected process to start and have non zero pid")
|
|
|
|
}
|
|
|
|
ps, err = executor.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in waiting for command: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "hello world"
|
2016-02-25 04:06:43 +00:00
|
|
|
file := filepath.Join(ctx.AllocDir.LogDir(), "web.stdout.0")
|
|
|
|
output, err := ioutil.ReadFile(file)
|
2016-02-05 19:07:16 +00:00
|
|
|
if err != nil {
|
2016-02-25 04:06:43 +00:00
|
|
|
t.Fatalf("Couldn't read file %v", file)
|
2016-02-05 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
act := strings.TrimSpace(string(output))
|
|
|
|
if act != expected {
|
|
|
|
t.Fatalf("Command output incorrectly: want %v; got %v", expected, act)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 19:05:08 +00:00
|
|
|
func TestExecutor_DestroyCgroup(t *testing.T) {
|
|
|
|
testutil.ExecCompatible(t)
|
|
|
|
|
|
|
|
execCmd := ExecCommand{Cmd: "/bin/bash", Args: []string{"-c", "/usr/bin/yes"}}
|
|
|
|
ctx := testExecutorContext(t)
|
|
|
|
ctx.LogConfig.MaxFiles = 1
|
|
|
|
ctx.LogConfig.MaxFileSizeMB = 300
|
|
|
|
//defer ctx.AllocDir.Destroy()
|
|
|
|
|
|
|
|
ctx.FSIsolation = true
|
|
|
|
ctx.ResourceLimits = true
|
|
|
|
ctx.UnprivilegedUser = true
|
|
|
|
|
|
|
|
executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
|
|
|
|
ps, err := executor.LaunchCmd(&execCmd, ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in launching command: %v", err)
|
|
|
|
}
|
|
|
|
if ps.Pid == 0 {
|
|
|
|
t.Fatalf("expected process to start and have non zero pid")
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
_, err = executor.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in waiting for command: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
executor.Exit()
|
|
|
|
file := filepath.Join(ctx.AllocDir.LogDir(), "web.stdout.0")
|
|
|
|
finfo, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error stating stdout file: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
finfo1, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error stating stdout file: %v", err)
|
|
|
|
}
|
|
|
|
if finfo.Size() != finfo1.Size() {
|
|
|
|
t.Fatalf("Expected size: %v, actual: %v", finfo.Size(), finfo1.Size())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:39:29 +00:00
|
|
|
func TestExecutor_Start_Kill(t *testing.T) {
|
|
|
|
execCmd := ExecCommand{Cmd: "/bin/sleep", Args: []string{"10 && hello world"}}
|
|
|
|
ctx := testExecutorContext(t)
|
2016-02-05 18:49:54 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
2016-02-04 23:39:29 +00:00
|
|
|
executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
|
|
|
|
ps, err := executor.LaunchCmd(&execCmd, ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in launching command: %v", err)
|
|
|
|
}
|
|
|
|
if ps.Pid == 0 {
|
|
|
|
t.Fatalf("expected process to start and have non zero pid")
|
|
|
|
}
|
|
|
|
ps, err = executor.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in waiting for command: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-25 04:06:43 +00:00
|
|
|
file := filepath.Join(ctx.AllocDir.LogDir(), "web.stdout.0")
|
2016-02-05 19:07:16 +00:00
|
|
|
time.Sleep(time.Duration(tu.TestMultiplier()*2) * time.Second)
|
2016-02-04 23:39:29 +00:00
|
|
|
|
2016-02-25 04:06:43 +00:00
|
|
|
output, err := ioutil.ReadFile(file)
|
2016-02-04 23:39:29 +00:00
|
|
|
if err != nil {
|
2016-02-25 04:06:43 +00:00
|
|
|
t.Fatalf("Couldn't read file %v", file)
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := ""
|
|
|
|
act := strings.TrimSpace(string(output))
|
|
|
|
if act != expected {
|
|
|
|
t.Fatalf("Command output incorrectly: want %v; got %v", expected, act)
|
|
|
|
}
|
|
|
|
}
|