github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/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 !testutil.DockerIsConnected(t) { 20 t.SkipNow() 21 } 22 23 task := &structs.Task{ 24 Name: "redis-demo", 25 Driver: "docker", 26 Config: map[string]interface{}{ 27 "image": "busybox", 28 "load": "busybox.tar", 29 "command": "/bin/sh", 30 "args": []string{"local/test.sh"}, 31 }, 32 Resources: &structs.Resources{ 33 MemoryMB: 256, 34 CPU: 512, 35 }, 36 LogConfig: &structs.LogConfig{ 37 MaxFiles: 10, 38 MaxFileSizeMB: 10, 39 }, 40 } 41 42 ctx := testDockerDriverContexts(t, task) 43 //ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"} 44 defer ctx.AllocDir.Destroy() 45 d := NewDockerDriver(ctx.DriverCtx) 46 47 // Copy the image into the task's directory 48 copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar") 49 50 testFile := filepath.Join(ctx.ExecCtx.TaskDir.LocalDir, "test.sh") 51 testData := []byte(` 52 at_term() { 53 echo 'Terminated.' 54 exit 3 55 } 56 trap at_term USR1 57 while true; do 58 sleep 1 59 done 60 `) 61 if err := ioutil.WriteFile(testFile, testData, 0777); err != nil { 62 t.Fatalf("Failed to write data: %v", err) 63 } 64 65 _, err := d.Prestart(ctx.ExecCtx, task) 66 if err != nil { 67 t.Fatalf("error in prestart: %v", err) 68 } 69 handle, err := d.Start(ctx.ExecCtx, task) 70 if err != nil { 71 t.Fatalf("err: %v", err) 72 } 73 if handle == nil { 74 t.Fatalf("missing handle") 75 } 76 defer handle.Kill() 77 78 waitForExist(t, handle.(*DockerHandle).client, handle.(*DockerHandle)) 79 80 time.Sleep(1 * time.Second) 81 if err := handle.Signal(syscall.SIGUSR1); err != nil { 82 t.Fatalf("Signal returned an error: %v", err) 83 } 84 85 select { 86 case res := <-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 }