open-nomad/drivers/docker/driver_unix_test.go

99 lines
2.6 KiB
Go
Raw Normal View History

// +build !windows
package docker
import (
2018-11-12 12:39:55 +00:00
"context"
"fmt"
"io/ioutil"
2018-11-12 12:39:55 +00:00
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/hashicorp/nomad/client/testutil"
tu "github.com/hashicorp/nomad/testutil"
2018-11-12 12:39:55 +00:00
"github.com/stretchr/testify/require"
)
func TestDockerDriver_Signal(t *testing.T) {
if !tu.IsTravis() {
t.Parallel()
}
if !testutil.DockerIsConnected(t) {
t.Skip("Docker not connected")
}
2018-11-12 12:39:55 +00:00
task, cfg, _ := dockerTask(t)
cfg.Command = "/bin/sh"
cfg.Args = []string{"local/test.sh"}
require.NoError(t, task.EncodeConcreteDriverConfig(cfg))
2018-11-12 12:39:55 +00:00
driver := dockerDriverHarness(t, nil)
cleanup := driver.MkAllocDir(task, true)
defer cleanup()
// Copy the image into the task's directory
2018-11-12 12:39:55 +00:00
copyImage(t, task.TaskDir(), "busybox.tar")
2018-11-12 12:39:55 +00:00
testFile := filepath.Join(task.TaskDir().LocalDir, "test.sh")
testData := []byte(`
at_term() {
echo 'Terminated.' > $NOMAD_TASK_DIR/output
exit 3
}
trap at_term INT
while true; do
echo 'sleeping'
sleep 0.2
done
`)
2018-11-12 12:39:55 +00:00
require.NoError(t, ioutil.WriteFile(testFile, testData, 0777))
_, _, err := driver.StartTask(task)
require.NoError(t, err)
defer driver.DestroyTask(task.ID, true)
require.NoError(t, driver.WaitUntilStarted(task.ID, time.Duration(tu.TestMultiplier()*5)*time.Second))
handle, ok := driver.Impl().(*Driver).tasks.Get(task.ID)
require.True(t, ok)
waitForExist(t, newTestDockerClient(t), handle.containerID)
2018-11-12 12:39:55 +00:00
require.NoError(t, handle.Kill(time.Duration(tu.TestMultiplier()*5)*time.Second, os.Interrupt))
2018-11-12 12:39:55 +00:00
waitCh, err := driver.WaitTask(context.Background(), task.ID)
require.NoError(t, err)
select {
2018-11-12 12:39:55 +00:00
case res := <-waitCh:
if res.Successful() {
2018-11-12 12:39:55 +00:00
require.Fail(t, "should err: %v", res)
}
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
2018-11-12 12:39:55 +00:00
require.Fail(t, "timeout")
}
// Check the log file to see it exited because of the signal
2018-11-12 12:39:55 +00:00
outputFile := filepath.Join(task.TaskDir().LocalDir, "output")
act, err := ioutil.ReadFile(outputFile)
if err != nil {
t.Fatalf("Couldn't read expected output: %v", err)
}
exp := "Terminated."
if strings.TrimSpace(string(act)) != exp {
t.Fatalf("Command outputted %v; want %v", act, exp)
}
2018-11-12 12:39:55 +00:00
}
func TestDockerDriver_containerBinds(t *testing.T) {
task, cfg, _ := dockerTask(t)
driver := dockerDriverHarness(t, nil)
cleanup := driver.MkAllocDir(task, false)
defer cleanup()
binds, err := driver.Impl().(*Driver).containerBinds(task, cfg)
require.NoError(t, err)
require.Contains(t, binds, fmt.Sprintf("%s:/alloc", task.TaskDir().SharedAllocDir))
require.Contains(t, binds, fmt.Sprintf("%s:/local", task.TaskDir().LocalDir))
require.Contains(t, binds, fmt.Sprintf("%s:/secrets", task.TaskDir().SecretsDir))
}