github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/client/driver/executor/checks_test.go (about)

     1  package executor
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	docker "github.com/fsouza/go-dockerclient"
    11  
    12  	cstructs "github.com/hashicorp/nomad/client/driver/structs"
    13  	"github.com/hashicorp/nomad/client/testutil"
    14  )
    15  
    16  func TestExecScriptCheckNoIsolation(t *testing.T) {
    17  	check := &ExecScriptCheck{
    18  		id:          "foo",
    19  		cmd:         "/bin/echo",
    20  		args:        []string{"hello", "world"},
    21  		taskDir:     "/tmp",
    22  		FSIsolation: false,
    23  	}
    24  
    25  	res := check.Run()
    26  	expectedOutput := "hello world"
    27  	expectedExitCode := 0
    28  	if res.Err != nil {
    29  		t.Fatalf("err: %v", res.Err)
    30  	}
    31  	if strings.TrimSpace(res.Output) != expectedOutput {
    32  		t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output)
    33  	}
    34  
    35  	if res.ExitCode != expectedExitCode {
    36  		t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode)
    37  	}
    38  }
    39  
    40  func TestExecScriptCheckWithIsolation(t *testing.T) {
    41  	testutil.ExecCompatible(t)
    42  
    43  	execCmd := ExecCommand{Cmd: "/bin/echo", Args: []string{"hello world"}}
    44  	ctx := testExecutorContext(t)
    45  	defer ctx.AllocDir.Destroy()
    46  
    47  	execCmd.FSIsolation = true
    48  	execCmd.ResourceLimits = true
    49  	execCmd.User = cstructs.DefaultUnpriviledgedUser
    50  
    51  	executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
    52  	_, err := executor.LaunchCmd(&execCmd, ctx)
    53  	if err != nil {
    54  		t.Fatalf("error in launching command: %v", err)
    55  	}
    56  
    57  	check := &ExecScriptCheck{
    58  		id:          "foo",
    59  		cmd:         "/bin/echo",
    60  		args:        []string{"hello", "world"},
    61  		taskDir:     ctx.AllocDir.TaskDirs["web"],
    62  		FSIsolation: true,
    63  	}
    64  
    65  	res := check.Run()
    66  	expectedOutput := "hello world"
    67  	expectedExitCode := 0
    68  	if res.Err != nil {
    69  		t.Fatalf("err: %v", res.Err)
    70  	}
    71  	if strings.TrimSpace(res.Output) != expectedOutput {
    72  		t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output)
    73  	}
    74  
    75  	if res.ExitCode != expectedExitCode {
    76  		t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode)
    77  	}
    78  }
    79  
    80  func TestDockerScriptCheck(t *testing.T) {
    81  	if !testutil.DockerIsConnected(t) {
    82  		return
    83  	}
    84  	client, err := docker.NewClientFromEnv()
    85  	if err != nil {
    86  		t.Fatalf("error creating docker client: %v", err)
    87  	}
    88  
    89  	if err := client.PullImage(docker.PullImageOptions{Repository: "busybox", Tag: "latest"},
    90  		docker.AuthConfiguration{}); err != nil {
    91  		t.Fatalf("error pulling redis: %v", err)
    92  	}
    93  
    94  	container, err := client.CreateContainer(docker.CreateContainerOptions{
    95  		Config: &docker.Config{
    96  			Image: "busybox",
    97  			Cmd:   []string{"/bin/sleep", "1000"},
    98  		},
    99  	})
   100  	if err != nil {
   101  		t.Fatalf("error creating container: %v", err)
   102  	}
   103  	defer removeContainer(client, container.ID)
   104  
   105  	if err := client.StartContainer(container.ID, container.HostConfig); err != nil {
   106  		t.Fatalf("error starting container", err)
   107  	}
   108  
   109  	check := &DockerScriptCheck{
   110  		id:          "1",
   111  		interval:    5 * time.Second,
   112  		containerID: container.ID,
   113  		logger:      log.New(os.Stdout, "", log.LstdFlags),
   114  		cmd:         "/bin/echo",
   115  		args:        []string{"hello", "world"},
   116  	}
   117  
   118  	res := check.Run()
   119  	expectedOutput := "hello world"
   120  	expectedExitCode := 0
   121  	if res.Err != nil {
   122  		t.Fatalf("err: %v", res.Err)
   123  	}
   124  	if strings.TrimSpace(res.Output) != expectedOutput {
   125  		t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output)
   126  	}
   127  
   128  	if res.ExitCode != expectedExitCode {
   129  		t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode)
   130  	}
   131  }
   132  
   133  // removeContainer kills and removes a container
   134  func removeContainer(client *docker.Client, containerID string) {
   135  	client.KillContainer(docker.KillContainerOptions{ID: containerID})
   136  	client.RemoveContainer(docker.RemoveContainerOptions{ID: containerID, RemoveVolumes: true, Force: true})
   137  }