github.com/ilhicas/nomad@v0.11.8/drivers/exec/driver_unix_test.go (about) 1 // +build darwin dragonfly freebsd linux netbsd openbsd solaris 2 3 package exec 4 5 import ( 6 "context" 7 "fmt" 8 "testing" 9 "time" 10 11 ctestutils "github.com/hashicorp/nomad/client/testutil" 12 "github.com/hashicorp/nomad/helper/testlog" 13 "github.com/hashicorp/nomad/helper/uuid" 14 "github.com/hashicorp/nomad/plugins/drivers" 15 dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils" 16 "github.com/hashicorp/nomad/testutil" 17 "github.com/stretchr/testify/require" 18 "golang.org/x/sys/unix" 19 ) 20 21 func TestExecDriver_StartWaitStop(t *testing.T) { 22 t.Parallel() 23 require := require.New(t) 24 ctestutils.ExecCompatible(t) 25 26 ctx, cancel := context.WithCancel(context.Background()) 27 defer cancel() 28 29 d := NewExecDriver(ctx, testlog.HCLogger(t)) 30 harness := dtestutil.NewDriverHarness(t, d) 31 task := &drivers.TaskConfig{ 32 ID: uuid.Generate(), 33 Name: "test", 34 Resources: testResources, 35 } 36 37 taskConfig := map[string]interface{}{ 38 "command": "/bin/sleep", 39 "args": []string{"600"}, 40 } 41 require.NoError(task.EncodeConcreteDriverConfig(&taskConfig)) 42 43 cleanup := harness.MkAllocDir(task, false) 44 defer cleanup() 45 46 handle, _, err := harness.StartTask(task) 47 require.NoError(err) 48 49 ch, err := harness.WaitTask(context.Background(), handle.Config.ID) 50 require.NoError(err) 51 52 require.NoError(harness.WaitUntilStarted(task.ID, 1*time.Second)) 53 54 go func() { 55 harness.StopTask(task.ID, 2*time.Second, "SIGINT") 56 }() 57 58 select { 59 case result := <-ch: 60 require.Equal(int(unix.SIGINT), result.Signal) 61 case <-time.After(10 * time.Second): 62 require.Fail("timeout waiting for task to shutdown") 63 } 64 65 // Ensure that the task is marked as dead, but account 66 // for WaitTask() closing channel before internal state is updated 67 testutil.WaitForResult(func() (bool, error) { 68 status, err := harness.InspectTask(task.ID) 69 if err != nil { 70 return false, fmt.Errorf("inspecting task failed: %v", err) 71 } 72 if status.State != drivers.TaskStateExited { 73 return false, fmt.Errorf("task hasn't exited yet; status: %v", status.State) 74 } 75 76 return true, nil 77 }, func(err error) { 78 require.NoError(err) 79 }) 80 81 require.NoError(harness.DestroyTask(task.ID, true)) 82 } 83 84 func TestExec_ExecTaskStreaming(t *testing.T) { 85 t.Parallel() 86 require := require.New(t) 87 88 ctx, cancel := context.WithCancel(context.Background()) 89 defer cancel() 90 91 d := NewExecDriver(ctx, testlog.HCLogger(t)) 92 harness := dtestutil.NewDriverHarness(t, d) 93 defer harness.Kill() 94 95 task := &drivers.TaskConfig{ 96 ID: uuid.Generate(), 97 Name: "sleep", 98 } 99 100 cleanup := harness.MkAllocDir(task, false) 101 defer cleanup() 102 103 tc := &TaskConfig{ 104 Command: "/bin/sleep", 105 Args: []string{"9000"}, 106 } 107 require.NoError(task.EncodeConcreteDriverConfig(&tc)) 108 109 _, _, err := harness.StartTask(task) 110 require.NoError(err) 111 defer d.DestroyTask(task.ID, true) 112 113 dtestutil.ExecTaskStreamingConformanceTests(t, harness, task.ID) 114 115 }