2016-07-10 06:56:31 +00:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-01-26 04:58:24 +00:00
|
|
|
"time"
|
2016-07-10 06:56:31 +00:00
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2016-08-04 22:03:56 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/env"
|
2016-12-03 01:04:07 +00:00
|
|
|
dstructs "github.com/hashicorp/nomad/client/driver/structs"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2016-07-10 06:56:31 +00:00
|
|
|
"github.com/hashicorp/nomad/client/testutil"
|
2016-08-04 22:03:56 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2016-07-10 06:56:31 +00:00
|
|
|
)
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// testExecutorContextWithChroot returns an ExecutorContext and AllocDir with
|
|
|
|
// chroot. Use testExecutorContext if you don't need a chroot.
|
|
|
|
//
|
|
|
|
// The caller is responsible for calling AllocDir.Destroy() to cleanup.
|
|
|
|
func testExecutorContextWithChroot(t *testing.T) (*ExecutorContext, *allocdir.AllocDir) {
|
|
|
|
chrootEnv := map[string]string{
|
|
|
|
"/etc/ld.so.cache": "/etc/ld.so.cache",
|
|
|
|
"/etc/ld.so.conf": "/etc/ld.so.conf",
|
|
|
|
"/etc/ld.so.conf.d": "/etc/ld.so.conf.d",
|
|
|
|
"/lib": "/lib",
|
|
|
|
"/lib64": "/lib64",
|
|
|
|
"/usr/lib": "/usr/lib",
|
|
|
|
"/bin/ls": "/bin/ls",
|
|
|
|
"/bin/echo": "/bin/echo",
|
|
|
|
"/bin/bash": "/bin/bash",
|
2017-01-09 23:40:53 +00:00
|
|
|
"/bin/sleep": "/bin/sleep",
|
2016-12-03 01:04:07 +00:00
|
|
|
"/foobar": "/does/not/exist",
|
|
|
|
}
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
2017-05-26 23:46:03 +00:00
|
|
|
taskEnv := env.NewBuilder(mock.Node(), alloc, task, "global").Build()
|
2016-12-03 01:04:07 +00:00
|
|
|
|
2017-01-06 18:32:40 +00:00
|
|
|
allocDir := allocdir.NewAllocDir(testLogger(), filepath.Join(os.TempDir(), alloc.ID))
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := allocDir.Build(); err != nil {
|
|
|
|
log.Fatalf("AllocDir.Build() failed: %v", err)
|
|
|
|
}
|
2017-03-02 23:54:12 +00:00
|
|
|
if err := allocDir.NewTaskDir(task.Name).Build(false, chrootEnv, cstructs.FSIsolationChroot); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
allocDir.Destroy()
|
|
|
|
log.Fatalf("allocDir.NewTaskDir(%q) failed: %v", task.Name, err)
|
|
|
|
}
|
|
|
|
td := allocDir.TaskDirs[task.Name]
|
2016-08-04 22:03:56 +00:00
|
|
|
ctx := &ExecutorContext{
|
2016-12-03 01:04:07 +00:00
|
|
|
TaskEnv: taskEnv,
|
|
|
|
Task: task,
|
|
|
|
TaskDir: td.Dir,
|
|
|
|
LogDir: td.LogDir,
|
2016-08-04 22:03:56 +00:00
|
|
|
}
|
2016-12-03 01:04:07 +00:00
|
|
|
return ctx, allocDir
|
2016-08-04 22:03:56 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 06:56:31 +00:00
|
|
|
func TestExecutor_IsolationAndConstraints(t *testing.T) {
|
2017-07-21 19:14:54 +00:00
|
|
|
t.Parallel()
|
2016-07-10 06:56:31 +00:00
|
|
|
testutil.ExecCompatible(t)
|
|
|
|
|
2016-08-08 21:04:09 +00:00
|
|
|
execCmd := ExecCommand{Cmd: "/bin/ls", Args: []string{"-F", "/", "/etc/"}}
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx, allocDir := testExecutorContextWithChroot(t)
|
|
|
|
defer allocDir.Destroy()
|
2016-07-10 06:56:31 +00:00
|
|
|
|
|
|
|
execCmd.FSIsolation = true
|
|
|
|
execCmd.ResourceLimits = true
|
2018-01-08 20:56:07 +00:00
|
|
|
execCmd.User = dstructs.DefaultUnprivilegedUser
|
2016-07-10 06:56:31 +00:00
|
|
|
|
|
|
|
executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
|
2016-10-12 18:35:29 +00:00
|
|
|
|
|
|
|
if err := executor.SetContext(ctx); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
2016-10-12 18:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ps, err := executor.LaunchCmd(&execCmd)
|
2016-07-10 06:56:31 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
_, err = executor.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in waiting for command: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the resource contraints were applied
|
|
|
|
memLimits := filepath.Join(ps.IsolationConfig.CgroupPaths["memory"], "memory.limit_in_bytes")
|
|
|
|
data, err := ioutil.ReadFile(memLimits)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expectedMemLim := strconv.Itoa(ctx.Task.Resources.MemoryMB * 1024 * 1024)
|
|
|
|
actualMemLim := strings.TrimSpace(string(data))
|
|
|
|
if actualMemLim != expectedMemLim {
|
|
|
|
t.Fatalf("actual mem limit: %v, expected: %v", string(data), expectedMemLim)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := executor.Exit(); err != nil {
|
|
|
|
t.Fatalf("error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if Nomad has actually removed the cgroups
|
|
|
|
if _, err := os.Stat(memLimits); err == nil {
|
|
|
|
t.Fatalf("file %v hasn't been removed", memLimits)
|
|
|
|
}
|
|
|
|
|
2016-09-02 19:44:05 +00:00
|
|
|
expected := `/:
|
|
|
|
alloc/
|
|
|
|
bin/
|
|
|
|
dev/
|
|
|
|
etc/
|
|
|
|
lib/
|
|
|
|
lib64/
|
|
|
|
local/
|
|
|
|
proc/
|
|
|
|
secrets/
|
|
|
|
tmp/
|
|
|
|
usr/
|
|
|
|
|
|
|
|
/etc/:
|
|
|
|
ld.so.cache
|
|
|
|
ld.so.conf
|
|
|
|
ld.so.conf.d/`
|
2016-12-03 01:04:07 +00:00
|
|
|
file := filepath.Join(ctx.LogDir, "web.stdout.0")
|
2016-07-10 06:56:31 +00:00
|
|
|
output, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't read file %v", file)
|
|
|
|
}
|
|
|
|
|
|
|
|
act := strings.TrimSpace(string(output))
|
|
|
|
if act != expected {
|
|
|
|
t.Fatalf("Command output incorrectly: want %v; got %v", expected, act)
|
|
|
|
}
|
|
|
|
}
|
2017-01-26 04:58:24 +00:00
|
|
|
|
|
|
|
func TestExecutor_ClientCleanup(t *testing.T) {
|
2017-07-21 19:14:54 +00:00
|
|
|
t.Parallel()
|
2017-01-26 04:58:24 +00:00
|
|
|
testutil.ExecCompatible(t)
|
|
|
|
|
|
|
|
ctx, allocDir := testExecutorContextWithChroot(t)
|
|
|
|
ctx.Task.LogConfig.MaxFiles = 1
|
|
|
|
ctx.Task.LogConfig.MaxFileSizeMB = 300
|
|
|
|
defer allocDir.Destroy()
|
|
|
|
|
|
|
|
executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
|
|
|
|
|
|
|
|
if err := executor.SetContext(ctx); err != nil {
|
|
|
|
t.Fatalf("Unexpected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to run a command which will produce continuous output but not
|
|
|
|
// too quickly to ensure executor.Exit() stops the process.
|
|
|
|
execCmd := ExecCommand{Cmd: "/bin/bash", Args: []string{"-c", "while true; do /bin/echo X; /bin/sleep 1; done"}}
|
|
|
|
execCmd.FSIsolation = true
|
|
|
|
execCmd.ResourceLimits = true
|
|
|
|
execCmd.User = "nobody"
|
|
|
|
|
|
|
|
ps, err := executor.LaunchCmd(&execCmd)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
if err := executor.Exit(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file := filepath.Join(ctx.LogDir, "web.stdout.0")
|
|
|
|
finfo, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error stating stdout file: %v", err)
|
|
|
|
}
|
|
|
|
if finfo.Size() == 0 {
|
|
|
|
t.Fatal("Nothing in stdout; expected at least one byte.")
|
|
|
|
}
|
|
|
|
time.Sleep(2 * 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())
|
|
|
|
}
|
|
|
|
}
|