Merge pull request #2521 from hashicorp/t-windows

Variety of Windows specific test fixes
This commit is contained in:
Alex Dadgar 2017-04-04 16:47:46 -07:00 committed by GitHub
commit ac401f5e18
12 changed files with 460 additions and 401 deletions

View file

@ -3,11 +3,17 @@ image: Visual Studio 2017
clone_folder: c:\gopath\src\github.com\hashicorp\nomad clone_folder: c:\gopath\src\github.com\hashicorp\nomad
environment: environment:
GOPATH: c:\gopath GOPATH: c:\gopath
GOBIN: c:\gopath\bin
install: install:
- cmd: set PATH=%GOBIN%;c:\go\bin;%PATH%
- cmd: echo %Path% - cmd: echo %Path%
- cmd: go version - cmd: go version
- cmd: go env - cmd: go env
- cmd: go get github.com/hashicorp/vault - ps: mkdir C:\gopath\bin
- cmd: go install - ps: appveyor DownloadFile "https://releases.hashicorp.com/vault/0.7.0/vault_0.7.0_windows_amd64.zip" -FileName "C:\\gopath\\bin\\vault.zip"
- ps: Expand-Archive C:\gopath\bin\vault.zip -DestinationPath C:\gopath\bin
- ps: appveyor DownloadFile "https://releases.hashicorp.com/consul/0.7.0/consul_0.7.0_windows_amd64.zip" -FileName "C:\\gopath\\bin\\consul.zip"
- ps: Expand-Archive C:\gopath\bin\consul.zip -DestinationPath C:\gopath\bin
- cmd: go install -tags nomad_test
build_script: build_script:
- cmd: go test -tags nomad_test ./... - cmd: go test -tags nomad_test ./...

View file

@ -10,7 +10,6 @@ import (
"runtime/debug" "runtime/debug"
"strconv" "strconv"
"strings" "strings"
"syscall"
"testing" "testing"
"time" "time"
@ -1048,95 +1047,6 @@ func TestDockerDriver_Stats(t *testing.T) {
} }
} }
func TestDockerDriver_Signal(t *testing.T) {
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)
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
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() {
echo 'Terminated.'
exit 3
}
trap at_term USR1
while true; do
sleep 1
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)
}
handle, err := d.Start(ctx.ExecCtx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
defer handle.Kill()
waitForExist(t, handle.(*DockerHandle).client, handle.(*DockerHandle))
time.Sleep(1 * time.Second)
if err := handle.Signal(syscall.SIGUSR1); err != nil {
t.Fatalf("Signal returned an error: %v", err)
}
select {
case res := <-handle.WaitCh():
if res.Successful() {
t.Fatalf("should err: %v", res)
}
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
t.Fatalf("timeout")
}
// Check the log file to see it exited because of the signal
outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "redis-demo.stdout.0")
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)
}
}
func setupDockerVolumes(t *testing.T, cfg *config.Config, hostpath string) (*structs.Task, Driver, *ExecContext, string, func()) { func setupDockerVolumes(t *testing.T, cfg *config.Config, hostpath string) (*structs.Task, Driver, *ExecContext, string, func()) {
if !testutil.DockerIsConnected(t) { if !testutil.DockerIsConnected(t) {
t.SkipNow() t.SkipNow()

View file

@ -0,0 +1,105 @@
// +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) {
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)
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
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() {
echo 'Terminated.'
exit 3
}
trap at_term USR1
while true; do
sleep 1
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)
}
handle, err := d.Start(ctx.ExecCtx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
defer handle.Kill()
waitForExist(t, handle.(*DockerHandle).client, handle.(*DockerHandle))
time.Sleep(1 * time.Second)
if err := handle.Signal(syscall.SIGUSR1); err != nil {
t.Fatalf("Signal returned an error: %v", err)
}
select {
case res := <-handle.WaitCh():
if res.Successful() {
t.Fatalf("should err: %v", res)
}
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
t.Fatalf("timeout")
}
// Check the log file to see it exited because of the signal
outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "redis-demo.stdout.0")
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)
}
}

View file

@ -1,14 +1,11 @@
package driver package driver
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings" "strings"
"syscall"
"testing" "testing"
"time" "time"
@ -90,79 +87,6 @@ func TestExecDriver_StartOpen_Wait(t *testing.T) {
handle.Kill() handle.Kill()
handle2.Kill() handle2.Kill()
} }
func TestExecDriver_KillUserPid_OnPluginReconnectFailure(t *testing.T) {
ctestutils.ExecCompatible(t)
task := &structs.Task{
Name: "sleep",
Driver: "exec",
Config: map[string]interface{}{
"command": "/bin/sleep",
"args": []string{"1000000"},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
}
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewExecDriver(ctx.DriverCtx)
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
handle, err := d.Start(ctx.ExecCtx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
defer handle.Kill()
id := &execId{}
if err := json.Unmarshal([]byte(handle.ID()), id); err != nil {
t.Fatalf("Failed to parse handle '%s': %v", handle.ID(), err)
}
pluginPid := id.PluginConfig.Pid
proc, err := os.FindProcess(pluginPid)
if err != nil {
t.Fatalf("can't find plugin pid: %v", pluginPid)
}
if err := proc.Kill(); err != nil {
t.Fatalf("can't kill plugin pid: %v", err)
}
// Attempt to open
handle2, err := d.Open(ctx.ExecCtx, handle.ID())
if err == nil {
t.Fatalf("expected error")
}
if handle2 != nil {
handle2.Kill()
t.Fatalf("expected handle2 to be nil")
}
// Test if the userpid is still present
userProc, _ := os.FindProcess(id.UserPid)
for retry := 3; retry > 0; retry-- {
if err = userProc.Signal(syscall.Signal(0)); err != nil {
// Process is gone as expected; exit
return
}
// Killing processes is async; wait and check again
time.Sleep(time.Second)
}
if err = userProc.Signal(syscall.Signal(0)); err == nil {
t.Fatalf("expected user process to die")
}
}
func TestExecDriver_Start_Wait(t *testing.T) { func TestExecDriver_Start_Wait(t *testing.T) {
ctestutils.ExecCompatible(t) ctestutils.ExecCompatible(t)
task := &structs.Task{ task := &structs.Task{
@ -321,85 +245,6 @@ func TestExecDriver_Start_Kill_Wait(t *testing.T) {
} }
} }
func TestExecDriver_Signal(t *testing.T) {
ctestutils.ExecCompatible(t)
task := &structs.Task{
Name: "signal",
Driver: "exec",
Config: map[string]interface{}{
"command": "/bin/bash",
"args": []string{"test.sh"},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
KillTimeout: 10 * time.Second,
}
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewExecDriver(ctx.DriverCtx)
testFile := filepath.Join(ctx.ExecCtx.TaskDir.Dir, "test.sh")
testData := []byte(`
at_term() {
echo 'Terminated.'
exit 3
}
trap at_term USR1
while true; do
sleep 1
done
`)
if err := ioutil.WriteFile(testFile, testData, 0777); err != nil {
t.Fatalf("Failed to write data: %v", err)
}
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
handle, err := d.Start(ctx.ExecCtx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
go func() {
time.Sleep(100 * time.Millisecond)
err := handle.Signal(syscall.SIGUSR1)
if err != nil {
t.Fatalf("err: %v", err)
}
}()
// Task should terminate quickly
select {
case res := <-handle.WaitCh():
if res.Successful() {
t.Fatal("should err")
}
case <-time.After(time.Duration(testutil.TestMultiplier()*6) * time.Second):
t.Fatalf("timeout")
}
// Check the log file to see it exited because of the signal
outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "signal.stdout.0")
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.Logf("Read from %v", outputFile)
t.Fatalf("Command outputted %v; want %v", act, exp)
}
}
func TestExecDriverUser(t *testing.T) { func TestExecDriverUser(t *testing.T) {
ctestutils.ExecCompatible(t) ctestutils.ExecCompatible(t)
task := &structs.Task{ task := &structs.Task{

View file

@ -0,0 +1,170 @@
// +build !windows
package driver
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
ctestutils "github.com/hashicorp/nomad/client/testutil"
)
func TestExecDriver_KillUserPid_OnPluginReconnectFailure(t *testing.T) {
ctestutils.ExecCompatible(t)
task := &structs.Task{
Name: "sleep",
Driver: "exec",
Config: map[string]interface{}{
"command": "/bin/sleep",
"args": []string{"1000000"},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
}
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewExecDriver(ctx.DriverCtx)
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
handle, err := d.Start(ctx.ExecCtx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
defer handle.Kill()
id := &execId{}
if err := json.Unmarshal([]byte(handle.ID()), id); err != nil {
t.Fatalf("Failed to parse handle '%s': %v", handle.ID(), err)
}
pluginPid := id.PluginConfig.Pid
proc, err := os.FindProcess(pluginPid)
if err != nil {
t.Fatalf("can't find plugin pid: %v", pluginPid)
}
if err := proc.Kill(); err != nil {
t.Fatalf("can't kill plugin pid: %v", err)
}
// Attempt to open
handle2, err := d.Open(ctx.ExecCtx, handle.ID())
if err == nil {
t.Fatalf("expected error")
}
if handle2 != nil {
handle2.Kill()
t.Fatalf("expected handle2 to be nil")
}
// Test if the userpid is still present
userProc, _ := os.FindProcess(id.UserPid)
for retry := 3; retry > 0; retry-- {
if err = userProc.Signal(syscall.Signal(0)); err != nil {
// Process is gone as expected; exit
return
}
// Killing processes is async; wait and check again
time.Sleep(time.Second)
}
if err = userProc.Signal(syscall.Signal(0)); err == nil {
t.Fatalf("expected user process to die")
}
}
func TestExecDriver_Signal(t *testing.T) {
ctestutils.ExecCompatible(t)
task := &structs.Task{
Name: "signal",
Driver: "exec",
Config: map[string]interface{}{
"command": "/bin/bash",
"args": []string{"test.sh"},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
KillTimeout: 10 * time.Second,
}
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewExecDriver(ctx.DriverCtx)
testFile := filepath.Join(ctx.ExecCtx.TaskDir.Dir, "test.sh")
testData := []byte(`
at_term() {
echo 'Terminated.'
exit 3
}
trap at_term USR1
while true; do
sleep 1
done
`)
if err := ioutil.WriteFile(testFile, testData, 0777); err != nil {
t.Fatalf("Failed to write data: %v", err)
}
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
handle, err := d.Start(ctx.ExecCtx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
go func() {
time.Sleep(100 * time.Millisecond)
err := handle.Signal(syscall.SIGUSR1)
if err != nil {
t.Fatalf("err: %v", err)
}
}()
// Task should terminate quickly
select {
case res := <-handle.WaitCh():
if res.Successful() {
t.Fatal("should err")
}
case <-time.After(time.Duration(testutil.TestMultiplier()*6) * time.Second):
t.Fatalf("timeout")
}
// Check the log file to see it exited because of the signal
outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "signal.stdout.0")
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.Logf("Read from %v", outputFile)
t.Fatalf("Command outputted %v; want %v", act, exp)
}
}

View file

@ -6,7 +6,6 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings" "strings"
"syscall"
"testing" "testing"
"time" "time"
@ -299,81 +298,3 @@ func TestRawExecDriverUser(t *testing.T) {
t.Fatalf("Expecting '%v' in '%v'", msg, err) t.Fatalf("Expecting '%v' in '%v'", msg, err)
} }
} }
func TestRawExecDriver_Signal(t *testing.T) {
task := &structs.Task{
Name: "signal",
Driver: "raw_exec",
Config: map[string]interface{}{
"command": "/bin/bash",
"args": []string{"test.sh"},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
KillTimeout: 10 * time.Second,
}
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewRawExecDriver(ctx.DriverCtx)
testFile := filepath.Join(ctx.ExecCtx.TaskDir.Dir, "test.sh")
testData := []byte(`
at_term() {
echo 'Terminated.'
exit 3
}
trap at_term USR1
while true; do
sleep 1
done
`)
if err := ioutil.WriteFile(testFile, testData, 0777); err != nil {
t.Fatalf("Failed to write data: %v", err)
}
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
handle, err := d.Start(ctx.ExecCtx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
go func() {
time.Sleep(100 * time.Millisecond)
err := handle.Signal(syscall.SIGUSR1)
if err != nil {
t.Fatalf("err: %v", err)
}
}()
// Task should terminate quickly
select {
case res := <-handle.WaitCh():
if res.Successful() {
t.Fatal("should err")
}
case <-time.After(time.Duration(testutil.TestMultiplier()*6) * time.Second):
t.Fatalf("timeout")
}
// Check the log file to see it exited because of the signal
outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "signal.stdout.0")
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.Logf("Read from %v", outputFile)
t.Fatalf("Command outputted %v; want %v", act, exp)
}
}

View file

@ -0,0 +1,93 @@
// +build !windows
package driver
import (
"io/ioutil"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
)
func TestRawExecDriver_Signal(t *testing.T) {
task := &structs.Task{
Name: "signal",
Driver: "raw_exec",
Config: map[string]interface{}{
"command": "/bin/bash",
"args": []string{"test.sh"},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
KillTimeout: 10 * time.Second,
}
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewRawExecDriver(ctx.DriverCtx)
testFile := filepath.Join(ctx.ExecCtx.TaskDir.Dir, "test.sh")
testData := []byte(`
at_term() {
echo 'Terminated.'
exit 3
}
trap at_term USR1
while true; do
sleep 1
done
`)
if err := ioutil.WriteFile(testFile, testData, 0777); err != nil {
t.Fatalf("Failed to write data: %v", err)
}
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
handle, err := d.Start(ctx.ExecCtx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
go func() {
time.Sleep(100 * time.Millisecond)
err := handle.Signal(syscall.SIGUSR1)
if err != nil {
t.Fatalf("err: %v", err)
}
}()
// Task should terminate quickly
select {
case res := <-handle.WaitCh():
if res.Successful() {
t.Fatal("should err")
}
case <-time.After(time.Duration(testutil.TestMultiplier()*6) * time.Second):
t.Fatalf("timeout")
}
// Check the log file to see it exited because of the signal
outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "signal.stdout.0")
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.Logf("Read from %v", outputFile)
t.Fatalf("Command outputted %v; want %v", act, exp)
}
}

View file

@ -645,66 +645,6 @@ func TestTaskRunner_RestartTask(t *testing.T) {
} }
} }
// This test is just to make sure we are resilient to failures when a restart or
// signal is triggered and the task is not running.
func TestTaskRunner_RestartSignalTask_NotRunning(t *testing.T) {
alloc := mock.Alloc()
task := alloc.Job.TaskGroups[0].Tasks[0]
task.Driver = "mock_driver"
task.Config = map[string]interface{}{
"exit_code": "0",
"run_for": "100s",
}
// Use vault to block the start
task.Vault = &structs.Vault{Policies: []string{"default"}}
ctx := testTaskRunnerFromAlloc(t, true, alloc)
ctx.tr.MarkReceived()
defer ctx.Cleanup()
// Control when we get a Vault token
token := "1234"
waitCh := make(chan struct{})
defer close(waitCh)
handler := func(*structs.Allocation, []string) (map[string]string, error) {
<-waitCh
return map[string]string{task.Name: token}, nil
}
ctx.tr.vaultClient.(*vaultclient.MockVaultClient).DeriveTokenFn = handler
go ctx.tr.Run()
select {
case <-ctx.tr.WaitCh():
t.Fatalf("premature exit")
case <-time.After(1 * time.Second):
}
// Send a signal and restart
if err := ctx.tr.Signal("test", "don't panic", syscall.SIGCHLD); err != nil {
t.Fatalf("Signalling errored: %v", err)
}
// Send a restart
ctx.tr.Restart("test", "don't panic")
if len(ctx.upd.events) != 2 {
t.Fatalf("should have 2 ctx.updates: %#v", ctx.upd.events)
}
if ctx.upd.state != structs.TaskStatePending {
t.Fatalf("TaskState %v; want %v", ctx.upd.state, structs.TaskStatePending)
}
if ctx.upd.events[0].Type != structs.TaskReceived {
t.Fatalf("First Event was %v; want %v", ctx.upd.events[0].Type, structs.TaskReceived)
}
if ctx.upd.events[1].Type != structs.TaskSetup {
t.Fatalf("Second Event was %v; want %v", ctx.upd.events[1].Type, structs.TaskSetup)
}
}
func TestTaskRunner_KillTask(t *testing.T) { func TestTaskRunner_KillTask(t *testing.T) {
alloc := mock.Alloc() alloc := mock.Alloc()
task := alloc.Job.TaskGroups[0].Tasks[0] task := alloc.Job.TaskGroups[0].Tasks[0]

View file

@ -0,0 +1,73 @@
// +build !windows
package client
import (
"syscall"
"testing"
"time"
"github.com/hashicorp/nomad/client/vaultclient"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
)
// This test is just to make sure we are resilient to failures when a restart or
// signal is triggered and the task is not running.
func TestTaskRunner_RestartSignalTask_NotRunning(t *testing.T) {
alloc := mock.Alloc()
task := alloc.Job.TaskGroups[0].Tasks[0]
task.Driver = "mock_driver"
task.Config = map[string]interface{}{
"exit_code": "0",
"run_for": "100s",
}
// Use vault to block the start
task.Vault = &structs.Vault{Policies: []string{"default"}}
ctx := testTaskRunnerFromAlloc(t, true, alloc)
ctx.tr.MarkReceived()
defer ctx.Cleanup()
// Control when we get a Vault token
token := "1234"
waitCh := make(chan struct{})
defer close(waitCh)
handler := func(*structs.Allocation, []string) (map[string]string, error) {
<-waitCh
return map[string]string{task.Name: token}, nil
}
ctx.tr.vaultClient.(*vaultclient.MockVaultClient).DeriveTokenFn = handler
go ctx.tr.Run()
select {
case <-ctx.tr.WaitCh():
t.Fatalf("premature exit")
case <-time.After(1 * time.Second):
}
// Send a signal and restart
if err := ctx.tr.Signal("test", "don't panic", syscall.SIGCHLD); err != nil {
t.Fatalf("Signalling errored: %v", err)
}
// Send a restart
ctx.tr.Restart("test", "don't panic")
if len(ctx.upd.events) != 2 {
t.Fatalf("should have 2 ctx.updates: %#v", ctx.upd.events)
}
if ctx.upd.state != structs.TaskStatePending {
t.Fatalf("TaskState %v; want %v", ctx.upd.state, structs.TaskStatePending)
}
if ctx.upd.events[0].Type != structs.TaskReceived {
t.Fatalf("First Event was %v; want %v", ctx.upd.events[0].Type, structs.TaskReceived)
}
if ctx.upd.events[1].Type != structs.TaskSetup {
t.Fatalf("Second Event was %v; want %v", ctx.upd.events[1].Type, structs.TaskSetup)
}
}

View file

@ -24,6 +24,7 @@ import (
"testing" "testing"
"github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/nomad/helper/discover"
) )
// offset is used to atomically increment the port numbers. // offset is used to atomically increment the port numbers.
@ -126,8 +127,9 @@ type TestServer struct {
// NewTestServer creates a new TestServer, and makes a call to // NewTestServer creates a new TestServer, and makes a call to
// an optional callback function to modify the configuration. // an optional callback function to modify the configuration.
func NewTestServer(t *testing.T, cb ServerConfigCallback) *TestServer { func NewTestServer(t *testing.T, cb ServerConfigCallback) *TestServer {
if path, err := exec.LookPath("nomad"); err != nil || path == "" { path, err := discover.NomadExecutable()
t.Skip("nomad not found on $PATH, skipping") if err != nil {
t.Skipf("nomad not found, skipping: %v", err)
} }
dataDir, err := ioutil.TempDir("", "nomad") dataDir, err := ioutil.TempDir("", "nomad")
@ -175,7 +177,7 @@ func NewTestServer(t *testing.T, cb ServerConfigCallback) *TestServer {
} }
// Start the server // Start the server
cmd := exec.Command("nomad", args...) cmd := exec.Command(path, args...)
cmd.Stdout = stdout cmd.Stdout = stdout
cmd.Stderr = stderr cmd.Stderr = stderr
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"runtime"
"testing" "testing"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
@ -46,7 +47,11 @@ func NewTestVault(t *testing.T) *TestVault {
http := fmt.Sprintf("http://127.0.0.1:%d", port) http := fmt.Sprintf("http://127.0.0.1:%d", port)
root := fmt.Sprintf("-dev-root-token-id=%s", token) root := fmt.Sprintf("-dev-root-token-id=%s", token)
cmd := exec.Command("vault", "server", "-dev", bind, root) bin := "vault"
if runtime.GOOS == "windows" {
bin = "vault.exe"
}
cmd := exec.Command(bin, "server", "-dev", bind, root)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr

View file

@ -1,11 +0,0 @@
package main // import "github.com/hashicorp/vault"
import (
"os"
"github.com/hashicorp/vault/cli"
)
func main() {
os.Exit(cli.Run(os.Args[1:]))
}