github.com/ncodes/nomad@v0.5.7-0.20170403112158-97adf4a74fb3/client/driver/executor/checks_linux_test.go (about)

     1  package executor
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	dstructs "github.com/ncodes/nomad/client/driver/structs"
    10  	"github.com/ncodes/nomad/client/testutil"
    11  )
    12  
    13  func TestExecScriptCheckWithIsolation(t *testing.T) {
    14  	testutil.ExecCompatible(t)
    15  
    16  	execCmd := ExecCommand{Cmd: "/bin/echo", Args: []string{"hello world"}}
    17  	ctx, allocDir := testExecutorContextWithChroot(t)
    18  	defer allocDir.Destroy()
    19  
    20  	execCmd.FSIsolation = true
    21  	execCmd.ResourceLimits = true
    22  	execCmd.User = dstructs.DefaultUnpriviledgedUser
    23  
    24  	executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
    25  
    26  	if err := executor.SetContext(ctx); err != nil {
    27  		t.Fatalf("Unexpected error")
    28  	}
    29  
    30  	_, err := executor.LaunchCmd(&execCmd)
    31  	if err != nil {
    32  		t.Fatalf("error in launching command: %v", err)
    33  	}
    34  
    35  	check := &ExecScriptCheck{
    36  		id:          "foo",
    37  		cmd:         "/bin/echo",
    38  		args:        []string{"hello", "world"},
    39  		taskDir:     ctx.TaskDir,
    40  		FSIsolation: true,
    41  	}
    42  
    43  	res := check.Run()
    44  	expectedOutput := "hello world"
    45  	expectedExitCode := 0
    46  	if res.Err != nil {
    47  		t.Fatalf("err: %v", res.Err)
    48  	}
    49  	if strings.TrimSpace(res.Output) != expectedOutput {
    50  		t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output)
    51  	}
    52  
    53  	if res.ExitCode != expectedExitCode {
    54  		t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode)
    55  	}
    56  }