5dee1141d1
* client/executor: refactor client to remove interpolation * executor: POC libcontainer based executor * vendor: use hashicorp libcontainer fork * vendor: add libcontainer/nsenter dep * executor: updated executor interface to simplify operations * executor: implement logging pipe * logmon: new logmon plugin to manage task logs * driver/executor: use logmon for log management * executor: fix tests and windows build * executor: fix logging key names * executor: fix test failures * executor: add config field to toggle between using libcontainer and standard executors * logmon: use discover utility to discover nomad executable * executor: only call libcontainer-shim on main in linux * logmon: use seperate path configs for stdout/stderr fifos * executor: windows fixes * executor: created reusable pid stats collection utility that can be used in an executor * executor: update fifo.Open calls * executor: fix build * remove executor from docker driver * executor: Shutdown func to kill and cleanup executor and its children * executor: move linux specific universal executor funcs to seperate file * move logmon initialization to a task runner hook * client: doc fixes and renaming from code review * taskrunner: use shared config struct for logmon fifo fields * taskrunner: logmon only needs to be started once per task
138 lines
3 KiB
Go
138 lines
3 KiB
Go
// +build !windows
|
|
|
|
package driver
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/nomad/helper/testtask"
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
"github.com/hashicorp/nomad/testutil"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRawExecDriver_User(t *testing.T) {
|
|
t.Parallel()
|
|
if runtime.GOOS != "linux" {
|
|
t.Skip("Linux only test")
|
|
}
|
|
task := &structs.Task{
|
|
Name: "sleep",
|
|
Driver: "raw_exec",
|
|
User: "alice",
|
|
Config: map[string]interface{}{
|
|
"command": testtask.Path(),
|
|
"args": []string{"sleep", "45s"},
|
|
},
|
|
LogConfig: &structs.LogConfig{
|
|
MaxFiles: 10,
|
|
MaxFileSizeMB: 10,
|
|
},
|
|
Resources: basicResources,
|
|
}
|
|
testtask.SetTaskEnv(task)
|
|
|
|
ctx := testDriverContexts(t, task)
|
|
defer ctx.Destroy()
|
|
d := NewRawExecDriver(ctx.DriverCtx)
|
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
|
t.Fatalf("prestart err: %v", err)
|
|
}
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
|
if err == nil {
|
|
resp.Handle.Kill()
|
|
t.Fatalf("Should've failed")
|
|
}
|
|
msg := "unknown user alice"
|
|
if !strings.Contains(err.Error(), msg) {
|
|
t.Fatalf("Expecting '%v' in '%v'", msg, err)
|
|
}
|
|
}
|
|
|
|
func TestRawExecDriver_Signal(t *testing.T) {
|
|
t.Parallel()
|
|
task := &structs.Task{
|
|
Name: "signal",
|
|
Driver: "raw_exec",
|
|
Config: map[string]interface{}{
|
|
"command": "/bin/bash",
|
|
"args": []string{"test.sh"},
|
|
},
|
|
LogConfig: &structs.LogConfig{
|
|
MaxFiles: 10,
|
|
MaxFileSizeMB: 10,
|
|
},
|
|
Resources: basicResources,
|
|
KillTimeout: 10 * time.Second,
|
|
}
|
|
|
|
ctx := testDriverContexts(t, task)
|
|
defer ctx.Destroy()
|
|
d := NewRawExecDriver(ctx.DriverCtx)
|
|
|
|
testFile := filepath.Join(ctx.ExecCtx.TaskDir.Dir, "test.sh")
|
|
testData := []byte(`
|
|
at_term() {
|
|
echo 'Terminated.'
|
|
exit 3
|
|
}
|
|
trap at_term USR1
|
|
while true; do
|
|
sleep 1
|
|
done
|
|
`)
|
|
if err := ioutil.WriteFile(testFile, testData, 0777); err != nil {
|
|
t.Fatalf("Failed to write data: %v", err)
|
|
}
|
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
|
t.Fatalf("prestart err: %v", err)
|
|
}
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
|
|
go func() {
|
|
time.Sleep(100 * time.Millisecond)
|
|
err := resp.Handle.Signal(syscall.SIGUSR1)
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Task should terminate quickly
|
|
select {
|
|
case res := <-resp.Handle.WaitCh():
|
|
if res.Successful() {
|
|
t.Fatal("should err")
|
|
}
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*6) * time.Second):
|
|
t.Fatalf("timeout")
|
|
}
|
|
|
|
// Check the log file to see it exited because of the signal
|
|
outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "signal.stdout.0")
|
|
exp := "Terminated."
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
act, err := ioutil.ReadFile(outputFile)
|
|
if err != nil {
|
|
return false, fmt.Errorf("Couldn't read expected output: %v", err)
|
|
}
|
|
|
|
if strings.TrimSpace(string(act)) != exp {
|
|
t.Logf("Read from %v", outputFile)
|
|
return false, fmt.Errorf("Command outputted %v; want %v", act, exp)
|
|
}
|
|
return true, nil
|
|
}, func(err error) { require.NoError(t, err) })
|
|
}
|