2015-09-04 04:00:16 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2015-11-17 13:12:49 +00:00
|
|
|
"encoding/json"
|
2015-10-15 23:59:08 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2015-11-17 13:12:49 +00:00
|
|
|
"strings"
|
2015-09-04 04:00:16 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-11-10 23:54:31 +00:00
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
2015-09-04 04:00:16 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2015-10-15 23:59:08 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/environment"
|
2015-11-14 06:07:13 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/driver/structs"
|
2015-09-04 04:00:16 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2015-10-07 00:53:05 +00:00
|
|
|
func testDockerDriverContext(task string) *DriverContext {
|
|
|
|
cfg := testConfig()
|
|
|
|
cfg.DevMode = true
|
|
|
|
return NewDriverContext(task, cfg, cfg.Node, testLogger())
|
|
|
|
}
|
|
|
|
|
2015-11-10 23:54:31 +00:00
|
|
|
// dockerIsConnected checks to see if a docker daemon is available (local or remote)
|
2015-11-11 01:43:08 +00:00
|
|
|
func dockerIsConnected(t *testing.T) bool {
|
2015-11-10 23:54:31 +00:00
|
|
|
client, err := docker.NewClientFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-11 01:48:06 +00:00
|
|
|
// Creating a client doesn't actually connect, so make sure we do something
|
|
|
|
// like call Version() on it.
|
2015-11-10 23:54:31 +00:00
|
|
|
env, err := client.Version()
|
|
|
|
if err != nil {
|
2015-11-11 01:43:08 +00:00
|
|
|
t.Logf("Failed to connect to docker daemon: %s", err)
|
2015-11-10 23:54:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-11 01:43:08 +00:00
|
|
|
t.Logf("Successfully connected to docker daemon running version %s", env.Get("Version"))
|
2015-11-10 23:54:31 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-11-11 01:43:08 +00:00
|
|
|
func dockerIsRemote(t *testing.T) bool {
|
2015-11-10 23:54:31 +00:00
|
|
|
client, err := docker.NewClientFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Technically this could be a local tcp socket but for testing purposes
|
|
|
|
// we'll just assume that tcp is only used for remote connections.
|
|
|
|
if client.Endpoint()[0:3] == "tcp" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2015-09-25 23:49:26 +00:00
|
|
|
}
|
2015-09-04 04:00:16 +00:00
|
|
|
|
|
|
|
func TestDockerDriver_Handle(t *testing.T) {
|
|
|
|
h := &dockerHandle{
|
|
|
|
imageID: "imageid",
|
|
|
|
containerID: "containerid",
|
|
|
|
doneCh: make(chan struct{}),
|
2015-11-14 06:07:13 +00:00
|
|
|
waitCh: make(chan *cstructs.WaitResult, 1),
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := h.ID()
|
|
|
|
expected := `DOCKER:{"ImageID":"imageid","ContainerID":"containerid"}`
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("Expected `%s`, found `%s`", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 23:54:31 +00:00
|
|
|
// This test should always pass, even if docker daemon is not available
|
2015-09-04 04:00:16 +00:00
|
|
|
func TestDockerDriver_Fingerprint(t *testing.T) {
|
2015-10-07 00:53:05 +00:00
|
|
|
d := NewDockerDriver(testDockerDriverContext(""))
|
2015-09-04 04:00:16 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
apply, err := d.Fingerprint(&config.Config{}, node)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-11-11 01:43:08 +00:00
|
|
|
if apply != dockerIsConnected(t) {
|
|
|
|
t.Fatalf("Fingerprinter should detect when docker is available")
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
2015-10-12 20:15:37 +00:00
|
|
|
if node.Attributes["driver.docker"] != "1" {
|
2015-11-11 01:43:08 +00:00
|
|
|
t.Log("Docker daemon not available. The remainder of the docker tests will be skipped.")
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
|
|
|
t.Logf("Found docker version %s", node.Attributes["driver.docker.version"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_StartOpen_Wait(t *testing.T) {
|
2015-11-11 01:43:08 +00:00
|
|
|
if !dockerIsConnected(t) {
|
2015-09-04 04:00:16 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
task := &structs.Task{
|
2015-10-07 00:53:05 +00:00
|
|
|
Name: "redis-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-09-25 23:49:26 +00:00
|
|
|
"image": "redis",
|
2015-09-04 04:00:16 +00:00
|
|
|
},
|
2015-09-24 07:59:57 +00:00
|
|
|
Resources: basicResources,
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
2015-09-25 23:49:14 +00:00
|
|
|
|
2015-10-07 00:53:05 +00:00
|
|
|
driverCtx := testDockerDriverContext(task.Name)
|
2015-09-25 23:49:14 +00:00
|
|
|
ctx := testDriverExecContext(task, driverCtx)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(driverCtx)
|
|
|
|
|
2015-09-04 04:00:16 +00:00
|
|
|
handle, err := d.Start(ctx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
defer handle.Kill()
|
|
|
|
|
|
|
|
// Attempt to open
|
|
|
|
handle2, err := d.Open(ctx, handle.ID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle2 == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_Start_Wait(t *testing.T) {
|
2015-11-11 01:43:08 +00:00
|
|
|
if !dockerIsConnected(t) {
|
2015-09-04 04:00:16 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
task := &structs.Task{
|
2015-10-07 00:53:05 +00:00
|
|
|
Name: "redis-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-09-26 00:35:33 +00:00
|
|
|
"image": "redis",
|
2015-10-15 23:59:08 +00:00
|
|
|
"command": "redis-server",
|
2015-11-18 23:16:42 +00:00
|
|
|
"args": []string{"-v"},
|
2015-09-04 04:00:16 +00:00
|
|
|
},
|
2015-09-09 08:08:31 +00:00
|
|
|
Resources: &structs.Resources{
|
2015-09-25 23:49:26 +00:00
|
|
|
MemoryMB: 256,
|
2015-09-09 08:08:31 +00:00
|
|
|
CPU: 512,
|
|
|
|
},
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
2015-09-25 23:49:14 +00:00
|
|
|
|
2015-10-07 00:53:05 +00:00
|
|
|
driverCtx := testDockerDriverContext(task.Name)
|
2015-09-25 23:49:14 +00:00
|
|
|
ctx := testDriverExecContext(task, driverCtx)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(driverCtx)
|
|
|
|
|
2015-09-04 04:00:16 +00:00
|
|
|
handle, err := d.Start(ctx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
defer handle.Kill()
|
|
|
|
|
|
|
|
// Update should be a no-op
|
|
|
|
err = handle.Update(task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-11-14 06:07:13 +00:00
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
2015-09-26 00:35:33 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-09-04 04:00:16 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 23:59:08 +00:00
|
|
|
func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) {
|
2015-11-11 01:48:06 +00:00
|
|
|
// This test requires that the alloc dir be mounted into docker as a volume.
|
|
|
|
// Because this cannot happen when docker is run remotely, e.g. when running
|
|
|
|
// docker in a VM, we skip this when we detect Docker is being run remotely.
|
2015-11-11 01:43:08 +00:00
|
|
|
if !dockerIsConnected(t) || dockerIsRemote(t) {
|
2015-10-15 23:59:08 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := []byte{'w', 'i', 'n'}
|
|
|
|
file := "output.txt"
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "redis-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-10-15 23:59:08 +00:00
|
|
|
"image": "redis",
|
|
|
|
"command": "/bin/bash",
|
2015-11-18 23:16:42 +00:00
|
|
|
"args": []string{
|
|
|
|
"-c",
|
|
|
|
fmt.Sprintf(`sleep 1; echo -n %s > $%s/%s`,
|
|
|
|
string(exp), environment.AllocDir, file),
|
|
|
|
},
|
2015-10-15 23:59:08 +00:00
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
driverCtx := testDockerDriverContext(task.Name)
|
|
|
|
ctx := testDriverExecContext(task, driverCtx)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(driverCtx)
|
|
|
|
|
|
|
|
handle, err := d.Start(ctx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
defer handle.Kill()
|
|
|
|
|
|
|
|
select {
|
2015-11-14 06:07:13 +00:00
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
2015-10-15 23:59:08 +00:00
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that data was written to the shared alloc directory.
|
|
|
|
outputFile := filepath.Join(ctx.AllocDir.SharedDir, file)
|
|
|
|
act, err := ioutil.ReadFile(outputFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't read expected output: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("Command outputted %v; want %v", act, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 04:00:16 +00:00
|
|
|
func TestDockerDriver_Start_Kill_Wait(t *testing.T) {
|
2015-11-11 01:43:08 +00:00
|
|
|
if !dockerIsConnected(t) {
|
2015-09-04 04:00:16 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
task := &structs.Task{
|
2015-10-07 00:53:05 +00:00
|
|
|
Name: "redis-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-09-26 05:43:19 +00:00
|
|
|
"image": "redis",
|
2015-10-15 23:59:08 +00:00
|
|
|
"command": "/bin/sleep",
|
2015-11-18 23:16:42 +00:00
|
|
|
"args": []string{"10"},
|
2015-09-04 04:00:16 +00:00
|
|
|
},
|
2015-09-24 07:59:57 +00:00
|
|
|
Resources: basicResources,
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
2015-09-25 23:49:14 +00:00
|
|
|
|
2015-10-07 00:53:05 +00:00
|
|
|
driverCtx := testDockerDriverContext(task.Name)
|
2015-09-25 23:49:14 +00:00
|
|
|
ctx := testDriverExecContext(task, driverCtx)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(driverCtx)
|
|
|
|
|
2015-09-04 04:00:16 +00:00
|
|
|
handle, err := d.Start(ctx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
defer handle.Kill()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
err := handle.Kill()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-11-14 06:07:13 +00:00
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if res.Successful() {
|
|
|
|
t.Fatalf("should err: %v", res)
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
2015-09-26 05:43:19 +00:00
|
|
|
case <-time.After(10 * time.Second):
|
2015-09-04 04:00:16 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 23:49:26 +00:00
|
|
|
|
2015-09-26 00:35:33 +00:00
|
|
|
func taskTemplate() *structs.Task {
|
|
|
|
return &structs.Task{
|
2015-10-15 23:59:08 +00:00
|
|
|
Name: "redis-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-09-25 23:49:26 +00:00
|
|
|
"image": "redis",
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
|
|
|
IP: "127.0.0.1",
|
2015-11-15 08:37:00 +00:00
|
|
|
ReservedPorts: []structs.Port{{"main", 11110}},
|
2015-11-18 03:21:36 +00:00
|
|
|
DynamicPorts: []structs.Port{{"REDIS", 43330}},
|
2015-09-25 23:49:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-09-26 00:35:33 +00:00
|
|
|
}
|
2015-09-25 23:49:26 +00:00
|
|
|
|
2015-09-26 00:35:33 +00:00
|
|
|
func TestDocker_StartN(t *testing.T) {
|
2015-11-11 01:43:08 +00:00
|
|
|
if !dockerIsConnected(t) {
|
2015-10-06 07:27:05 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2015-09-26 00:35:33 +00:00
|
|
|
|
|
|
|
task1 := taskTemplate()
|
2015-11-15 08:37:00 +00:00
|
|
|
task1.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 11110}
|
2015-11-18 03:21:36 +00:00
|
|
|
task1.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43331}
|
2015-09-26 00:35:33 +00:00
|
|
|
|
|
|
|
task2 := taskTemplate()
|
2015-11-15 08:37:00 +00:00
|
|
|
task2.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 22222}
|
2015-11-18 03:21:36 +00:00
|
|
|
task2.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43332}
|
2015-09-26 00:35:33 +00:00
|
|
|
|
|
|
|
task3 := taskTemplate()
|
2015-11-15 08:37:00 +00:00
|
|
|
task3.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 33333}
|
2015-11-18 03:21:36 +00:00
|
|
|
task3.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43333}
|
2015-09-26 00:35:33 +00:00
|
|
|
|
|
|
|
taskList := []*structs.Task{task1, task2, task3}
|
2015-09-25 23:49:26 +00:00
|
|
|
|
2015-09-26 00:35:33 +00:00
|
|
|
handles := make([]DriverHandle, len(taskList))
|
|
|
|
|
2015-10-06 23:33:02 +00:00
|
|
|
t.Logf("==> Starting %d tasks", len(taskList))
|
2015-09-26 00:35:33 +00:00
|
|
|
|
|
|
|
// Let's spin up a bunch of things
|
|
|
|
var err error
|
|
|
|
for idx, task := range taskList {
|
2015-10-07 00:53:05 +00:00
|
|
|
driverCtx := testDockerDriverContext(task.Name)
|
2015-09-26 01:35:23 +00:00
|
|
|
ctx := testDriverExecContext(task, driverCtx)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(driverCtx)
|
|
|
|
|
2015-09-26 00:35:33 +00:00
|
|
|
handles[idx], err = d.Start(ctx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed starting task #%d: %s", idx+1, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("==> All tasks are started. Terminating...")
|
|
|
|
|
|
|
|
for idx, handle := range handles {
|
2015-10-15 23:59:08 +00:00
|
|
|
if handle == nil {
|
|
|
|
t.Errorf("Bad handle for task #%d", idx+1)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-26 00:35:33 +00:00
|
|
|
err := handle.Kill()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed stopping task #%d: %s", idx+1, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("==> Test complete!")
|
2015-09-25 23:49:26 +00:00
|
|
|
}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
|
|
|
func TestDocker_StartNVersions(t *testing.T) {
|
2015-11-11 01:43:08 +00:00
|
|
|
if !dockerIsConnected(t) {
|
2015-10-06 07:27:05 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
|
|
|
task1 := taskTemplate()
|
|
|
|
task1.Config["image"] = "redis"
|
2015-11-15 08:37:00 +00:00
|
|
|
task1.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 11110}
|
2015-11-18 03:21:36 +00:00
|
|
|
task1.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43331}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
|
|
|
task2 := taskTemplate()
|
|
|
|
task2.Config["image"] = "redis:latest"
|
2015-11-15 08:37:00 +00:00
|
|
|
task2.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 22222}
|
2015-11-18 03:21:36 +00:00
|
|
|
task2.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43332}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
|
|
|
task3 := taskTemplate()
|
|
|
|
task3.Config["image"] = "redis:3.0"
|
2015-11-15 08:37:00 +00:00
|
|
|
task3.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 33333}
|
2015-11-18 03:21:36 +00:00
|
|
|
task3.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43333}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
|
|
|
taskList := []*structs.Task{task1, task2, task3}
|
|
|
|
|
|
|
|
handles := make([]DriverHandle, len(taskList))
|
|
|
|
|
2015-10-06 23:33:02 +00:00
|
|
|
t.Logf("==> Starting %d tasks", len(taskList))
|
2015-09-26 00:38:07 +00:00
|
|
|
|
|
|
|
// Let's spin up a bunch of things
|
|
|
|
var err error
|
|
|
|
for idx, task := range taskList {
|
2015-10-07 00:53:05 +00:00
|
|
|
driverCtx := testDockerDriverContext(task.Name)
|
2015-09-26 01:35:23 +00:00
|
|
|
ctx := testDriverExecContext(task, driverCtx)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(driverCtx)
|
|
|
|
|
2015-09-26 00:38:07 +00:00
|
|
|
handles[idx], err = d.Start(ctx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed starting task #%d: %s", idx+1, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("==> All tasks are started. Terminating...")
|
|
|
|
|
|
|
|
for idx, handle := range handles {
|
2015-10-15 23:59:08 +00:00
|
|
|
if handle == nil {
|
|
|
|
t.Errorf("Bad handle for task #%d", idx+1)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-26 00:38:07 +00:00
|
|
|
err := handle.Kill()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed stopping task #%d: %s", idx+1, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("==> Test complete!")
|
|
|
|
}
|
2015-10-01 02:15:24 +00:00
|
|
|
|
|
|
|
func TestDockerHostNet(t *testing.T) {
|
2015-11-11 01:43:08 +00:00
|
|
|
if !dockerIsConnected(t) {
|
2015-10-06 07:27:05 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
2015-10-02 17:54:04 +00:00
|
|
|
task := &structs.Task{
|
2015-10-15 23:59:08 +00:00
|
|
|
Name: "redis-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-10-02 17:54:04 +00:00
|
|
|
"image": "redis",
|
|
|
|
"network_mode": "host",
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
}
|
2015-10-07 00:53:05 +00:00
|
|
|
driverCtx := testDockerDriverContext(task.Name)
|
2015-10-01 02:15:24 +00:00
|
|
|
ctx := testDriverExecContext(task, driverCtx)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(driverCtx)
|
|
|
|
|
|
|
|
handle, err := d.Start(ctx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
defer handle.Kill()
|
|
|
|
}
|
2015-11-17 13:12:49 +00:00
|
|
|
|
|
|
|
func TestDockerLabels(t *testing.T) {
|
|
|
|
if !dockerIsConnected(t) {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
task := taskTemplate()
|
|
|
|
task.Config["labels"] = []map[string]string{
|
|
|
|
map[string]string{
|
|
|
|
"label1": "value1",
|
|
|
|
"label2": "value2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
driverCtx := testDockerDriverContext(task.Name)
|
|
|
|
ctx := testDriverExecContext(task, driverCtx)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(driverCtx)
|
|
|
|
|
|
|
|
handle, err := d.Start(ctx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
2015-11-18 06:02:23 +00:00
|
|
|
defer handle.Kill()
|
2015-11-17 13:12:49 +00:00
|
|
|
|
|
|
|
client, err := docker.NewClientFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't know if is queriable in a clean way
|
|
|
|
parts := strings.SplitN(handle.ID(), ":", 2)
|
|
|
|
var pid dockerPID
|
|
|
|
err = json.Unmarshal([]byte(parts[1]), &pid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := client.InspectContainer(pid.ContainerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want, got := 2, len(container.Config.Labels); want != got {
|
|
|
|
t.Errorf("Wrong labels count for docker job. Expect: %d, got: %d", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want, got := "value1", container.Config.Labels["label1"]; want != got {
|
|
|
|
t.Errorf("Wrong label value docker job. Expect: %s, got: %s", want, got)
|
|
|
|
}
|
|
|
|
}
|
2015-11-18 05:41:00 +00:00
|
|
|
|
|
|
|
func TestDockerDNS(t *testing.T) {
|
|
|
|
if !dockerIsConnected(t) {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
task := taskTemplate()
|
|
|
|
task.Config["dns_servers"] = []string{"8.8.8.8", "8.8.4.4"}
|
2015-11-18 06:02:23 +00:00
|
|
|
task.Config["dns_search_domains"] = []string{"example.com", "example.org", "example.net"}
|
2015-11-18 05:41:00 +00:00
|
|
|
|
|
|
|
driverCtx := testDockerDriverContext(task.Name)
|
|
|
|
ctx := testDriverExecContext(task, driverCtx)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(driverCtx)
|
|
|
|
|
|
|
|
handle, err := d.Start(ctx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
2015-11-18 06:02:23 +00:00
|
|
|
defer handle.Kill()
|
2015-11-18 05:41:00 +00:00
|
|
|
|
|
|
|
client, err := docker.NewClientFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't know if is queriable in a clean way
|
|
|
|
parts := strings.SplitN(handle.ID(), ":", 2)
|
|
|
|
var pid dockerPID
|
|
|
|
err = json.Unmarshal([]byte(parts[1]), &pid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := client.InspectContainer(pid.ContainerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-11-18 06:02:23 +00:00
|
|
|
if !reflect.DeepEqual(task.Config["dns_servers"], container.HostConfig.DNS) {
|
|
|
|
t.Errorf("DNS Servers don't match.\nExpected:\n%s\nGot:\n%s\n", task.Config["dns_servers"], container.HostConfig.DNS)
|
2015-11-18 05:41:00 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 06:02:23 +00:00
|
|
|
if !reflect.DeepEqual(task.Config["dns_search_domains"], container.HostConfig.DNSSearch) {
|
|
|
|
t.Errorf("DNS Servers don't match.\nExpected:\n%s\nGot:\n%s\n", task.Config["dns_search_domains"], container.HostConfig.DNSSearch)
|
2015-11-18 05:41:00 +00:00
|
|
|
}
|
|
|
|
}
|