2018-11-27 19:03:58 +00:00
|
|
|
package testutils
|
2018-09-26 17:33:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-11-13 01:09:27 +00:00
|
|
|
"context"
|
2018-09-26 05:18:03 +00:00
|
|
|
"fmt"
|
2018-09-26 17:33:37 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2018-09-26 05:18:03 +00:00
|
|
|
"runtime"
|
2019-01-04 23:33:27 +00:00
|
|
|
"strings"
|
2018-09-26 17:33:37 +00:00
|
|
|
"time"
|
|
|
|
|
2019-01-15 19:46:12 +00:00
|
|
|
testing "github.com/mitchellh/go-testing-interface"
|
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2018-09-26 17:33:37 +00:00
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2018-09-26 05:18:03 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2018-10-16 02:37:58 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-09-26 05:18:03 +00:00
|
|
|
"github.com/hashicorp/nomad/client/logmon"
|
2018-11-30 11:18:39 +00:00
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-09-26 05:18:03 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2018-11-09 04:38:47 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
2018-11-27 19:03:58 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
2018-11-13 01:09:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-09-26 17:33:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DriverHarness struct {
|
2018-11-27 19:03:58 +00:00
|
|
|
drivers.DriverPlugin
|
2018-09-26 17:33:37 +00:00
|
|
|
client *plugin.GRPCClient
|
|
|
|
server *plugin.GRPCServer
|
|
|
|
t testing.T
|
2018-09-26 05:18:03 +00:00
|
|
|
lm logmon.LogMon
|
|
|
|
logger hclog.Logger
|
2018-11-27 19:03:58 +00:00
|
|
|
impl drivers.DriverPlugin
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 19:03:58 +00:00
|
|
|
func (d *DriverHarness) Impl() drivers.DriverPlugin {
|
2018-11-09 04:38:47 +00:00
|
|
|
return d.impl
|
|
|
|
}
|
2018-11-27 19:03:58 +00:00
|
|
|
func NewDriverHarness(t testing.T, d drivers.DriverPlugin) *DriverHarness {
|
2018-09-26 05:18:03 +00:00
|
|
|
logger := testlog.HCLogger(t).Named("driver_harness")
|
2018-11-27 19:03:58 +00:00
|
|
|
|
2019-01-25 14:38:41 +00:00
|
|
|
pd := drivers.NewDriverPlugin(d, logger)
|
2018-11-27 19:03:58 +00:00
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
client, server := plugin.TestPluginGRPCConn(t,
|
|
|
|
map[string]plugin.Plugin{
|
2018-11-27 19:03:58 +00:00
|
|
|
base.PluginTypeDriver: pd,
|
|
|
|
base.PluginTypeBase: &base.PluginBase{Impl: d},
|
|
|
|
"logmon": logmon.NewPlugin(logmon.NewLogMon(logger.Named("logmon"))),
|
2018-09-26 17:33:37 +00:00
|
|
|
},
|
2018-09-26 05:18:03 +00:00
|
|
|
)
|
2018-09-26 17:33:37 +00:00
|
|
|
|
|
|
|
raw, err := client.Dispense(base.PluginTypeDriver)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err dispensing plugin: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-27 19:03:58 +00:00
|
|
|
dClient := raw.(drivers.DriverPlugin)
|
2018-09-26 17:33:37 +00:00
|
|
|
h := &DriverHarness{
|
|
|
|
client: client,
|
|
|
|
server: server,
|
|
|
|
DriverPlugin: dClient,
|
2018-09-26 05:18:03 +00:00
|
|
|
logger: logger,
|
2018-10-16 02:37:58 +00:00
|
|
|
t: t,
|
2018-11-09 04:38:47 +00:00
|
|
|
impl: d,
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
raw, err = client.Dispense("logmon")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err dispensing plugin: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.lm = raw.(logmon.LogMon)
|
2018-09-26 17:33:37 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DriverHarness) Kill() {
|
|
|
|
h.client.Close()
|
|
|
|
h.server.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MkAllocDir creates a tempory directory and allocdir structure.
|
2018-09-26 05:18:03 +00:00
|
|
|
// If enableLogs is set to true a logmon instance will be started to write logs
|
|
|
|
// to the LogDir of the task
|
2018-09-26 17:33:37 +00:00
|
|
|
// A cleanup func is returned and should be defered so as to not leak dirs
|
|
|
|
// between tests.
|
2018-11-27 19:03:58 +00:00
|
|
|
func (h *DriverHarness) MkAllocDir(t *drivers.TaskConfig, enableLogs bool) func() {
|
2018-09-26 05:18:03 +00:00
|
|
|
dir, err := ioutil.TempDir("", "nomad_driver_harness-")
|
2018-09-26 17:33:37 +00:00
|
|
|
require.NoError(h.t, err)
|
2018-09-26 05:18:03 +00:00
|
|
|
t.AllocDir = dir
|
|
|
|
|
|
|
|
allocDir := allocdir.NewAllocDir(h.logger, dir)
|
|
|
|
require.NoError(h.t, allocDir.Build())
|
|
|
|
taskDir := allocDir.NewTaskDir(t.Name)
|
2018-10-16 02:37:58 +00:00
|
|
|
|
|
|
|
caps, err := h.Capabilities()
|
|
|
|
require.NoError(h.t, err)
|
|
|
|
|
|
|
|
fsi := caps.FSIsolation
|
2019-01-04 21:11:25 +00:00
|
|
|
require.NoError(h.t, taskDir.Build(fsi == drivers.FSIsolationChroot, config.DefaultChrootEnv))
|
2018-09-26 05:18:03 +00:00
|
|
|
|
2018-11-09 04:38:47 +00:00
|
|
|
task := &structs.Task{
|
2018-11-27 03:42:45 +00:00
|
|
|
Name: t.Name,
|
|
|
|
Env: t.Env,
|
|
|
|
}
|
2018-12-13 23:06:48 +00:00
|
|
|
|
|
|
|
// Create the mock allocation
|
|
|
|
alloc := mock.Alloc()
|
2018-11-27 03:42:45 +00:00
|
|
|
if t.Resources != nil {
|
2018-12-13 23:06:48 +00:00
|
|
|
alloc.AllocatedResources.Tasks[task.Name] = t.Resources.NomadResources
|
2018-11-09 04:38:47 +00:00
|
|
|
}
|
2018-12-13 23:06:48 +00:00
|
|
|
|
|
|
|
taskBuilder := taskenv.NewBuilder(mock.Node(), alloc, task, "global")
|
2019-01-04 23:33:27 +00:00
|
|
|
SetEnvvars(taskBuilder, fsi, taskDir, config.DefaultConfig())
|
2018-11-09 04:38:47 +00:00
|
|
|
|
|
|
|
taskEnv := taskBuilder.Build()
|
|
|
|
if t.Env == nil {
|
2018-11-28 22:19:48 +00:00
|
|
|
t.Env = taskEnv.Map()
|
2018-11-09 04:38:47 +00:00
|
|
|
} else {
|
2018-11-28 22:19:48 +00:00
|
|
|
for k, v := range taskEnv.Map() {
|
2018-11-09 04:38:47 +00:00
|
|
|
if _, ok := t.Env[k]; !ok {
|
|
|
|
t.Env[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
//logmon
|
|
|
|
if enableLogs {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
id := uuid.Generate()[:8]
|
2018-09-26 19:32:26 +00:00
|
|
|
t.StdoutPath = fmt.Sprintf("//./pipe/%s-%s.stdout", t.Name, id)
|
|
|
|
t.StderrPath = fmt.Sprintf("//./pipe/%s-%s.stderr", t.Name, id)
|
2018-09-26 05:18:03 +00:00
|
|
|
} else {
|
2018-09-26 19:32:26 +00:00
|
|
|
t.StdoutPath = filepath.Join(taskDir.LogDir, fmt.Sprintf(".%s.stdout.fifo", t.Name))
|
|
|
|
t.StderrPath = filepath.Join(taskDir.LogDir, fmt.Sprintf(".%s.stderr.fifo", t.Name))
|
2018-09-26 05:18:03 +00:00
|
|
|
}
|
|
|
|
err = h.lm.Start(&logmon.LogConfig{
|
|
|
|
LogDir: taskDir.LogDir,
|
|
|
|
StdoutLogFile: fmt.Sprintf("%s.stdout", t.Name),
|
|
|
|
StderrLogFile: fmt.Sprintf("%s.stderr", t.Name),
|
2018-09-26 19:32:26 +00:00
|
|
|
StdoutFifo: t.StdoutPath,
|
|
|
|
StderrFifo: t.StderrPath,
|
2018-09-26 05:18:03 +00:00
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
})
|
|
|
|
require.NoError(h.t, err)
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
h.lm.Stop()
|
2018-12-07 19:03:13 +00:00
|
|
|
h.client.Close()
|
2018-09-26 05:18:03 +00:00
|
|
|
allocDir.Destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
2018-09-26 19:32:26 +00:00
|
|
|
if h.lm != nil {
|
|
|
|
h.lm.Stop()
|
|
|
|
}
|
2018-12-07 19:03:13 +00:00
|
|
|
h.client.Close()
|
2018-09-26 05:18:03 +00:00
|
|
|
allocDir.Destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitUntilStarted will block until the task for the given ID is in the running
|
|
|
|
// state or the timeout is reached
|
|
|
|
func (h *DriverHarness) WaitUntilStarted(taskID string, timeout time.Duration) error {
|
|
|
|
deadline := time.Now().Add(timeout)
|
2018-11-27 19:03:58 +00:00
|
|
|
var lastState drivers.TaskState
|
2018-09-26 05:18:03 +00:00
|
|
|
for {
|
|
|
|
status, err := h.InspectTask(taskID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-27 19:03:58 +00:00
|
|
|
if status.State == drivers.TaskStateRunning {
|
2018-09-26 05:18:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
lastState = status.State
|
|
|
|
if time.Now().After(deadline) {
|
|
|
|
return fmt.Errorf("task never transitioned to running, currently '%s'", lastState)
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MockDriver is used for testing.
|
|
|
|
// Each function can be set as a closure to make assertions about how data
|
|
|
|
// is passed through the base plugin layer.
|
|
|
|
type MockDriver struct {
|
|
|
|
base.MockPlugin
|
|
|
|
TaskConfigSchemaF func() (*hclspec.Spec, error)
|
2018-11-27 19:03:58 +00:00
|
|
|
FingerprintF func(context.Context) (<-chan *drivers.Fingerprint, error)
|
|
|
|
CapabilitiesF func() (*drivers.Capabilities, error)
|
|
|
|
RecoverTaskF func(*drivers.TaskHandle) error
|
2019-01-04 23:01:35 +00:00
|
|
|
StartTaskF func(*drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error)
|
2018-11-27 19:03:58 +00:00
|
|
|
WaitTaskF func(context.Context, string) (<-chan *drivers.ExitResult, error)
|
2018-09-26 17:33:37 +00:00
|
|
|
StopTaskF func(string, time.Duration, string) error
|
|
|
|
DestroyTaskF func(string, bool) error
|
2018-11-27 19:03:58 +00:00
|
|
|
InspectTaskF func(string) (*drivers.TaskStatus, error)
|
2018-12-11 20:27:50 +00:00
|
|
|
TaskStatsF func(context.Context, string, time.Duration) (<-chan *drivers.TaskResourceUsage, error)
|
2018-11-27 19:03:58 +00:00
|
|
|
TaskEventsF func(context.Context) (<-chan *drivers.TaskEvent, error)
|
2018-09-26 17:33:37 +00:00
|
|
|
SignalTaskF func(string, string) error
|
2018-11-27 19:03:58 +00:00
|
|
|
ExecTaskF func(string, []string, time.Duration) (*drivers.ExecTaskResult, error)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MockDriver) TaskConfigSchema() (*hclspec.Spec, error) { return d.TaskConfigSchemaF() }
|
2018-11-27 19:03:58 +00:00
|
|
|
func (d *MockDriver) Fingerprint(ctx context.Context) (<-chan *drivers.Fingerprint, error) {
|
2018-09-26 17:33:37 +00:00
|
|
|
return d.FingerprintF(ctx)
|
|
|
|
}
|
2018-11-27 19:03:58 +00:00
|
|
|
func (d *MockDriver) Capabilities() (*drivers.Capabilities, error) { return d.CapabilitiesF() }
|
|
|
|
func (d *MockDriver) RecoverTask(h *drivers.TaskHandle) error { return d.RecoverTaskF(h) }
|
2019-01-04 23:01:35 +00:00
|
|
|
func (d *MockDriver) StartTask(c *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
|
2018-10-04 19:08:20 +00:00
|
|
|
return d.StartTaskF(c)
|
|
|
|
}
|
2018-11-27 19:03:58 +00:00
|
|
|
func (d *MockDriver) WaitTask(ctx context.Context, id string) (<-chan *drivers.ExitResult, error) {
|
2018-09-26 17:33:37 +00:00
|
|
|
return d.WaitTaskF(ctx, id)
|
|
|
|
}
|
|
|
|
func (d *MockDriver) StopTask(taskID string, timeout time.Duration, signal string) error {
|
|
|
|
return d.StopTaskF(taskID, timeout, signal)
|
|
|
|
}
|
|
|
|
func (d *MockDriver) DestroyTask(taskID string, force bool) error {
|
|
|
|
return d.DestroyTaskF(taskID, force)
|
|
|
|
}
|
2018-11-27 19:03:58 +00:00
|
|
|
func (d *MockDriver) InspectTask(taskID string) (*drivers.TaskStatus, error) {
|
|
|
|
return d.InspectTaskF(taskID)
|
|
|
|
}
|
2018-12-11 20:27:50 +00:00
|
|
|
func (d *MockDriver) TaskStats(ctx context.Context, taskID string, i time.Duration) (<-chan *drivers.TaskResourceUsage, error) {
|
|
|
|
return d.TaskStats(ctx, taskID, i)
|
2018-10-04 19:08:20 +00:00
|
|
|
}
|
2018-11-27 19:03:58 +00:00
|
|
|
func (d *MockDriver) TaskEvents(ctx context.Context) (<-chan *drivers.TaskEvent, error) {
|
2018-09-26 17:33:37 +00:00
|
|
|
return d.TaskEventsF(ctx)
|
|
|
|
}
|
|
|
|
func (d *MockDriver) SignalTask(taskID string, signal string) error {
|
|
|
|
return d.SignalTask(taskID, signal)
|
|
|
|
}
|
2018-11-27 19:03:58 +00:00
|
|
|
func (d *MockDriver) ExecTask(taskID string, cmd []string, timeout time.Duration) (*drivers.ExecTaskResult, error) {
|
2018-09-26 17:33:37 +00:00
|
|
|
return d.ExecTaskF(taskID, cmd, timeout)
|
|
|
|
}
|
2019-01-04 23:33:27 +00:00
|
|
|
|
|
|
|
// SetEnvvars sets path and host env vars depending on the FS isolation used.
|
|
|
|
func SetEnvvars(envBuilder *taskenv.Builder, fsi drivers.FSIsolation, taskDir *allocdir.TaskDir, conf *config.Config) {
|
|
|
|
// Set driver-specific environment variables
|
|
|
|
switch fsi {
|
|
|
|
case drivers.FSIsolationNone:
|
|
|
|
// Use host paths
|
|
|
|
envBuilder.SetAllocDir(taskDir.SharedAllocDir)
|
|
|
|
envBuilder.SetTaskLocalDir(taskDir.LocalDir)
|
|
|
|
envBuilder.SetSecretsDir(taskDir.SecretsDir)
|
|
|
|
default:
|
|
|
|
// filesystem isolation; use container paths
|
|
|
|
envBuilder.SetAllocDir(allocdir.SharedAllocContainerPath)
|
|
|
|
envBuilder.SetTaskLocalDir(allocdir.TaskLocalContainerPath)
|
|
|
|
envBuilder.SetSecretsDir(allocdir.TaskSecretsContainerPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the host environment variables for non-image based drivers
|
|
|
|
if fsi != drivers.FSIsolationImage {
|
|
|
|
filter := strings.Split(conf.ReadDefault("env.blacklist", config.DefaultEnvBlacklist), ",")
|
|
|
|
envBuilder.SetHostEnvvars(filter)
|
|
|
|
}
|
|
|
|
}
|