github.com/ncodes/nomad@v0.5.7-0.20170403112158-97adf4a74fb3/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  	"github.com/ncodes/nomad/client/testutil"
    13  )
    14  
    15  func TestExecScriptCheckNoIsolation(t *testing.T) {
    16  	check := &ExecScriptCheck{
    17  		id:          "foo",
    18  		cmd:         "/bin/echo",
    19  		args:        []string{"hello", "world"},
    20  		taskDir:     "/tmp",
    21  		FSIsolation: false,
    22  	}
    23  
    24  	res := check.Run()
    25  	expectedOutput := "hello world"
    26  	expectedExitCode := 0
    27  	if res.Err != nil {
    28  		t.Fatalf("err: %v", res.Err)
    29  	}
    30  	if strings.TrimSpace(res.Output) != expectedOutput {
    31  		t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output)
    32  	}
    33  
    34  	if res.ExitCode != expectedExitCode {
    35  		t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode)
    36  	}
    37  }
    38  
    39  func TestDockerScriptCheck(t *testing.T) {
    40  	if !testutil.DockerIsConnected(t) {
    41  		return
    42  	}
    43  	client, err := docker.NewClientFromEnv()
    44  	if err != nil {
    45  		t.Fatalf("error creating docker client: %v", err)
    46  	}
    47  
    48  	if err := client.PullImage(docker.PullImageOptions{Repository: "busybox", Tag: "latest"},
    49  		docker.AuthConfiguration{}); err != nil {
    50  		t.Fatalf("error pulling redis: %v", err)
    51  	}
    52  
    53  	container, err := client.CreateContainer(docker.CreateContainerOptions{
    54  		Config: &docker.Config{
    55  			Image: "busybox",
    56  			Cmd:   []string{"/bin/sleep", "1000"},
    57  		},
    58  	})
    59  	if err != nil {
    60  		t.Fatalf("error creating container: %v", err)
    61  	}
    62  	defer removeContainer(client, container.ID)
    63  
    64  	if err := client.StartContainer(container.ID, container.HostConfig); err != nil {
    65  		t.Fatalf("error starting container: %v", err)
    66  	}
    67  
    68  	check := &DockerScriptCheck{
    69  		id:          "1",
    70  		interval:    5 * time.Second,
    71  		containerID: container.ID,
    72  		logger:      log.New(os.Stdout, "", log.LstdFlags),
    73  		cmd:         "/bin/echo",
    74  		args:        []string{"hello", "world"},
    75  	}
    76  
    77  	res := check.Run()
    78  	expectedOutput := "hello world"
    79  	expectedExitCode := 0
    80  	if res.Err != nil {
    81  		t.Fatalf("err: %v", res.Err)
    82  	}
    83  	if strings.TrimSpace(res.Output) != expectedOutput {
    84  		t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output)
    85  	}
    86  
    87  	if res.ExitCode != expectedExitCode {
    88  		t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode)
    89  	}
    90  }
    91  
    92  // removeContainer kills and removes a container
    93  func removeContainer(client *docker.Client, containerID string) {
    94  	client.KillContainer(docker.KillContainerOptions{ID: containerID})
    95  	client.RemoveContainer(docker.RemoveContainerOptions{ID: containerID, RemoveVolumes: true, Force: true})
    96  }