2018-09-26 19:33:25 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2018-09-30 03:05:14 +00:00
|
|
|
package rawexec
|
2018-09-26 19:33:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-11 14:36:02 +00:00
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2018-09-26 19:33:25 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/helper/testtask"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2019-01-11 16:28:52 +00:00
|
|
|
basePlug "github.com/hashicorp/nomad/plugins/base"
|
2018-09-26 19:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-11-27 19:03:58 +00:00
|
|
|
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
|
2018-09-26 19:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-11 16:28:52 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2018-09-26 19:33:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRawExecDriver_User(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
t.Skip("Linux only test")
|
|
|
|
}
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
d := NewRawExecDriver(testlog.HCLogger(t))
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-09-26 19:33:25 +00:00
|
|
|
|
|
|
|
task := &drivers.TaskConfig{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Name: "sleep",
|
|
|
|
User: "alice",
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := harness.MkAllocDir(task, false)
|
|
|
|
defer cleanup()
|
|
|
|
|
2018-10-11 14:36:02 +00:00
|
|
|
taskConfig := map[string]interface{}{}
|
|
|
|
taskConfig["command"] = testtask.Path()
|
|
|
|
taskConfig["args"] = []string{"sleep", "45s"}
|
2018-09-26 19:33:25 +00:00
|
|
|
|
2018-10-11 14:36:02 +00:00
|
|
|
encodeDriverHelper(require, task, taskConfig)
|
2018-09-26 19:33:25 +00:00
|
|
|
testtask.SetTaskConfigEnv(task)
|
2018-10-11 14:36:02 +00:00
|
|
|
|
|
|
|
_, _, err := harness.StartTask(task)
|
2018-09-26 19:33:25 +00:00
|
|
|
require.Error(err)
|
|
|
|
msg := "unknown user alice"
|
|
|
|
require.Contains(err.Error(), msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRawExecDriver_Signal(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
t.Skip("Linux only test")
|
|
|
|
}
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
d := NewRawExecDriver(testlog.HCLogger(t))
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-09-26 19:33:25 +00:00
|
|
|
|
|
|
|
task := &drivers.TaskConfig{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Name: "signal",
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := harness.MkAllocDir(task, true)
|
|
|
|
defer cleanup()
|
|
|
|
|
2018-10-11 14:36:02 +00:00
|
|
|
taskConfig := map[string]interface{}{}
|
|
|
|
taskConfig["command"] = "/bin/bash"
|
|
|
|
taskConfig["args"] = []string{"test.sh"}
|
|
|
|
|
|
|
|
encodeDriverHelper(require, task, taskConfig)
|
|
|
|
testtask.SetTaskConfigEnv(task)
|
2018-09-26 19:33:25 +00:00
|
|
|
|
|
|
|
testFile := filepath.Join(task.TaskDir().Dir, "test.sh")
|
|
|
|
testData := []byte(`
|
|
|
|
at_term() {
|
|
|
|
echo 'Terminated.'
|
|
|
|
exit 3
|
|
|
|
}
|
|
|
|
trap at_term USR1
|
|
|
|
while true; do
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
`)
|
|
|
|
require.NoError(ioutil.WriteFile(testFile, testData, 0777))
|
|
|
|
|
|
|
|
testtask.SetTaskConfigEnv(task)
|
2018-10-11 14:36:02 +00:00
|
|
|
_, _, err := harness.StartTask(task)
|
2018-09-26 19:33:25 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
require.NoError(harness.SignalTask(task.ID, "SIGUSR1"))
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Task should terminate quickly
|
|
|
|
waitCh, err := harness.WaitTask(context.Background(), task.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
select {
|
|
|
|
case res := <-waitCh:
|
|
|
|
require.False(res.Successful())
|
|
|
|
require.Equal(3, res.ExitCode)
|
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*6) * time.Second):
|
|
|
|
require.Fail("WaitTask timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the log file to see it exited because of the signal
|
|
|
|
outputFile := filepath.Join(task.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(err) })
|
|
|
|
}
|
2019-01-11 16:28:52 +00:00
|
|
|
|
|
|
|
func TestRawExecDriver_StartWaitStop(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
d := NewRawExecDriver(testlog.HCLogger(t))
|
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
|
|
|
defer harness.Kill()
|
|
|
|
|
|
|
|
// Disable cgroups so test works without root
|
|
|
|
config := &Config{NoCgroups: true}
|
|
|
|
var data []byte
|
|
|
|
require.NoError(basePlug.MsgPackEncode(&data, config))
|
|
|
|
bconfig := &basePlug.Config{PluginConfig: data}
|
|
|
|
require.NoError(harness.SetConfig(bconfig))
|
|
|
|
|
|
|
|
task := &drivers.TaskConfig{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Name: "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
taskConfig := map[string]interface{}{}
|
|
|
|
taskConfig["command"] = testtask.Path()
|
|
|
|
taskConfig["args"] = []string{"sleep", "100s"}
|
|
|
|
|
|
|
|
encodeDriverHelper(require, task, taskConfig)
|
|
|
|
|
|
|
|
cleanup := harness.MkAllocDir(task, false)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
handle, _, err := harness.StartTask(task)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
ch, err := harness.WaitTask(context.Background(), handle.Config.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
require.NoError(harness.WaitUntilStarted(task.ID, 1*time.Second))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
harness.StopTask(task.ID, 2*time.Second, "SIGINT")
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case result := <-ch:
|
|
|
|
require.Equal(int(unix.SIGINT), result.Signal)
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
require.Fail("timeout waiting for task to shutdown")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the task is marked as dead, but account
|
|
|
|
// for WaitTask() closing channel before internal state is updated
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
status, err := harness.InspectTask(task.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("inspecting task failed: %v", err)
|
|
|
|
}
|
|
|
|
if status.State != drivers.TaskStateExited {
|
|
|
|
return false, fmt.Errorf("task hasn't exited yet; status: %v", status.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(harness.DestroyTask(task.ID, true))
|
|
|
|
}
|