github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/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  	d := NewExecDriver(testlog.HCLogger(t))
    27  	harness := dtestutil.NewDriverHarness(t, d)
    28  	task := &drivers.TaskConfig{
    29  		ID:        uuid.Generate(),
    30  		Name:      "test",
    31  		Resources: testResources,
    32  	}
    33  
    34  	taskConfig := map[string]interface{}{
    35  		"command": "/bin/sleep",
    36  		"args":    []string{"600"},
    37  	}
    38  	require.NoError(task.EncodeConcreteDriverConfig(&taskConfig))
    39  
    40  	cleanup := harness.MkAllocDir(task, false)
    41  	defer cleanup()
    42  
    43  	handle, _, err := harness.StartTask(task)
    44  	require.NoError(err)
    45  
    46  	ch, err := harness.WaitTask(context.Background(), handle.Config.ID)
    47  	require.NoError(err)
    48  
    49  	require.NoError(harness.WaitUntilStarted(task.ID, 1*time.Second))
    50  
    51  	go func() {
    52  		harness.StopTask(task.ID, 2*time.Second, "SIGINT")
    53  	}()
    54  
    55  	select {
    56  	case result := <-ch:
    57  		require.Equal(int(unix.SIGINT), result.Signal)
    58  	case <-time.After(10 * time.Second):
    59  		require.Fail("timeout waiting for task to shutdown")
    60  	}
    61  
    62  	// Ensure that the task is marked as dead, but account
    63  	// for WaitTask() closing channel before internal state is updated
    64  	testutil.WaitForResult(func() (bool, error) {
    65  		status, err := harness.InspectTask(task.ID)
    66  		if err != nil {
    67  			return false, fmt.Errorf("inspecting task failed: %v", err)
    68  		}
    69  		if status.State != drivers.TaskStateExited {
    70  			return false, fmt.Errorf("task hasn't exited yet; status: %v", status.State)
    71  		}
    72  
    73  		return true, nil
    74  	}, func(err error) {
    75  		require.NoError(err)
    76  	})
    77  
    78  	require.NoError(harness.DestroyTask(task.ID, true))
    79  }
    80  
    81  func TestExec_ExecTaskStreaming(t *testing.T) {
    82  	t.Parallel()
    83  	require := require.New(t)
    84  
    85  	d := NewExecDriver(testlog.HCLogger(t))
    86  	harness := dtestutil.NewDriverHarness(t, d)
    87  	defer harness.Kill()
    88  
    89  	task := &drivers.TaskConfig{
    90  		ID:   uuid.Generate(),
    91  		Name: "sleep",
    92  	}
    93  
    94  	cleanup := harness.MkAllocDir(task, false)
    95  	defer cleanup()
    96  
    97  	tc := &TaskConfig{
    98  		Command: "/bin/sleep",
    99  		Args:    []string{"9000"},
   100  	}
   101  	require.NoError(task.EncodeConcreteDriverConfig(&tc))
   102  
   103  	_, _, err := harness.StartTask(task)
   104  	require.NoError(err)
   105  	defer d.DestroyTask(task.ID, true)
   106  
   107  	dtestutil.ExecTaskStreamingConformanceTests(t, harness, task.ID)
   108  
   109  }