2013-01-18 19:13:39 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-06-11 18:46:23 -04:00
|
|
|
"bytes"
|
2013-04-19 23:46:07 -04:00
|
|
|
"fmt"
|
2013-03-11 08:42:36 -04:00
|
|
|
"io"
|
2014-07-24 16:37:44 -04:00
|
|
|
std_log "log"
|
2013-04-19 23:46:07 -04:00
|
|
|
"net"
|
2013-11-13 14:25:55 -05:00
|
|
|
"net/url"
|
2013-01-18 19:13:39 -05:00
|
|
|
"os"
|
2013-10-18 01:40:41 -04:00
|
|
|
"path/filepath"
|
2014-05-09 03:09:33 -04:00
|
|
|
"runtime"
|
2013-05-30 09:45:39 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2013-06-21 22:40:42 -04:00
|
|
|
"syscall"
|
2013-01-18 19:13:39 -05:00
|
|
|
"testing"
|
2013-04-02 13:06:49 -04:00
|
|
|
"time"
|
2014-05-09 14:09:59 -04:00
|
|
|
|
2014-10-24 18:11:48 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2014-10-08 13:03:57 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/nat"
|
2014-08-12 12:10:43 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2014-10-30 08:48:30 -04:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
"github.com/docker/docker/utils"
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
|
2013-06-24 17:15:52 -04:00
|
|
|
const (
|
2014-02-27 07:47:59 -05:00
|
|
|
unitTestImageName = "docker-test-image"
|
|
|
|
unitTestImageID = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
|
|
|
|
unitTestImageIDShort = "83599e29c455"
|
|
|
|
unitTestNetworkBridge = "testdockbr0"
|
|
|
|
unitTestStoreBase = "/var/lib/docker/unit-tests"
|
2014-08-06 15:55:50 -04:00
|
|
|
unitTestDockerTmpdir = "/var/lib/docker/tmp"
|
2014-02-27 07:47:59 -05:00
|
|
|
testDaemonAddr = "127.0.0.1:4270"
|
|
|
|
testDaemonProto = "tcp"
|
|
|
|
testDaemonHttpsProto = "tcp"
|
|
|
|
testDaemonHttpsAddr = "localhost:4271"
|
|
|
|
testDaemonRogueHttpsAddr = "localhost:4272"
|
2013-06-24 17:15:52 -04:00
|
|
|
)
|
2013-02-26 20:45:46 -05:00
|
|
|
|
2013-07-24 18:48:18 -04:00
|
|
|
var (
|
2014-04-17 17:43:01 -04:00
|
|
|
// FIXME: globalDaemon is deprecated by globalEngine. All tests should be converted.
|
|
|
|
globalDaemon *daemon.Daemon
|
2014-02-27 07:47:59 -05:00
|
|
|
globalEngine *engine.Engine
|
|
|
|
globalHttpsEngine *engine.Engine
|
|
|
|
globalRogueHttpsEngine *engine.Engine
|
|
|
|
startFds int
|
|
|
|
startGoroutines int
|
2013-07-24 18:48:18 -04:00
|
|
|
)
|
2013-06-24 21:22:02 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
// FIXME: nuke() is deprecated by Daemon.Nuke()
|
|
|
|
func nuke(daemon *daemon.Daemon) error {
|
|
|
|
return daemon.Nuke()
|
2013-03-12 03:08:41 -04:00
|
|
|
}
|
|
|
|
|
2013-11-14 01:10:20 -05:00
|
|
|
// FIXME: cleanup and nuke are redundant.
|
|
|
|
func cleanup(eng *engine.Engine, t *testing.T) error {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemonFromEngine(eng, t)
|
|
|
|
for _, container := range daemon.List() {
|
2013-06-24 21:22:02 -04:00
|
|
|
container.Kill()
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon.Destroy(container)
|
2013-06-24 21:22:02 -04:00
|
|
|
}
|
2013-12-12 17:39:35 -05:00
|
|
|
job := eng.Job("images")
|
|
|
|
images, err := job.Stdout.AddTable()
|
2013-06-24 21:22:02 -04:00
|
|
|
if err != nil {
|
2013-12-12 17:39:35 -05:00
|
|
|
t.Fatal(err)
|
2013-06-24 21:22:02 -04:00
|
|
|
}
|
2013-12-12 17:39:35 -05:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, image := range images.Data {
|
2014-01-29 15:31:49 -05:00
|
|
|
if image.Get("Id") != unitTestImageID {
|
2014-01-31 14:00:06 -05:00
|
|
|
eng.Job("image_delete", image.Get("Id")).Run()
|
2013-06-24 21:22:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-02-13 17:01:44 -05:00
|
|
|
func init() {
|
2013-11-25 23:48:03 -05:00
|
|
|
// Always use the same driver (vfs) for all integration tests.
|
|
|
|
// To test other drivers, we need a dedicated driver validation suite.
|
|
|
|
os.Setenv("DOCKER_DRIVER", "vfs")
|
2013-08-29 18:55:24 -04:00
|
|
|
os.Setenv("TEST", "1")
|
2014-08-06 15:55:50 -04:00
|
|
|
os.Setenv("DOCKER_TMPDIR", unitTestDockerTmpdir)
|
2013-08-29 18:55:24 -04:00
|
|
|
|
2013-02-14 18:37:45 -05:00
|
|
|
// Hack to run sys init during unit testing
|
2014-08-08 19:27:13 -04:00
|
|
|
if reexec.Init() {
|
2013-03-14 05:30:50 -04:00
|
|
|
return
|
2013-02-13 17:01:44 -05:00
|
|
|
}
|
2013-02-14 18:37:45 -05:00
|
|
|
|
2013-06-21 22:40:42 -04:00
|
|
|
if uid := syscall.Geteuid(); uid != 0 {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Fatalf("docker tests need to be run as root")
|
2013-03-15 05:03:41 -04:00
|
|
|
}
|
|
|
|
|
2013-10-18 01:40:41 -04:00
|
|
|
// Copy dockerinit into our current testing directory, if provided (so we can test a separate dockerinit binary)
|
|
|
|
if dockerinit := os.Getenv("TEST_DOCKERINIT_PATH"); dockerinit != "" {
|
|
|
|
src, err := os.Open(dockerinit)
|
|
|
|
if err != nil {
|
2014-08-13 21:36:26 -04:00
|
|
|
log.Fatalf("Unable to open TEST_DOCKERINIT_PATH: %s", err)
|
2013-10-18 01:40:41 -04:00
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
dst, err := os.OpenFile(filepath.Join(filepath.Dir(utils.SelfPath()), "dockerinit"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0555)
|
|
|
|
if err != nil {
|
2014-08-13 21:36:26 -04:00
|
|
|
log.Fatalf("Unable to create dockerinit in test directory: %s", err)
|
2013-10-18 01:40:41 -04:00
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
if _, err := io.Copy(dst, src); err != nil {
|
2014-08-13 21:36:26 -04:00
|
|
|
log.Fatalf("Unable to copy dockerinit to TEST_DOCKERINIT_PATH: %s", err)
|
2013-10-18 01:40:41 -04:00
|
|
|
}
|
|
|
|
dst.Close()
|
|
|
|
src.Close()
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
// Setup the base daemon, which will be duplicated for each test.
|
2013-10-16 16:10:20 -04:00
|
|
|
// (no tests are run directly in the base)
|
|
|
|
setupBaseImage()
|
2013-05-06 05:31:22 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
// Create the "global daemon" with a long-running daemons for integration tests
|
2013-10-16 16:10:20 -04:00
|
|
|
spawnGlobalDaemon()
|
2014-02-27 07:47:59 -05:00
|
|
|
spawnLegitHttpsDaemon()
|
|
|
|
spawnRogueHttpsDaemon()
|
2014-05-09 03:09:33 -04:00
|
|
|
startFds, startGoroutines = utils.GetTotalUsedFds(), runtime.NumGoroutine()
|
2013-10-16 16:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupBaseImage() {
|
2014-07-24 16:37:44 -04:00
|
|
|
eng := newTestEngine(std_log.New(os.Stderr, "", 0), false, unitTestStoreBase)
|
2014-06-02 15:54:33 -04:00
|
|
|
job := eng.Job("image_inspect", unitTestImageName)
|
2014-01-29 15:16:47 -05:00
|
|
|
img, _ := job.Stdout.AddEnv()
|
2013-07-08 18:23:04 -04:00
|
|
|
// If the unit test is not found, try to download it.
|
2014-06-02 15:54:33 -04:00
|
|
|
if err := job.Run(); err != nil || img.Get("Id") != unitTestImageID {
|
2013-07-08 18:23:04 -04:00
|
|
|
// Retrieve the Image
|
2014-01-22 16:35:35 -05:00
|
|
|
job = eng.Job("pull", unitTestImageName)
|
2014-08-12 12:10:43 -04:00
|
|
|
job.Stdout.Add(ioutils.NopWriteCloser(os.Stdout))
|
2014-01-22 16:35:35 -05:00
|
|
|
if err := job.Run(); err != nil {
|
2013-11-11 12:05:38 -05:00
|
|
|
log.Fatalf("Unable to pull the test image: %s", err)
|
2013-07-08 18:23:04 -04:00
|
|
|
}
|
2013-02-14 18:37:45 -05:00
|
|
|
}
|
2013-10-16 16:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func spawnGlobalDaemon() {
|
2014-04-17 17:43:01 -04:00
|
|
|
if globalDaemon != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Global daemon already exists. Skipping.")
|
2013-10-16 16:10:20 -04:00
|
|
|
return
|
|
|
|
}
|
2014-07-24 16:37:44 -04:00
|
|
|
t := std_log.New(os.Stderr, "", 0)
|
2013-10-27 03:13:45 -04:00
|
|
|
eng := NewTestEngine(t)
|
2013-11-14 01:10:20 -05:00
|
|
|
globalEngine = eng
|
2014-04-17 17:43:01 -04:00
|
|
|
globalDaemon = mkDaemonFromEngine(eng, t)
|
2013-10-16 16:10:20 -04:00
|
|
|
|
2013-06-24 17:59:37 -04:00
|
|
|
// Spawn a Daemon
|
|
|
|
go func() {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Spawning global daemon for integration tests")
|
2013-10-27 03:13:45 -04:00
|
|
|
listenURL := &url.URL{
|
2013-11-13 14:25:55 -05:00
|
|
|
Scheme: testDaemonProto,
|
|
|
|
Host: testDaemonAddr,
|
2013-10-27 03:13:45 -04:00
|
|
|
}
|
|
|
|
job := eng.Job("serveapi", listenURL.String())
|
2013-12-12 17:39:35 -05:00
|
|
|
job.SetenvBool("Logging", true)
|
2013-10-27 03:13:45 -04:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
log.Fatalf("Unable to spawn the test daemon: %s", err)
|
2013-06-24 17:59:37 -04:00
|
|
|
}
|
|
|
|
}()
|
2014-02-15 23:49:50 -05:00
|
|
|
|
2013-06-24 21:22:02 -04:00
|
|
|
// Give some time to ListenAndServer to actually start
|
2013-10-16 16:10:20 -04:00
|
|
|
// FIXME: use inmem transports instead of tcp
|
2013-06-24 21:22:02 -04:00
|
|
|
time.Sleep(time.Second)
|
2014-02-15 23:49:50 -05:00
|
|
|
|
|
|
|
if err := eng.Job("acceptconnections").Run(); err != nil {
|
|
|
|
log.Fatalf("Unable to accept connections for test api: %s", err)
|
|
|
|
}
|
2013-02-13 17:01:44 -05:00
|
|
|
}
|
|
|
|
|
2014-02-27 07:47:59 -05:00
|
|
|
func spawnLegitHttpsDaemon() {
|
|
|
|
if globalHttpsEngine != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
globalHttpsEngine = spawnHttpsDaemon(testDaemonHttpsAddr, "fixtures/https/ca.pem",
|
|
|
|
"fixtures/https/server-cert.pem", "fixtures/https/server-key.pem")
|
|
|
|
}
|
|
|
|
|
|
|
|
func spawnRogueHttpsDaemon() {
|
|
|
|
if globalRogueHttpsEngine != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
globalRogueHttpsEngine = spawnHttpsDaemon(testDaemonRogueHttpsAddr, "fixtures/https/ca.pem",
|
|
|
|
"fixtures/https/server-rogue-cert.pem", "fixtures/https/server-rogue-key.pem")
|
|
|
|
}
|
|
|
|
|
|
|
|
func spawnHttpsDaemon(addr, cacert, cert, key string) *engine.Engine {
|
2014-07-24 16:37:44 -04:00
|
|
|
t := std_log.New(os.Stderr, "", 0)
|
2014-02-27 07:47:59 -05:00
|
|
|
root, err := newTestDirectory(unitTestStoreBase)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-08 05:12:39 -04:00
|
|
|
// FIXME: here we don't use NewTestEngine because it configures the daemon with Autorestart=false,
|
2014-02-27 07:47:59 -05:00
|
|
|
// and we want to set it to true.
|
|
|
|
|
|
|
|
eng := newTestEngine(t, true, root)
|
|
|
|
|
|
|
|
// Spawn a Daemon
|
|
|
|
go func() {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Spawning https daemon for integration tests")
|
2014-02-27 07:47:59 -05:00
|
|
|
listenURL := &url.URL{
|
|
|
|
Scheme: testDaemonHttpsProto,
|
|
|
|
Host: addr,
|
|
|
|
}
|
|
|
|
job := eng.Job("serveapi", listenURL.String())
|
|
|
|
job.SetenvBool("Logging", true)
|
|
|
|
job.SetenvBool("Tls", true)
|
|
|
|
job.SetenvBool("TlsVerify", true)
|
|
|
|
job.Setenv("TlsCa", cacert)
|
|
|
|
job.Setenv("TlsCert", cert)
|
|
|
|
job.Setenv("TlsKey", key)
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
log.Fatalf("Unable to spawn the test daemon: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Give some time to ListenAndServer to actually start
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
if err := eng.Job("acceptconnections").Run(); err != nil {
|
|
|
|
log.Fatalf("Unable to accept connections for test api: %s", err)
|
|
|
|
}
|
|
|
|
return eng
|
|
|
|
}
|
|
|
|
|
2013-05-24 21:32:21 -04:00
|
|
|
// FIXME: test that ImagePull(json=true) send correct json output
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
func GetTestImage(daemon *daemon.Daemon) *image.Image {
|
|
|
|
imgs, err := daemon.Graph().Map()
|
2013-02-26 20:45:46 -05:00
|
|
|
if err != nil {
|
2013-11-29 19:53:20 -05:00
|
|
|
log.Fatalf("Unable to get the test image: %s", err)
|
2013-02-26 20:45:46 -05:00
|
|
|
}
|
2013-08-31 23:31:21 -04:00
|
|
|
for _, image := range imgs {
|
|
|
|
if image.ID == unitTestImageID {
|
|
|
|
return image
|
2013-06-27 20:53:57 -04:00
|
|
|
}
|
|
|
|
}
|
2014-04-17 17:43:01 -04:00
|
|
|
log.Fatalf("Test image %v not found in %s: %s", unitTestImageID, daemon.Graph().Root, imgs)
|
2013-10-09 09:47:49 -04:00
|
|
|
return nil
|
2013-02-26 20:45:46 -05:00
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
func TestDaemonCreate(t *testing.T) {
|
|
|
|
daemon := mkDaemon(t)
|
|
|
|
defer nuke(daemon)
|
2013-01-18 19:13:39 -05:00
|
|
|
|
|
|
|
// Make sure we start we 0 containers
|
2014-04-17 17:43:01 -04:00
|
|
|
if len(daemon.List()) != 0 {
|
|
|
|
t.Errorf("Expected 0 containers, %v found", len(daemon.List()))
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
2013-05-07 14:18:13 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
container, _, err := daemon.Create(&runconfig.Config{
|
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-03-23 15:39:09 -04:00
|
|
|
Cmd: []string{"ls", "-al"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2014-09-25 17:23:59 -04:00
|
|
|
&runconfig.HostConfig{},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2014-04-17 17:43:01 -04:00
|
|
|
if err := daemon.Destroy(container); err != nil {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Make sure we can find the newly created container with List()
|
2014-04-17 17:43:01 -04:00
|
|
|
if len(daemon.List()) != 1 {
|
|
|
|
t.Errorf("Expected 1 container, %v found", len(daemon.List()))
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the container List() returns is the right one
|
2014-04-17 17:43:01 -04:00
|
|
|
if daemon.List()[0].ID != container.ID {
|
|
|
|
t.Errorf("Unexpected container %v returned by List", daemon.List()[0])
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we can get the container with Get()
|
2014-12-16 18:06:35 -05:00
|
|
|
if _, err := daemon.Get(container.ID); err != nil {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Errorf("Unable to get newly created container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it is the right container
|
2014-12-16 18:06:35 -05:00
|
|
|
if c, _ := daemon.Get(container.ID); c != container {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Errorf("Get() returned the wrong container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure Exists returns it as existing
|
2014-04-17 17:43:01 -04:00
|
|
|
if !daemon.Exists(container.ID) {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Errorf("Exists() returned false for a newly created container")
|
|
|
|
}
|
2013-05-07 14:18:13 -04:00
|
|
|
|
2013-11-10 16:50:34 -05:00
|
|
|
// Test that conflict error displays correct details
|
2014-04-17 17:43:01 -04:00
|
|
|
testContainer, _, _ := daemon.Create(
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2014-04-17 17:43:01 -04:00
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-11-10 16:50:34 -05:00
|
|
|
Cmd: []string{"ls", "-al"},
|
|
|
|
},
|
2014-09-25 17:23:59 -04:00
|
|
|
&runconfig.HostConfig{},
|
2013-11-10 16:50:34 -05:00
|
|
|
"conflictname",
|
|
|
|
)
|
2014-09-25 17:23:59 -04:00
|
|
|
if _, _, err := daemon.Create(&runconfig.Config{Image: GetTestImage(daemon).ID, Cmd: []string{"ls", "-al"}}, &runconfig.HostConfig{}, testContainer.Name); err == nil || !strings.Contains(err.Error(), utils.TruncateID(testContainer.ID)) {
|
2013-11-10 16:50:34 -05:00
|
|
|
t.Fatalf("Name conflict error doesn't include the correct short id. Message was: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
// Make sure create with bad parameters returns an error
|
2014-09-25 17:23:59 -04:00
|
|
|
if _, _, err = daemon.Create(&runconfig.Config{Image: GetTestImage(daemon).ID}, &runconfig.HostConfig{}, ""); err == nil {
|
2013-05-07 14:18:13 -04:00
|
|
|
t.Fatal("Builder.Create should throw an error when Cmd is missing")
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
if _, _, err := daemon.Create(
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2014-04-17 17:43:01 -04:00
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-05-07 14:18:13 -04:00
|
|
|
Cmd: []string{},
|
|
|
|
},
|
2014-09-25 17:23:59 -04:00
|
|
|
&runconfig.HostConfig{},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-10-08 21:42:53 -04:00
|
|
|
); err == nil {
|
2013-05-07 14:18:13 -04:00
|
|
|
t.Fatal("Builder.Create should throw an error when Cmd is empty")
|
|
|
|
}
|
2013-10-03 10:46:07 -04:00
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
config := &runconfig.Config{
|
2014-04-17 17:43:01 -04:00
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-10-03 10:46:07 -04:00
|
|
|
Cmd: []string{"/bin/ls"},
|
|
|
|
PortSpecs: []string{"80"},
|
|
|
|
}
|
2014-09-25 17:23:59 -04:00
|
|
|
container, _, err = daemon.Create(config, &runconfig.HostConfig{}, "")
|
2013-10-03 10:46:07 -04:00
|
|
|
|
2014-06-08 02:37:31 -04:00
|
|
|
_, err = daemon.Commit(container, "testrepo", "testtag", "", "", true, config)
|
2013-10-03 10:46:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2013-10-30 14:13:10 -04:00
|
|
|
|
|
|
|
// test expose 80:8000
|
2014-04-17 17:43:01 -04:00
|
|
|
container, warnings, err := daemon.Create(&runconfig.Config{
|
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-10-30 14:13:10 -04:00
|
|
|
Cmd: []string{"ls", "-al"},
|
|
|
|
PortSpecs: []string{"80:8000"},
|
|
|
|
},
|
2014-09-25 17:23:59 -04:00
|
|
|
&runconfig.HostConfig{},
|
2013-10-30 14:13:10 -04:00
|
|
|
"",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-30 16:18:48 -04:00
|
|
|
if warnings == nil || len(warnings) != 1 {
|
2013-10-30 14:13:10 -04:00
|
|
|
t.Error("Expected a warning, got none")
|
|
|
|
}
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDestroy(t *testing.T) {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemon(t)
|
|
|
|
defer nuke(daemon)
|
2013-10-08 21:42:53 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
container, _, err := daemon.Create(&runconfig.Config{
|
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-03-23 15:39:09 -04:00
|
|
|
Cmd: []string{"ls", "-al"},
|
2014-09-25 17:23:59 -04:00
|
|
|
},
|
|
|
|
&runconfig.HostConfig{},
|
|
|
|
"")
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Destroy
|
2014-04-17 17:43:01 -04:00
|
|
|
if err := daemon.Destroy(container); err != nil {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
// Make sure daemon.Exists() behaves correctly
|
|
|
|
if daemon.Exists("test_destroy") {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Errorf("Exists() returned true")
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
// Make sure daemon.List() doesn't list the destroyed container
|
|
|
|
if len(daemon.List()) != 0 {
|
|
|
|
t.Errorf("Expected 0 container, %v found", len(daemon.List()))
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
// Make sure daemon.Get() refuses to return the unexisting container
|
2014-12-16 18:06:35 -05:00
|
|
|
if c, _ := daemon.Get(container.ID); c != nil {
|
|
|
|
t.Errorf("Got a container that should not exist")
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test double destroy
|
2014-04-17 17:43:01 -04:00
|
|
|
if err := daemon.Destroy(container); err == nil {
|
2013-01-18 19:13:39 -05:00
|
|
|
// It should have failed
|
|
|
|
t.Errorf("Double destroy did not fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemon(t)
|
|
|
|
defer nuke(daemon)
|
2013-04-24 18:35:28 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
container1, _, _ := mkContainer(daemon, []string{"_", "ls", "-al"}, t)
|
|
|
|
defer daemon.Destroy(container1)
|
2013-01-18 19:13:39 -05:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
container2, _, _ := mkContainer(daemon, []string{"_", "ls", "-al"}, t)
|
|
|
|
defer daemon.Destroy(container2)
|
2013-01-18 19:13:39 -05:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
container3, _, _ := mkContainer(daemon, []string{"_", "ls", "-al"}, t)
|
|
|
|
defer daemon.Destroy(container3)
|
2013-01-18 19:13:39 -05:00
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
if c, _ := daemon.Get(container1.ID); c != container1 {
|
|
|
|
t.Errorf("Get(test1) returned %v while expecting %v", c, container1)
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
if c, _ := daemon.Get(container2.ID); c != container2 {
|
|
|
|
t.Errorf("Get(test2) returned %v while expecting %v", c, container2)
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
if c, _ := daemon.Get(container3.ID); c != container3 {
|
|
|
|
t.Errorf("Get(test3) returned %v while expecting %v", c, container3)
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-01-25 17:09:21 -05:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
func startEchoServerContainer(t *testing.T, proto string) (*daemon.Daemon, *daemon.Container, string) {
|
2013-10-08 21:42:53 -04:00
|
|
|
var (
|
2014-05-09 14:09:59 -04:00
|
|
|
err error
|
|
|
|
id string
|
|
|
|
outputBuffer = bytes.NewBuffer(nil)
|
|
|
|
strPort string
|
|
|
|
eng = NewTestEngine(t)
|
|
|
|
daemon = mkDaemonFromEngine(eng, t)
|
|
|
|
port = 5554
|
|
|
|
p nat.Port
|
2013-10-08 21:42:53 -04:00
|
|
|
)
|
2013-11-14 01:10:20 -05:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon.Nuke()
|
2013-11-14 01:10:20 -05:00
|
|
|
}
|
|
|
|
}()
|
2013-10-08 21:42:53 -04:00
|
|
|
|
2013-05-30 09:45:39 -04:00
|
|
|
for {
|
|
|
|
port += 1
|
2013-06-11 18:46:23 -04:00
|
|
|
strPort = strconv.Itoa(port)
|
|
|
|
var cmd string
|
|
|
|
if proto == "tcp" {
|
|
|
|
cmd = "socat TCP-LISTEN:" + strPort + ",reuseaddr,fork EXEC:/bin/cat"
|
|
|
|
} else if proto == "udp" {
|
|
|
|
cmd = "socat UDP-RECVFROM:" + strPort + ",fork EXEC:/bin/cat"
|
|
|
|
} else {
|
|
|
|
t.Fatal(fmt.Errorf("Unknown protocol %v", proto))
|
|
|
|
}
|
2014-02-11 19:48:44 -05:00
|
|
|
ep := make(map[nat.Port]struct{}, 1)
|
|
|
|
p = nat.Port(fmt.Sprintf("%s/%s", strPort, proto))
|
2013-10-04 22:25:15 -04:00
|
|
|
ep[p] = struct{}{}
|
|
|
|
|
2013-11-14 01:10:20 -05:00
|
|
|
jobCreate := eng.Job("create")
|
|
|
|
jobCreate.Setenv("Image", unitTestImageID)
|
|
|
|
jobCreate.SetenvList("Cmd", []string{"sh", "-c", cmd})
|
|
|
|
jobCreate.SetenvList("PortSpecs", []string{fmt.Sprintf("%s/%s", strPort, proto)})
|
|
|
|
jobCreate.SetenvJson("ExposedPorts", ep)
|
2014-05-09 14:09:59 -04:00
|
|
|
jobCreate.Stdout.Add(outputBuffer)
|
2013-11-14 01:10:20 -05:00
|
|
|
if err := jobCreate.Run(); err != nil {
|
2013-05-30 09:45:39 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-05-09 14:09:59 -04:00
|
|
|
id = engine.Tail(outputBuffer, 1)
|
2014-04-17 17:43:01 -04:00
|
|
|
// FIXME: this relies on the undocumented behavior of daemon.Create
|
2013-11-14 01:10:20 -05:00
|
|
|
// which will return a nil error AND container if the exposed ports
|
|
|
|
// are invalid. That behavior should be fixed!
|
|
|
|
if id != "" {
|
2013-10-04 22:25:15 -04:00
|
|
|
break
|
|
|
|
}
|
2013-10-24 18:09:01 -04:00
|
|
|
t.Logf("Port %v already in use, trying another one", strPort)
|
2013-05-30 09:45:39 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2013-11-14 01:10:20 -05:00
|
|
|
|
|
|
|
jobStart := eng.Job("start", id)
|
2014-02-11 19:48:44 -05:00
|
|
|
portBindings := make(map[nat.Port][]nat.PortBinding)
|
|
|
|
portBindings[p] = []nat.PortBinding{
|
2013-10-04 22:25:15 -04:00
|
|
|
{},
|
|
|
|
}
|
2013-11-14 01:10:20 -05:00
|
|
|
if err := jobStart.SetenvJson("PortsBindings", portBindings); err != nil {
|
2013-06-11 18:46:23 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 01:10:20 -05:00
|
|
|
if err := jobStart.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := daemon.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2013-11-14 01:10:20 -05:00
|
|
|
}
|
2013-04-23 13:28:40 -04:00
|
|
|
|
|
|
|
setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
|
2014-08-31 11:20:35 -04:00
|
|
|
for !container.IsRunning() {
|
2013-04-23 13:28:40 -04:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-07-02 06:47:37 -04:00
|
|
|
// Even if the state is running, lets give some time to lxc to spawn the process
|
2014-08-31 11:20:35 -04:00
|
|
|
container.WaitStop(500 * time.Millisecond)
|
2013-07-02 06:47:37 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
strPort = container.NetworkSettings.Ports[p][0].HostPort
|
2014-04-17 17:43:01 -04:00
|
|
|
return daemon, container, strPort
|
2013-06-11 18:46:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run a container with a TCP port allocated, and test that it can receive connections on localhost
|
|
|
|
func TestAllocateTCPPortLocalhost(t *testing.T) {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon, container, port := startEchoServerContainer(t, "tcp")
|
|
|
|
defer nuke(daemon)
|
2013-06-11 18:46:23 -04:00
|
|
|
defer container.Kill()
|
|
|
|
|
2013-07-10 21:02:41 -04:00
|
|
|
for i := 0; i != 10; i++ {
|
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%v", port))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2013-06-11 18:46:23 -04:00
|
|
|
|
2013-07-10 21:02:41 -04:00
|
|
|
input := bytes.NewBufferString("well hello there\n")
|
|
|
|
_, err = conn.Write(input.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buf := make([]byte, 16)
|
|
|
|
read := 0
|
|
|
|
conn.SetReadDeadline(time.Now().Add(3 * time.Second))
|
|
|
|
read, err = conn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
if err, ok := err.(*net.OpError); ok {
|
|
|
|
if err.Err == syscall.ECONNRESET {
|
|
|
|
t.Logf("Connection reset by the proxy, socat is probably not listening yet, trying again in a sec")
|
|
|
|
conn.Close()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err.Timeout() {
|
|
|
|
t.Log("Timeout, trying again")
|
|
|
|
conn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
output := string(buf[:read])
|
|
|
|
if !strings.Contains(output, "well hello there") {
|
|
|
|
t.Fatal(fmt.Errorf("[%v] doesn't contain [well hello there]", output))
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
2013-06-11 18:46:23 -04:00
|
|
|
}
|
2013-07-10 21:02:41 -04:00
|
|
|
|
|
|
|
t.Fatal("No reply from the container")
|
2013-06-11 18:46:23 -04:00
|
|
|
}
|
|
|
|
|
2013-07-10 21:02:41 -04:00
|
|
|
// Run a container with an UDP port allocated, and test that it can receive connections on localhost
|
2013-06-11 18:46:23 -04:00
|
|
|
func TestAllocateUDPPortLocalhost(t *testing.T) {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon, container, port := startEchoServerContainer(t, "udp")
|
|
|
|
defer nuke(daemon)
|
2013-06-11 18:46:23 -04:00
|
|
|
defer container.Kill()
|
|
|
|
|
|
|
|
conn, err := net.Dial("udp", fmt.Sprintf("localhost:%v", port))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
input := bytes.NewBufferString("well hello there\n")
|
|
|
|
buf := make([]byte, 16)
|
2013-07-10 21:02:41 -04:00
|
|
|
// Try for a minute, for some reason the select in socat may take ages
|
|
|
|
// to return even though everything on the path seems fine (i.e: the
|
|
|
|
// UDPProxy forwards the traffic correctly and you can see the packets
|
|
|
|
// on the interface from within the container).
|
|
|
|
for i := 0; i != 120; i++ {
|
2013-06-11 18:46:23 -04:00
|
|
|
_, err := conn.Write(input.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-10 21:02:41 -04:00
|
|
|
conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
|
2013-06-11 18:46:23 -04:00
|
|
|
read, err := conn.Read(buf)
|
|
|
|
if err == nil {
|
|
|
|
output := string(buf[:read])
|
|
|
|
if strings.Contains(output, "well hello there") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatal("No reply from the container")
|
2013-04-19 23:46:07 -04:00
|
|
|
}
|
|
|
|
|
2013-01-25 17:09:21 -05:00
|
|
|
func TestRestore(t *testing.T) {
|
2013-11-14 01:10:20 -05:00
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon1 := mkDaemonFromEngine(eng, t)
|
|
|
|
defer daemon1.Nuke()
|
2013-02-26 20:45:46 -05:00
|
|
|
// Create a container with one instance of docker
|
2014-04-17 17:43:01 -04:00
|
|
|
container1, _, _ := mkContainer(daemon1, []string{"_", "ls", "-al"}, t)
|
|
|
|
defer daemon1.Destroy(container1)
|
2013-03-31 23:14:54 -04:00
|
|
|
|
|
|
|
// Create a second container meant to be killed
|
2014-04-17 17:43:01 -04:00
|
|
|
container2, _, _ := mkContainer(daemon1, []string{"-i", "_", "/bin/cat"}, t)
|
|
|
|
defer daemon1.Destroy(container2)
|
2013-03-31 23:14:54 -04:00
|
|
|
|
|
|
|
// Start the container non blocking
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container2.Start(); err != nil {
|
2013-03-31 23:14:54 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-31 11:20:35 -04:00
|
|
|
if !container2.IsRunning() {
|
2013-06-04 14:00:22 -04:00
|
|
|
t.Fatalf("Container %v should appear as running but isn't", container2.ID)
|
2013-04-02 10:01:43 -04:00
|
|
|
}
|
|
|
|
|
2013-03-31 23:14:54 -04:00
|
|
|
// Simulate a crash/manual quit of dockerd: process dies, states stays 'Running'
|
2014-12-04 16:12:29 -05:00
|
|
|
cStdin := container2.StdinPipe()
|
2013-04-02 13:06:49 -04:00
|
|
|
cStdin.Close()
|
2014-08-31 11:20:35 -04:00
|
|
|
if _, err := container2.WaitStop(2 * time.Second); err != nil {
|
2013-04-03 13:48:02 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-31 11:20:35 -04:00
|
|
|
container2.SetRunning(42)
|
2013-04-02 13:06:49 -04:00
|
|
|
container2.ToDisk()
|
2013-03-31 23:14:54 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
if len(daemon1.List()) != 2 {
|
|
|
|
t.Errorf("Expected 2 container, %v found", len(daemon1.List()))
|
2013-01-25 19:20:34 -05:00
|
|
|
}
|
|
|
|
if err := container1.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
2013-01-25 17:09:21 -05:00
|
|
|
}
|
|
|
|
|
2014-08-31 11:20:35 -04:00
|
|
|
if !container2.IsRunning() {
|
2013-06-04 14:00:22 -04:00
|
|
|
t.Fatalf("Container %v should appear as running but isn't", container2.ID)
|
2013-04-02 10:01:43 -04:00
|
|
|
}
|
|
|
|
|
2013-01-25 19:20:34 -05:00
|
|
|
// Here are are simulating a docker restart - that is, reloading all containers
|
|
|
|
// from scratch
|
2014-04-22 22:24:47 -04:00
|
|
|
eng = newTestEngine(t, false, daemon1.Config().Root)
|
|
|
|
daemon2 := mkDaemonFromEngine(eng, t)
|
|
|
|
if len(daemon2.List()) != 2 {
|
|
|
|
t.Errorf("Expected 2 container, %v found", len(daemon2.List()))
|
2013-03-31 23:14:54 -04:00
|
|
|
}
|
|
|
|
runningCount := 0
|
2014-04-17 17:43:01 -04:00
|
|
|
for _, c := range daemon2.List() {
|
2014-08-31 11:20:35 -04:00
|
|
|
if c.IsRunning() {
|
2013-06-04 14:00:22 -04:00
|
|
|
t.Errorf("Running container found: %v (%v)", c.ID, c.Path)
|
2013-03-31 23:14:54 -04:00
|
|
|
runningCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if runningCount != 0 {
|
|
|
|
t.Fatalf("Expected 0 container alive, %d found", runningCount)
|
2013-01-25 17:09:21 -05:00
|
|
|
}
|
2014-12-16 18:06:35 -05:00
|
|
|
container3, err := daemon2.Get(container1.ID)
|
|
|
|
if err != nil {
|
2013-01-25 19:20:34 -05:00
|
|
|
t.Fatal("Unable to Get container")
|
|
|
|
}
|
2013-04-02 10:13:42 -04:00
|
|
|
if err := container3.Run(); err != nil {
|
2013-01-25 19:20:34 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-12 13:38:48 -05:00
|
|
|
container2.SetStopped(&execdriver.ExitStatus{ExitCode: 0})
|
2013-01-25 17:09:21 -05:00
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
func TestDefaultContainerName(t *testing.T) {
|
2013-10-27 22:20:00 -04:00
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemonFromEngine(eng, t)
|
|
|
|
defer nuke(daemon)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err := parseRun([]string{unitTestImageID, "echo test"})
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := daemon.Get(createNamedTestContainer(eng, config, t, "some_name"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
containerID := container.ID
|
|
|
|
|
2013-10-28 19:58:59 -04:00
|
|
|
if container.Name != "/some_name" {
|
|
|
|
t.Fatalf("Expect /some_name got %s", container.Name)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
c, err := daemon.Get("/some_name")
|
|
|
|
if err != nil {
|
2013-11-14 01:10:20 -05:00
|
|
|
t.Fatalf("Couldn't retrieve test container as /some_name")
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
|
|
|
if c.ID != containerID {
|
2013-11-14 01:10:20 -05:00
|
|
|
t.Fatalf("Container /some_name has ID %s instead of %s", c.ID, containerID)
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 19:58:59 -04:00
|
|
|
func TestRandomContainerName(t *testing.T) {
|
2013-10-27 22:20:00 -04:00
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemonFromEngine(eng, t)
|
|
|
|
defer nuke(daemon)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err := parseRun([]string{GetTestImage(daemon).ID, "echo test"})
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := daemon.Get(createTestContainer(eng, config, t))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
containerID := container.ID
|
|
|
|
|
2013-10-28 19:58:59 -04:00
|
|
|
if container.Name == "" {
|
|
|
|
t.Fatalf("Expected not empty container name")
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
if c, err := daemon.Get(container.Name); err != nil {
|
2013-11-14 01:10:20 -05:00
|
|
|
log.Fatalf("Could not lookup container %s by its name", container.Name)
|
|
|
|
} else if c.ID != containerID {
|
|
|
|
log.Fatalf("Looking up container name %s returned id %s instead of %s", container.Name, c.ID, containerID)
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 19:57:14 -05:00
|
|
|
func TestContainerNameValidation(t *testing.T) {
|
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemonFromEngine(eng, t)
|
|
|
|
defer nuke(daemon)
|
2013-12-17 19:57:14 -05:00
|
|
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
Name string
|
|
|
|
Valid bool
|
|
|
|
}{
|
|
|
|
{"abc-123_AAA.1", true},
|
|
|
|
{"\000asdf", false},
|
|
|
|
} {
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err := parseRun([]string{unitTestImageID, "echo test"})
|
2013-12-17 19:57:14 -05:00
|
|
|
if err != nil {
|
|
|
|
if !test.Valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-09 14:09:59 -04:00
|
|
|
var outputBuffer = bytes.NewBuffer(nil)
|
2013-12-17 19:57:14 -05:00
|
|
|
job := eng.Job("create", test.Name)
|
|
|
|
if err := job.ImportEnv(config); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-05-09 14:09:59 -04:00
|
|
|
job.Stdout.Add(outputBuffer)
|
2013-12-17 19:57:14 -05:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
if !test.Valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := daemon.Get(engine.Tail(outputBuffer, 1))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-12-17 19:57:14 -05:00
|
|
|
|
|
|
|
if container.Name != "/"+test.Name {
|
|
|
|
t.Fatalf("Expect /%s got %s", test.Name, container.Name)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
if c, err := daemon.Get("/" + test.Name); err != nil {
|
2013-12-17 19:57:14 -05:00
|
|
|
t.Fatalf("Couldn't retrieve test container as /%s", test.Name)
|
|
|
|
} else if c.ID != container.ID {
|
|
|
|
t.Fatalf("Container /%s has ID %s instead of %s", test.Name, c.ID, container.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
func TestLinkChildContainer(t *testing.T) {
|
2013-10-27 22:20:00 -04:00
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemonFromEngine(eng, t)
|
|
|
|
defer nuke(daemon)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err := parseRun([]string{unitTestImageID, "echo test"})
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := daemon.Get(createNamedTestContainer(eng, config, t, "/webapp"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
webapp, err := daemon.GetByName("/webapp")
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if webapp.ID != container.ID {
|
|
|
|
t.Fatalf("Expect webapp id to match container id: %s != %s", webapp.ID, container.ID)
|
|
|
|
}
|
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err = parseRun([]string{GetTestImage(daemon).ID, "echo test"})
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
childContainer, err := daemon.Get(createTestContainer(eng, config, t))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
if err := daemon.RegisterLink(webapp, childContainer, "db"); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the child by it's new name
|
2014-04-17 17:43:01 -04:00
|
|
|
db, err := daemon.GetByName("/webapp/db")
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if db.ID != childContainer.ID {
|
|
|
|
t.Fatalf("Expect db id to match container id: %s != %s", db.ID, childContainer.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAllChildren(t *testing.T) {
|
2013-10-27 22:20:00 -04:00
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemonFromEngine(eng, t)
|
|
|
|
defer nuke(daemon)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err := parseRun([]string{unitTestImageID, "echo test"})
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := daemon.Get(createNamedTestContainer(eng, config, t, "/webapp"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
webapp, err := daemon.GetByName("/webapp")
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if webapp.ID != container.ID {
|
|
|
|
t.Fatalf("Expect webapp id to match container id: %s != %s", webapp.ID, container.ID)
|
|
|
|
}
|
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err = parseRun([]string{unitTestImageID, "echo test"})
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
childContainer, err := daemon.Get(createTestContainer(eng, config, t))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
if err := daemon.RegisterLink(webapp, childContainer, "db"); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
children, err := daemon.Children("/webapp")
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if children == nil {
|
|
|
|
t.Fatal("Children should not be nil")
|
|
|
|
}
|
|
|
|
if len(children) == 0 {
|
|
|
|
t.Fatal("Children should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range children {
|
|
|
|
if key != "/webapp/db" {
|
|
|
|
t.Fatalf("Expected /webapp/db got %s", key)
|
|
|
|
}
|
|
|
|
if value.ID != childContainer.ID {
|
|
|
|
t.Fatalf("Expected id %s got %s", childContainer.ID, value.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-03 12:20:14 -05:00
|
|
|
|
|
|
|
func TestDestroyWithInitLayer(t *testing.T) {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemon(t)
|
|
|
|
defer nuke(daemon)
|
2013-12-03 12:20:14 -05:00
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
container, _, err := daemon.Create(&runconfig.Config{
|
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-12-03 12:20:14 -05:00
|
|
|
Cmd: []string{"ls", "-al"},
|
2014-09-25 17:23:59 -04:00
|
|
|
},
|
|
|
|
&runconfig.HostConfig{},
|
|
|
|
"")
|
2013-12-03 12:20:14 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Destroy
|
2014-04-17 17:43:01 -04:00
|
|
|
if err := daemon.Destroy(container); err != nil {
|
2013-12-03 12:20:14 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
// Make sure daemon.Exists() behaves correctly
|
|
|
|
if daemon.Exists("test_destroy") {
|
2013-12-03 12:20:14 -05:00
|
|
|
t.Fatalf("Exists() returned true")
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
// Make sure daemon.List() doesn't list the destroyed container
|
|
|
|
if len(daemon.List()) != 0 {
|
|
|
|
t.Fatalf("Expected 0 container, %v found", len(daemon.List()))
|
2013-12-03 12:20:14 -05:00
|
|
|
}
|
|
|
|
|
2014-04-17 17:43:01 -04:00
|
|
|
driver := daemon.Graph().Driver()
|
2013-12-03 12:20:14 -05:00
|
|
|
|
|
|
|
// Make sure that the container does not exist in the driver
|
2014-04-17 19:47:27 -04:00
|
|
|
if _, err := driver.Get(container.ID, ""); err == nil {
|
2013-12-03 12:20:14 -05:00
|
|
|
t.Fatal("Conttainer should not exist in the driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the init layer is removed from the driver
|
2014-04-17 19:47:27 -04:00
|
|
|
if _, err := driver.Get(fmt.Sprintf("%s-init", container.ID), ""); err == nil {
|
2013-12-03 12:20:14 -05:00
|
|
|
t.Fatal("Container's init layer should not exist in the driver")
|
|
|
|
}
|
|
|
|
}
|