github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/driver/docker_unix_test.go (about) 1 // +build !windows 2 3 package driver 4 5 import ( 6 "io/ioutil" 7 "path/filepath" 8 "strings" 9 "syscall" 10 "testing" 11 "time" 12 13 "github.com/hashicorp/nomad/client/testutil" 14 "github.com/hashicorp/nomad/nomad/structs" 15 tu "github.com/hashicorp/nomad/testutil" 16 ) 17 18 func TestDockerDriver_Signal(t *testing.T) { 19 if !tu.IsTravis() { 20 t.Parallel() 21 } 22 if !testutil.DockerIsConnected(t) { 23 t.SkipNow() 24 } 25 26 task := &structs.Task{ 27 Name: "redis-demo", 28 Driver: "docker", 29 Config: map[string]interface{}{ 30 "image": "busybox", 31 "load": "busybox.tar", 32 "command": "/bin/sh", 33 "args": []string{"local/test.sh"}, 34 }, 35 Resources: &structs.Resources{ 36 MemoryMB: 256, 37 CPU: 512, 38 }, 39 LogConfig: &structs.LogConfig{ 40 MaxFiles: 10, 41 MaxFileSizeMB: 10, 42 }, 43 } 44 45 ctx := testDockerDriverContexts(t, task) 46 //ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"} 47 defer ctx.AllocDir.Destroy() 48 d := NewDockerDriver(ctx.DriverCtx) 49 50 // Copy the image into the task's directory 51 copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar") 52 53 testFile := filepath.Join(ctx.ExecCtx.TaskDir.LocalDir, "test.sh") 54 testData := []byte(` 55 at_term() { 56 echo 'Terminated.' 57 exit 3 58 } 59 trap at_term USR1 60 while true; do 61 sleep 1 62 done 63 `) 64 if err := ioutil.WriteFile(testFile, testData, 0777); err != nil { 65 t.Fatalf("Failed to write data: %v", err) 66 } 67 68 _, err := d.Prestart(ctx.ExecCtx, task) 69 if err != nil { 70 t.Fatalf("error in prestart: %v", err) 71 } 72 resp, err := d.Start(ctx.ExecCtx, task) 73 if err != nil { 74 t.Fatalf("err: %v", err) 75 } 76 defer resp.Handle.Kill() 77 78 waitForExist(t, resp.Handle.(*DockerHandle).client, resp.Handle.(*DockerHandle)) 79 80 time.Sleep(1 * time.Second) 81 if err := resp.Handle.Signal(syscall.SIGUSR1); err != nil { 82 t.Fatalf("Signal returned an error: %v", err) 83 } 84 85 select { 86 case res := <-resp.Handle.WaitCh(): 87 if res.Successful() { 88 t.Fatalf("should err: %v", res) 89 } 90 case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second): 91 t.Fatalf("timeout") 92 } 93 94 // Check the log file to see it exited because of the signal 95 outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "redis-demo.stdout.0") 96 act, err := ioutil.ReadFile(outputFile) 97 if err != nil { 98 t.Fatalf("Couldn't read expected output: %v", err) 99 } 100 101 exp := "Terminated." 102 if strings.TrimSpace(string(act)) != exp { 103 t.Fatalf("Command outputted %v; want %v", act, exp) 104 } 105 }