2016-02-05 00:03:17 +00:00
|
|
|
package executor
|
2016-02-04 23:39:29 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-24 18:37:45 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-02-04 23:39:29 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2016-04-01 20:28:20 +00:00
|
|
|
"syscall"
|
2016-02-04 23:39:29 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
tu "github.com/hashicorp/nomad/testutil"
|
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2016-02-04 23:39:29 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
|
|
|
"github.com/hashicorp/nomad/client/driver/env"
|
2016-12-03 01:04:07 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2016-02-04 23:39:29 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2018-09-24 18:37:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-04 23:39:29 +00:00
|
|
|
)
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
var executorFactories = map[string]func(hclog.Logger) Executor{}
|
|
|
|
var universalFactory = func(l hclog.Logger) Executor {
|
|
|
|
return NewExecutor(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
executorFactories["UniversalExecutor"] = universalFactory
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// testExecutorContext returns an ExecutorContext and AllocDir.
|
|
|
|
//
|
|
|
|
// The caller is responsible for calling AllocDir.Destroy() to cleanup.
|
2018-09-24 18:37:45 +00:00
|
|
|
func testExecutorCommand(t *testing.T) (*ExecCommand, *allocdir.AllocDir) {
|
2016-02-04 23:39:29 +00:00
|
|
|
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-02-04 23:39:29 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
allocDir := allocdir.NewAllocDir(testlog.HCLogger(t), filepath.Join(os.TempDir(), alloc.ID))
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := allocDir.Build(); err != nil {
|
2018-06-13 22:33:25 +00:00
|
|
|
t.Fatalf("AllocDir.Build() failed: %v", err)
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
2017-03-02 23:54:12 +00:00
|
|
|
if err := allocDir.NewTaskDir(task.Name).Build(false, nil, cstructs.FSIsolationNone); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
allocDir.Destroy()
|
2018-06-13 22:33:25 +00:00
|
|
|
t.Fatalf("allocDir.NewTaskDir(%q) failed: %v", task.Name, err)
|
2016-12-03 01:04:07 +00:00
|
|
|
}
|
|
|
|
td := allocDir.TaskDirs[task.Name]
|
2018-09-24 18:37:45 +00:00
|
|
|
cmd := &ExecCommand{
|
|
|
|
Env: taskEnv.List(),
|
2016-12-03 01:04:07 +00:00
|
|
|
TaskDir: td.Dir,
|
2018-09-24 18:37:45 +00:00
|
|
|
Resources: &Resources{
|
|
|
|
CPU: task.Resources.CPU,
|
|
|
|
MemoryMB: task.Resources.MemoryMB,
|
|
|
|
IOPS: task.Resources.IOPS,
|
|
|
|
DiskMB: task.Resources.DiskMB,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
configureTLogging(cmd)
|
|
|
|
return cmd, allocDir
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
type bufferCloser struct {
|
|
|
|
bytes.Buffer
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func (_ *bufferCloser) Close() error { return nil }
|
2016-10-12 18:35:29 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func configureTLogging(cmd *ExecCommand) (stdout bufferCloser, stderr bufferCloser) {
|
|
|
|
cmd.stdout = &stdout
|
|
|
|
cmd.stderr = &stderr
|
|
|
|
return
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func TestExecutor_Start_Invalid(pt *testing.T) {
|
|
|
|
pt.Parallel()
|
|
|
|
invalid := "/bin/foobar"
|
|
|
|
for name, factory := range executorFactories {
|
|
|
|
pt.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
execCmd, allocDir := testExecutorCommand(t)
|
|
|
|
execCmd.Cmd = invalid
|
|
|
|
execCmd.Args = []string{"1"}
|
|
|
|
defer allocDir.Destroy()
|
|
|
|
executor := factory(testlog.HCLogger(t))
|
|
|
|
defer executor.Shutdown("", 0)
|
|
|
|
|
|
|
|
_, err := executor.Launch(execCmd)
|
|
|
|
require.Error(err)
|
|
|
|
})
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func TestExecutor_Start_Wait_Failure_Code(pt *testing.T) {
|
|
|
|
pt.Parallel()
|
|
|
|
for name, factory := range executorFactories {
|
|
|
|
pt.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
execCmd, allocDir := testExecutorCommand(t)
|
|
|
|
execCmd.Cmd = "/bin/date"
|
|
|
|
execCmd.Args = []string{"fail"}
|
|
|
|
defer allocDir.Destroy()
|
|
|
|
executor := factory(testlog.HCLogger(t))
|
|
|
|
defer executor.Shutdown("", 0)
|
|
|
|
|
|
|
|
ps, err := executor.Launch(execCmd)
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotZero(ps.Pid)
|
|
|
|
ps, _ = executor.Wait()
|
|
|
|
require.NotZero(ps.ExitCode, "expected exit code to be non zero")
|
|
|
|
require.NoError(executor.Shutdown("SIGINT", 100*time.Millisecond))
|
|
|
|
})
|
2016-04-01 20:28:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func TestExecutor_Start_Wait(pt *testing.T) {
|
|
|
|
pt.Parallel()
|
|
|
|
for name, factory := range executorFactories {
|
|
|
|
pt.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
execCmd, allocDir := testExecutorCommand(t)
|
|
|
|
execCmd.Cmd = "/bin/echo"
|
|
|
|
execCmd.Args = []string{"hello world"}
|
|
|
|
defer allocDir.Destroy()
|
|
|
|
executor := factory(testlog.HCLogger(t))
|
|
|
|
defer executor.Shutdown("", 0)
|
|
|
|
|
|
|
|
ps, err := executor.Launch(execCmd)
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotZero(ps.Pid)
|
|
|
|
|
|
|
|
ps, err = executor.Wait()
|
|
|
|
require.NoError(err)
|
|
|
|
require.NoError(executor.Shutdown("SIGINT", 100*time.Millisecond))
|
|
|
|
|
|
|
|
expected := "hello world"
|
|
|
|
tu.WaitForResult(func() (bool, error) {
|
|
|
|
output := execCmd.stdout.(*bufferCloser).String()
|
|
|
|
act := strings.TrimSpace(string(output))
|
|
|
|
if expected != act {
|
|
|
|
return false, fmt.Errorf("expected: '%s' actual: '%s'", expected, act)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
})
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|
2016-02-04 23:39:29 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func TestExecutor_WaitExitSignal(pt *testing.T) {
|
|
|
|
pt.Parallel()
|
|
|
|
for name, factory := range executorFactories {
|
|
|
|
pt.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
execCmd, allocDir := testExecutorCommand(t)
|
|
|
|
execCmd.Cmd = "/bin/sleep"
|
|
|
|
execCmd.Args = []string{"10000"}
|
|
|
|
defer allocDir.Destroy()
|
|
|
|
executor := factory(testlog.HCLogger(t))
|
|
|
|
defer executor.Shutdown("", 0)
|
|
|
|
|
|
|
|
ps, err := executor.Launch(execCmd)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
_, err := executor.Stats()
|
|
|
|
require.NoError(err)
|
|
|
|
//require.NotEmpty(ru.Pids)
|
|
|
|
proc, err := os.FindProcess(ps.Pid)
|
|
|
|
require.NoError(err)
|
|
|
|
err = proc.Signal(syscall.SIGKILL)
|
|
|
|
require.NoError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ps, err = executor.Wait()
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(ps.Signal, int(syscall.SIGKILL))
|
|
|
|
})
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|
2016-02-04 23:39:29 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func TestExecutor_Start_Kill(pt *testing.T) {
|
|
|
|
pt.Parallel()
|
|
|
|
for name, factory := range executorFactories {
|
|
|
|
pt.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
execCmd, allocDir := testExecutorCommand(t)
|
|
|
|
execCmd.Cmd = "/bin/sleep"
|
|
|
|
execCmd.Args = []string{"10 && hello world"}
|
|
|
|
defer allocDir.Destroy()
|
|
|
|
executor := factory(testlog.HCLogger(t))
|
|
|
|
defer executor.Shutdown("", 0)
|
|
|
|
|
|
|
|
ps, err := executor.Launch(execCmd)
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotZero(ps.Pid)
|
|
|
|
|
|
|
|
require.NoError(executor.Shutdown("SIGINT", 100*time.Millisecond))
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(tu.TestMultiplier()*2) * time.Second)
|
|
|
|
output := execCmd.stdout.(*bufferCloser).String()
|
|
|
|
expected := ""
|
|
|
|
act := strings.TrimSpace(string(output))
|
|
|
|
if act != expected {
|
|
|
|
t.Fatalf("Command output incorrectly: want %v; got %v", expected, act)
|
|
|
|
}
|
|
|
|
})
|
2016-02-04 23:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-19 19:18:10 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func TestUniversalExecutor_MakeExecutable(t *testing.T) {
|
2017-07-21 19:14:54 +00:00
|
|
|
t.Parallel()
|
2016-03-19 19:18:10 +00:00
|
|
|
// Create a temp file
|
|
|
|
f, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
|
|
|
|
// Set its permissions to be non-executable
|
|
|
|
f.Chmod(os.FileMode(0610))
|
|
|
|
|
2018-03-11 18:05:31 +00:00
|
|
|
// Make a fake executor
|
2018-09-24 18:37:45 +00:00
|
|
|
executor := NewExecutor(testlog.HCLogger(t)).(*UniversalExecutor)
|
2016-03-19 19:18:10 +00:00
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
err = executor.makeExecutable(f.Name())
|
2016-03-19 19:18:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("makeExecutable() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the permissions
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Stat() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
act := stat.Mode().Perm()
|
|
|
|
exp := os.FileMode(0755)
|
|
|
|
if act != exp {
|
2017-02-28 00:00:19 +00:00
|
|
|
t.Fatalf("expected permissions %v; got %v", exp, act)
|
2016-03-19 19:18:10 +00:00
|
|
|
}
|
|
|
|
}
|