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"
|
2016-09-27 20:13:55 +00:00
|
|
|
"os"
|
2015-10-15 23:59:08 +00:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2017-10-18 20:43:26 +00:00
|
|
|
"runtime"
|
2015-11-20 05:29:37 +00:00
|
|
|
"runtime/debug"
|
2017-06-13 21:02:11 +00:00
|
|
|
"sort"
|
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"
|
2017-10-23 23:51:40 +00:00
|
|
|
"github.com/hashicorp/consul/lib/freeport"
|
2017-07-07 17:14:10 +00:00
|
|
|
sockaddr "github.com/hashicorp/go-sockaddr"
|
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"
|
2018-01-25 16:30:15 +00:00
|
|
|
"github.com/hashicorp/nomad/client/fingerprint"
|
2016-12-03 01:04:07 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2016-03-26 00:10:08 +00:00
|
|
|
"github.com/hashicorp/nomad/client/testutil"
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2017-09-29 16:58:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2016-09-27 20:13:55 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
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"
|
2017-11-13 01:47:55 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-23 22:05:00 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
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-19 23:35:01 +00:00
|
|
|
// Returns a task with a reserved and dynamic port. The ports are returned
|
|
|
|
// respectively.
|
2017-10-19 04:45:18 +00:00
|
|
|
func dockerTask(t *testing.T) (*structs.Task, int, int) {
|
2017-10-23 23:51:40 +00:00
|
|
|
ports := freeport.GetT(t, 2)
|
2017-10-25 17:49:34 +00:00
|
|
|
dockerReserved := ports[0]
|
|
|
|
dockerDynamic := ports[1]
|
2015-11-20 03:08:21 +00:00
|
|
|
return &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "redis-demo",
|
|
|
|
Driver: "docker",
|
2015-11-20 03:08:21 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"image": "busybox",
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "busybox.tar",
|
2016-08-12 02:23:48 +00:00
|
|
|
"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{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-11-20 03:08:21 +00:00
|
|
|
IP: "127.0.0.1",
|
2017-10-25 17:49:34 +00:00
|
|
|
ReservedPorts: []structs.Port{{Label: "main", Value: dockerReserved}},
|
|
|
|
DynamicPorts: []structs.Port{{Label: "REDIS", Value: dockerDynamic}},
|
2015-11-20 03:08:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-25 17:49:34 +00:00
|
|
|
}, dockerReserved, dockerDynamic
|
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.
|
2017-11-14 23:25:52 +00:00
|
|
|
func dockerSetup(t *testing.T, task *structs.Task) (*docker.Client, *DockerHandle, func()) {
|
2016-11-11 16:38:16 +00:00
|
|
|
client := newTestDockerClient(t)
|
|
|
|
return dockerSetupWithClient(t, task, client)
|
|
|
|
}
|
2015-11-20 03:08:21 +00:00
|
|
|
|
2017-02-24 21:20:40 +00:00
|
|
|
func testDockerDriverContexts(t *testing.T, task *structs.Task) *testContext {
|
2016-12-03 01:04:07 +00:00
|
|
|
tctx := testDriverContexts(t, task)
|
2017-02-24 21:20:40 +00:00
|
|
|
|
|
|
|
// Drop the delay
|
|
|
|
tctx.DriverCtx.config.Options = make(map[string]string)
|
|
|
|
tctx.DriverCtx.config.Options[dockerImageRemoveDelayConfigOption] = "1s"
|
|
|
|
|
|
|
|
return tctx
|
|
|
|
}
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
func dockerSetupWithClient(t *testing.T, task *structs.Task, client *docker.Client) (*docker.Client, *DockerHandle, func()) {
|
2017-10-18 20:43:26 +00:00
|
|
|
t.Helper()
|
2017-02-24 21:20:40 +00:00
|
|
|
tctx := testDockerDriverContexts(t, task)
|
2016-12-03 01:04:07 +00:00
|
|
|
driver := NewDockerDriver(tctx.DriverCtx)
|
|
|
|
copyImage(t, tctx.ExecCtx.TaskDir, "busybox.tar")
|
2015-11-20 03:08:21 +00:00
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
presp, err := driver.Prestart(tctx.ExecCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
2018-01-23 22:05:00 +00:00
|
|
|
if presp != nil && presp.CreatedResources != nil {
|
|
|
|
driver.Cleanup(tctx.ExecCtx, presp.CreatedResources)
|
|
|
|
}
|
2016-12-03 01:04:07 +00:00
|
|
|
tctx.AllocDir.Destroy()
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
// Update the exec ctx with the driver network env vars
|
|
|
|
tctx.ExecCtx.TaskEnv = tctx.EnvBuilder.SetDriverNetwork(presp.Network).Build()
|
2017-02-24 21:20:40 +00:00
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
sresp, err := driver.Start(tctx.ExecCtx, task)
|
2015-11-20 03:08:21 +00:00
|
|
|
if err != nil {
|
2017-06-13 21:02:11 +00:00
|
|
|
driver.Cleanup(tctx.ExecCtx, presp.CreatedResources)
|
2016-12-03 01:04:07 +00:00
|
|
|
tctx.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
|
|
|
}
|
2017-02-24 21:20:40 +00:00
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
if sresp.Handle == nil {
|
|
|
|
driver.Cleanup(tctx.ExecCtx, presp.CreatedResources)
|
2016-12-03 01:04:07 +00:00
|
|
|
tctx.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
|
|
|
}
|
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
// At runtime this is handled by TaskRunner
|
2017-06-23 23:51:38 +00:00
|
|
|
tctx.ExecCtx.TaskEnv = tctx.EnvBuilder.SetDriverNetwork(sresp.Network).Build()
|
2017-06-13 21:02:11 +00:00
|
|
|
|
2015-11-20 03:08:21 +00:00
|
|
|
cleanup := func() {
|
2017-06-13 21:02:11 +00:00
|
|
|
driver.Cleanup(tctx.ExecCtx, presp.CreatedResources)
|
|
|
|
sresp.Handle.Kill()
|
2016-12-03 01:04:07 +00:00
|
|
|
tctx.AllocDir.Destroy()
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
return client, sresp.Handle.(*DockerHandle), cleanup
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 16:38:16 +00:00
|
|
|
func newTestDockerClient(t *testing.T) *docker.Client {
|
2017-10-18 20:43:26 +00:00
|
|
|
t.Helper()
|
2016-11-11 16:38:16 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2016-11-11 16:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := docker.NewClientFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to initialize client: %s\nStack\n%s", err, debug.Stack())
|
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2018-01-25 16:30:15 +00:00
|
|
|
|
2017-02-24 21:20:40 +00:00
|
|
|
ctx := testDockerDriverContexts(t, &structs.Task{Name: "foo", Driver: "docker", Resources: basicResources})
|
|
|
|
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
|
2016-12-03 01:04:07 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(ctx.DriverCtx)
|
2015-09-04 04:00:16 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
|
|
|
request := &cstructs.FingerprintRequest{Config: &config.Config{}, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := d.Fingerprint(request, &response)
|
2015-09-04 04:00:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
attributes := response.Attributes
|
2018-01-26 16:21:07 +00:00
|
|
|
if testutil.DockerIsConnected(t) && attributes["driver.docker"] == "" {
|
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
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
if 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.")
|
2018-01-30 17:57:37 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// if docker is available, make sure that the response is tagged as
|
|
|
|
// applicable
|
2018-01-31 22:03:55 +00:00
|
|
|
if !response.Detected {
|
2018-01-30 17:57:37 +00:00
|
|
|
t.Fatalf("expected response to be applicable")
|
|
|
|
}
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
t.Logf("Found docker version %s", attributes["driver.docker.version"])
|
2015-09-04 04:00:16 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 17:14:10 +00:00
|
|
|
// TestDockerDriver_Fingerprint_Bridge asserts that if Docker is running we set
|
|
|
|
// the bridge network's IP as a node attribute. See #2785
|
|
|
|
func TestDockerDriver_Fingerprint_Bridge(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-07-07 17:14:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("requires Docker")
|
|
|
|
}
|
2017-10-18 20:43:26 +00:00
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
t.Skip("expect only on linux")
|
|
|
|
}
|
2017-07-07 17:14:10 +00:00
|
|
|
|
|
|
|
// This seems fragile, so we might need to reconsider this test if it
|
|
|
|
// proves flaky
|
|
|
|
expectedAddr, err := sockaddr.GetInterfaceIP("docker0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get ip for docker0: %v", err)
|
|
|
|
}
|
|
|
|
if expectedAddr == "" {
|
|
|
|
t.Fatalf("unable to get ip for docker bridge")
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:43:26 +00:00
|
|
|
conf := testConfig(t)
|
2017-07-07 17:14:10 +00:00
|
|
|
conf.Node = mock.Node()
|
2018-06-13 22:33:25 +00:00
|
|
|
dd := NewDockerDriver(NewDriverContext("", "", "", "", conf, conf.Node, testlog.Logger(t), nil))
|
2018-01-24 14:09:53 +00:00
|
|
|
|
|
|
|
request := &cstructs.FingerprintRequest{Config: conf, Node: conf.Node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
2018-01-25 16:30:15 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
err = dd.Fingerprint(request, &response)
|
2017-07-07 17:14:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error fingerprinting docker: %v", err)
|
|
|
|
}
|
2018-01-26 16:21:07 +00:00
|
|
|
|
2018-01-31 22:03:55 +00:00
|
|
|
if !response.Detected {
|
2018-01-30 17:57:37 +00:00
|
|
|
t.Fatalf("expected response to be applicable")
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := response.Attributes
|
|
|
|
if attributes == nil {
|
|
|
|
t.Fatalf("expected attributes to be set")
|
|
|
|
}
|
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
if attributes["driver.docker"] == "" {
|
2017-07-07 17:14:10 +00:00
|
|
|
t.Fatalf("expected Docker to be enabled but false was returned")
|
|
|
|
}
|
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
if found := attributes["driver.docker.bridge_ip"]; found != expectedAddr {
|
2017-07-07 17:14:10 +00:00
|
|
|
t.Fatalf("expected bridge ip %q but found: %q", expectedAddr, found)
|
|
|
|
}
|
2018-01-26 16:21:07 +00:00
|
|
|
t.Logf("docker bridge ip: %q", attributes["driver.docker.bridge_ip"])
|
2017-07-07 17:14:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 16:30:15 +00:00
|
|
|
func TestDockerDriver_Check_DockerHealthStatus(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("requires Docker")
|
|
|
|
}
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
t.Skip("expect only on linux")
|
|
|
|
}
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
expectedAddr, err := sockaddr.GetInterfaceIP("docker0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get ip for docker0: %v", err)
|
|
|
|
}
|
|
|
|
if expectedAddr == "" {
|
|
|
|
t.Fatalf("unable to get ip for docker bridge")
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := testConfig(t)
|
|
|
|
conf.Node = mock.Node()
|
2018-06-13 22:33:25 +00:00
|
|
|
dd := NewDockerDriver(NewDriverContext("", "", "", "", conf, conf.Node, testlog.Logger(t), nil))
|
2018-01-25 16:30:15 +00:00
|
|
|
|
|
|
|
request := &cstructs.HealthCheckRequest{}
|
|
|
|
var response cstructs.HealthCheckResponse
|
|
|
|
|
|
|
|
dc, ok := dd.(fingerprint.HealthCheck)
|
|
|
|
require.True(ok)
|
|
|
|
err = dc.HealthCheck(request, &response)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
driverInfo := response.Drivers["docker"]
|
|
|
|
require.NotNil(driverInfo)
|
|
|
|
require.True(driverInfo.Healthy)
|
|
|
|
}
|
|
|
|
|
2015-09-04 04:00:16 +00:00
|
|
|
func TestDockerDriver_StartOpen_Wait(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-03-26 00:10:08 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2015-11-20 21:50:47 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 04:00:16 +00:00
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "nc-demo",
|
|
|
|
Driver: "docker",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "busybox.tar",
|
2016-08-12 02:23:48 +00:00
|
|
|
"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
|
|
|
|
2017-02-24 21:20:40 +00:00
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
|
2016-12-03 01:04:07 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
2015-09-25 23:49:14 +00:00
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
_, err := d.Prestart(ctx.ExecCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-01-10 21:24:45 +00:00
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2015-09-04 04:00:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
if resp.Handle == nil {
|
2015-09-04 04:00:16 +00:00
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
defer resp.Handle.Kill()
|
2015-09-04 04:00:16 +00:00
|
|
|
|
|
|
|
// Attempt to open
|
2017-06-13 21:02:11 +00:00
|
|
|
resp2, err := d.Open(ctx.ExecCtx, resp.Handle.ID())
|
2015-09-04 04:00:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
if resp2 == nil {
|
2015-09-04 04:00:16 +00:00
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_Start_Wait(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
2015-09-04 04:00:16 +00:00
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "nc-demo",
|
|
|
|
Driver: "docker",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "busybox.tar",
|
2016-08-12 02:23:48 +00:00
|
|
|
"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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
// TestDockerDriver_Start_StoppedContainer asserts that Nomad will detect a
|
|
|
|
// stopped task container, remove it, and start a new container.
|
|
|
|
//
|
|
|
|
// See https://github.com/hashicorp/nomad/issues/3419
|
|
|
|
func TestDockerDriver_Start_StoppedContainer(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "nc-demo",
|
|
|
|
Driver: "docker",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"load": "busybox.tar",
|
|
|
|
"image": "busybox",
|
|
|
|
"command": "sleep",
|
|
|
|
"args": []string{"9000"},
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 100,
|
|
|
|
CPU: 100,
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 1,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tctx := testDockerDriverContexts(t, task)
|
|
|
|
defer tctx.AllocDir.Destroy()
|
|
|
|
|
|
|
|
copyImage(t, tctx.ExecCtx.TaskDir, "busybox.tar")
|
|
|
|
client := newTestDockerClient(t)
|
|
|
|
driver := NewDockerDriver(tctx.DriverCtx).(*DockerDriver)
|
|
|
|
driverConfig := &DockerDriverConfig{ImageName: "busybox", LoadImage: "busybox.tar"}
|
|
|
|
if _, err := driver.loadImage(driverConfig, client, tctx.ExecCtx.TaskDir); err != nil {
|
|
|
|
t.Fatalf("error loading image: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a container of the same name but don't start it. This mimics
|
|
|
|
// the case of dockerd getting restarted and stopping containers while
|
|
|
|
// Nomad is watching them.
|
|
|
|
opts := docker.CreateContainerOptions{
|
|
|
|
Name: fmt.Sprintf("%s-%s", task.Name, tctx.DriverCtx.allocID),
|
|
|
|
Config: &docker.Config{
|
|
|
|
Image: "busybox",
|
|
|
|
Cmd: []string{"sleep", "9000"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if _, err := client.CreateContainer(opts); err != nil {
|
|
|
|
t.Fatalf("error creating initial container: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now assert that the driver can still start normally
|
|
|
|
presp, err := driver.Prestart(tctx.ExecCtx, task)
|
|
|
|
if err != nil {
|
|
|
|
driver.Cleanup(tctx.ExecCtx, presp.CreatedResources)
|
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
|
|
|
defer driver.Cleanup(tctx.ExecCtx, presp.CreatedResources)
|
|
|
|
|
|
|
|
sresp, err := driver.Start(tctx.ExecCtx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to start driver: %s", err)
|
|
|
|
}
|
|
|
|
handle := sresp.Handle.(*DockerHandle)
|
|
|
|
waitForExist(t, client, handle)
|
|
|
|
handle.Kill()
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:42:17 +00:00
|
|
|
func TestDockerDriver_Start_LoadImage(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-06-12 18:28:43 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2016-06-12 18:28:43 +00:00
|
|
|
}
|
2016-03-30 20:42:17 +00:00
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "busybox-demo",
|
|
|
|
Driver: "docker",
|
2016-03-30 20:42:17 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "busybox.tar",
|
2017-10-18 20:43:26 +00:00
|
|
|
"command": "/bin/sh",
|
2016-03-30 20:42:17 +00:00
|
|
|
"args": []string{
|
2017-10-18 20:43:26 +00:00
|
|
|
"-c",
|
|
|
|
"echo hello > $NOMAD_TASK_DIR/output",
|
2016-03-30 20:42:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-24 21:20:40 +00:00
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
|
2016-12-03 01:04:07 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(ctx.DriverCtx)
|
2016-03-30 20:42:17 +00:00
|
|
|
|
2016-08-17 22:10:30 +00:00
|
|
|
// Copy the image into the task's directory
|
2016-12-03 01:04:07 +00:00
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
2016-03-30 20:42:17 +00:00
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
_, err := d.Prestart(ctx.ExecCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2016-03-30 20:42:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
defer resp.Handle.Kill()
|
2016-03-30 20:42:17 +00:00
|
|
|
|
|
|
|
select {
|
2017-06-13 21:02:11 +00:00
|
|
|
case res := <-resp.Handle.WaitCh():
|
2016-03-30 20:42:17 +00:00
|
|
|
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.
|
2017-10-18 20:43:26 +00:00
|
|
|
outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LocalDir, "output")
|
2016-03-30 20:42:17 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-29 00:53:25 +00:00
|
|
|
func TestDockerDriver_Start_BadPull_Recoverable(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-10-29 00:53:25 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2016-10-29 00:53:25 +00:00
|
|
|
}
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "busybox-demo",
|
|
|
|
Driver: "docker",
|
2016-10-29 00:53:25 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "127.0.1.1:32121/foo", // bad path
|
|
|
|
"command": "/bin/echo",
|
|
|
|
"args": []string{
|
|
|
|
"hello",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-24 21:20:40 +00:00
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
|
2016-12-03 01:04:07 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(ctx.DriverCtx)
|
2016-10-29 00:53:25 +00:00
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
_, err := d.Prestart(ctx.ExecCtx, task)
|
2016-10-29 00:53:25 +00:00
|
|
|
if err == nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("want error in prestart: %v", err)
|
2016-10-29 00:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if rerr, ok := err.(*structs.RecoverableError); !ok {
|
|
|
|
t.Fatalf("want recoverable error: %+v", err)
|
2017-03-27 23:27:24 +00:00
|
|
|
} else if !rerr.IsRecoverable() {
|
2016-10-29 00:53:25 +00:00
|
|
|
t.Fatalf("error not recoverable: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 23:59:08 +00:00
|
|
|
func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
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) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2015-10-15 23:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exp := []byte{'w', 'i', 'n'}
|
|
|
|
file := "output.txt"
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "nc-demo",
|
|
|
|
Driver: "docker",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"image": "busybox",
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "busybox.tar",
|
2016-08-12 02:23:48 +00:00
|
|
|
"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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-24 21:20:40 +00:00
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
|
2016-12-03 01:04:07 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
2015-10-15 23:59:08 +00:00
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
_, err := d.Prestart(ctx.ExecCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2015-10-15 23:59:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
defer resp.Handle.Kill()
|
2015-10-15 23:59:08 +00:00
|
|
|
|
|
|
|
select {
|
2017-06-13 21:02:11 +00:00
|
|
|
case res := <-resp.Handle.WaitCh():
|
2015-11-14 06:07:13 +00:00
|
|
|
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-12-03 01:04:07 +00:00
|
|
|
outputFile := filepath.Join(ctx.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) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
2015-09-04 04:00:16 +00:00
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "nc-demo",
|
|
|
|
Driver: "docker",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"image": "busybox",
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "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) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-03-26 00:10:08 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2015-10-06 07:27:05 +00:00
|
|
|
}
|
2015-09-26 00:35:33 +00:00
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task1, _, _ := dockerTask(t)
|
|
|
|
task2, _, _ := dockerTask(t)
|
|
|
|
task3, _, _ := dockerTask(t)
|
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
|
|
|
|
for idx, task := range taskList {
|
2017-02-24 21:20:40 +00:00
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
|
2016-12-03 01:04:07 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
2015-09-26 01:35:23 +00:00
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
_, err := d.Prestart(ctx.ExecCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart #%d: %v", idx+1, err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2015-09-26 00:35:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed starting task #%d: %s", idx+1, err)
|
2017-06-13 21:02:11 +00:00
|
|
|
continue
|
2015-09-26 00:35:33 +00:00
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
handles[idx] = resp.Handle
|
2015-09-26 00:35:33 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-03-26 00:10:08 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2015-10-06 07:27:05 +00:00
|
|
|
}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task1, _, _ := dockerTask(t)
|
2016-08-12 02:23:48 +00:00
|
|
|
task1.Config["image"] = "busybox"
|
2017-02-24 21:20:40 +00:00
|
|
|
task1.Config["load"] = "busybox.tar"
|
2015-09-26 00:38:07 +00:00
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task2, _, _ := dockerTask(t)
|
2016-08-12 02:23:48 +00:00
|
|
|
task2.Config["image"] = "busybox:musl"
|
2017-02-24 21:20:40 +00:00
|
|
|
task2.Config["load"] = "busybox_musl.tar"
|
2018-06-01 00:14:59 +00:00
|
|
|
task2.Config["args"] = []string{"-l", "-p", "0"}
|
2015-09-26 00:38:07 +00:00
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task3, _, _ := dockerTask(t)
|
2016-08-12 02:23:48 +00:00
|
|
|
task3.Config["image"] = "busybox:glibc"
|
2017-02-24 21:20:40 +00:00
|
|
|
task3.Config["load"] = "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))
|
2018-06-01 00:14:59 +00:00
|
|
|
client := newTestDockerClient(t)
|
2015-09-26 00:38:07 +00:00
|
|
|
|
|
|
|
// Let's spin up a bunch of things
|
|
|
|
for idx, task := range taskList {
|
2017-02-24 21:20:40 +00:00
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
|
2016-12-03 01:04:07 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox_musl.tar")
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox_glibc.tar")
|
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
_, err := d.Prestart(ctx.ExecCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart #%d: %v", idx+1, err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2015-09-26 00:38:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed starting task #%d: %s", idx+1, err)
|
2017-06-13 21:02:11 +00:00
|
|
|
continue
|
2015-09-26 00:38:07 +00:00
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
handles[idx] = resp.Handle
|
2018-06-01 00:14:59 +00:00
|
|
|
waitForExist(t, client, resp.Handle.(*DockerHandle))
|
2015-09-26 00:38:07 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2017-11-14 23:25:52 +00:00
|
|
|
handle.logger.Printf("[DEBUG] docker.test: waiting for container %s to exist...", handle.ContainerID())
|
2016-07-12 17:36:06 +00:00
|
|
|
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)
|
|
|
|
})
|
2017-11-14 23:25:52 +00:00
|
|
|
handle.logger.Printf("[DEBUG] docker.test: ...container %s exists!", handle.ContainerID())
|
2016-07-12 17:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_NetworkMode_Host(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
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-12-03 01:04:07 +00:00
|
|
|
Name: "nc-demo",
|
|
|
|
Driver: "docker",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"image": "busybox",
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "busybox.tar",
|
2016-08-12 02:23:48 +00:00
|
|
|
"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()
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2016-07-12 16:59:34 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
container, err := client.InspectContainer(handle.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-11-11 16:38:16 +00:00
|
|
|
func TestDockerDriver_NetworkAliases_Bridge(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 16:38:16 +00:00
|
|
|
// Because go-dockerclient doesn't provide api for query network aliases, just check that
|
|
|
|
// a container can be created with a 'network_aliases' property
|
|
|
|
|
|
|
|
// Create network, network-scoped alias is supported only for containers in user defined networks
|
|
|
|
client := newTestDockerClient(t)
|
|
|
|
networkOpts := docker.CreateNetworkOptions{Name: "foobar", Driver: "bridge"}
|
|
|
|
network, err := client.CreateNetwork(networkOpts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer client.RemoveNetwork(network.ID)
|
|
|
|
|
|
|
|
expected := []string{"foobar"}
|
|
|
|
task := &structs.Task{
|
2017-01-06 01:02:41 +00:00
|
|
|
Name: "nc-demo",
|
|
|
|
Driver: "docker",
|
2016-11-11 16:38:16 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "busybox.tar",
|
2016-11-11 16:38:16 +00:00
|
|
|
"command": "/bin/nc",
|
|
|
|
"args": []string{"-l", "127.0.0.1", "-p", "0"},
|
|
|
|
"network_mode": network.Name,
|
|
|
|
"network_aliases": expected,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetupWithClient(t, task, client)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2016-11-11 16:38:16 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
_, err = client.InspectContainer(handle.ContainerID())
|
2016-11-11 16:38:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 08:18:26 +00:00
|
|
|
func TestDockerDriver_Sysctl_Ulimit(t *testing.T) {
|
2017-11-17 23:46:04 +00:00
|
|
|
task, _, _ := dockerTask(t)
|
2017-03-29 14:55:30 +00:00
|
|
|
expectedUlimits := map[string]string{
|
2017-03-30 14:12:36 +00:00
|
|
|
"nproc": "4242",
|
|
|
|
"nofile": "2048:4096",
|
2017-03-29 14:55:30 +00:00
|
|
|
}
|
2017-03-31 08:18:26 +00:00
|
|
|
task.Config["sysctl"] = []map[string]string{
|
2017-11-18 15:23:09 +00:00
|
|
|
{
|
2017-03-29 14:55:30 +00:00
|
|
|
"net.core.somaxconn": "16384",
|
|
|
|
},
|
|
|
|
}
|
2017-03-31 08:18:26 +00:00
|
|
|
task.Config["ulimit"] = []map[string]string{
|
2017-03-29 14:55:30 +00:00
|
|
|
expectedUlimits,
|
|
|
|
}
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-11-17 23:46:04 +00:00
|
|
|
waitForExist(t, client, handle)
|
2017-03-29 14:55:30 +00:00
|
|
|
|
2017-11-17 23:46:04 +00:00
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
2017-11-20 17:15:09 +00:00
|
|
|
assert.Nil(t, err, "unexpected error: %v", err)
|
2017-03-29 14:55:30 +00:00
|
|
|
|
2017-11-20 17:15:09 +00:00
|
|
|
want := "16384"
|
|
|
|
got := container.HostConfig.Sysctls["net.core.somaxconn"]
|
|
|
|
assert.Equal(t, want, got, "Wrong net.core.somaxconn config for docker job. Expect: %s, got: %s", want, got)
|
|
|
|
|
|
|
|
expectedUlimitLen := 2
|
|
|
|
actualUlimitLen := len(container.HostConfig.Ulimits)
|
|
|
|
assert.Equal(t, want, got, "Wrong number of ulimit configs for docker job. Expect: %d, got: %d", expectedUlimitLen, actualUlimitLen)
|
2017-03-29 14:55:30 +00:00
|
|
|
|
|
|
|
for _, got := range container.HostConfig.Ulimits {
|
2017-11-20 17:15:09 +00:00
|
|
|
if expectedStr, ok := expectedUlimits[got.Name]; !ok {
|
2017-03-29 14:55:30 +00:00
|
|
|
t.Errorf("%s config unexpected for docker job.", got.Name)
|
|
|
|
} else {
|
2017-11-20 17:15:09 +00:00
|
|
|
if !strings.Contains(expectedStr, ":") {
|
2017-03-29 14:55:30 +00:00
|
|
|
expectedStr = expectedStr + ":" + expectedStr
|
|
|
|
}
|
|
|
|
|
|
|
|
splitted := strings.SplitN(expectedStr, ":", 2)
|
|
|
|
soft, _ := strconv.Atoi(splitted[0])
|
|
|
|
hard, _ := strconv.Atoi(splitted[1])
|
2017-11-20 18:07:18 +00:00
|
|
|
assert.Equal(t, int64(soft), got.Soft, "Wrong soft %s ulimit for docker job. Expect: %d, got: %d", got.Name, soft, got.Soft)
|
|
|
|
assert.Equal(t, int64(hard), got.Hard, "Wrong hard %s ulimit for docker job. Expect: %d, got: %d", got.Name, hard, got.Hard)
|
2017-03-29 14:55:30 +00:00
|
|
|
|
2017-11-20 18:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_Sysctl_Ulimit_Errors(t *testing.T) {
|
|
|
|
brokenConfigs := []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"nofile": "",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"nofile": "abc:1234",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"nofile": "1234:abc",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
test_cases := []struct {
|
|
|
|
ulimitConfig interface{}
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{[]interface{}{brokenConfigs[0]}, fmt.Errorf("Malformed ulimit specification nofile: \"\", cannot be empty")},
|
|
|
|
{[]interface{}{brokenConfigs[1]}, fmt.Errorf("Malformed soft ulimit nofile: abc:1234")},
|
|
|
|
{[]interface{}{brokenConfigs[2]}, fmt.Errorf("Malformed hard ulimit nofile: 1234:abc")},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range test_cases {
|
|
|
|
task, _, _ := dockerTask(t)
|
|
|
|
task.Config["ulimit"] = tc.ulimitConfig
|
|
|
|
|
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
driver := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
|
2017-11-20 19:04:38 +00:00
|
|
|
_, err := driver.Prestart(ctx.ExecCtx, task)
|
|
|
|
assert.NotNil(t, err, "Expected non nil error")
|
|
|
|
assert.Equal(t, err.Error(), tc.err.Error(), "unexpected error in prestart, got %v, expected %v", err, tc.err)
|
2017-03-29 14:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
func TestDockerDriver_Labels(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task, _, _ := dockerTask(t)
|
2015-11-17 13:12:49 +00:00
|
|
|
task.Config["labels"] = []map[string]string{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-11-17 13:12:49 +00:00
|
|
|
"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
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2016-07-12 17:36:06 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
container, err := client.InspectContainer(handle.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-12-28 18:18:38 +00:00
|
|
|
func TestDockerDriver_ForcePull_IsInvalidConfig(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task, _, _ := dockerTask(t)
|
2016-12-28 18:18:38 +00:00
|
|
|
task.Config["force_pull"] = "nothing"
|
|
|
|
|
2017-02-24 21:20:40 +00:00
|
|
|
ctx := testDockerDriverContexts(t, task)
|
2017-01-09 21:55:50 +00:00
|
|
|
defer ctx.AllocDir.Destroy()
|
2017-02-24 21:20:40 +00:00
|
|
|
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
|
2017-01-09 21:55:50 +00:00
|
|
|
driver := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
if _, err := driver.Prestart(ctx.ExecCtx, task); err == nil {
|
2016-12-28 18:18:38 +00:00
|
|
|
t.Fatalf("error expected in prestart")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_ForcePull(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task, _, _ := dockerTask(t)
|
2016-12-28 18:18:38 +00:00
|
|
|
task.Config["force_pull"] = "true"
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2016-12-28 18:18:38 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
_, err := client.InspectContainer(handle.ContainerID())
|
2016-12-28 18:18:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:36:40 +00:00
|
|
|
func TestDockerDriver_ForcePull_RepoDigest(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
task, _, _ := dockerTask(t)
|
|
|
|
task.Config["load"] = ""
|
|
|
|
task.Config["image"] = "library/busybox@sha256:58ac43b2cc92c687a32c8be6278e50a063579655fe3090125dcb2af0ff9e1a64"
|
2018-05-14 18:23:02 +00:00
|
|
|
localDigest := "sha256:8ac48589692a53a9b8c2d1ceaa6b402665aa7fe667ba51ccc03002300856d8c7"
|
2018-05-14 14:36:40 +00:00
|
|
|
task.Config["force_pull"] = "true"
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
waitForExist(t, client, handle)
|
|
|
|
|
2018-05-14 18:23:02 +00:00
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, localDigest, container.Image)
|
2018-05-14 14:36:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 23:18:49 +00:00
|
|
|
func TestDockerDriver_SecurityOpt(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task, _, _ := dockerTask(t)
|
2017-05-19 23:59:07 +00:00
|
|
|
task.Config["security_opt"] = []string{"seccomp=unconfined"}
|
2017-05-19 23:18:49 +00:00
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2017-05-19 23:18:49 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
2017-05-19 23:18:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(task.Config["security_opt"], container.HostConfig.SecurityOpt) {
|
|
|
|
t.Errorf("Security Opts don't match.\nExpected:\n%s\nGot:\n%s\n", task.Config["security_opt"], container.HostConfig.SecurityOpt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:56:57 +00:00
|
|
|
func TestDockerDriver_Capabilities(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
2018-01-21 11:14:24 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Capabilities not supported on windows")
|
|
|
|
}
|
2018-01-14 18:56:57 +00:00
|
|
|
|
2018-01-21 11:14:24 +00:00
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
CapAdd []string
|
|
|
|
CapDrop []string
|
|
|
|
Whitelist string
|
|
|
|
StartError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "default-whitelist-add-allowed",
|
|
|
|
CapAdd: []string{"fowner", "mknod"},
|
|
|
|
CapDrop: []string{"all"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "default-whitelist-add-forbidden",
|
|
|
|
CapAdd: []string{"net_admin"},
|
|
|
|
StartError: "net_admin",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "default-whitelist-drop-existing",
|
|
|
|
CapDrop: []string{"fowner", "mknod"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "restrictive-whitelist-drop-all",
|
|
|
|
CapDrop: []string{"all"},
|
|
|
|
Whitelist: "fowner,mknod",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "restrictive-whitelist-add-allowed",
|
|
|
|
CapAdd: []string{"fowner", "mknod"},
|
|
|
|
CapDrop: []string{"all"},
|
|
|
|
Whitelist: "fowner,mknod",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "restrictive-whitelist-add-forbidden",
|
|
|
|
CapAdd: []string{"net_admin", "mknod"},
|
|
|
|
CapDrop: []string{"all"},
|
|
|
|
Whitelist: "fowner,mknod",
|
|
|
|
StartError: "net_admin",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "permissive-whitelist",
|
|
|
|
CapAdd: []string{"net_admin", "mknod"},
|
|
|
|
Whitelist: "all",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "permissive-whitelist-add-all",
|
|
|
|
CapAdd: []string{"all"},
|
|
|
|
Whitelist: "all",
|
|
|
|
},
|
|
|
|
}
|
2018-01-14 18:56:57 +00:00
|
|
|
|
2018-01-21 11:14:24 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
client := newTestDockerClient(t)
|
|
|
|
task, _, _ := dockerTask(t)
|
|
|
|
if len(tc.CapAdd) > 0 {
|
|
|
|
task.Config["cap_add"] = tc.CapAdd
|
|
|
|
}
|
|
|
|
if len(tc.CapDrop) > 0 {
|
|
|
|
task.Config["cap_drop"] = tc.CapDrop
|
|
|
|
}
|
2018-01-14 18:56:57 +00:00
|
|
|
|
2018-01-21 11:14:24 +00:00
|
|
|
tctx := testDockerDriverContexts(t, task)
|
|
|
|
if tc.Whitelist != "" {
|
|
|
|
tctx.DriverCtx.config.Options[dockerCapsWhitelistConfigOption] = tc.Whitelist
|
|
|
|
}
|
2018-01-14 18:56:57 +00:00
|
|
|
|
2018-01-21 11:14:24 +00:00
|
|
|
driver := NewDockerDriver(tctx.DriverCtx)
|
|
|
|
copyImage(t, tctx.ExecCtx.TaskDir, "busybox.tar")
|
|
|
|
defer tctx.AllocDir.Destroy()
|
2018-01-14 18:56:57 +00:00
|
|
|
|
2018-01-21 11:14:24 +00:00
|
|
|
presp, err := driver.Prestart(tctx.ExecCtx, task)
|
|
|
|
defer driver.Cleanup(tctx.ExecCtx, presp.CreatedResources)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error in prestart: %v", err)
|
|
|
|
}
|
2018-01-14 18:56:57 +00:00
|
|
|
|
2018-01-21 11:14:24 +00:00
|
|
|
sresp, err := driver.Start(tctx.ExecCtx, task)
|
|
|
|
if err == nil && tc.StartError != "" {
|
|
|
|
t.Fatalf("Expected error in start: %v", tc.StartError)
|
|
|
|
} else if err != nil {
|
|
|
|
if tc.StartError == "" {
|
|
|
|
t.Fatalf("Failed to start driver: %s\nStack\n%s", err, debug.Stack())
|
|
|
|
} else if !strings.Contains(err.Error(), tc.StartError) {
|
|
|
|
t.Fatalf("Expect error containing \"%s\", got %v", tc.StartError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sresp.Handle == nil {
|
|
|
|
t.Fatalf("handle is nil\nStack\n%s", debug.Stack())
|
|
|
|
}
|
|
|
|
defer sresp.Handle.Kill()
|
|
|
|
handle := sresp.Handle.(*DockerHandle)
|
|
|
|
|
|
|
|
waitForExist(t, client, handle)
|
|
|
|
|
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error inspecting container: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tc.CapAdd, container.HostConfig.CapAdd) {
|
|
|
|
t.Errorf("CapAdd doesn't match.\nExpected:\n%s\nGot:\n%s\n", tc.CapAdd, container.HostConfig.CapAdd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tc.CapDrop, container.HostConfig.CapDrop) {
|
|
|
|
t.Errorf("CapDrop doesn't match.\nExpected:\n%s\nGot:\n%s\n", tc.CapDrop, container.HostConfig.CapDrop)
|
|
|
|
}
|
|
|
|
})
|
2018-01-14 18:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 17:36:06 +00:00
|
|
|
func TestDockerDriver_DNS(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task, _, _ := dockerTask(t)
|
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"}
|
2017-08-09 11:30:06 +00:00
|
|
|
task.Config["dns_options"] = []string{"ndots:1"}
|
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
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2016-07-12 17:36:06 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
container, err := client.InspectContainer(handle.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) {
|
2017-08-11 07:24:15 +00:00
|
|
|
t.Errorf("DNS Search Domains don't match.\nExpected:\n%s\nGot:\n%s\n", task.Config["dns_search_domains"], container.HostConfig.DNSSearch)
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
2017-08-09 11:30:06 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(task.Config["dns_options"], container.HostConfig.DNSOptions) {
|
2017-08-11 07:24:15 +00:00
|
|
|
t.Errorf("DNS Options don't match.\nExpected:\n%s\nGot:\n%s\n", task.Config["dns_options"], container.HostConfig.DNSOptions)
|
2017-08-09 11:30:06 +00:00
|
|
|
}
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 19:46:25 +00:00
|
|
|
func TestDockerDriver_MACAddress(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task, _, _ := dockerTask(t)
|
2017-07-06 19:46:25 +00:00
|
|
|
task.Config["mac_address"] = "00:16:3e:00:00:00"
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2017-07-06 19:46:25 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
2017-07-06 19:46:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.NetworkSettings.MacAddress != task.Config["mac_address"] {
|
|
|
|
t.Errorf("expected mac_address=%q but found %q", task.Config["mac_address"], container.NetworkSettings.MacAddress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 14:35:49 +00:00
|
|
|
func TestDockerWorkDir(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task, _, _ := dockerTask(t)
|
2016-08-03 14:35:49 +00:00
|
|
|
task.Config["work_dir"] = "/some/path"
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
2016-08-03 14:35:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want, got := "/some/path", container.Config.WorkingDir; want != got {
|
2017-02-28 00:00:19 +00:00
|
|
|
t.Errorf("Wrong working directory for docker job. Expect: %s, got: %s", want, got)
|
2016-08-03 14:35:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task, res, dyn := dockerTask(t)
|
2015-11-20 03:08:21 +00:00
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2016-07-12 17:36:06 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
container, err := client.InspectContainer(handle.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{}{
|
2017-09-26 22:26:33 +00:00
|
|
|
docker.Port(fmt.Sprintf("%d/tcp", res)): {},
|
|
|
|
docker.Port(fmt.Sprintf("%d/udp", res)): {},
|
|
|
|
docker.Port(fmt.Sprintf("%d/tcp", dyn)): {},
|
|
|
|
docker.Port(fmt.Sprintf("%d/udp", dyn)): {},
|
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{
|
2017-09-26 22:26:33 +00:00
|
|
|
docker.Port(fmt.Sprintf("%d/tcp", res)): {{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", res)}},
|
|
|
|
docker.Port(fmt.Sprintf("%d/udp", res)): {{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", res)}},
|
|
|
|
docker.Port(fmt.Sprintf("%d/tcp", dyn)): {{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", dyn)}},
|
|
|
|
docker.Port(fmt.Sprintf("%d/udp", dyn)): {{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) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:45:18 +00:00
|
|
|
task, res, dyn := dockerTask(t)
|
2015-11-20 03:08:21 +00:00
|
|
|
task.Config["port_map"] = []map[string]string{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-11-20 03:08:21 +00:00
|
|
|
"main": "8080",
|
|
|
|
"REDIS": "6379",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2016-07-12 17:36:06 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
container, err := client.InspectContainer(handle.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{}{
|
2017-09-26 22:26:33 +00:00
|
|
|
docker.Port("8080/tcp"): {},
|
|
|
|
docker.Port("8080/udp"): {},
|
|
|
|
docker.Port("6379/tcp"): {},
|
|
|
|
docker.Port("6379/udp"): {},
|
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{
|
2017-09-26 22:26:33 +00:00
|
|
|
docker.Port("8080/tcp"): {{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", res)}},
|
|
|
|
docker.Port("8080/udp"): {{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", res)}},
|
|
|
|
docker.Port("6379/tcp"): {{HostIP: "127.0.0.1", HostPort: fmt.Sprintf("%d", dyn)}},
|
|
|
|
docker.Port("6379/udp"): {{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{
|
2017-06-23 23:51:38 +00:00
|
|
|
"NOMAD_PORT_main": "8080",
|
|
|
|
"NOMAD_PORT_REDIS": "6379",
|
2017-07-23 02:04:36 +00:00
|
|
|
"NOMAD_HOST_PORT_main": strconv.Itoa(res),
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
sort.Strings(container.Config.Env)
|
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) {
|
2017-05-23 00:46:40 +00:00
|
|
|
t.Errorf("Expected to find %s in container environment:\n%s\n\n", search, strings.Join(container.Config.Env, "\n"))
|
2015-11-20 03:08:21 +00:00
|
|
|
}
|
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) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 11:57:31 +00:00
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "redis-demo",
|
|
|
|
User: "alice",
|
|
|
|
Driver: "docker",
|
2016-03-23 11:57:31 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-08-12 02:23:48 +00:00
|
|
|
"image": "busybox",
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "busybox.tar",
|
2016-08-12 02:23:48 +00:00
|
|
|
"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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-24 21:20:40 +00:00
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
//ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"}
|
2016-12-03 01:04:07 +00:00
|
|
|
driver := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
2016-03-23 11:57:31 +00:00
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
_, err := driver.Prestart(ctx.ExecCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-23 11:57:31 +00:00
|
|
|
// It should fail because the user "alice" does not exist on the given
|
|
|
|
// image.
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := driver.Start(ctx.ExecCtx, task)
|
2016-03-23 11:57:31 +00:00
|
|
|
if err == nil {
|
2017-06-13 21:02:11 +00:00
|
|
|
resp.Handle.Kill()
|
2016-03-23 11:57:31 +00:00
|
|
|
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) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 05:00:14 +00:00
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "redis-demo",
|
|
|
|
Driver: "docker",
|
2016-04-12 05:00:14 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "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
|
2017-11-14 23:25:52 +00:00
|
|
|
_, err := client.InspectContainer(handle.containerID)
|
2016-04-12 09:34:59 +00:00
|
|
|
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) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 02:08:17 +00:00
|
|
|
task := &structs.Task{
|
2017-01-03 18:15:42 +00:00
|
|
|
Name: "sleep",
|
|
|
|
Driver: "docker",
|
2016-05-28 02:08:17 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "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()
|
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2016-07-12 17:36:06 +00:00
|
|
|
|
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-10-07 19:37:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 21:00:27 +00:00
|
|
|
func setupDockerVolumes(t *testing.T, cfg *config.Config, hostpath string) (*structs.Task, Driver, *ExecContext, string, func()) {
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
2016-09-27 20:13:55 +00:00
|
|
|
|
|
|
|
randfn := fmt.Sprintf("test-%d", rand.Int())
|
2016-10-20 21:00:27 +00:00
|
|
|
hostfile := filepath.Join(hostpath, randfn)
|
2016-10-27 18:02:38 +00:00
|
|
|
containerPath := "/mnt/vol"
|
|
|
|
containerFile := filepath.Join(containerPath, randfn)
|
2016-09-27 20:13:55 +00:00
|
|
|
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "ls",
|
|
|
|
Env: map[string]string{"VOL_PATH": containerPath},
|
|
|
|
Driver: "docker",
|
2016-09-27 20:13:55 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
2017-02-24 21:20:40 +00:00
|
|
|
"load": "busybox.tar",
|
2016-09-27 20:13:55 +00:00
|
|
|
"command": "touch",
|
2016-10-20 21:00:27 +00:00
|
|
|
"args": []string{containerFile},
|
2016-10-27 18:02:38 +00:00
|
|
|
"volumes": []string{fmt.Sprintf("%s:${VOL_PATH}", hostpath)},
|
2016-09-27 20:13:55 +00:00
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: basicResources,
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// Build alloc and task directory structure
|
2018-06-13 22:33:25 +00:00
|
|
|
allocDir := allocdir.NewAllocDir(testlog.Logger(t), filepath.Join(cfg.AllocDir, uuid.Generate()))
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := allocDir.Build(); err != nil {
|
|
|
|
t.Fatalf("failed to build alloc dir: %v", err)
|
|
|
|
}
|
|
|
|
taskDir := allocDir.NewTaskDir(task.Name)
|
2017-03-02 23:50:18 +00:00
|
|
|
if err := taskDir.Build(false, nil, cstructs.FSIsolationImage); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
allocDir.Destroy()
|
|
|
|
t.Fatalf("failed to build task dir: %v", err)
|
|
|
|
}
|
2018-02-07 05:24:16 +00:00
|
|
|
copyImage(t, taskDir, "busybox.tar")
|
2016-12-03 01:04:07 +00:00
|
|
|
|
2018-02-07 05:24:16 +00:00
|
|
|
// Setup driver
|
2016-09-27 20:13:55 +00:00
|
|
|
alloc := mock.Alloc()
|
2018-06-13 22:33:25 +00:00
|
|
|
logger := testlog.Logger(t)
|
2018-02-07 05:24:16 +00:00
|
|
|
emitter := func(m string, args ...interface{}) {
|
|
|
|
logger.Printf("[EVENT] "+m, args...)
|
|
|
|
}
|
2018-06-13 22:33:25 +00:00
|
|
|
driverCtx := NewDriverContext(alloc.Job.Name, alloc.TaskGroup, task.Name, alloc.ID, cfg, cfg.Node, testlog.Logger(t), emitter)
|
2018-02-07 05:24:16 +00:00
|
|
|
driver := NewDockerDriver(driverCtx)
|
|
|
|
|
|
|
|
// Setup execCtx
|
2017-05-23 00:46:40 +00:00
|
|
|
envBuilder := env.NewBuilder(cfg.Node, alloc, task, cfg.Region)
|
2018-02-07 05:24:16 +00:00
|
|
|
SetEnvvars(envBuilder, driver.FSIsolation(), taskDir, cfg)
|
2017-05-23 00:46:40 +00:00
|
|
|
execCtx := NewExecContext(taskDir, envBuilder.Build())
|
2018-02-07 05:24:16 +00:00
|
|
|
|
|
|
|
// Setup cleanup function
|
2016-09-27 20:13:55 +00:00
|
|
|
cleanup := func() {
|
2016-12-03 01:04:07 +00:00
|
|
|
allocDir.Destroy()
|
2016-10-20 21:00:27 +00:00
|
|
|
if filepath.IsAbs(hostpath) {
|
|
|
|
os.RemoveAll(hostpath)
|
|
|
|
}
|
2016-09-27 20:13:55 +00:00
|
|
|
}
|
2016-10-20 21:00:27 +00:00
|
|
|
return task, driver, execCtx, hostfile, cleanup
|
2016-09-27 20:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_VolumesDisabled(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 20:43:26 +00:00
|
|
|
cfg := testConfig(t)
|
2016-10-27 19:31:53 +00:00
|
|
|
cfg.Options = map[string]string{
|
|
|
|
dockerVolumesConfigOption: "false",
|
|
|
|
"docker.cleanup.image": "false",
|
|
|
|
}
|
2016-09-27 20:13:55 +00:00
|
|
|
|
2016-10-20 21:00:27 +00:00
|
|
|
{
|
|
|
|
tmpvol, err := ioutil.TempDir("", "nomadtest_docker_volumesdisabled")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temporary dir: %v", err)
|
|
|
|
}
|
2016-09-27 20:13:55 +00:00
|
|
|
|
2016-10-20 21:00:27 +00:00
|
|
|
task, driver, execCtx, _, cleanup := setupDockerVolumes(t, cfg, tmpvol)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
_, err = driver.Prestart(execCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2016-10-20 21:00:27 +00:00
|
|
|
if _, err := driver.Start(execCtx, task); err == nil {
|
|
|
|
t.Fatalf("Started driver successfully when volumes should have been disabled.")
|
|
|
|
}
|
2016-09-27 20:13:55 +00:00
|
|
|
}
|
2016-10-20 21:00:27 +00:00
|
|
|
|
|
|
|
// Relative paths should still be allowed
|
|
|
|
{
|
|
|
|
task, driver, execCtx, fn, cleanup := setupDockerVolumes(t, cfg, ".")
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
_, err := driver.Prestart(execCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := driver.Start(execCtx, task)
|
2016-10-20 21:00:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
defer resp.Handle.Kill()
|
2016-10-20 21:00:27 +00:00
|
|
|
|
|
|
|
select {
|
2017-06-13 21:02:11 +00:00
|
|
|
case res := <-resp.Handle.WaitCh():
|
2016-10-20 21:00:27 +00:00
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("unexpected err: %v", res)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*10) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
if _, err := ioutil.ReadFile(filepath.Join(execCtx.TaskDir.Dir, fn)); err != nil {
|
2016-10-20 21:00:27 +00:00
|
|
|
t.Fatalf("unexpected error reading %s: %v", fn, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 23:20:53 +00:00
|
|
|
// Volume Drivers should be rejected (error)
|
|
|
|
{
|
|
|
|
task, driver, execCtx, _, cleanup := setupDockerVolumes(t, cfg, "fake_flocker_vol")
|
|
|
|
defer cleanup()
|
|
|
|
task.Config["volume_driver"] = "flocker"
|
|
|
|
|
|
|
|
if _, err := driver.Prestart(execCtx, task); err != nil {
|
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := driver.Start(execCtx, task); err == nil {
|
|
|
|
t.Fatalf("Started driver successfully when volume drivers should have been disabled.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_VolumesEnabled(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 20:43:26 +00:00
|
|
|
cfg := testConfig(t)
|
2016-09-27 20:13:55 +00:00
|
|
|
|
2016-10-20 21:00:27 +00:00
|
|
|
tmpvol, err := ioutil.TempDir("", "nomadtest_docker_volumesenabled")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temporary dir: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:43:26 +00:00
|
|
|
// Evaluate symlinks so it works on MacOS
|
|
|
|
tmpvol, err = filepath.EvalSymlinks(tmpvol)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error evaluating symlinks: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-10-20 21:00:27 +00:00
|
|
|
task, driver, execCtx, hostpath, cleanup := setupDockerVolumes(t, cfg, tmpvol)
|
2016-09-27 20:13:55 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
_, err = driver.Prestart(execCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := driver.Start(execCtx, task)
|
2016-09-27 20:13:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to start docker driver: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
defer resp.Handle.Kill()
|
2016-09-27 20:13:55 +00:00
|
|
|
|
|
|
|
select {
|
2017-06-13 21:02:11 +00:00
|
|
|
case res := <-resp.Handle.WaitCh():
|
2016-09-27 20:13:55 +00:00
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("unexpected err: %v", res)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*10) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ioutil.ReadFile(hostpath); err != nil {
|
|
|
|
t.Fatalf("unexpected error reading %s: %v", hostpath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 21:02:57 +00:00
|
|
|
func TestDockerDriver_Mounts(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-09-05 21:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
goodMount := map[string]interface{}{
|
|
|
|
"target": "/nomad",
|
|
|
|
"volume_options": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"labels": []interface{}{
|
|
|
|
map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
"driver_config": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "local",
|
|
|
|
"options": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"readonly": true,
|
|
|
|
"source": "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Mounts []interface{}
|
|
|
|
Error string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "good-one",
|
|
|
|
Error: "",
|
|
|
|
Mounts: []interface{}{goodMount},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "good-many",
|
|
|
|
Error: "",
|
|
|
|
Mounts: []interface{}{goodMount, goodMount, goodMount},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "multiple volume options",
|
|
|
|
Error: "Only one volume_options stanza allowed",
|
|
|
|
Mounts: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"target": "/nomad",
|
|
|
|
"volume_options": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"driver_config": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"driver_config": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "multiple driver configs",
|
|
|
|
Error: "volume driver config may only be specified once",
|
|
|
|
Mounts: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"target": "/nomad",
|
|
|
|
"volume_options": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"driver_config": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "local",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "multiple volume labels",
|
|
|
|
Error: "labels may only be",
|
|
|
|
Mounts: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"target": "/nomad",
|
|
|
|
"volume_options": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"labels": []interface{}{
|
|
|
|
map[string]string{"foo": "bar"},
|
|
|
|
map[string]string{"baz": "bam"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "multiple driver options",
|
|
|
|
Error: "driver options may only",
|
|
|
|
Mounts: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"target": "/nomad",
|
|
|
|
"volume_options": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"driver_config": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "local",
|
|
|
|
"options": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"bam": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "redis-demo",
|
|
|
|
Driver: "docker",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
|
|
|
"load": "busybox.tar",
|
|
|
|
"command": "/bin/sleep",
|
|
|
|
"args": []string{"10000"},
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Name, func(t *testing.T) {
|
|
|
|
// Build the task
|
|
|
|
task.Config["mounts"] = c.Mounts
|
|
|
|
|
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
driver := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
|
|
|
|
_, err := driver.Prestart(ctx.ExecCtx, task)
|
|
|
|
if err == nil && c.Error != "" {
|
|
|
|
t.Fatalf("expected error: %v", c.Error)
|
|
|
|
} else if err != nil {
|
|
|
|
if c.Error == "" {
|
|
|
|
t.Fatalf("unexpected error in prestart: %v", err)
|
|
|
|
} else if !strings.Contains(err.Error(), c.Error) {
|
|
|
|
t.Fatalf("expected error %q; got %v", c.Error, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
// TestDockerDriver_Cleanup ensures Cleanup removes only downloaded images.
|
|
|
|
func TestDockerDriver_Cleanup(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-01-10 21:24:45 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-01-10 21:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
imageName := "hello-world:latest"
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "cleanup_test",
|
|
|
|
Driver: "docker",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": imageName,
|
|
|
|
},
|
|
|
|
}
|
2017-02-24 21:20:40 +00:00
|
|
|
tctx := testDockerDriverContexts(t, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
defer tctx.AllocDir.Destroy()
|
|
|
|
|
|
|
|
// Run Prestart
|
|
|
|
driver := NewDockerDriver(tctx.DriverCtx).(*DockerDriver)
|
2017-05-23 00:46:40 +00:00
|
|
|
resp, err := driver.Prestart(tctx.ExecCtx, task)
|
2017-01-10 21:24:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-05-23 00:46:40 +00:00
|
|
|
res := resp.CreatedResources
|
2017-01-10 21:24:45 +00:00
|
|
|
if len(res.Resources) == 0 || len(res.Resources[dockerImageResKey]) == 0 {
|
|
|
|
t.Fatalf("no created resources: %#v", res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
2017-01-18 00:13:40 +00:00
|
|
|
rescopy := res.Copy()
|
|
|
|
if err := driver.Cleanup(tctx.ExecCtx, rescopy); err != nil {
|
2017-01-13 01:21:54 +00:00
|
|
|
t.Fatalf("Cleanup failed: %v", err)
|
|
|
|
}
|
2017-01-10 21:24:45 +00:00
|
|
|
|
2017-01-18 00:13:40 +00:00
|
|
|
// Make sure rescopy is updated
|
|
|
|
if len(rescopy.Resources) > 0 {
|
|
|
|
t.Errorf("Cleanup should have cleared resource map: %#v", rescopy.Resources)
|
|
|
|
}
|
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
// Ensure image was removed
|
2017-02-24 21:20:40 +00:00
|
|
|
tu.WaitForResult(func() (bool, error) {
|
|
|
|
if _, err := client.InspectImage(driver.driverConfig.ImageName); err == nil {
|
|
|
|
return false, fmt.Errorf("image exists but should have been removed. Does another %v container exist?", imageName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
2017-01-13 01:21:54 +00:00
|
|
|
|
|
|
|
// The image doesn't exist which shouldn't be an error when calling
|
|
|
|
// Cleanup, so call it again to make sure.
|
2017-01-14 00:46:08 +00:00
|
|
|
if err := driver.Cleanup(tctx.ExecCtx, res.Copy()); err != nil {
|
2017-01-13 01:21:54 +00:00
|
|
|
t.Fatalf("Cleanup failed: %v", err)
|
|
|
|
}
|
2017-01-10 21:24:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
func copyImage(t *testing.T, taskDir *allocdir.TaskDir, image string) {
|
|
|
|
dst := filepath.Join(taskDir.LocalDir, image)
|
2016-08-12 18:39:58 +00:00
|
|
|
copyFile(filepath.Join("./test-resources/docker", image), dst, t)
|
2016-08-12 02:23:48 +00:00
|
|
|
}
|
2017-01-23 22:48:02 +00:00
|
|
|
|
|
|
|
func TestDockerDriver_AuthConfiguration(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-10-19 23:55:10 +00:00
|
|
|
if !testutil.DockerIsConnected(t) {
|
2017-10-23 22:50:27 +00:00
|
|
|
t.Skip("Docker not connected")
|
2017-10-19 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 22:48:02 +00:00
|
|
|
path := "./test-resources/docker/auth.json"
|
|
|
|
cases := []struct {
|
|
|
|
Repo string
|
|
|
|
AuthConfig *docker.AuthConfiguration
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Repo: "lolwhat.com/what:1337",
|
2017-05-29 10:44:13 +00:00
|
|
|
AuthConfig: nil,
|
2017-01-23 22:48:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Repo: "redis:3.2",
|
|
|
|
AuthConfig: &docker.AuthConfiguration{
|
|
|
|
Username: "test",
|
|
|
|
Password: "1234",
|
|
|
|
Email: "",
|
|
|
|
ServerAddress: "https://index.docker.io/v1/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Repo: "quay.io/redis:3.2",
|
|
|
|
AuthConfig: &docker.AuthConfiguration{
|
|
|
|
Username: "test",
|
|
|
|
Password: "5678",
|
|
|
|
Email: "",
|
|
|
|
ServerAddress: "quay.io",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Repo: "other.io/redis:3.2",
|
|
|
|
AuthConfig: &docker.AuthConfiguration{
|
|
|
|
Username: "test",
|
|
|
|
Password: "abcd",
|
|
|
|
Email: "",
|
|
|
|
ServerAddress: "https://other.io/v1/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cases {
|
2017-05-29 10:44:13 +00:00
|
|
|
act, err := authFromDockerConfig(path)(c.Repo)
|
2017-01-23 22:48:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d failed: %v", i+1, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(act, c.AuthConfig) {
|
|
|
|
t.Fatalf("Test %d failed: Unexpected auth config: got %+v; want %+v", i+1, act, c.AuthConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 15:45:06 +00:00
|
|
|
|
|
|
|
func TestDockerDriver_OOMKilled(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "oom-killed",
|
|
|
|
Driver: "docker",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
|
|
|
"load": "busybox.tar",
|
|
|
|
"command": "sh",
|
|
|
|
// Incrementally creates a bigger and bigger variable.
|
|
|
|
"args": []string{"-c", "x=a; while true; do eval x='$x$x'; done"},
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 250,
|
|
|
|
MemoryMB: 10,
|
|
|
|
DiskMB: 20,
|
|
|
|
Networks: []*structs.NetworkResource{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if res.Successful() {
|
2017-11-03 22:04:59 +00:00
|
|
|
t.Fatalf("expected error, but container exited successful")
|
2017-10-27 15:45:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 18:30:52 +00:00
|
|
|
if res.Err.Error() != "OOM Killed" {
|
2017-10-27 15:45:06 +00:00
|
|
|
t.Fatalf("not killed by OOM killer: %s", res.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Successfully killed by OOM killer")
|
|
|
|
|
|
|
|
case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
2017-11-06 18:27:13 +00:00
|
|
|
|
|
|
|
func TestDockerDriver_Devices_IsInvalidConfig(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
brokenConfigs := []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"host_path": "",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"host_path": "/dev/sda1",
|
|
|
|
"cgroup_permissions": "rxb",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
test_cases := []struct {
|
|
|
|
deviceConfig interface{}
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{[]interface{}{brokenConfigs[0]}, fmt.Errorf("host path must be set in configuration for devices")},
|
|
|
|
{[]interface{}{brokenConfigs[1]}, fmt.Errorf("invalid cgroup permission string: \"rxb\"")},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range test_cases {
|
|
|
|
task, _, _ := dockerTask(t)
|
|
|
|
task.Config["devices"] = tc.deviceConfig
|
|
|
|
|
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
driver := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
|
|
|
|
if _, err := driver.Prestart(ctx.ExecCtx, task); err == nil || err.Error() != tc.err.Error() {
|
|
|
|
t.Fatalf("error expected in prestart, got %v, expected %v", err, tc.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 01:23:23 +00:00
|
|
|
|
|
|
|
func TestDockerDriver_Device_Success(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
t.Skip("test device mounts only on linux")
|
|
|
|
}
|
|
|
|
|
2017-11-13 01:47:55 +00:00
|
|
|
hostPath := "/dev/random"
|
|
|
|
containerPath := "/dev/myrandom"
|
|
|
|
perms := "rwm"
|
|
|
|
|
2017-12-12 01:23:14 +00:00
|
|
|
expectedDevice := docker.Device{
|
|
|
|
PathOnHost: hostPath,
|
|
|
|
PathInContainer: containerPath,
|
|
|
|
CgroupPermissions: perms,
|
|
|
|
}
|
2017-11-09 01:23:23 +00:00
|
|
|
config := map[string]interface{}{
|
2017-11-13 01:47:55 +00:00
|
|
|
"host_path": hostPath,
|
|
|
|
"container_path": containerPath,
|
2017-11-09 01:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
task, _, _ := dockerTask(t)
|
|
|
|
task.Config["devices"] = []interface{}{config}
|
|
|
|
|
2017-11-13 01:47:55 +00:00
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
2017-11-09 01:23:23 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
waitForExist(t, client, handle)
|
2017-11-13 01:47:55 +00:00
|
|
|
|
2017-11-14 23:25:52 +00:00
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
2017-11-13 01:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2017-11-09 01:23:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 01:47:55 +00:00
|
|
|
assert.NotEmpty(t, container.HostConfig.Devices, "Expected one device")
|
|
|
|
assert.Equal(t, expectedDevice, container.HostConfig.Devices[0], "Incorrect device ")
|
2018-01-23 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerDriver_Entrypoint(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
entrypoint := []string{"/bin/sh", "-c"}
|
|
|
|
task, _, _ := dockerTask(t)
|
|
|
|
task.Config["entrypoint"] = entrypoint
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
waitForExist(t, client, handle)
|
|
|
|
|
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-11-13 01:47:55 +00:00
|
|
|
|
2018-01-23 22:05:00 +00:00
|
|
|
require.Len(t, container.Config.Entrypoint, 2, "Expected one entrypoint")
|
|
|
|
require.Equal(t, entrypoint, container.Config.Entrypoint, "Incorrect entrypoint ")
|
2017-11-09 01:23:23 +00:00
|
|
|
}
|
2017-11-30 21:53:35 +00:00
|
|
|
|
|
|
|
func TestDockerDriver_Kill(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tasks started with a signal that is not supported should not error
|
2017-12-06 21:52:44 +00:00
|
|
|
task := &structs.Task{
|
|
|
|
Name: "nc-demo",
|
|
|
|
Driver: "docker",
|
|
|
|
KillSignal: "SIGKILL",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"load": "busybox.tar",
|
|
|
|
"image": "busybox",
|
|
|
|
"command": "/bin/nc",
|
|
|
|
"args": []string{"-l", "127.0.0.1", "-p", "0"},
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: basicResources,
|
|
|
|
}
|
2017-11-30 21:53:35 +00:00
|
|
|
|
2017-12-06 21:52:44 +00:00
|
|
|
ctx := testDockerDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewDockerDriver(ctx.DriverCtx)
|
|
|
|
copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
|
2017-11-30 21:53:35 +00:00
|
|
|
|
2017-12-06 21:52:44 +00:00
|
|
|
_, err := d.Prestart(ctx.ExecCtx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-11-30 21:53:35 +00:00
|
|
|
|
2017-12-06 21:52:44 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.NotNil(resp.Handle)
|
2017-11-30 21:53:35 +00:00
|
|
|
|
2017-12-06 21:52:44 +00:00
|
|
|
handle := resp.Handle.(*DockerHandle)
|
|
|
|
waitForExist(t, client, handle)
|
|
|
|
err = handle.Kill()
|
|
|
|
assert.Nil(err)
|
2017-11-30 21:53:35 +00:00
|
|
|
}
|
2018-01-27 13:38:29 +00:00
|
|
|
|
|
|
|
func TestDockerDriver_ReadonlyRootfs(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
task, _, _ := dockerTask(t)
|
|
|
|
task.Config["readonly_rootfs"] = true
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
waitForExist(t, client, handle)
|
|
|
|
|
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
|
|
|
assert.Nil(t, err, "Error inspecting container: %v", err)
|
|
|
|
|
|
|
|
assert.True(t, container.HostConfig.ReadonlyRootfs, "ReadonlyRootfs option not set")
|
|
|
|
}
|
2018-02-01 22:21:47 +00:00
|
|
|
|
2018-03-16 00:52:43 +00:00
|
|
|
// fakeDockerClient can be used in places that accept an interface for the
|
|
|
|
// docker client such as createContainer.
|
|
|
|
type fakeDockerClient struct{}
|
|
|
|
|
|
|
|
func (fakeDockerClient) CreateContainer(docker.CreateContainerOptions) (*docker.Container, error) {
|
|
|
|
return nil, fmt.Errorf("volume is attached on another node")
|
|
|
|
}
|
|
|
|
func (fakeDockerClient) InspectContainer(id string) (*docker.Container, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
func (fakeDockerClient) ListContainers(docker.ListContainersOptions) ([]docker.APIContainers, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
func (fakeDockerClient) RemoveContainer(opts docker.RemoveContainerOptions) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestDockerDriver_VolumeError asserts volume related errors when creating a
|
|
|
|
// container are recoverable.
|
|
|
|
func TestDockerDriver_VolumeError(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup
|
|
|
|
task, _, _ := dockerTask(t)
|
|
|
|
tctx := testDockerDriverContexts(t, task)
|
|
|
|
driver := NewDockerDriver(tctx.DriverCtx).(*DockerDriver)
|
|
|
|
driver.driverConfig = &DockerDriverConfig{ImageName: "test"}
|
|
|
|
|
|
|
|
// assert volume error is recoverable
|
|
|
|
_, err := driver.createContainer(fakeDockerClient{}, docker.CreateContainerOptions{})
|
|
|
|
require.True(t, structs.IsRecoverable(err))
|
|
|
|
}
|
|
|
|
|
2018-02-01 22:21:47 +00:00
|
|
|
func TestDockerDriver_AdvertiseIPv6Address(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
2018-02-06 00:24:30 +00:00
|
|
|
|
2018-02-01 22:21:47 +00:00
|
|
|
expectedPrefix := "2001:db8:1::242:ac11"
|
|
|
|
expectedAdvertise := true
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "nc-demo",
|
|
|
|
Driver: "docker",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "busybox",
|
|
|
|
"load": "busybox.tar",
|
|
|
|
"command": "/bin/nc",
|
|
|
|
"args": []string{"-l", "127.0.0.1", "-p", "0"},
|
|
|
|
"advertise_ipv6_address": expectedAdvertise,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := newTestDockerClient(t)
|
2018-02-06 00:24:30 +00:00
|
|
|
|
|
|
|
// Make sure IPv6 is enabled
|
|
|
|
net, err := client.NetworkInfo("bridge")
|
|
|
|
if err != nil {
|
|
|
|
t.Skip("error retrieving bridge network information, skipping")
|
|
|
|
}
|
|
|
|
if net == nil || !net.EnableIPv6 {
|
|
|
|
t.Skip("IPv6 not enabled on bridge network, skipping")
|
|
|
|
}
|
|
|
|
|
2018-02-01 22:21:47 +00:00
|
|
|
tctx := testDockerDriverContexts(t, task)
|
|
|
|
driver := NewDockerDriver(tctx.DriverCtx)
|
|
|
|
copyImage(t, tctx.ExecCtx.TaskDir, "busybox.tar")
|
|
|
|
defer tctx.AllocDir.Destroy()
|
|
|
|
|
|
|
|
presp, err := driver.Prestart(tctx.ExecCtx, task)
|
|
|
|
defer driver.Cleanup(tctx.ExecCtx, presp.CreatedResources)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error in prestart: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sresp, err := driver.Start(tctx.ExecCtx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error in start: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sresp.Handle == nil {
|
|
|
|
t.Fatalf("handle is nil\nStack\n%s", debug.Stack())
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expectedAdvertise, sresp.Network.AutoAdvertise, "Wrong autoadvertise. Expect: %s, got: %s", expectedAdvertise, sresp.Network.AutoAdvertise)
|
|
|
|
|
|
|
|
if !strings.HasPrefix(sresp.Network.IP, expectedPrefix) {
|
2018-02-06 00:24:30 +00:00
|
|
|
t.Fatalf("Got IP address %q want ip address with prefix %q", sresp.Network.IP, expectedPrefix)
|
2018-02-01 22:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer sresp.Handle.Kill()
|
|
|
|
handle := sresp.Handle.(*DockerHandle)
|
|
|
|
|
|
|
|
waitForExist(t, client, handle)
|
|
|
|
|
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error inspecting container: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(container.NetworkSettings.GlobalIPv6Address, expectedPrefix) {
|
|
|
|
t.Fatalf("Got GlobalIPv6address %s want GlobalIPv6address with prefix %s", expectedPrefix, container.NetworkSettings.GlobalIPv6Address)
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 14:36:40 +00:00
|
|
|
|
|
|
|
func TestParseDockerImage(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Image string
|
|
|
|
Repo string
|
|
|
|
Tag string
|
|
|
|
}{
|
|
|
|
{"library/hello-world:1.0", "library/hello-world", "1.0"},
|
|
|
|
{"library/hello-world", "library/hello-world", "latest"},
|
|
|
|
{"library/hello-world:latest", "library/hello-world", "latest"},
|
|
|
|
{"library/hello-world@sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77", "library/hello-world@sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77", ""},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.Image, func(t *testing.T) {
|
|
|
|
repo, tag := parseDockerImage(test.Image)
|
2018-05-14 18:23:48 +00:00
|
|
|
require.Equal(t, test.Repo, repo)
|
|
|
|
require.Equal(t, test.Tag, tag)
|
2018-05-14 14:36:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 18:24:03 +00:00
|
|
|
|
|
|
|
func TestDockerImageRef(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Image string
|
|
|
|
Repo string
|
|
|
|
Tag string
|
|
|
|
}{
|
|
|
|
{"library/hello-world:1.0", "library/hello-world", "1.0"},
|
|
|
|
{"library/hello-world:latest", "library/hello-world", "latest"},
|
|
|
|
{"library/hello-world@sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77", "library/hello-world@sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77", ""},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.Image, func(t *testing.T) {
|
|
|
|
image := dockerImageRef(test.Repo, test.Tag)
|
|
|
|
require.Equal(t, test.Image, image)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-07-16 20:43:34 +00:00
|
|
|
|
|
|
|
func TestDockerDriver_CPUCFSPeriod(t *testing.T) {
|
|
|
|
if !tu.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if !testutil.DockerIsConnected(t) {
|
|
|
|
t.Skip("Docker not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
task, _, _ := dockerTask(t)
|
|
|
|
task.Config["cpu_hard_limit"] = true
|
|
|
|
task.Config["cpu_cfs_period"] = 1000000
|
|
|
|
|
|
|
|
client, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
waitForExist(t, client, handle)
|
|
|
|
|
|
|
|
container, err := client.InspectContainer(handle.ContainerID())
|
|
|
|
assert.Nil(t, err, "Error inspecting container: %v", err)
|
|
|
|
assert.Equal(t, int64(1000000), container.HostConfig.CPUPeriod, "cpu_cfs_period option incorrectly set")
|
|
|
|
}
|