github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/client/driver/executor/checks_test.go (about) 1 package executor 2 3 import ( 4 "log" 5 "os" 6 "strings" 7 "testing" 8 "time" 9 10 docker "github.com/fsouza/go-dockerclient" 11 12 cstructs "github.com/hashicorp/nomad/client/driver/structs" 13 "github.com/hashicorp/nomad/client/testutil" 14 ) 15 16 func TestExecScriptCheckNoIsolation(t *testing.T) { 17 check := &ExecScriptCheck{ 18 id: "foo", 19 cmd: "/bin/echo", 20 args: []string{"hello", "world"}, 21 taskDir: "/tmp", 22 FSIsolation: false, 23 } 24 25 res := check.Run() 26 expectedOutput := "hello world" 27 expectedExitCode := 0 28 if res.Err != nil { 29 t.Fatalf("err: %v", res.Err) 30 } 31 if strings.TrimSpace(res.Output) != expectedOutput { 32 t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output) 33 } 34 35 if res.ExitCode != expectedExitCode { 36 t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode) 37 } 38 } 39 40 func TestExecScriptCheckWithIsolation(t *testing.T) { 41 testutil.ExecCompatible(t) 42 43 execCmd := ExecCommand{Cmd: "/bin/echo", Args: []string{"hello world"}} 44 ctx := testExecutorContext(t) 45 defer ctx.AllocDir.Destroy() 46 47 execCmd.FSIsolation = true 48 execCmd.ResourceLimits = true 49 execCmd.User = cstructs.DefaultUnpriviledgedUser 50 51 executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags)) 52 53 if err := executor.SetContext(ctx); err != nil { 54 t.Fatalf("Unexpected error") 55 } 56 57 _, err := executor.LaunchCmd(&execCmd) 58 if err != nil { 59 t.Fatalf("error in launching command: %v", err) 60 } 61 62 check := &ExecScriptCheck{ 63 id: "foo", 64 cmd: "/bin/echo", 65 args: []string{"hello", "world"}, 66 taskDir: ctx.AllocDir.TaskDirs["web"], 67 FSIsolation: true, 68 } 69 70 res := check.Run() 71 expectedOutput := "hello world" 72 expectedExitCode := 0 73 if res.Err != nil { 74 t.Fatalf("err: %v", res.Err) 75 } 76 if strings.TrimSpace(res.Output) != expectedOutput { 77 t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output) 78 } 79 80 if res.ExitCode != expectedExitCode { 81 t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode) 82 } 83 } 84 85 func TestDockerScriptCheck(t *testing.T) { 86 if !testutil.DockerIsConnected(t) { 87 return 88 } 89 client, err := docker.NewClientFromEnv() 90 if err != nil { 91 t.Fatalf("error creating docker client: %v", err) 92 } 93 94 if err := client.PullImage(docker.PullImageOptions{Repository: "busybox", Tag: "latest"}, 95 docker.AuthConfiguration{}); err != nil { 96 t.Fatalf("error pulling redis: %v", err) 97 } 98 99 container, err := client.CreateContainer(docker.CreateContainerOptions{ 100 Config: &docker.Config{ 101 Image: "busybox", 102 Cmd: []string{"/bin/sleep", "1000"}, 103 }, 104 }) 105 if err != nil { 106 t.Fatalf("error creating container: %v", err) 107 } 108 defer removeContainer(client, container.ID) 109 110 if err := client.StartContainer(container.ID, container.HostConfig); err != nil { 111 t.Fatalf("error starting container", err) 112 } 113 114 check := &DockerScriptCheck{ 115 id: "1", 116 interval: 5 * time.Second, 117 containerID: container.ID, 118 logger: log.New(os.Stdout, "", log.LstdFlags), 119 cmd: "/bin/echo", 120 args: []string{"hello", "world"}, 121 } 122 123 res := check.Run() 124 expectedOutput := "hello world" 125 expectedExitCode := 0 126 if res.Err != nil { 127 t.Fatalf("err: %v", res.Err) 128 } 129 if strings.TrimSpace(res.Output) != expectedOutput { 130 t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output) 131 } 132 133 if res.ExitCode != expectedExitCode { 134 t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode) 135 } 136 } 137 138 // removeContainer kills and removes a container 139 func removeContainer(client *docker.Client, containerID string) { 140 client.KillContainer(docker.KillContainerOptions{ID: containerID}) 141 client.RemoveContainer(docker.RemoveContainerOptions{ID: containerID, RemoveVolumes: true, Force: true}) 142 }