github.com/djenriquez/nomad-1@v0.8.1/client/task_runner_unix_test.go (about)

     1  // +build !windows
     2  
     3  package client
     4  
     5  import (
     6  	"syscall"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/hashicorp/nomad/client/vaultclient"
    11  	"github.com/hashicorp/nomad/nomad/mock"
    12  	"github.com/hashicorp/nomad/nomad/structs"
    13  )
    14  
    15  // This test is just to make sure we are resilient to failures when a restart or
    16  // signal is triggered and the task is not running.
    17  func TestTaskRunner_RestartSignalTask_NotRunning(t *testing.T) {
    18  	alloc := mock.Alloc()
    19  	task := alloc.Job.TaskGroups[0].Tasks[0]
    20  	task.Driver = "mock_driver"
    21  	task.Config = map[string]interface{}{
    22  		"exit_code": "0",
    23  		"run_for":   "100s",
    24  	}
    25  
    26  	// Use vault to block the start
    27  	task.Vault = &structs.Vault{Policies: []string{"default"}}
    28  
    29  	ctx := testTaskRunnerFromAlloc(t, true, alloc)
    30  	ctx.tr.MarkReceived()
    31  	defer ctx.Cleanup()
    32  
    33  	// Control when we get a Vault token
    34  	token := "1234"
    35  	waitCh := make(chan struct{})
    36  	defer close(waitCh)
    37  	handler := func(*structs.Allocation, []string) (map[string]string, error) {
    38  		<-waitCh
    39  		return map[string]string{task.Name: token}, nil
    40  	}
    41  	ctx.tr.vaultClient.(*vaultclient.MockVaultClient).DeriveTokenFn = handler
    42  	go ctx.tr.Run()
    43  
    44  	select {
    45  	case <-ctx.tr.WaitCh():
    46  		t.Fatalf("premature exit")
    47  	case <-time.After(1 * time.Second):
    48  	}
    49  
    50  	// Send a signal and restart
    51  	if err := ctx.tr.Signal("test", "don't panic", syscall.SIGCHLD); err != nil {
    52  		t.Fatalf("Signalling errored: %v", err)
    53  	}
    54  
    55  	// Send a restart
    56  	ctx.tr.Restart("test", "don't panic", false)
    57  
    58  	if len(ctx.upd.events) != 2 {
    59  		t.Fatalf("should have 2 ctx.updates: %#v", ctx.upd.events)
    60  	}
    61  
    62  	if ctx.upd.state != structs.TaskStatePending {
    63  		t.Fatalf("TaskState %v; want %v", ctx.upd.state, structs.TaskStatePending)
    64  	}
    65  
    66  	if ctx.upd.events[0].Type != structs.TaskReceived {
    67  		t.Fatalf("First Event was %v; want %v", ctx.upd.events[0].Type, structs.TaskReceived)
    68  	}
    69  
    70  	if ctx.upd.events[1].Type != structs.TaskSetup {
    71  		t.Fatalf("Second Event was %v; want %v", ctx.upd.events[1].Type, structs.TaskSetup)
    72  	}
    73  }