2015-09-04 04:00:16 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2015-10-15 23:59:08 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-01-19 23:35:01 +00:00
|
|
|
"math/rand"
|
2015-10-15 23:59:08 +00:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2015-11-20 05:29:37 +00:00
|
|
|
"runtime/debug"
|
2016-07-09 01:27:51 +00:00
|
|
|
"strconv"
|
2016-03-23 11:57:31 +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"
|
2016-03-30 20:42:17 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2015-09-04 04:00:16 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2016-01-06 02:02:11 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/env"
|
2016-03-26 00:10:08 +00:00
|
|
|
"github.com/hashicorp/nomad/client/testutil"
|
2015-09-04 04:00:16 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-03-26 00:10:08 +00:00
|
|
|
tu "github.com/hashicorp/nomad/testutil"
|
2015-09-04 04:00:16 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2016-01-20 20:00:20 +00:00
|
|
|
// Ports used by tests
|
|
|
|
var (
|
|
|
|
docker_reserved = 32768 + int(rand.Int31n(25000))
|
|
|
|
docker_dynamic = 32768 + int(rand.Int31n(25000))
|
|
|
|
)
|
|
|
|
|
2016-01-19 23:35:01 +00:00
|
|
|
// Returns a task with a reserved and dynamic port. The ports are returned
|
|
|
|
// respectively.
|
|
|
|
func dockerTask() (*structs.Task, int, int) {
|
2016-01-20 20:00:20 +00:00
|
|
|
docker_reserved += 1
|
|
|
|
docker_dynamic += 1
|
2015-11-20 03:08:21 +00:00
|
|
|
return &structs.Task{
|
|
|
|
Name: "redis-demo",
|
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"image": "busybox",
|
|
|
|
"load": []string{"busybox.tar"},
|
|
|
|
"command": "/bin/nc",
|
2016-08-17 20:56:33 +00:00
|
|
|
"args": []string{"-l", "127.0.0.1", "-p", "0"},
|
2015-11-20 03:08:21 +00:00
|
|
|
},
|
2016-02-10 21:29:06 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-11-20 03:08:21 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
|
|
|
IP: "127.0.0.1",
|
2016-01-20 20:00:20 +00:00
|
|
|
ReservedPorts: []structs.Port{{"main", docker_reserved}},
|
|
|
|
DynamicPorts: []structs.Port{{"REDIS", docker_dynamic}},
|
2015-11-20 03:08:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-01-20 20:00:20 +00:00
|
|
|
}, docker_reserved, docker_dynamic
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dockerSetup does all of the basic setup you need to get a running docker
|
|
|
|
// process up and running for testing. Use like:
|
|
|
|
//
|
|
|
|
// task := taskTemplate()
|
|
|
|
// // do custom task configuration
|
|
|
|
// client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
// defer cleanup()
|
|
|
|
// // do test stuff
|
|
|
|
//
|
|
|
|
// If there is a problem during setup this function will abort or skip the test
|
|
|
|
// and indicate the reason.
|
|
|
|
func dockerSetup(t *testing.T, task *structs.Task) (*docker.Client, DriverHandle, func()) {
|
2016-03-26 00:10:08 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2015-11-20 03:08:21 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := docker.NewClientFromEnv()
|
|
|
|
if err != nil {
|
2015-11-20 05:29:37 +00:00
|
|
|
t.Fatalf("Failed to initialize client: %s\nStack\n%s", err, debug.Stack())
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
2015-11-20 03:08:21 +00:00
|
|
|
driver := NewDockerDriver(driverCtx)
|
2016-08-12 18:39:58 +00:00
|
|
|
copyImage(execCtx, task, "busybox.tar", t)
|
2015-11-20 03:08:21 +00:00
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handle, err := driver.Start(execCtx, task)
|
2015-11-20 03:08:21 +00:00
|
|
|
if err != nil {
|
2016-01-06 02:02:11 +00:00
|
|
|
execCtx.AllocDir.Destroy()
|
2015-11-20 05:29:37 +00:00
|
|
|
t.Fatalf("Failed to start driver: %s\nStack\n%s", err, debug.Stack())
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
if handle == nil {
|
2016-01-06 02:02:11 +00:00
|
|
|
execCtx.AllocDir.Destroy()
|
2015-11-20 05:29:37 +00:00
|
|
|
t.Fatalf("handle is nil\nStack\n%s", debug.Stack())
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := func() {
|
|
|
|
handle.Kill()
|
2016-01-06 02:02:11 +00:00
|
|
|
execCtx.AllocDir.Destroy()
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client, handle, cleanup
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-08-23 17:39:40 +00:00
|
|
|
driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo", Resources: basicResources})
|
2016-01-06 02:02:11 +00:00
|
|
|
d := NewDockerDriver(driverCtx)
|
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)
|
|
|
|
}
|
2016-03-26 00:10:08 +00:00
|
|
|
if apply != testutil.DockerIsConnected(t) {
|
2015-11-11 01:43:08 +00:00
|
|
|
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) {
|
2016-03-26 00:10:08 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2015-11-20 21:50:47 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
2015-09-04 04:00:16 +00:00
|
|
|
task := &structs.Task{
|
2016-08-12 02:23:48 +00:00
|
|
|
Name: "nc-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"load": []string{"busybox.tar"},
|
|
|
|
"image": "busybox",
|
|
|
|
"command": "/bin/nc",
|
2016-08-17 20:56:33 +00:00
|
|
|
"args": []string{"-l", "127.0.0.1", "-p", "0"},
|
2015-09-04 04:00:16 +00:00
|
|
|
},
|
2016-02-10 21:29:06 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
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
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2015-09-25 23:49:14 +00:00
|
|
|
d := NewDockerDriver(driverCtx)
|
2016-08-12 18:39:58 +00:00
|
|
|
copyImage(execCtx, task, "busybox.tar", t)
|
2015-09-25 23:49:14 +00:00
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handle, err := d.Start(execCtx, task)
|
2015-09-04 04:00:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
defer handle.Kill()
|
|
|
|
|
|
|
|
// Attempt to open
|
2016-01-06 02:02:11 +00:00
|
|
|
handle2, err := d.Open(execCtx, handle.ID())
|
2015-09-04 04:00:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle2 == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_Start_Wait(t *testing.T) {
|
|
|
|
task := &structs.Task{
|
2016-08-12 02:23:48 +00:00
|
|
|
Name: "nc-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"load": []string{"busybox.tar"},
|
|
|
|
"image": "busybox",
|
|
|
|
"command": "/bin/echo",
|
|
|
|
"args": []string{"hello"},
|
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,
|
|
|
|
},
|
2016-02-10 21:29:06 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
2015-09-25 23:49:14 +00:00
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
_, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
2015-09-04 04:00:16 +00:00
|
|
|
|
|
|
|
// Update should be a no-op
|
2015-11-20 03:08:21 +00:00
|
|
|
err := handle.Update(task)
|
2015-09-04 04:00:16 +00:00
|
|
|
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
|
|
|
}
|
2016-03-26 00:10:08 +00:00
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
|
2015-09-04 04:00:16 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:42:17 +00:00
|
|
|
func TestDockerDriver_Start_LoadImage(t *testing.T) {
|
2016-06-12 18:28:43 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2016-03-30 20:42:17 +00:00
|
|
|
task := &structs.Task{
|
|
|
|
Name: "busybox-demo",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
2016-03-31 00:21:07 +00:00
|
|
|
"load": []string{"busybox.tar"},
|
2016-03-30 20:42:17 +00:00
|
|
|
"command": "/bin/echo",
|
|
|
|
"args": []string{
|
|
|
|
"hello",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(driverCtx)
|
|
|
|
|
2016-08-17 22:10:30 +00:00
|
|
|
// Copy the image into the task's directory
|
|
|
|
copyImage(execCtx, task, "busybox.tar", t)
|
2016-03-30 20:42:17 +00:00
|
|
|
|
|
|
|
handle, err := d.Start(execCtx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
defer handle.Kill()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that data was written to the shared alloc directory.
|
|
|
|
outputFile := filepath.Join(execCtx.AllocDir.LogDir(), "busybox-demo.stdout.0")
|
|
|
|
act, err := ioutil.ReadFile(outputFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't read expected output: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := "hello"
|
|
|
|
if strings.TrimSpace(string(act)) != exp {
|
|
|
|
t.Fatalf("Command outputted %v; want %v", act, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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.
|
2016-03-26 00:10:08 +00:00
|
|
|
if !testutil.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{
|
2016-08-12 02:23:48 +00:00
|
|
|
Name: "nc-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"image": "busybox",
|
|
|
|
"load": []string{"busybox.tar"},
|
|
|
|
"command": "/bin/sh",
|
2015-11-18 23:16:42 +00:00
|
|
|
"args": []string{
|
|
|
|
"-c",
|
|
|
|
fmt.Sprintf(`sleep 1; echo -n %s > $%s/%s`,
|
2016-01-06 02:02:11 +00:00
|
|
|
string(exp), env.AllocDir, file),
|
2015-11-18 23:16:42 +00:00
|
|
|
},
|
2015-10-15 23:59:08 +00:00
|
|
|
},
|
2016-02-10 21:29:06 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-10-15 23:59:08 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2015-10-15 23:59:08 +00:00
|
|
|
d := NewDockerDriver(driverCtx)
|
2016-08-12 18:39:58 +00:00
|
|
|
copyImage(execCtx, task, "busybox.tar", t)
|
2015-10-15 23:59:08 +00:00
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handle, err := d.Start(execCtx, task)
|
2015-10-15 23:59:08 +00:00
|
|
|
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
|
|
|
}
|
2016-03-26 00:10:08 +00:00
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
|
2015-10-15 23:59:08 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that data was written to the shared alloc directory.
|
2016-01-06 02:02:11 +00:00
|
|
|
outputFile := filepath.Join(execCtx.AllocDir.SharedDir, file)
|
2015-10-15 23:59:08 +00:00
|
|
|
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) {
|
|
|
|
task := &structs.Task{
|
2016-08-12 02:23:48 +00:00
|
|
|
Name: "nc-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"image": "busybox",
|
|
|
|
"load": []string{"busybox.tar"},
|
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
|
|
|
},
|
2016-02-10 21:29:06 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
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-11-20 03:08:21 +00:00
|
|
|
_, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
2015-09-04 04:00:16 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-03-26 00:10:08 +00:00
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*10) * time.Second):
|
2015-09-04 04:00:16 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 23:49:26 +00:00
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
func TestDockerDriver_StartN(t *testing.T) {
|
2016-03-26 00:10:08 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2015-10-06 07:27:05 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2015-09-26 00:35:33 +00:00
|
|
|
|
2016-01-19 23:35:01 +00:00
|
|
|
task1, _, _ := dockerTask()
|
|
|
|
task2, _, _ := dockerTask()
|
|
|
|
task3, _, _ := dockerTask()
|
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))
|
|
|
|
|
2016-06-12 20:16:07 +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 {
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2015-09-26 01:35:23 +00:00
|
|
|
d := NewDockerDriver(driverCtx)
|
2016-08-12 18:39:58 +00:00
|
|
|
copyImage(execCtx, task, "busybox.tar", t)
|
2015-09-26 01:35:23 +00:00
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handles[idx], err = d.Start(execCtx, task)
|
2015-09-26 00:35:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed starting task #%d: %s", idx+1, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 20:16:07 +00:00
|
|
|
t.Log("All tasks are started. Terminating...")
|
2015-09-26 00:35:33 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 20:16:07 +00:00
|
|
|
t.Log("Test complete!")
|
2015-09-25 23:49:26 +00:00
|
|
|
}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
func TestDockerDriver_StartNVersions(t *testing.T) {
|
2016-03-26 00:10:08 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2015-10-06 07:27:05 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
2016-01-19 23:35:01 +00:00
|
|
|
task1, _, _ := dockerTask()
|
2016-08-12 02:23:48 +00:00
|
|
|
task1.Config["image"] = "busybox"
|
|
|
|
task1.Config["load"] = []string{"busybox.tar"}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
2016-01-19 23:35:01 +00:00
|
|
|
task2, _, _ := dockerTask()
|
2016-08-12 02:23:48 +00:00
|
|
|
task2.Config["image"] = "busybox:musl"
|
|
|
|
task2.Config["load"] = []string{"busybox_musl.tar"}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
2016-01-19 23:35:01 +00:00
|
|
|
task3, _, _ := dockerTask()
|
2016-08-12 02:23:48 +00:00
|
|
|
task3.Config["image"] = "busybox:glibc"
|
|
|
|
task3.Config["load"] = []string{"busybox_glibc.tar"}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
|
|
|
taskList := []*structs.Task{task1, task2, task3}
|
|
|
|
|
|
|
|
handles := make([]DriverHandle, len(taskList))
|
|
|
|
|
2016-06-12 20:16:07 +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 {
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2015-09-26 01:35:23 +00:00
|
|
|
d := NewDockerDriver(driverCtx)
|
2016-08-12 18:39:58 +00:00
|
|
|
copyImage(execCtx, task, "busybox.tar", t)
|
|
|
|
copyImage(execCtx, task, "busybox_musl.tar", t)
|
|
|
|
copyImage(execCtx, task, "busybox_glibc.tar", t)
|
2015-09-26 01:35:23 +00:00
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handles[idx], err = d.Start(execCtx, task)
|
2015-09-26 00:38:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed starting task #%d: %s", idx+1, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 20:16:07 +00:00
|
|
|
t.Log("All tasks are started. Terminating...")
|
2015-09-26 00:38:07 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 20:16:07 +00:00
|
|
|
t.Log("Test complete!")
|
2015-09-26 00:38:07 +00:00
|
|
|
}
|
2015-10-01 02:15:24 +00:00
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
func waitForExist(t *testing.T, client *docker.Client, handle *DockerHandle) {
|
|
|
|
tu.WaitForResult(func() (bool, error) {
|
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*docker.NoSuchContainer); !ok {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return container != nil, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_NetworkMode_Host(t *testing.T) {
|
2015-11-20 03:08:21 +00:00
|
|
|
expected := "host"
|
2015-10-06 07:27:05 +00:00
|
|
|
|
2015-10-02 17:54:04 +00:00
|
|
|
task := &structs.Task{
|
2016-08-12 02:23:48 +00:00
|
|
|
Name: "nc-demo",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"image": "busybox",
|
|
|
|
"load": []string{"busybox.tar"},
|
|
|
|
"command": "/bin/nc",
|
2016-08-17 20:56:33 +00:00
|
|
|
"args": []string{"-l", "127.0.0.1", "-p", "0"},
|
2015-11-20 03:08:21 +00:00
|
|
|
"network_mode": expected,
|
2015-10-02 17:54:04 +00:00
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
2016-02-10 21:29:06 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-10-02 17:54:04 +00:00
|
|
|
}
|
2015-10-01 02:15:24 +00:00
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
waitForExist(t, client, handle.(*DockerHandle))
|
2016-07-12 16:59:34 +00:00
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
|
2015-10-01 02:15:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-11-20 03:08:21 +00:00
|
|
|
|
|
|
|
actual := container.HostConfig.NetworkMode
|
|
|
|
if actual != expected {
|
2016-07-12 17:36:06 +00:00
|
|
|
t.Fatalf("Got network mode %q; want %q", expected, actual)
|
2015-10-01 02:15:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-17 13:12:49 +00:00
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
func TestDockerDriver_Labels(t *testing.T) {
|
2016-01-19 23:35:01 +00:00
|
|
|
task, _, _ := dockerTask()
|
2015-11-17 13:12:49 +00:00
|
|
|
task.Config["labels"] = []map[string]string{
|
|
|
|
map[string]string{
|
|
|
|
"label1": "value1",
|
|
|
|
"label2": "value2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
2015-11-17 13:12:49 +00:00
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
waitForExist(t, client, handle.(*DockerHandle))
|
|
|
|
|
2015-11-19 22:20:41 +00:00
|
|
|
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
|
2015-11-17 13:12:49 +00:00
|
|
|
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
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
func TestDockerDriver_DNS(t *testing.T) {
|
2016-01-19 23:35:01 +00:00
|
|
|
task, _, _ := dockerTask()
|
2015-11-18 05:41:00 +00:00
|
|
|
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
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
2015-11-18 05:41:00 +00:00
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
waitForExist(t, client, handle.(*DockerHandle))
|
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
|
2015-11-18 05:41:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-11-20 03:08:21 +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-20 03:08:21 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 14:35:49 +00:00
|
|
|
func TestDockerWorkDir(t *testing.T) {
|
|
|
|
task, _, _ := dockerTask()
|
|
|
|
task.Config["work_dir"] = "/some/path"
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want, got := "/some/path", container.Config.WorkingDir; want != got {
|
|
|
|
t.Errorf("Wrong working directory for docker job. Expect: %d, got: %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
func inSlice(needle string, haystack []string) bool {
|
|
|
|
for _, h := range haystack {
|
|
|
|
if h == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
func TestDockerDriver_PortsNoMap(t *testing.T) {
|
2016-01-19 23:35:01 +00:00
|
|
|
task, res, dyn := dockerTask()
|
2015-11-20 03:08:21 +00:00
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
waitForExist(t, client, handle.(*DockerHandle))
|
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
|
2015-11-18 05:41:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
// Verify that the correct ports are EXPOSED
|
|
|
|
expectedExposedPorts := map[docker.Port]struct{}{
|
2016-01-19 23:35:01 +00:00
|
|
|
docker.Port(fmt.Sprintf("%d/tcp", res)): struct{}{},
|
|
|
|
docker.Port(fmt.Sprintf("%d/udp", res)): struct{}{},
|
|
|
|
docker.Port(fmt.Sprintf("%d/tcp", dyn)): struct{}{},
|
|
|
|
docker.Port(fmt.Sprintf("%d/udp", dyn)): struct{}{},
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(container.Config.ExposedPorts, expectedExposedPorts) {
|
|
|
|
t.Errorf("Exposed ports don't match.\nExpected:\n%s\nGot:\n%s\n", expectedExposedPorts, container.Config.ExposedPorts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the correct ports are FORWARDED
|
|
|
|
expectedPortBindings := map[docker.Port][]docker.PortBinding{
|
2016-01-19 23:35:01 +00:00
|
|
|
docker.Port(fmt.Sprintf("%d/tcp", res)): []docker.PortBinding{docker.PortBinding{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", res)}},
|
|
|
|
docker.Port(fmt.Sprintf("%d/udp", res)): []docker.PortBinding{docker.PortBinding{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", res)}},
|
|
|
|
docker.Port(fmt.Sprintf("%d/tcp", dyn)): []docker.PortBinding{docker.PortBinding{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", dyn)}},
|
|
|
|
docker.Port(fmt.Sprintf("%d/udp", dyn)): []docker.PortBinding{docker.PortBinding{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", dyn)}},
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(container.HostConfig.PortBindings, expectedPortBindings) {
|
|
|
|
t.Errorf("Forwarded ports don't match.\nExpected:\n%s\nGot:\n%s\n", expectedPortBindings, container.HostConfig.PortBindings)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedEnvironment := map[string]string{
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_ADDR_main": fmt.Sprintf("127.0.0.1:%d", res),
|
|
|
|
"NOMAD_ADDR_REDIS": fmt.Sprintf("127.0.0.1:%d", dyn),
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range expectedEnvironment {
|
|
|
|
search := fmt.Sprintf("%s=%s", key, val)
|
|
|
|
if !inSlice(search, container.Config.Env) {
|
|
|
|
t.Errorf("Expected to find %s in container environment: %+v", search, container.Config.Env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
func TestDockerDriver_PortsMapping(t *testing.T) {
|
2016-01-19 23:35:01 +00:00
|
|
|
task, res, dyn := dockerTask()
|
2015-11-20 03:08:21 +00:00
|
|
|
task.Config["port_map"] = []map[string]string{
|
|
|
|
map[string]string{
|
|
|
|
"main": "8080",
|
|
|
|
"REDIS": "6379",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
waitForExist(t, client, handle.(*DockerHandle))
|
|
|
|
|
2015-11-19 22:20:41 +00:00
|
|
|
container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID())
|
2015-11-18 05:41:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
// Verify that the correct ports are EXPOSED
|
|
|
|
expectedExposedPorts := map[docker.Port]struct{}{
|
|
|
|
docker.Port("8080/tcp"): struct{}{},
|
|
|
|
docker.Port("8080/udp"): struct{}{},
|
|
|
|
docker.Port("6379/tcp"): struct{}{},
|
|
|
|
docker.Port("6379/udp"): struct{}{},
|
2015-11-18 05:41:00 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
if !reflect.DeepEqual(container.Config.ExposedPorts, expectedExposedPorts) {
|
|
|
|
t.Errorf("Exposed ports don't match.\nExpected:\n%s\nGot:\n%s\n", expectedExposedPorts, container.Config.ExposedPorts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the correct ports are FORWARDED
|
|
|
|
expectedPortBindings := map[docker.Port][]docker.PortBinding{
|
2016-01-19 23:35:01 +00:00
|
|
|
docker.Port("8080/tcp"): []docker.PortBinding{docker.PortBinding{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", res)}},
|
|
|
|
docker.Port("8080/udp"): []docker.PortBinding{docker.PortBinding{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", res)}},
|
|
|
|
docker.Port("6379/tcp"): []docker.PortBinding{docker.PortBinding{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", dyn)}},
|
|
|
|
docker.Port("6379/udp"): []docker.PortBinding{docker.PortBinding{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", dyn)}},
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(container.HostConfig.PortBindings, expectedPortBindings) {
|
|
|
|
t.Errorf("Forwarded ports don't match.\nExpected:\n%s\nGot:\n%s\n", expectedPortBindings, container.HostConfig.PortBindings)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedEnvironment := map[string]string{
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_ADDR_main": "127.0.0.1:8080",
|
|
|
|
"NOMAD_ADDR_REDIS": "127.0.0.1:6379",
|
2016-07-09 01:27:51 +00:00
|
|
|
"NOMAD_HOST_PORT_main": strconv.Itoa(docker_reserved),
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range expectedEnvironment {
|
|
|
|
search := fmt.Sprintf("%s=%s", key, val)
|
|
|
|
if !inSlice(search, container.Config.Env) {
|
|
|
|
t.Errorf("Expected to find %s in container environment: %+v", search, container.Config.Env)
|
|
|
|
}
|
2015-11-18 05:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-23 11:57:31 +00:00
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
func TestDockerDriver_User(t *testing.T) {
|
2016-03-23 11:57:31 +00:00
|
|
|
task := &structs.Task{
|
|
|
|
Name: "redis-demo",
|
|
|
|
User: "alice",
|
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"image": "busybox",
|
|
|
|
"load": []string{"busybox.tar"},
|
|
|
|
"command": "/bin/sleep",
|
2016-03-23 11:57:31 +00:00
|
|
|
"args": []string{"10000"},
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-26 00:10:08 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2016-03-23 11:57:31 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
driver := NewDockerDriver(driverCtx)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2016-08-12 18:39:58 +00:00
|
|
|
copyImage(execCtx, task, "busybox.tar", t)
|
2016-03-23 11:57:31 +00:00
|
|
|
|
|
|
|
// It should fail because the user "alice" does not exist on the given
|
|
|
|
// image.
|
|
|
|
handle, err := driver.Start(execCtx, task)
|
|
|
|
if err == nil {
|
|
|
|
handle.Kill()
|
|
|
|
t.Fatalf("Should've failed")
|
|
|
|
}
|
2016-05-07 22:46:44 +00:00
|
|
|
|
2016-08-12 02:34:36 +00:00
|
|
|
if !strings.Contains(err.Error(), "alice") {
|
2016-05-07 22:46:44 +00:00
|
|
|
t.Fatalf("Expected failure string not found, found %q instead", err.Error())
|
2016-03-23 11:57:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-12 05:00:14 +00:00
|
|
|
|
|
|
|
func TestDockerDriver_CleanupContainer(t *testing.T) {
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "redis-demo",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
2016-08-12 02:23:48 +00:00
|
|
|
"load": []string{"busybox.tar"},
|
2016-04-12 05:00:14 +00:00
|
|
|
"command": "/bin/echo",
|
|
|
|
"args": []string{"hello"},
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Update should be a no-op
|
|
|
|
err := handle.Update(task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
|
|
|
}
|
2016-04-12 09:34:59 +00:00
|
|
|
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
// Ensure that the container isn't present
|
|
|
|
_, err := client.InspectContainer(handle.(*DockerHandle).containerID)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected to not get container")
|
|
|
|
}
|
|
|
|
|
2016-04-12 05:00:14 +00:00
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
2016-05-28 02:08:17 +00:00
|
|
|
|
|
|
|
func TestDockerDriver_Stats(t *testing.T) {
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "sleep",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
2016-08-12 02:23:48 +00:00
|
|
|
"load": []string{"busybox.tar"},
|
2016-05-28 02:08:17 +00:00
|
|
|
"command": "/bin/sleep",
|
|
|
|
"args": []string{"100"},
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: basicResources,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
waitForExist(t, client, handle.(*DockerHandle))
|
|
|
|
|
2016-05-28 02:08:17 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
ru, err := handle.Stats()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if ru.ResourceUsage == nil {
|
|
|
|
handle.Kill()
|
|
|
|
t.Fatalf("expected resource usage")
|
|
|
|
}
|
|
|
|
err = handle.Kill()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if res.Successful() {
|
|
|
|
t.Fatalf("should err: %v", res)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*10) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-08-12 02:23:48 +00:00
|
|
|
|
2016-08-12 18:39:58 +00:00
|
|
|
func copyImage(execCtx *ExecContext, task *structs.Task, image string, t *testing.T) {
|
2016-08-12 02:23:48 +00:00
|
|
|
taskDir, _ := execCtx.AllocDir.TaskDirs[task.Name]
|
2016-08-12 18:39:58 +00:00
|
|
|
dst := filepath.Join(taskDir, allocdir.TaskLocal, image)
|
|
|
|
copyFile(filepath.Join("./test-resources/docker", image), dst, t)
|
2016-08-12 02:23:48 +00:00
|
|
|
}
|