github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/client/driver/raw_exec_unix_test.go (about)

     1  // +build !windows
     2  
     3  package driver
     4  
     5  import (
     6  	"io/ioutil"
     7  	"path/filepath"
     8  	"runtime"
     9  	"strings"
    10  	"syscall"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/hashicorp/nomad/helper/testtask"
    15  	"github.com/hashicorp/nomad/nomad/structs"
    16  	"github.com/hashicorp/nomad/testutil"
    17  )
    18  
    19  func TestRawExecDriver_User(t *testing.T) {
    20  	t.Parallel()
    21  	if runtime.GOOS != "linux" {
    22  		t.Skip("Linux only test")
    23  	}
    24  	task := &structs.Task{
    25  		Name:   "sleep",
    26  		Driver: "raw_exec",
    27  		User:   "alice",
    28  		Config: map[string]interface{}{
    29  			"command": testtask.Path(),
    30  			"args":    []string{"sleep", "45s"},
    31  		},
    32  		LogConfig: &structs.LogConfig{
    33  			MaxFiles:      10,
    34  			MaxFileSizeMB: 10,
    35  		},
    36  		Resources: basicResources,
    37  	}
    38  	testtask.SetTaskEnv(task)
    39  
    40  	ctx := testDriverContexts(t, task)
    41  	defer ctx.AllocDir.Destroy()
    42  	d := NewRawExecDriver(ctx.DriverCtx)
    43  
    44  	if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
    45  		t.Fatalf("prestart err: %v", err)
    46  	}
    47  	resp, err := d.Start(ctx.ExecCtx, task)
    48  	if err == nil {
    49  		resp.Handle.Kill()
    50  		t.Fatalf("Should've failed")
    51  	}
    52  	msg := "unknown user alice"
    53  	if !strings.Contains(err.Error(), msg) {
    54  		t.Fatalf("Expecting '%v' in '%v'", msg, err)
    55  	}
    56  }
    57  
    58  func TestRawExecDriver_Signal(t *testing.T) {
    59  	t.Parallel()
    60  	task := &structs.Task{
    61  		Name:   "signal",
    62  		Driver: "raw_exec",
    63  		Config: map[string]interface{}{
    64  			"command": "/bin/bash",
    65  			"args":    []string{"test.sh"},
    66  		},
    67  		LogConfig: &structs.LogConfig{
    68  			MaxFiles:      10,
    69  			MaxFileSizeMB: 10,
    70  		},
    71  		Resources:   basicResources,
    72  		KillTimeout: 10 * time.Second,
    73  	}
    74  
    75  	ctx := testDriverContexts(t, task)
    76  	defer ctx.AllocDir.Destroy()
    77  	d := NewRawExecDriver(ctx.DriverCtx)
    78  
    79  	testFile := filepath.Join(ctx.ExecCtx.TaskDir.Dir, "test.sh")
    80  	testData := []byte(`
    81  at_term() {
    82      echo 'Terminated.'
    83      exit 3
    84  }
    85  trap at_term USR1
    86  while true; do
    87      sleep 1
    88  done
    89  	`)
    90  	if err := ioutil.WriteFile(testFile, testData, 0777); err != nil {
    91  		t.Fatalf("Failed to write data: %v", err)
    92  	}
    93  
    94  	if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
    95  		t.Fatalf("prestart err: %v", err)
    96  	}
    97  	resp, err := d.Start(ctx.ExecCtx, task)
    98  	if err != nil {
    99  		t.Fatalf("err: %v", err)
   100  	}
   101  
   102  	go func() {
   103  		time.Sleep(100 * time.Millisecond)
   104  		err := resp.Handle.Signal(syscall.SIGUSR1)
   105  		if err != nil {
   106  			t.Fatalf("err: %v", err)
   107  		}
   108  	}()
   109  
   110  	// Task should terminate quickly
   111  	select {
   112  	case res := <-resp.Handle.WaitCh():
   113  		if res.Successful() {
   114  			t.Fatal("should err")
   115  		}
   116  	case <-time.After(time.Duration(testutil.TestMultiplier()*6) * time.Second):
   117  		t.Fatalf("timeout")
   118  	}
   119  
   120  	// Check the log file to see it exited because of the signal
   121  	outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "signal.stdout.0")
   122  	act, err := ioutil.ReadFile(outputFile)
   123  	if err != nil {
   124  		t.Fatalf("Couldn't read expected output: %v", err)
   125  	}
   126  
   127  	exp := "Terminated."
   128  	if strings.TrimSpace(string(act)) != exp {
   129  		t.Logf("Read from %v", outputFile)
   130  		t.Fatalf("Command outputted %v; want %v", act, exp)
   131  	}
   132  }