2017-04-04 22:12:14 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/testutil"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
tu "github.com/hashicorp/nomad/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDockerDriver_Signal(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-04-04 22:12:14 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "redis-demo",
|
|
|
|
Driver: "docker",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
|
|
|
"load": "busybox.tar",
|
|
|
|
"command": "/bin/sh",
|
|
|
|
"args": []string{"local/test.sh"},
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
|
|
|
|
// Copy the image into the task's directory
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
|
|
|
|
|
|
|
testFile := filepath.Join(ctx.ExecCtx.TaskDir.LocalDir, "test.sh")
|
|
|
|
testData := []byte(`
|
|
|
|
at_term() {
|
2017-10-18 20:43:26 +00:00
|
|
|
echo 'Terminated.' > $NOMAD_TASK_DIR/output
|
2017-04-04 22:12:14 +00:00
|
|
|
exit 3
|
|
|
|
}
|
2017-10-18 20:43:26 +00:00
|
|
|
trap at_term INT
|
2017-04-04 22:12:14 +00:00
|
|
|
while true; do
|
2017-10-18 20:43:26 +00:00
|
|
|
echo 'sleeping'
|
|
|
|
sleep 0.2
|
2017-04-04 22:12:14 +00:00
|
|
|
done
|
|
|
|
`)
|
|
|
|
if err := ioutil.WriteFile(testFile, testData, 0777); err != nil {
|
|
|
|
t.Fatalf("Failed to write data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := d.Prestart(ctx.ExecCtx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2017-04-04 22:12:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
defer resp.Handle.Kill()
|
2017-04-04 22:12:14 +00:00
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
waitForExist(t, resp.Handle.(*DockerHandle).client, resp.Handle.(*DockerHandle))
|
2017-04-04 22:12:14 +00:00
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
2017-10-18 20:43:26 +00:00
|
|
|
if err := resp.Handle.Signal(syscall.SIGINT); err != nil {
|
2017-04-04 22:12:14 +00:00
|
|
|
t.Fatalf("Signal returned an error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2017-06-13 21:02:11 +00:00
|
|
|
case res := <-resp.Handle.WaitCh():
|
2017-04-04 22:12:14 +00:00
|
|
|
if res.Successful() {
|
|
|
|
t.Fatalf("should err: %v", res)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
|
2017-10-18 20:43:26 +00:00
|
|
|
//t.Fatalf("timeout")
|
2017-04-04 22:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the log file to see it exited because of the signal
|
2017-10-18 20:43:26 +00:00
|
|
|
outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LocalDir, "output")
|
2017-04-04 22:12:14 +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)
|
|
|
|
}
|
|
|
|
}
|