Driver tests do not use hcl2/hcl, hclspec, or hclutils

This commit is contained in:
Alex Dadgar
2019-01-15 10:54:37 -08:00
committed by Michael Schurter
parent 2d23f4a038
commit e46d67a889
16 changed files with 208 additions and 285 deletions

View File

@@ -12,7 +12,7 @@ import (
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
"github.com/hashicorp/nomad/client/logmon"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/plugins/shared"
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
)
// logmonHook launches logmon and manages task logging
@@ -76,13 +76,13 @@ func reattachConfigFromHookData(data map[string]string) (*plugin.ReattachConfig,
return nil, nil
}
var cfg *shared.ReattachConfig
var cfg *pstructs.ReattachConfig
err := json.Unmarshal([]byte(data["reattach_config"]), cfg)
if err != nil {
return nil, err
}
return shared.ReattachConfigToGoPlugin(cfg)
return pstructs.ReattachConfigToGoPlugin(cfg)
}
func (h *logmonHook) Prestart(ctx context.Context,
@@ -117,7 +117,7 @@ func (h *logmonHook) Prestart(ctx context.Context,
}
}
rCfg := shared.ReattachConfigFromGoPlugin(h.logmonPluginClient.ReattachConfig())
rCfg := pstructs.ReattachConfigFromGoPlugin(h.logmonPluginClient.ReattachConfig())
jsonCfg, err := json.Marshal(rCfg)
if err != nil {
return err

View File

@@ -7,7 +7,7 @@ import (
"github.com/hashicorp/nomad/client/allocrunner/taskrunner/state"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/hashicorp/nomad/plugins/shared"
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
)
// allocRunnerMutableState08 is state that had to be written on each save as it
@@ -52,8 +52,8 @@ type taskRunnerHandle08 struct {
} `json:"PluginConfig"`
}
func (t *taskRunnerHandle08) reattachConfig() *shared.ReattachConfig {
return &shared.ReattachConfig{
func (t *taskRunnerHandle08) reattachConfig() *pstructs.ReattachConfig {
return &pstructs.ReattachConfig{
Network: t.PluginConfig.AddrNet,
Addr: t.PluginConfig.AddrName,
Pid: t.PluginConfig.Pid,

View File

@@ -260,13 +260,13 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
// pre 0.9 upgrade path check
if handle.Version == 0 {
var reattach shared.ReattachConfig
var reattach pstructs.ReattachConfig
d.logger.Debug("parsing pre09 driver state", "state", string(handle.DriverState))
if err := json.Unmarshal(handle.DriverState, &reattach); err != nil {
return err
}
reattachConfig, err := shared.ReattachConfigToGoPlugin(&reattach)
reattachConfig, err := pstructs.ReattachConfigToGoPlugin(&reattach)
if err != nil {
return err
}

View File

@@ -13,7 +13,6 @@ import (
"testing"
"time"
"github.com/hashicorp/hcl2/hcl"
ctestutils "github.com/hashicorp/nomad/client/testutil"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/helper/testtask"
@@ -21,8 +20,6 @@ import (
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/hashicorp/nomad/plugins/shared/hclutils"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
)
@@ -103,11 +100,11 @@ func TestExecDriver_StartWait(t *testing.T) {
Resources: testResources,
}
taskConfig := map[string]interface{}{
"command": "cat",
"args": []string{"/proc/self/cgroup"},
tc := &TaskConfig{
Command: "cat",
Args: []string{"/proc/self/cgroup"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
@@ -135,11 +132,11 @@ func TestExecDriver_StartWaitStopKill(t *testing.T) {
Resources: testResources,
}
taskConfig := map[string]interface{}{
"command": "/bin/bash",
"args": []string{"-c", "echo hi; sleep 600"},
tc := &TaskConfig{
Command: "/bin/bash",
Args: []string{"-c", "echo hi; sleep 600"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
@@ -196,11 +193,11 @@ func TestExecDriver_StartWaitRecover(t *testing.T) {
Resources: testResources,
}
taskConfig := map[string]interface{}{
"command": "/bin/sleep",
"args": []string{"5"},
tc := &TaskConfig{
Command: "/bin/sleep",
Args: []string{"5"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
@@ -266,11 +263,11 @@ func TestExecDriver_Stats(t *testing.T) {
Resources: testResources,
}
taskConfig := map[string]interface{}{
"command": "/bin/sleep",
"args": []string{"5"},
tc := &TaskConfig{
Command: "/bin/sleep",
Args: []string{"5"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
@@ -311,14 +308,14 @@ func TestExecDriver_Start_Wait_AllocDir(t *testing.T) {
exp := []byte{'w', 'i', 'n'}
file := "output.txt"
taskConfig := map[string]interface{}{
"command": "/bin/bash",
"args": []string{
tc := &TaskConfig{
Command: "/bin/bash",
Args: []string{
"-c",
fmt.Sprintf(`sleep 1; echo -n %s > /alloc/%s`, string(exp), file),
},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
handle, _, err := harness.StartTask(task)
require.NoError(err)
@@ -359,11 +356,11 @@ func TestExecDriver_User(t *testing.T) {
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
taskConfig := map[string]interface{}{
"command": "/bin/sleep",
"args": []string{"100"},
tc := &TaskConfig{
Command: "/bin/sleep",
Args: []string{"100"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
handle, _, err := harness.StartTask(task)
require.Error(err)
@@ -392,11 +389,11 @@ func TestExecDriver_HandlerExec(t *testing.T) {
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
taskConfig := map[string]interface{}{
"command": "/bin/sleep",
"args": []string{"9000"},
tc := &TaskConfig{
Command: "/bin/sleep",
Args: []string{"9000"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
handle, _, err := harness.StartTask(task)
require.NoError(err)
@@ -502,9 +499,9 @@ func TestExecDriver_DevicesAndMounts(t *testing.T) {
require.NoError(ioutil.WriteFile(task.StdoutPath, []byte{}, 660))
require.NoError(ioutil.WriteFile(task.StderrPath, []byte{}, 660))
taskConfig := map[string]interface{}{
"command": "/bin/bash",
"args": []string{"-c", `
tc := &TaskConfig{
Command: "/bin/bash",
Args: []string{"-c", `
export LANG=en.UTF-8
echo "mounted device /inserted-random: $(stat -c '%t:%T' /dev/inserted-random)"
echo "reading from ro path: $(cat /tmp/task-path-ro/testfile)"
@@ -516,7 +513,7 @@ touch /tmp/task-path-ro/testfile-from-ro && echo from-exec > /tmp/task-path-ro/
exit 0
`},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
@@ -549,15 +546,3 @@ touch: cannot touch '/tmp/task-path-ro/testfile-from-ro': Read-only file system`
require.NoError(err)
require.Equal("from-exec", strings.TrimSpace(string(fromRWContent)))
}
func encodeDriverHelper(require *require.Assertions, task *drivers.TaskConfig, taskConfig map[string]interface{}) {
evalCtx := &hcl.EvalContext{
Functions: hclutils.GetStdlibFuncs(),
}
spec, diag := hclspec.Convert(taskConfigSpec)
require.False(diag.HasErrors())
taskConfigCtyVal, diag := hclutils.ParseHclInterface(taskConfig, spec, evalCtx)
require.False(diag.HasErrors())
err := task.EncodeDriverConfig(taskConfigCtyVal)
require.Nil(err)
}

View File

@@ -35,7 +35,7 @@ func TestExecDriver_StartWaitStop(t *testing.T) {
"command": "/bin/sleep",
"args": []string{"600"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&taskConfig))
cleanup := harness.MkAllocDir(task, false)
defer cleanup()

View File

@@ -248,13 +248,13 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
// pre 0.9 upgrade path check
if handle.Version == 0 {
var reattach shared.ReattachConfig
var reattach pstructs.ReattachConfig
d.logger.Debug("parsing pre09 driver state", "state", string(handle.DriverState))
if err := json.Unmarshal(handle.DriverState, &reattach); err != nil {
return err
}
reattachConfig, err := shared.ReattachConfigToGoPlugin(&reattach)
reattachConfig, err := pstructs.ReattachConfigToGoPlugin(&reattach)
if err != nil {
return err
}

View File

@@ -13,14 +13,11 @@ import (
"context"
"time"
"github.com/hashicorp/hcl2/hcl"
ctestutil "github.com/hashicorp/nomad/client/testutil"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/hashicorp/nomad/plugins/shared/hclutils"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
)
@@ -66,11 +63,12 @@ func TestJavaDriver_Jar_Start_Wait(t *testing.T) {
d := NewDriver(testlog.HCLogger(t))
harness := dtestutil.NewDriverHarness(t, d)
task := basicTask(t, "demo-app", map[string]interface{}{
"jar_path": "demoapp.jar",
"args": []string{"1"},
"jvm_options": []string{"-Xmx64m", "-Xms32m"},
})
tc := &TaskConfig{
JarPath: "demoapp.jar",
Args: []string{"1"},
JvmOpts: []string{"-Xmx64m", "-Xms32m"},
}
task := basicTask(t, "demo-app", tc)
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
@@ -105,11 +103,12 @@ func TestJavaDriver_Jar_Stop_Wait(t *testing.T) {
d := NewDriver(testlog.HCLogger(t))
harness := dtestutil.NewDriverHarness(t, d)
task := basicTask(t, "demo-app", map[string]interface{}{
"jar_path": "demoapp.jar",
"args": []string{"600"},
"jvm_options": []string{"-Xmx64m", "-Xms32m"},
})
tc := &TaskConfig{
JarPath: "demoapp.jar",
Args: []string{"600"},
JvmOpts: []string{"-Xmx64m", "-Xms32m"},
}
task := basicTask(t, "demo-app", tc)
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
@@ -165,10 +164,11 @@ func TestJavaDriver_Class_Start_Wait(t *testing.T) {
d := NewDriver(testlog.HCLogger(t))
harness := dtestutil.NewDriverHarness(t, d)
task := basicTask(t, "demo-app", map[string]interface{}{
"class": "Hello",
"args": []string{"1"},
})
tc := &TaskConfig{
Class: "Hello",
Args: []string{"1"},
}
task := basicTask(t, "demo-app", tc)
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
@@ -242,7 +242,7 @@ func TestJavaCmdArgs(t *testing.T) {
}
}
func basicTask(t *testing.T, name string, taskConfig map[string]interface{}) *drivers.TaskConfig {
func basicTask(t *testing.T, name string, taskConfig *TaskConfig) *drivers.TaskConfig {
t.Helper()
task := &drivers.TaskConfig{
@@ -264,25 +264,10 @@ func basicTask(t *testing.T, name string, taskConfig map[string]interface{}) *dr
},
}
encodeDriverHelper(t, task, taskConfig)
require.NoError(t, task.EncodeConcreteDriverConfig(&taskConfig))
return task
}
func encodeDriverHelper(t *testing.T, task *drivers.TaskConfig, taskConfig map[string]interface{}) {
t.Helper()
evalCtx := &hcl.EvalContext{
Functions: hclutils.GetStdlibFuncs(),
}
spec, diag := hclspec.Convert(taskConfigSpec)
require.False(t, diag.HasErrors())
taskConfigCtyVal, diag := hclutils.ParseHclInterface(taskConfig, spec, evalCtx)
require.Empty(t, diag.Errs())
err := task.EncodeDriverConfig(taskConfigCtyVal)
require.Nil(t, err)
}
// copyFile moves an existing file to the destination
func copyFile(src, dst string, t *testing.T) {
in, err := os.Open(src)

View File

@@ -246,13 +246,13 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
// pre 0.9 upgrade path check
if handle.Version == 0 {
var reattach shared.ReattachConfig
var reattach pstructs.ReattachConfig
d.logger.Debug("parsing pre09 driver state", "state", string(handle.DriverState))
if err := json.Unmarshal(handle.DriverState, &reattach); err != nil {
return err
}
reattachConfig, err := shared.ReattachConfigToGoPlugin(&reattach)
reattachConfig, err := pstructs.ReattachConfigToGoPlugin(&reattach)
if err != nil {
return err
}

View File

@@ -9,15 +9,12 @@ import (
"testing"
"time"
"github.com/hashicorp/hcl2/hcl"
ctestutil "github.com/hashicorp/nomad/client/testutil"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/hashicorp/nomad/plugins/shared/hclutils"
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
@@ -57,17 +54,17 @@ func TestQemuDriver_Start_Wait_Stop(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"image_path": "linux-0.2.img",
"accelerator": "tcg",
"graceful_shutdown": false,
"port_map": []map[string]int{{
tc := &TaskConfig{
ImagePath: "linux-0.2.img",
Accelerator: "tcg",
GracefulShutdown: false,
PortMap: map[string]int{
"main": 22,
"web": 8080,
}},
"args": []string{"-nodefconfig", "-nodefaults"},
},
Args: []string{"-nodefconfig", "-nodefaults"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
@@ -199,19 +196,6 @@ func TestQemuDriver_GetMonitorPathNewQemu(t *testing.T) {
require.Nil(err)
}
//encodeDriverhelper sets up the task config spec and encodes qemu specific driver configuration
func encodeDriverHelper(require *require.Assertions, task *drivers.TaskConfig, taskConfig map[string]interface{}) {
evalCtx := &hcl.EvalContext{
Functions: hclutils.GetStdlibFuncs(),
}
spec, diag := hclspec.Convert(taskConfigSpec)
require.False(diag.HasErrors(), diag.Error())
taskConfigCtyVal, diag := hclutils.ParseHclInterface(taskConfig, spec, evalCtx)
require.False(diag.HasErrors(), diag.Error())
err := task.EncodeDriverConfig(taskConfigCtyVal)
require.Nil(err)
}
// copyFile moves an existing file to the destination
func copyFile(src, dst string, t *testing.T) {
in, err := os.Open(src)
@@ -268,17 +252,17 @@ func TestQemuDriver_User(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"image_path": "linux-0.2.img",
"accelerator": "tcg",
"graceful_shutdown": false,
"port_map": map[string]int{
tc := &TaskConfig{
ImagePath: "linux-0.2.img",
Accelerator: "tcg",
GracefulShutdown: false,
PortMap: map[string]int{
"main": 22,
"web": 8080,
},
"args": []string{"-nodefconfig", "-nodefaults"},
Args: []string{"-nodefconfig", "-nodefaults"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
@@ -324,17 +308,17 @@ func TestQemuDriver_Stats(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"image_path": "linux-0.2.img",
"accelerator": "tcg",
"graceful_shutdown": false,
"port_map": []map[string]int{{
tc := &TaskConfig{
ImagePath: "linux-0.2.img",
Accelerator: "tcg",
GracefulShutdown: false,
PortMap: map[string]int{
"main": 22,
"web": 8080,
}},
"args": []string{"-nodefconfig", "-nodefaults"},
},
Args: []string{"-nodefconfig", "-nodefaults"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, true)
defer cleanup()

View File

@@ -252,13 +252,13 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
// pre 0.9 upgrade path check
if handle.Version == 0 {
var reattach shared.ReattachConfig
var reattach pstructs.ReattachConfig
d.logger.Debug("parsing pre09 driver state", "state", string(handle.DriverState))
if err := json.Unmarshal(handle.DriverState, &reattach); err != nil {
return err
}
reattachConfig, err := shared.ReattachConfigToGoPlugin(&reattach)
reattachConfig, err := pstructs.ReattachConfigToGoPlugin(&reattach)
if err != nil {
return err
}

View File

@@ -13,7 +13,6 @@ import (
"testing"
"time"
"github.com/hashicorp/hcl2/hcl"
ctestutil "github.com/hashicorp/nomad/client/testutil"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/helper/testtask"
@@ -21,8 +20,6 @@ import (
basePlug "github.com/hashicorp/nomad/plugins/base"
"github.com/hashicorp/nomad/plugins/drivers"
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/hashicorp/nomad/plugins/shared/hclutils"
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
@@ -143,11 +140,13 @@ func TestRawExecDriver_StartWait(t *testing.T) {
Name: "test",
}
taskConfig := map[string]interface{}{}
taskConfig["command"] = "go"
taskConfig["args"] = []string{"version"}
tc := &TaskConfig{
Command: testtask.Path(),
Args: []string{"sleep", "10ms"},
}
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
encodeDriverHelper(require, task, taskConfig)
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
@@ -156,7 +155,14 @@ func TestRawExecDriver_StartWait(t *testing.T) {
ch, err := harness.WaitTask(context.Background(), handle.Config.ID)
require.NoError(err)
result := <-ch
var result *drivers.ExitResult
select {
case result = <-ch:
case <-time.After(5 * time.Second):
t.Fatal("timed out")
}
require.Zero(result.ExitCode)
require.Zero(result.Signal)
require.False(result.OOMKilled)
@@ -183,11 +189,11 @@ func TestRawExecDriver_StartWaitRecoverWaitStop(t *testing.T) {
ID: uuid.Generate(),
Name: "sleep",
}
taskConfig := map[string]interface{}{}
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"sleep", "100s"}
encodeDriverHelper(require, task, taskConfig)
tc := &TaskConfig{
Command: testtask.Path(),
Args: []string{"sleep", "100s"},
}
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
cleanup := harness.MkAllocDir(task, false)
@@ -267,10 +273,11 @@ func TestRawExecDriver_Start_Wait_AllocDir(t *testing.T) {
file := "output.txt"
outPath := fmt.Sprintf(`%s/%s`, task.TaskDir().SharedAllocDir, file)
taskConfig := map[string]interface{}{}
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"sleep", "1s", "write", string(exp), outPath}
encodeDriverHelper(require, task, taskConfig)
tc := &TaskConfig{
Command: testtask.Path(),
Args: []string{"sleep", "1s", "write", string(exp), outPath},
}
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
_, _, err := harness.StartTask(task)
@@ -318,10 +325,11 @@ func TestRawExecDriver_Start_Kill_Wait_Cgroup(t *testing.T) {
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
taskConfig := map[string]interface{}{}
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"fork/exec", pidFile, "pgrp", "0", "sleep", "20s"}
encodeDriverHelper(require, task, taskConfig)
tc := &TaskConfig{
Command: testtask.Path(),
Args: []string{"fork/exec", pidFile, "pgrp", "0", "sleep", "20s"},
}
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
_, _, err := harness.StartTask(task)
@@ -406,10 +414,11 @@ func TestRawExecDriver_Exec(t *testing.T) {
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
taskConfig := map[string]interface{}{}
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"sleep", "9000s"}
encodeDriverHelper(require, task, taskConfig)
tc := &TaskConfig{
Command: testtask.Path(),
Args: []string{"sleep", "9000s"},
}
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
_, _, err := harness.StartTask(task)
@@ -443,15 +452,3 @@ func TestRawExecDriver_Exec(t *testing.T) {
require.NoError(harness.DestroyTask(task.ID, true))
}
func encodeDriverHelper(require *require.Assertions, task *drivers.TaskConfig, taskConfig map[string]interface{}) {
evalCtx := &hcl.EvalContext{
Functions: hclutils.GetStdlibFuncs(),
}
spec, diag := hclspec.Convert(taskConfigSpec)
require.False(diag.HasErrors())
taskConfigCtyVal, diag := hclutils.ParseHclInterface(taskConfig, spec, evalCtx)
require.False(diag.HasErrors())
err := task.EncodeDriverConfig(taskConfigCtyVal)
require.Nil(err)
}

View File

@@ -43,11 +43,11 @@ func TestRawExecDriver_User(t *testing.T) {
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
taskConfig := map[string]interface{}{}
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"sleep", "45s"}
encodeDriverHelper(require, task, taskConfig)
tc := &TaskConfig{
Command: testtask.Path(),
Args: []string{"sleep", "45s"},
}
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
_, _, err := harness.StartTask(task)
@@ -74,11 +74,11 @@ func TestRawExecDriver_Signal(t *testing.T) {
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
taskConfig := map[string]interface{}{}
taskConfig["command"] = "/bin/bash"
taskConfig["args"] = []string{"test.sh"}
encodeDriverHelper(require, task, taskConfig)
tc := &TaskConfig{
Command: "/bin/bash",
Args: []string{"test.sh"},
}
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
testFile := filepath.Join(task.TaskDir().Dir, "test.sh")
@@ -94,7 +94,6 @@ done
`)
require.NoError(ioutil.WriteFile(testFile, testData, 0777))
testtask.SetTaskConfigEnv(task)
_, _, err := harness.StartTask(task)
require.NoError(err)
@@ -155,7 +154,7 @@ func TestRawExecDriver_StartWaitStop(t *testing.T) {
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"sleep", "100s"}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&taskConfig))
cleanup := harness.MkAllocDir(task, false)
defer cleanup()

View File

@@ -359,13 +359,13 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
// pre 0.9 upgrade path check
if handle.Version == 0 {
var reattach shared.ReattachConfig
var reattach pstructs.ReattachConfig
d.logger.Debug("parsing pre09 driver state", "state", string(handle.DriverState))
if err := json.Unmarshal(handle.DriverState, &reattach); err != nil {
return err
}
reattachConfig, err := shared.ReattachConfigToGoPlugin(&reattach)
reattachConfig, err := pstructs.ReattachConfigToGoPlugin(&reattach)
if err != nil {
return err
}

View File

@@ -13,7 +13,6 @@ import (
"testing"
"time"
"github.com/hashicorp/hcl2/hcl"
ctestutil "github.com/hashicorp/nomad/client/testutil"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/helper/testtask"
@@ -22,8 +21,6 @@ import (
basePlug "github.com/hashicorp/nomad/plugins/base"
"github.com/hashicorp/nomad/plugins/drivers"
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/hashicorp/nomad/plugins/shared/hclutils"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
@@ -109,16 +106,15 @@ func TestRktDriver_Start_Wait_Stop_DNS(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"trust_prefix": "coreos.com/etcd",
"image": "coreos.com/etcd:v2.0.4",
"command": "/etcd",
"dns_servers": []string{"8.8.8.8", "8.8.4.4"},
"dns_search_domains": []string{"example.com", "example.org", "example.net"},
"net": []string{"host"},
tc := &TaskConfig{
TrustPrefix: "coreos.com/etcd",
ImageName: "coreos.com/etcd:v2.0.4",
Command: "/etcd",
DNSServers: []string{"8.8.8.8", "8.8.4.4"},
DNSSearchDomains: []string{"example.com", "example.org", "example.net"},
Net: []string{"host"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
@@ -193,16 +189,15 @@ func TestRktDriver_Start_Wait_Stop(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"trust_prefix": "coreos.com/etcd",
"image": "coreos.com/etcd:v2.0.4",
"command": "/etcd",
"args": []string{"--version"},
"net": []string{"none"},
"debug": true,
tc := &TaskConfig{
TrustPrefix: "coreos.com/etcd",
ImageName: "coreos.com/etcd:v2.0.4",
Command: "/etcd",
Args: []string{"--version"},
Net: []string{"none"},
Debug: true,
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
@@ -252,15 +247,14 @@ func TestRktDriver_Start_Wait_Skip_Trust(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"image": "coreos.com/etcd:v2.0.4",
"command": "/etcd",
"args": []string{"--version"},
"net": []string{"none"},
"debug": true,
tc := &TaskConfig{
ImageName: "coreos.com/etcd:v2.0.4",
Command: "/etcd",
Args: []string{"--version"},
Net: []string{"none"},
Debug: true,
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
cleanup := harness.MkAllocDir(task, true)
@@ -311,16 +305,15 @@ func TestRktDriver_InvalidTrustPrefix(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"trust_prefix": "example.com/invalid",
"image": "coreos.com/etcd:v2.0.4",
"command": "/etcd",
"args": []string{"--version"},
"net": []string{"none"},
"debug": true,
tc := &TaskConfig{
TrustPrefix: "example.com/invalid",
ImageName: "coreos.com/etcd:v2.0.4",
Command: "/etcd",
Args: []string{"--version"},
Net: []string{"none"},
Debug: true,
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
cleanup := harness.MkAllocDir(task, true)
@@ -366,12 +359,11 @@ func TestRktDriver_StartWaitRecoverWaitStop(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"image": "coreos.com/etcd:v2.0.4",
"command": "/etcd",
tc := &TaskConfig{
ImageName: "coreos.com/etcd:v2.0.4",
Command: "/etcd",
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
@@ -477,18 +469,18 @@ func TestRktDriver_Start_Wait_Volume(t *testing.T) {
defer os.RemoveAll(tmpvol)
hostpath := filepath.Join(tmpvol, file)
taskConfig := map[string]interface{}{
"image": "docker://redis:3.2-alpine",
"command": "/bin/sh",
"args": []string{
tc := &TaskConfig{
ImageName: "docker://redis:3.2-alpine",
Command: "/bin/sh",
Args: []string{
"-c",
fmt.Sprintf("echo -n %s > /foo/%s", string(exp), file),
},
"net": []string{"none"},
"volumes": []string{fmt.Sprintf("%s:/foo", tmpvol)},
Net: []string{"none"},
Volumes: []string{fmt.Sprintf("%s:/foo", tmpvol)},
}
require.NoError(task.EncodeConcreteDriverConfig(&tc))
encodeDriverHelper(require, task, taskConfig)
testtask.SetTaskConfigEnv(task)
cleanup := harness.MkAllocDir(task, true)
@@ -566,17 +558,16 @@ func TestRktDriver_Start_Wait_TaskMounts(t *testing.T) {
file := "output.txt"
hostpath := filepath.Join(tmpvol, file)
taskConfig := map[string]interface{}{
"image": "docker://redis:3.2-alpine",
"command": "/bin/sh",
"args": []string{
tc := &TaskConfig{
ImageName: "docker://redis:3.2-alpine",
Command: "/bin/sh",
Args: []string{
"-c",
fmt.Sprintf("echo -n %s > /foo/%s", string(exp), file),
},
"net": []string{"none"},
Net: []string{"none"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
cleanup := harness.MkAllocDir(task, true)
@@ -638,15 +629,14 @@ func TestRktDriver_PortMapping(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"image": "docker://redis:3.2-alpine",
"port_map": map[string]string{
tc := &TaskConfig{
ImageName: "docker://redis:3.2-alpine",
PortMap: map[string]string{
"main": "6379-tcp",
},
"debug": "true",
Debug: true,
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
@@ -690,15 +680,15 @@ func TestRktDriver_UserGroup(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"image": "docker://redis:3.2-alpine",
"group": "nogroup",
"command": "sleep",
"args": []string{"9000"},
"net": []string{"none"},
tc := &TaskConfig{
ImageName: "docker://redis:3.2-alpine",
Group: "nogroup",
Command: "sleep",
Args: []string{"9000"},
Net: []string{"none"},
}
require.NoError(task.EncodeConcreteDriverConfig(&tc))
encodeDriverHelper(require, task, taskConfig)
testtask.SetTaskConfigEnv(task)
cleanup := harness.MkAllocDir(task, true)
@@ -756,13 +746,12 @@ func TestRktDriver_Exec(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"trust_prefix": "coreos.com/etcd",
"image": "coreos.com/etcd:v2.0.4",
"net": []string{"none"},
tc := &TaskConfig{
TrustPrefix: "coreos.com/etcd",
ImageName: "coreos.com/etcd:v2.0.4",
Net: []string{"none"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
cleanup := harness.MkAllocDir(task, true)
@@ -838,14 +827,13 @@ func TestRktDriver_Stats(t *testing.T) {
},
}
taskConfig := map[string]interface{}{
"trust_prefix": "coreos.com/etcd",
"image": "coreos.com/etcd:v2.0.4",
"command": "/etcd",
"net": []string{"none"},
tc := &TaskConfig{
TrustPrefix: "coreos.com/etcd",
ImageName: "coreos.com/etcd:v2.0.4",
Command: "/etcd",
Net: []string{"none"},
}
encodeDriverHelper(require, task, taskConfig)
require.NoError(task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)
cleanup := harness.MkAllocDir(task, true)
@@ -878,18 +866,3 @@ func TestRktDriver_Stats(t *testing.T) {
require.NoError(harness.DestroyTask(task.ID, true))
}
func encodeDriverHelper(require *require.Assertions, task *drivers.TaskConfig, taskConfig map[string]interface{}) {
evalCtx := &hcl.EvalContext{
Functions: hclutils.GetStdlibFuncs(),
}
spec, diag := hclspec.Convert(taskConfigSpec)
require.False(diag.HasErrors())
taskConfigCtyVal, diag := hclutils.ParseHclInterface(taskConfig, spec, evalCtx)
if diag.HasErrors() {
fmt.Println("conversion error", diag.Error())
}
require.False(diag.HasErrors())
err := task.EncodeDriverConfig(taskConfigCtyVal)
require.Nil(err)
}