Get raw exec tests compiling and passing again

This commit is contained in:
Preetha Appan 2018-10-11 09:36:02 -05:00 committed by Michael Schurter
parent 8cf669b5aa
commit c2d2bfa06c
3 changed files with 75 additions and 48 deletions

View File

@ -12,12 +12,15 @@ 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"
"github.com/hashicorp/nomad/helper/uuid"
basePlug "github.com/hashicorp/nomad/plugins/base"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/hashicorp/nomad/plugins/shared"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
)
@ -111,14 +114,16 @@ func TestRawExecDriver_StartWait(t *testing.T) {
ID: uuid.Generate(),
Name: "test",
}
task.EncodeDriverConfig(&TaskConfig{
Command: "go",
Args: []string{"version"},
})
taskConfig := map[string]interface{}{}
taskConfig["command"] = "go"
taskConfig["args"] = []string{"version"}
encodeDriverHelper(require, task, taskConfig)
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
handle, err := harness.StartTask(task)
handle, _, err := harness.StartTask(task)
require.NoError(err)
ch, err := harness.WaitTask(context.Background(), handle.Config.ID)
@ -138,14 +143,17 @@ func TestRawExecDriver_StartWaitStop(t *testing.T) {
ID: uuid.Generate(),
Name: "test",
}
task.EncodeDriverConfig(&TaskConfig{
Command: testtask.Path(),
Args: []string{"sleep", "100s"},
})
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)
handle, _, err := harness.StartTask(task)
require.NoError(err)
ch, err := harness.WaitTask(context.Background(), handle.Config.ID)
@ -196,15 +204,17 @@ func TestRawExecDriver_StartWaitRecoverWaitStop(t *testing.T) {
ID: uuid.Generate(),
Name: "sleep",
}
task.EncodeDriverConfig(&TaskConfig{
Command: testtask.Path(),
Args: []string{"sleep", "100s"},
})
taskConfig := map[string]interface{}{}
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"sleep", "100s"}
encodeDriverHelper(require, task, taskConfig)
testtask.SetTaskConfigEnv(task)
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
handle, err := harness.StartTask(task)
handle, _, err := harness.StartTask(task)
require.NoError(err)
ch, err := harness.WaitTask(context.Background(), task.ID)
@ -277,17 +287,14 @@ func TestRawExecDriver_Start_Wait_AllocDir(t *testing.T) {
exp := []byte("win")
file := "output.txt"
outPath := fmt.Sprintf(`%s/%s`, task.TaskDir().SharedAllocDir, file)
task.EncodeDriverConfig(&TaskConfig{
Command: testtask.Path(),
Args: []string{
"sleep", "1s", "write",
string(exp), outPath,
},
})
taskConfig := map[string]interface{}{}
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"sleep", "1s", "write", string(exp), outPath}
encodeDriverHelper(require, task, taskConfig)
testtask.SetTaskConfigEnv(task)
_, err := harness.StartTask(task)
_, _, err := harness.StartTask(task)
require.NoError(err)
// Task should terminate quickly
@ -331,14 +338,13 @@ func TestRawExecDriver_Start_Kill_Wait_Cgroup(t *testing.T) {
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
task.EncodeDriverConfig(&TaskConfig{
Command: testtask.Path(),
Args: []string{"fork/exec", pidFile, "pgrp", "0", "sleep", "20s"},
})
taskConfig := map[string]interface{}{}
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"fork/exec", pidFile, "pgrp", "0", "sleep", "20s"}
encodeDriverHelper(require, task, taskConfig)
testtask.SetTaskConfigEnv(task)
_, err := harness.StartTask(task)
_, _, err := harness.StartTask(task)
require.NoError(err)
// Find the process
@ -419,14 +425,13 @@ func TestRawExecDriver_Exec(t *testing.T) {
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
task.EncodeDriverConfig(&TaskConfig{
Command: testtask.Path(),
Args: []string{"sleep", "9000s"},
})
taskConfig := map[string]interface{}{}
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"sleep", "9000s"}
encodeDriverHelper(require, task, taskConfig)
testtask.SetTaskConfigEnv(task)
_, err := harness.StartTask(task)
_, _, err := harness.StartTask(task)
require.NoError(err)
// Exec a command that should work
@ -443,3 +448,15 @@ 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: shared.GetStdlibFuncs(),
}
spec, diag := hclspec.Convert(taskConfigSpec)
require.False(diag.HasErrors())
taskConfigCtyVal, diag := shared.ParseHclInterface(taskConfig, spec, evalCtx)
require.False(diag.HasErrors())
err := task.EncodeDriverConfig(taskConfigCtyVal)
require.Nil(err)
}

View File

@ -4,12 +4,13 @@ package rawexec
import (
"context"
"runtime"
"testing"
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/hashicorp/nomad/helper/testlog"
@ -39,13 +40,14 @@ func TestRawExecDriver_User(t *testing.T) {
cleanup := harness.MkAllocDir(task, false)
defer cleanup()
task.EncodeDriverConfig(&TaskConfig{
Command: testtask.Path(),
Args: []string{"sleep", "45s"},
})
taskConfig := map[string]interface{}{}
taskConfig["command"] = testtask.Path()
taskConfig["args"] = []string{"sleep", "45s"}
encodeDriverHelper(require, task, taskConfig)
testtask.SetTaskConfigEnv(task)
_, err := harness.StartTask(task)
_, _, err := harness.StartTask(task)
require.Error(err)
msg := "unknown user alice"
require.Contains(err.Error(), msg)
@ -69,10 +71,12 @@ func TestRawExecDriver_Signal(t *testing.T) {
cleanup := harness.MkAllocDir(task, true)
defer cleanup()
task.EncodeDriverConfig(&TaskConfig{
Command: "/bin/bash",
Args: []string{"test.sh"},
})
taskConfig := map[string]interface{}{}
taskConfig["command"] = "/bin/bash"
taskConfig["args"] = []string{"test.sh"}
encodeDriverHelper(require, task, taskConfig)
testtask.SetTaskConfigEnv(task)
testFile := filepath.Join(task.TaskDir().Dir, "test.sh")
testData := []byte(`
@ -88,7 +92,7 @@ done
require.NoError(ioutil.WriteFile(testFile, testData, 0777))
testtask.SetTaskConfigEnv(task)
_, err := harness.StartTask(task)
_, _, err := harness.StartTask(task)
require.NoError(err)
go func() {

View File

@ -136,8 +136,10 @@ func resourcesFromProto(pb *proto.Resources) *Resources {
}
func resourcesToProto(r *Resources) *proto.Resources {
if r == nil {
return nil
}
var pb proto.Resources
if r.NomadResources != nil {
pb.RawResources = &proto.RawResources{
Cpu: int64(r.NomadResources.CPU),
@ -404,3 +406,7 @@ func resourceUsageFromProto(pb *proto.TaskResourceUsage) *cstructs.ResourceUsage
MemoryStats: &memory,
}
}
func BytesToMB(bytes int64) int64 {
return bytes / (1024 * 1024)
}