2018-11-27 19:03:58 +00:00
|
|
|
package testutils
|
2018-09-26 17:33:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-11-13 01:09:27 +00:00
|
|
|
"context"
|
2018-09-26 17:33:37 +00:00
|
|
|
"sync"
|
2021-06-15 07:31:08 +00:00
|
|
|
"sync/atomic"
|
2018-09-26 17:33:37 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-03-18 11:27:32 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-11-27 19:03:58 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-11-21 00:30:39 +00:00
|
|
|
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-11-27 19:03:58 +00:00
|
|
|
var _ drivers.DriverPlugin = (*MockDriver)(nil)
|
|
|
|
|
|
|
|
// Very simple test to ensure the test harness works as expected
|
|
|
|
func TestDriverHarness(t *testing.T) {
|
|
|
|
handle := &drivers.TaskHandle{Config: &drivers.TaskConfig{Name: "mock"}}
|
|
|
|
d := &MockDriver{
|
2019-01-04 23:01:35 +00:00
|
|
|
StartTaskF: func(task *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
|
2018-11-27 19:03:58 +00:00
|
|
|
return handle, nil, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
harness := NewDriverHarness(t, d)
|
|
|
|
defer harness.Kill()
|
|
|
|
actual, _, err := harness.StartTask(&drivers.TaskConfig{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, handle.Config.Name, actual.Config.Name)
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
type testDriverState struct {
|
|
|
|
Pid int
|
|
|
|
Log string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBaseDriver_Fingerprint(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2018-11-27 19:03:58 +00:00
|
|
|
fingerprints := []*drivers.Fingerprint{
|
2018-09-26 17:33:37 +00:00
|
|
|
{
|
2018-11-21 00:30:39 +00:00
|
|
|
Attributes: map[string]*pstructs.Attribute{"foo": pstructs.NewStringAttribute("bar")},
|
2018-11-27 19:03:58 +00:00
|
|
|
Health: drivers.HealthStateUnhealthy,
|
2018-09-26 17:33:37 +00:00
|
|
|
HealthDescription: "starting up",
|
|
|
|
},
|
|
|
|
{
|
2018-11-21 00:30:39 +00:00
|
|
|
Attributes: map[string]*pstructs.Attribute{"foo": pstructs.NewStringAttribute("bar")},
|
2018-11-27 19:03:58 +00:00
|
|
|
Health: drivers.HealthStateHealthy,
|
2018-09-26 17:33:37 +00:00
|
|
|
HealthDescription: "running",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-15 07:31:08 +00:00
|
|
|
var complete atomic.Value
|
|
|
|
complete.Store(false)
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
impl := &MockDriver{
|
2018-11-27 19:03:58 +00:00
|
|
|
FingerprintF: func(ctx context.Context) (<-chan *drivers.Fingerprint, error) {
|
|
|
|
ch := make(chan *drivers.Fingerprint)
|
2018-09-26 17:33:37 +00:00
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
ch <- fingerprints[0]
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
ch <- fingerprints[1]
|
2021-06-15 07:31:08 +00:00
|
|
|
complete.Store(true)
|
2018-09-26 17:33:37 +00:00
|
|
|
}()
|
|
|
|
return ch, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
harness := NewDriverHarness(t, impl)
|
|
|
|
defer harness.Kill()
|
|
|
|
|
|
|
|
ch, err := harness.Fingerprint(context.Background())
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
select {
|
|
|
|
case f := <-ch:
|
|
|
|
require.Exactly(f, fingerprints[0])
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
require.Fail("did not receive fingerprint[0]")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case f := <-ch:
|
|
|
|
require.Exactly(f, fingerprints[1])
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
require.Fail("did not receive fingerprint[1]")
|
|
|
|
}
|
|
|
|
}()
|
2021-06-15 07:31:08 +00:00
|
|
|
require.False(complete.Load().(bool))
|
2018-09-26 17:33:37 +00:00
|
|
|
wg.Wait()
|
2021-06-15 07:31:08 +00:00
|
|
|
require.True(complete.Load().(bool))
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBaseDriver_RecoverTask(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// build driver state and encode it into proto msg
|
|
|
|
state := testDriverState{Pid: 1, Log: "foo"}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := codec.NewEncoder(&buf, structs.MsgpackHandle)
|
|
|
|
enc.Encode(state)
|
|
|
|
|
|
|
|
// mock the RecoverTask driver call
|
|
|
|
impl := &MockDriver{
|
2018-11-27 19:03:58 +00:00
|
|
|
RecoverTaskF: func(h *drivers.TaskHandle) error {
|
2018-09-26 17:33:37 +00:00
|
|
|
var actual testDriverState
|
|
|
|
require.NoError(h.GetDriverState(&actual))
|
|
|
|
require.Equal(state, actual)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
harness := NewDriverHarness(t, impl)
|
|
|
|
defer harness.Kill()
|
|
|
|
|
2018-11-27 19:03:58 +00:00
|
|
|
handle := &drivers.TaskHandle{
|
2018-11-01 00:41:37 +00:00
|
|
|
DriverState: buf.Bytes(),
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
err := harness.RecoverTask(handle)
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBaseDriver_StartTask(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2018-11-27 19:03:58 +00:00
|
|
|
cfg := &drivers.TaskConfig{
|
2018-09-26 17:33:37 +00:00
|
|
|
ID: "foo",
|
|
|
|
}
|
|
|
|
state := &testDriverState{Pid: 1, Log: "log"}
|
2018-11-27 19:03:58 +00:00
|
|
|
var handle *drivers.TaskHandle
|
2018-09-26 17:33:37 +00:00
|
|
|
impl := &MockDriver{
|
2019-01-04 23:01:35 +00:00
|
|
|
StartTaskF: func(c *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
|
2019-01-16 14:28:41 +00:00
|
|
|
handle = drivers.NewTaskHandle(1)
|
2018-09-26 17:33:37 +00:00
|
|
|
handle.Config = c
|
2018-11-27 19:03:58 +00:00
|
|
|
handle.State = drivers.TaskStateRunning
|
2018-09-26 17:33:37 +00:00
|
|
|
handle.SetDriverState(state)
|
2018-10-16 23:52:58 +00:00
|
|
|
return handle, nil, nil
|
2018-09-26 17:33:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
harness := NewDriverHarness(t, impl)
|
|
|
|
defer harness.Kill()
|
2018-10-16 23:52:58 +00:00
|
|
|
resp, _, err := harness.StartTask(cfg)
|
2018-09-26 17:33:37 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(cfg.ID, resp.Config.ID)
|
|
|
|
require.Equal(handle.State, resp.State)
|
|
|
|
|
|
|
|
var actualState testDriverState
|
|
|
|
require.NoError(resp.GetDriverState(&actualState))
|
|
|
|
require.Equal(*state, actualState)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBaseDriver_WaitTask(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2018-11-27 19:03:58 +00:00
|
|
|
result := &drivers.ExitResult{ExitCode: 1, Signal: 9}
|
2018-09-26 17:33:37 +00:00
|
|
|
|
|
|
|
signalTask := make(chan struct{})
|
|
|
|
|
|
|
|
impl := &MockDriver{
|
2018-11-27 19:03:58 +00:00
|
|
|
WaitTaskF: func(_ context.Context, id string) (<-chan *drivers.ExitResult, error) {
|
|
|
|
ch := make(chan *drivers.ExitResult)
|
2018-09-26 17:33:37 +00:00
|
|
|
go func() {
|
|
|
|
<-signalTask
|
|
|
|
ch <- result
|
|
|
|
}()
|
|
|
|
return ch, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
harness := NewDriverHarness(t, impl)
|
|
|
|
defer harness.Kill()
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
var finished bool
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
ch, err := harness.WaitTask(context.TODO(), "foo")
|
|
|
|
require.NoError(err)
|
|
|
|
actualResult := <-ch
|
|
|
|
finished = true
|
|
|
|
require.Exactly(result, actualResult)
|
|
|
|
}()
|
|
|
|
require.False(finished)
|
|
|
|
close(signalTask)
|
|
|
|
wg.Wait()
|
|
|
|
require.True(finished)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBaseDriver_TaskEvents(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
now := time.Now().UTC().Truncate(time.Millisecond)
|
2018-11-27 19:03:58 +00:00
|
|
|
events := []*drivers.TaskEvent{
|
2018-09-26 17:33:37 +00:00
|
|
|
{
|
|
|
|
TaskID: "abc",
|
|
|
|
Timestamp: now,
|
|
|
|
Annotations: map[string]string{"foo": "bar"},
|
|
|
|
Message: "starting",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TaskID: "xyz",
|
|
|
|
Timestamp: now.Add(2 * time.Second),
|
|
|
|
Annotations: map[string]string{"foo": "bar"},
|
|
|
|
Message: "starting",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TaskID: "xyz",
|
|
|
|
Timestamp: now.Add(3 * time.Second),
|
|
|
|
Annotations: map[string]string{"foo": "bar"},
|
|
|
|
Message: "running",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TaskID: "abc",
|
|
|
|
Timestamp: now.Add(4 * time.Second),
|
|
|
|
Annotations: map[string]string{"foo": "bar"},
|
|
|
|
Message: "running",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl := &MockDriver{
|
2018-11-27 19:03:58 +00:00
|
|
|
TaskEventsF: func(ctx context.Context) (<-chan *drivers.TaskEvent, error) {
|
|
|
|
ch := make(chan *drivers.TaskEvent)
|
2018-09-26 17:33:37 +00:00
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
for _, event := range events {
|
|
|
|
ch <- event
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ch, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
harness := NewDriverHarness(t, impl)
|
|
|
|
defer harness.Kill()
|
|
|
|
|
|
|
|
ch, err := harness.TaskEvents(context.Background())
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
for _, event := range events {
|
|
|
|
select {
|
|
|
|
case actual := <-ch:
|
|
|
|
require.Exactly(actual, event)
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
require.Fail("failed to receive event")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-08-14 13:10:10 +00:00
|
|
|
|
|
|
|
func TestBaseDriver_Capabilities(t *testing.T) {
|
|
|
|
capabilities := &drivers.Capabilities{
|
|
|
|
NetIsolationModes: []drivers.NetIsolationMode{
|
|
|
|
drivers.NetIsolationModeHost,
|
|
|
|
drivers.NetIsolationModeGroup,
|
|
|
|
},
|
|
|
|
MustInitiateNetwork: true,
|
|
|
|
SendSignals: true,
|
|
|
|
Exec: true,
|
|
|
|
FSIsolation: drivers.FSIsolationNone,
|
|
|
|
}
|
|
|
|
d := &MockDriver{
|
|
|
|
CapabilitiesF: func() (*drivers.Capabilities, error) {
|
|
|
|
return capabilities, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
harness := NewDriverHarness(t, d)
|
|
|
|
defer harness.Kill()
|
|
|
|
|
|
|
|
caps, err := harness.Capabilities()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, capabilities, caps)
|
|
|
|
}
|