2018-09-30 03:05:14 +00:00
|
|
|
package rawexec
|
2018-09-19 19:56:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-26 05:18:03 +00:00
|
|
|
"fmt"
|
2018-09-25 18:12:58 +00:00
|
|
|
"io/ioutil"
|
2018-09-26 05:18:03 +00:00
|
|
|
"os"
|
2018-09-25 18:12:58 +00:00
|
|
|
"path/filepath"
|
2019-01-11 16:28:52 +00:00
|
|
|
"runtime"
|
2018-09-26 05:18:03 +00:00
|
|
|
"strconv"
|
2018-09-25 18:12:58 +00:00
|
|
|
"sync"
|
2018-09-26 05:18:03 +00:00
|
|
|
"syscall"
|
2018-09-19 19:56:50 +00:00
|
|
|
"testing"
|
2018-09-25 18:12:58 +00:00
|
|
|
"time"
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
ctestutil "github.com/hashicorp/nomad/client/testutil"
|
2019-02-12 19:46:37 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/hclutils"
|
2018-09-19 19:56:50 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-09-26 05:18:03 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testtask"
|
2018-09-19 19:56:50 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2018-09-26 05:18:03 +00:00
|
|
|
basePlug "github.com/hashicorp/nomad/plugins/base"
|
2018-09-26 14:42:02 +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-11-21 00:30:39 +00:00
|
|
|
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
2018-09-26 05:18:03 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2018-09-19 19:56:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if !testtask.Run() {
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 00:13:29 +00:00
|
|
|
func newEnabledRawExecDriver(t *testing.T) *Driver {
|
|
|
|
d := NewRawExecDriver(testlog.HCLogger(t)).(*Driver)
|
|
|
|
d.config.Enabled = true
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
func TestRawExecDriver_SetConfig(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
d := NewRawExecDriver(testlog.HCLogger(t))
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 00:16:22 +00:00
|
|
|
defer harness.Kill()
|
2018-09-26 05:18:03 +00:00
|
|
|
|
2018-12-18 00:40:58 +00:00
|
|
|
bconfig := &basePlug.Config{}
|
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
// Disable raw exec.
|
|
|
|
config := &Config{}
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
require.NoError(basePlug.MsgPackEncode(&data, config))
|
2018-12-18 00:40:58 +00:00
|
|
|
bconfig.PluginConfig = data
|
|
|
|
require.NoError(harness.SetConfig(bconfig))
|
2018-10-31 00:16:22 +00:00
|
|
|
require.Exactly(config, d.(*Driver).config)
|
2018-09-26 05:18:03 +00:00
|
|
|
|
|
|
|
config.Enabled = true
|
|
|
|
config.NoCgroups = true
|
|
|
|
data = []byte{}
|
|
|
|
require.NoError(basePlug.MsgPackEncode(&data, config))
|
2018-12-18 00:40:58 +00:00
|
|
|
bconfig.PluginConfig = data
|
|
|
|
require.NoError(harness.SetConfig(bconfig))
|
2018-10-31 00:16:22 +00:00
|
|
|
require.Exactly(config, d.(*Driver).config)
|
2018-09-26 05:18:03 +00:00
|
|
|
|
|
|
|
config.NoCgroups = false
|
|
|
|
data = []byte{}
|
|
|
|
require.NoError(basePlug.MsgPackEncode(&data, config))
|
2018-12-18 00:40:58 +00:00
|
|
|
bconfig.PluginConfig = data
|
|
|
|
require.NoError(harness.SetConfig(bconfig))
|
2018-10-31 00:16:22 +00:00
|
|
|
require.Exactly(config, d.(*Driver).config)
|
2018-09-26 05:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRawExecDriver_Fingerprint(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-10-31 00:16:22 +00:00
|
|
|
fingerprintTest := func(config *Config, expected *drivers.Fingerprint) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
2019-08-30 00:13:29 +00:00
|
|
|
d := newEnabledRawExecDriver(t)
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 00:16:22 +00:00
|
|
|
defer harness.Kill()
|
2018-09-26 05:18:03 +00:00
|
|
|
|
2018-10-31 00:16:22 +00:00
|
|
|
var data []byte
|
|
|
|
require.NoError(basePlug.MsgPackEncode(&data, config))
|
2018-12-18 00:40:58 +00:00
|
|
|
bconfig := &basePlug.Config{
|
|
|
|
PluginConfig: data,
|
|
|
|
}
|
|
|
|
require.NoError(harness.SetConfig(bconfig))
|
2018-09-26 05:18:03 +00:00
|
|
|
|
2018-10-31 00:16:22 +00:00
|
|
|
fingerCh, err := harness.Fingerprint(context.Background())
|
|
|
|
require.NoError(err)
|
|
|
|
select {
|
|
|
|
case result := <-fingerCh:
|
|
|
|
require.Equal(expected, result)
|
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()) * time.Second):
|
|
|
|
require.Fail("timeout receiving fingerprint")
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 05:18:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 00:16:22 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Conf Config
|
|
|
|
Expected drivers.Fingerprint
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "Disabled",
|
|
|
|
Conf: Config{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
|
|
|
Expected: drivers.Fingerprint{
|
|
|
|
Attributes: nil,
|
|
|
|
Health: drivers.HealthStateUndetected,
|
|
|
|
HealthDescription: "disabled",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Enabled",
|
|
|
|
Conf: Config{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
Expected: drivers.Fingerprint{
|
2018-11-21 00:30:39 +00:00
|
|
|
Attributes: map[string]*pstructs.Attribute{"driver.raw_exec": pstructs.NewBoolAttribute(true)},
|
2018-10-31 00:16:22 +00:00
|
|
|
Health: drivers.HealthStateHealthy,
|
2019-01-07 04:04:15 +00:00
|
|
|
HealthDescription: drivers.DriverHealthy,
|
2018-10-31 00:16:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-09-26 05:18:03 +00:00
|
|
|
|
2018-10-31 00:16:22 +00:00
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, fingerprintTest(&tc.Conf, &tc.Expected))
|
2018-09-26 05:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 18:12:58 +00:00
|
|
|
func TestRawExecDriver_StartWait(t *testing.T) {
|
2018-09-19 19:56:50 +00:00
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2019-08-30 00:13:29 +00:00
|
|
|
d := newEnabledRawExecDriver(t)
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 00:16:22 +00:00
|
|
|
defer harness.Kill()
|
2018-09-26 14:42:02 +00:00
|
|
|
task := &drivers.TaskConfig{
|
2018-09-19 19:56:50 +00:00
|
|
|
ID: uuid.Generate(),
|
|
|
|
Name: "test",
|
|
|
|
}
|
2018-10-11 14:36:02 +00:00
|
|
|
|
2019-01-15 18:54:37 +00:00
|
|
|
tc := &TaskConfig{
|
|
|
|
Command: testtask.Path(),
|
|
|
|
Args: []string{"sleep", "10ms"},
|
|
|
|
}
|
|
|
|
require.NoError(task.EncodeConcreteDriverConfig(&tc))
|
|
|
|
testtask.SetTaskConfigEnv(task)
|
2018-10-11 14:36:02 +00:00
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
cleanup := harness.MkAllocDir(task, false)
|
2018-09-19 19:56:50 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2018-10-11 14:36:02 +00:00
|
|
|
handle, _, err := harness.StartTask(task)
|
2018-09-19 19:56:50 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
ch, err := harness.WaitTask(context.Background(), handle.Config.ID)
|
|
|
|
require.NoError(err)
|
2019-01-15 18:54:37 +00:00
|
|
|
|
|
|
|
var result *drivers.ExitResult
|
|
|
|
select {
|
|
|
|
case result = <-ch:
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:56:50 +00:00
|
|
|
require.Zero(result.ExitCode)
|
2018-10-31 00:16:22 +00:00
|
|
|
require.Zero(result.Signal)
|
|
|
|
require.False(result.OOMKilled)
|
|
|
|
require.NoError(result.Err)
|
2018-09-25 18:12:58 +00:00
|
|
|
require.NoError(harness.DestroyTask(task.ID, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRawExecDriver_StartWaitRecoverWaitStop(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2019-08-30 00:13:29 +00:00
|
|
|
d := newEnabledRawExecDriver(t)
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 00:16:22 +00:00
|
|
|
defer harness.Kill()
|
|
|
|
|
|
|
|
// Disable cgroups so test works without root
|
2019-08-30 00:13:29 +00:00
|
|
|
config := &Config{NoCgroups: true, Enabled: true}
|
2018-10-31 00:16:22 +00:00
|
|
|
var data []byte
|
|
|
|
require.NoError(basePlug.MsgPackEncode(&data, config))
|
2018-12-18 00:40:58 +00:00
|
|
|
bconfig := &basePlug.Config{PluginConfig: data}
|
|
|
|
require.NoError(harness.SetConfig(bconfig))
|
2018-10-31 00:16:22 +00:00
|
|
|
|
2018-09-26 14:42:02 +00:00
|
|
|
task := &drivers.TaskConfig{
|
2018-09-25 18:12:58 +00:00
|
|
|
ID: uuid.Generate(),
|
2018-09-26 05:18:03 +00:00
|
|
|
Name: "sleep",
|
2018-09-25 18:12:58 +00:00
|
|
|
}
|
2019-01-15 18:54:37 +00:00
|
|
|
tc := &TaskConfig{
|
|
|
|
Command: testtask.Path(),
|
|
|
|
Args: []string{"sleep", "100s"},
|
|
|
|
}
|
|
|
|
require.NoError(task.EncodeConcreteDriverConfig(&tc))
|
2018-10-11 14:36:02 +00:00
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
testtask.SetTaskConfigEnv(task)
|
|
|
|
cleanup := harness.MkAllocDir(task, false)
|
2018-09-25 18:12:58 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2018-10-11 14:36:02 +00:00
|
|
|
handle, _, err := harness.StartTask(task)
|
2018-09-25 18:12:58 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
ch, err := harness.WaitTask(context.Background(), task.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
var waitDone bool
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
result := <-ch
|
|
|
|
require.Error(result.Err)
|
|
|
|
waitDone = true
|
|
|
|
}()
|
|
|
|
|
|
|
|
originalStatus, err := d.InspectTask(task.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
|
2019-08-30 00:13:29 +00:00
|
|
|
d.tasks.Delete(task.ID)
|
2018-09-25 18:12:58 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
require.True(waitDone)
|
|
|
|
_, err = d.InspectTask(task.ID)
|
2018-09-26 14:42:02 +00:00
|
|
|
require.Equal(drivers.ErrTaskNotFound, err)
|
2018-09-25 18:12:58 +00:00
|
|
|
|
|
|
|
err = d.RecoverTask(handle)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
status, err := d.InspectTask(task.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Exactly(originalStatus, status)
|
|
|
|
|
|
|
|
ch, err = harness.WaitTask(context.Background(), task.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
waitDone = false
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
result := <-ch
|
|
|
|
require.NoError(result.Err)
|
2018-09-26 05:18:03 +00:00
|
|
|
require.NotZero(result.ExitCode)
|
2018-09-25 18:12:58 +00:00
|
|
|
require.Equal(9, result.Signal)
|
|
|
|
waitDone = true
|
|
|
|
}()
|
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
require.NoError(d.StopTask(task.ID, 0, "SIGKILL"))
|
2018-09-25 18:12:58 +00:00
|
|
|
wg.Wait()
|
2018-09-26 05:18:03 +00:00
|
|
|
require.NoError(d.DestroyTask(task.ID, false))
|
2018-09-25 18:12:58 +00:00
|
|
|
require.True(waitDone)
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
2018-09-26 05:18:03 +00:00
|
|
|
|
|
|
|
func TestRawExecDriver_Start_Wait_AllocDir(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2019-08-30 00:13:29 +00:00
|
|
|
d := newEnabledRawExecDriver(t)
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 00:16:22 +00:00
|
|
|
defer harness.Kill()
|
2018-09-26 05:18:03 +00:00
|
|
|
|
2018-09-26 14:42:02 +00:00
|
|
|
task := &drivers.TaskConfig{
|
2018-09-26 05:18:03 +00:00
|
|
|
ID: uuid.Generate(),
|
|
|
|
Name: "sleep",
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := harness.MkAllocDir(task, false)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
exp := []byte("win")
|
|
|
|
file := "output.txt"
|
|
|
|
outPath := fmt.Sprintf(`%s/%s`, task.TaskDir().SharedAllocDir, file)
|
|
|
|
|
2019-01-15 18:54:37 +00:00
|
|
|
tc := &TaskConfig{
|
|
|
|
Command: testtask.Path(),
|
|
|
|
Args: []string{"sleep", "1s", "write", string(exp), outPath},
|
|
|
|
}
|
|
|
|
require.NoError(task.EncodeConcreteDriverConfig(&tc))
|
2018-09-26 05:18:03 +00:00
|
|
|
testtask.SetTaskConfigEnv(task)
|
|
|
|
|
2018-10-11 14:36:02 +00:00
|
|
|
_, _, err := harness.StartTask(task)
|
2018-09-26 05:18:03 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Task should terminate quickly
|
|
|
|
waitCh, err := harness.WaitTask(context.Background(), task.ID)
|
2018-10-05 02:36:40 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
select {
|
|
|
|
case res := <-waitCh:
|
|
|
|
require.NoError(res.Err)
|
|
|
|
require.True(res.Successful())
|
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*5) * time.Second):
|
|
|
|
require.Fail("WaitTask timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that data was written to the shared alloc directory.
|
|
|
|
outputFile := filepath.Join(task.TaskDir().SharedAllocDir, file)
|
|
|
|
act, err := ioutil.ReadFile(outputFile)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Exactly(exp, act)
|
|
|
|
require.NoError(harness.DestroyTask(task.ID, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test creates a process tree such that without cgroups tracking the
|
|
|
|
// processes cleanup of the children would not be possible. Thus the test
|
|
|
|
// asserts that the processes get killed properly when using cgroups.
|
|
|
|
func TestRawExecDriver_Start_Kill_Wait_Cgroup(t *testing.T) {
|
|
|
|
ctestutil.ExecCompatible(t)
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
pidFile := "pid"
|
|
|
|
|
2019-08-30 00:13:29 +00:00
|
|
|
d := newEnabledRawExecDriver(t)
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 00:16:22 +00:00
|
|
|
defer harness.Kill()
|
2018-09-26 05:18:03 +00:00
|
|
|
|
2018-09-26 14:42:02 +00:00
|
|
|
task := &drivers.TaskConfig{
|
2018-09-26 05:18:03 +00:00
|
|
|
ID: uuid.Generate(),
|
|
|
|
Name: "sleep",
|
|
|
|
User: "root",
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := harness.MkAllocDir(task, false)
|
|
|
|
defer cleanup()
|
|
|
|
|
2019-01-15 18:54:37 +00:00
|
|
|
tc := &TaskConfig{
|
|
|
|
Command: testtask.Path(),
|
|
|
|
Args: []string{"fork/exec", pidFile, "pgrp", "0", "sleep", "20s"},
|
|
|
|
}
|
|
|
|
require.NoError(task.EncodeConcreteDriverConfig(&tc))
|
2018-09-26 05:18:03 +00:00
|
|
|
testtask.SetTaskConfigEnv(task)
|
|
|
|
|
2018-10-11 14:36:02 +00:00
|
|
|
_, _, err := harness.StartTask(task)
|
2018-09-26 05:18:03 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Find the process
|
|
|
|
var pidData []byte
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
var err error
|
|
|
|
pidData, err = ioutil.ReadFile(filepath.Join(task.TaskDir().Dir, pidFile))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pidData) == 0 {
|
|
|
|
return false, fmt.Errorf("pidFile empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(string(pidData))
|
|
|
|
require.NoError(err, "failed to read pidData: %s", string(pidData))
|
|
|
|
|
|
|
|
// Check the pid is up
|
|
|
|
process, err := os.FindProcess(pid)
|
|
|
|
require.NoError(err)
|
|
|
|
require.NoError(process.Signal(syscall.Signal(0)))
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
err := harness.StopTask(task.ID, 0, "")
|
|
|
|
|
2019-02-20 12:52:27 +00:00
|
|
|
// Can't rely on the ordering between wait and kill on CI/travis...
|
|
|
|
if !testutil.IsCI() {
|
2018-09-26 05:18:03 +00:00
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Task should terminate quickly
|
|
|
|
waitCh, err := harness.WaitTask(context.Background(), task.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
select {
|
|
|
|
case res := <-waitCh:
|
|
|
|
require.False(res.Successful())
|
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*5) * time.Second):
|
|
|
|
require.Fail("WaitTask timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if err := process.Signal(syscall.Signal(0)); err == nil {
|
|
|
|
return false, fmt.Errorf("process should not exist: %v", pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
require.NoError(harness.DestroyTask(task.ID, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRawExecDriver_Exec(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2019-08-30 00:13:29 +00:00
|
|
|
d := newEnabledRawExecDriver(t)
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 00:16:22 +00:00
|
|
|
defer harness.Kill()
|
2018-09-26 05:18:03 +00:00
|
|
|
|
2018-09-26 14:42:02 +00:00
|
|
|
task := &drivers.TaskConfig{
|
2018-09-26 05:18:03 +00:00
|
|
|
ID: uuid.Generate(),
|
|
|
|
Name: "sleep",
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := harness.MkAllocDir(task, false)
|
|
|
|
defer cleanup()
|
|
|
|
|
2019-01-15 18:54:37 +00:00
|
|
|
tc := &TaskConfig{
|
|
|
|
Command: testtask.Path(),
|
|
|
|
Args: []string{"sleep", "9000s"},
|
|
|
|
}
|
|
|
|
require.NoError(task.EncodeConcreteDriverConfig(&tc))
|
2018-09-26 05:18:03 +00:00
|
|
|
testtask.SetTaskConfigEnv(task)
|
|
|
|
|
2018-10-11 14:36:02 +00:00
|
|
|
_, _, err := harness.StartTask(task)
|
2018-09-26 05:18:03 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
2019-01-11 16:28:52 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Exec a command that should work
|
2019-01-11 16:49:53 +00:00
|
|
|
res, err := harness.ExecTask(task.ID, []string{"cmd.exe", "/c", "echo", "hello"}, 1*time.Second)
|
2019-01-11 16:28:52 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.True(res.ExitResult.Successful())
|
2019-01-11 16:49:53 +00:00
|
|
|
require.Equal(string(res.Stdout), "hello\r\n")
|
2018-09-26 05:18:03 +00:00
|
|
|
|
2019-01-11 16:28:52 +00:00
|
|
|
// Exec a command that should fail
|
2019-01-11 16:49:53 +00:00
|
|
|
res, err = harness.ExecTask(task.ID, []string{"cmd.exe", "/c", "stat", "notarealfile123abc"}, 1*time.Second)
|
2019-01-11 16:28:52 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.False(res.ExitResult.Successful())
|
|
|
|
require.Contains(string(res.Stdout), "not recognized")
|
|
|
|
} else {
|
|
|
|
// Exec a command that should work
|
|
|
|
res, err := harness.ExecTask(task.ID, []string{"/usr/bin/stat", "/tmp"}, 1*time.Second)
|
|
|
|
require.NoError(err)
|
|
|
|
require.True(res.ExitResult.Successful())
|
|
|
|
require.True(len(res.Stdout) > 100)
|
|
|
|
|
|
|
|
// Exec a command that should fail
|
|
|
|
res, err = harness.ExecTask(task.ID, []string{"/usr/bin/stat", "notarealfile123abc"}, 1*time.Second)
|
|
|
|
require.NoError(err)
|
|
|
|
require.False(res.ExitResult.Successful())
|
|
|
|
require.Contains(string(res.Stdout), "No such file or directory")
|
|
|
|
}
|
2018-09-26 05:18:03 +00:00
|
|
|
|
|
|
|
require.NoError(harness.DestroyTask(task.ID, true))
|
|
|
|
}
|
2019-02-12 19:46:37 +00:00
|
|
|
|
|
|
|
func TestConfig_ParseAllHCL(t *testing.T) {
|
|
|
|
cfgStr := `
|
|
|
|
config {
|
|
|
|
command = "/bin/bash"
|
|
|
|
args = ["-c", "echo hello"]
|
|
|
|
}`
|
|
|
|
|
|
|
|
expected := &TaskConfig{
|
|
|
|
Command: "/bin/bash",
|
|
|
|
Args: []string{"-c", "echo hello"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var tc *TaskConfig
|
|
|
|
hclutils.NewConfigParser(taskConfigSpec).ParseHCL(t, cfgStr, &tc)
|
|
|
|
|
|
|
|
require.EqualValues(t, expected, tc)
|
|
|
|
}
|
2019-08-29 13:09:40 +00:00
|
|
|
|
|
|
|
func TestRawExecDriver_Disabled(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2019-08-30 00:13:29 +00:00
|
|
|
d := newEnabledRawExecDriver(t)
|
|
|
|
d.config.Enabled = false
|
2019-08-29 13:09:40 +00:00
|
|
|
|
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
|
|
|
defer harness.Kill()
|
|
|
|
task := &drivers.TaskConfig{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Name: "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
handle, _, err := harness.StartTask(task)
|
|
|
|
require.Error(err)
|
|
|
|
require.Contains(err.Error(), errDisabledDriver.Error())
|
|
|
|
require.Nil(handle)
|
|
|
|
}
|