2018-11-06 05:39:48 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2018-11-12 12:39:55 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-11-06 05:39:48 +00:00
|
|
|
"io/ioutil"
|
2018-11-12 12:39:55 +00:00
|
|
|
"os"
|
2018-11-06 05:39:48 +00:00
|
|
|
"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"
|
2018-11-06 05:39:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-06 05:39:48 +00:00
|
|
|
|
2018-11-12 12:39:55 +00:00
|
|
|
driver := dockerDriverHarness(t, nil)
|
|
|
|
cleanup := driver.MkAllocDir(task, true)
|
|
|
|
defer cleanup()
|
2018-11-06 05:39:48 +00:00
|
|
|
|
|
|
|
// Copy the image into the task's directory
|
2018-11-12 12:39:55 +00:00
|
|
|
copyImage(t, task.TaskDir(), "busybox.tar")
|
2018-11-06 05:39:48 +00:00
|
|
|
|
2018-11-12 12:39:55 +00:00
|
|
|
testFile := filepath.Join(task.TaskDir().LocalDir, "test.sh")
|
2018-11-06 05:39:48 +00:00
|
|
|
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)
|
2018-11-06 05:39:48 +00:00
|
|
|
|
2018-11-20 03:58:05 +00:00
|
|
|
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-06 05:39:48 +00:00
|
|
|
|
2018-11-12 12:39:55 +00:00
|
|
|
waitCh, err := driver.WaitTask(context.Background(), task.ID)
|
|
|
|
require.NoError(t, err)
|
2018-11-06 05:39:48 +00:00
|
|
|
select {
|
2018-11-12 12:39:55 +00:00
|
|
|
case res := <-waitCh:
|
2018-11-06 05:39:48 +00:00
|
|
|
if res.Successful() {
|
2018-11-12 12:39:55 +00:00
|
|
|
require.Fail(t, "should err: %v", res)
|
2018-11-06 05:39:48 +00:00
|
|
|
}
|
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
|
2018-11-12 12:39:55 +00:00
|
|
|
require.Fail(t, "timeout")
|
2018-11-06 05:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
2018-11-06 05:39:48 +00:00
|
|
|
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))
|
|
|
|
}
|