2013-01-19 00:13:39 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-06-11 22:46:23 +00:00
|
|
|
"bytes"
|
2013-04-20 03:46:07 +00:00
|
|
|
"fmt"
|
2013-11-14 06:10:20 +00:00
|
|
|
"github.com/dotcloud/docker"
|
2013-10-27 07:13:45 +00:00
|
|
|
"github.com/dotcloud/docker/engine"
|
2014-02-12 00:48:44 +00:00
|
|
|
"github.com/dotcloud/docker/nat"
|
2014-02-12 04:04:39 +00:00
|
|
|
"github.com/dotcloud/docker/runconfig"
|
2013-10-10 08:53:38 +00:00
|
|
|
"github.com/dotcloud/docker/sysinit"
|
2013-05-15 01:52:09 +00:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-03-11 12:42:36 +00:00
|
|
|
"io"
|
2013-05-30 13:45:39 +00:00
|
|
|
"log"
|
2013-04-20 03:46:07 +00:00
|
|
|
"net"
|
2013-11-13 19:25:55 +00:00
|
|
|
"net/url"
|
2013-01-19 00:13:39 +00:00
|
|
|
"os"
|
2013-10-18 05:40:41 +00:00
|
|
|
"path/filepath"
|
2013-07-24 22:48:18 +00:00
|
|
|
"runtime"
|
2013-05-30 13:45:39 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2013-06-22 02:40:42 +00:00
|
|
|
"syscall"
|
2013-01-19 00:13:39 +00:00
|
|
|
"testing"
|
2013-04-02 17:06:49 +00:00
|
|
|
"time"
|
2013-01-19 00:13:39 +00:00
|
|
|
)
|
|
|
|
|
2013-06-24 21:15:52 +00:00
|
|
|
const (
|
2013-07-23 19:55:38 +00:00
|
|
|
unitTestImageName = "docker-test-image"
|
|
|
|
unitTestImageID = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
|
2013-10-13 22:36:05 +00:00
|
|
|
unitTestImageIDShort = "83599e29c455"
|
2013-07-23 19:55:38 +00:00
|
|
|
unitTestNetworkBridge = "testdockbr0"
|
|
|
|
unitTestStoreBase = "/var/lib/docker/unit-tests"
|
|
|
|
testDaemonAddr = "127.0.0.1:4270"
|
|
|
|
testDaemonProto = "tcp"
|
2013-06-24 21:15:52 +00:00
|
|
|
)
|
2013-02-27 01:45:46 +00:00
|
|
|
|
2013-07-24 22:48:18 +00:00
|
|
|
var (
|
2013-11-14 06:10:20 +00:00
|
|
|
// FIXME: globalRuntime is deprecated by globalEngine. All tests should be converted.
|
|
|
|
globalRuntime *docker.Runtime
|
|
|
|
globalEngine *engine.Engine
|
2013-07-24 22:48:18 +00:00
|
|
|
startFds int
|
|
|
|
startGoroutines int
|
|
|
|
)
|
2013-06-25 01:22:02 +00:00
|
|
|
|
2013-11-14 06:10:20 +00:00
|
|
|
// FIXME: nuke() is deprecated by Runtime.Nuke()
|
|
|
|
func nuke(runtime *docker.Runtime) error {
|
|
|
|
return runtime.Nuke()
|
2013-03-12 07:08:41 +00:00
|
|
|
}
|
|
|
|
|
2013-11-14 06:10:20 +00:00
|
|
|
// FIXME: cleanup and nuke are redundant.
|
|
|
|
func cleanup(eng *engine.Engine, t *testing.T) error {
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
2013-06-25 01:22:02 +00:00
|
|
|
for _, container := range runtime.List() {
|
|
|
|
container.Kill()
|
|
|
|
runtime.Destroy(container)
|
|
|
|
}
|
2013-12-12 22:39:35 +00:00
|
|
|
job := eng.Job("images")
|
|
|
|
images, err := job.Stdout.AddTable()
|
2013-06-25 01:22:02 +00:00
|
|
|
if err != nil {
|
2013-12-12 22:39:35 +00:00
|
|
|
t.Fatal(err)
|
2013-06-25 01:22:02 +00:00
|
|
|
}
|
2013-12-12 22:39:35 +00:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, image := range images.Data {
|
2014-01-29 20:31:49 +00:00
|
|
|
if image.Get("Id") != unitTestImageID {
|
2014-01-31 19:00:06 +00:00
|
|
|
eng.Job("image_delete", image.Get("Id")).Run()
|
2013-06-25 01:22:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-11 12:42:36 +00:00
|
|
|
func layerArchive(tarfile string) (io.Reader, error) {
|
2013-02-27 01:45:46 +00:00
|
|
|
// FIXME: need to close f somewhere
|
|
|
|
f, err := os.Open(tarfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
2013-02-14 23:37:45 +00:00
|
|
|
|
2013-02-13 22:01:44 +00:00
|
|
|
func init() {
|
2013-11-26 04:48:03 +00: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 22:55:24 +00:00
|
|
|
os.Setenv("TEST", "1")
|
|
|
|
|
2013-02-14 23:37:45 +00:00
|
|
|
// Hack to run sys init during unit testing
|
2014-02-25 18:54:41 +00:00
|
|
|
if selfPath := utils.SelfPath(); strings.Contains(selfPath, ".dockerinit") {
|
2013-10-10 08:53:38 +00:00
|
|
|
sysinit.SysInit()
|
2013-03-14 09:30:50 +00:00
|
|
|
return
|
2013-02-13 22:01:44 +00:00
|
|
|
}
|
2013-02-14 23:37:45 +00:00
|
|
|
|
2013-06-22 02:40:42 +00:00
|
|
|
if uid := syscall.Geteuid(); uid != 0 {
|
2013-07-04 19:40:14 +00:00
|
|
|
log.Fatal("docker tests need to be run as root")
|
2013-03-15 09:03:41 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 05:40:41 +00: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 {
|
|
|
|
log.Fatalf("Unable to open TEST_DOCKERINIT_PATH: %s\n", err)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
log.Fatalf("Unable to create dockerinit in test directory: %s\n", err)
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
if _, err := io.Copy(dst, src); err != nil {
|
|
|
|
log.Fatalf("Unable to copy dockerinit to TEST_DOCKERINIT_PATH: %s\n", err)
|
|
|
|
}
|
|
|
|
dst.Close()
|
|
|
|
src.Close()
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:10:20 +00:00
|
|
|
// Setup the base runtime, which will be duplicated for each test.
|
|
|
|
// (no tests are run directly in the base)
|
|
|
|
setupBaseImage()
|
2013-05-06 09:31:22 +00:00
|
|
|
|
2013-10-16 20:10:20 +00:00
|
|
|
// Create the "global runtime" with a long-running daemon for integration tests
|
|
|
|
spawnGlobalDaemon()
|
|
|
|
startFds, startGoroutines = utils.GetTotalUsedFds(), runtime.NumGoroutine()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupBaseImage() {
|
2014-02-24 19:22:48 +00:00
|
|
|
eng := newTestEngine(log.New(os.Stderr, "", 0), false, unitTestStoreBase)
|
|
|
|
job := eng.Job("inspect", unitTestImageName, "image")
|
2014-01-29 20:16:47 +00:00
|
|
|
img, _ := job.Stdout.AddEnv()
|
2013-07-08 22:23:04 +00:00
|
|
|
// If the unit test is not found, try to download it.
|
2014-01-29 20:16:47 +00:00
|
|
|
if err := job.Run(); err != nil || img.Get("id") != unitTestImageID {
|
2013-07-08 22:23:04 +00:00
|
|
|
// Retrieve the Image
|
2014-01-22 21:35:35 +00:00
|
|
|
job = eng.Job("pull", unitTestImageName)
|
|
|
|
job.Stdout.Add(utils.NopWriteCloser(os.Stdout))
|
|
|
|
if err := job.Run(); err != nil {
|
2013-11-11 17:05:38 +00:00
|
|
|
log.Fatalf("Unable to pull the test image: %s", err)
|
2013-07-08 22:23:04 +00:00
|
|
|
}
|
2013-02-14 23:37:45 +00:00
|
|
|
}
|
2013-10-16 20:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func spawnGlobalDaemon() {
|
|
|
|
if globalRuntime != nil {
|
|
|
|
utils.Debugf("Global runtime already exists. Skipping.")
|
|
|
|
return
|
|
|
|
}
|
2013-10-27 07:13:45 +00:00
|
|
|
t := log.New(os.Stderr, "", 0)
|
|
|
|
eng := NewTestEngine(t)
|
2013-11-14 06:10:20 +00:00
|
|
|
globalEngine = eng
|
|
|
|
globalRuntime = mkRuntimeFromEngine(eng, t)
|
2013-10-16 20:10:20 +00:00
|
|
|
|
2013-06-24 21:59:37 +00:00
|
|
|
// Spawn a Daemon
|
|
|
|
go func() {
|
2013-10-16 20:10:20 +00:00
|
|
|
utils.Debugf("Spawning global daemon for integration tests")
|
2013-10-27 07:13:45 +00:00
|
|
|
listenURL := &url.URL{
|
2013-11-13 19:25:55 +00:00
|
|
|
Scheme: testDaemonProto,
|
|
|
|
Host: testDaemonAddr,
|
2013-10-27 07:13:45 +00:00
|
|
|
}
|
|
|
|
job := eng.Job("serveapi", listenURL.String())
|
2013-12-12 22:39:35 +00:00
|
|
|
job.SetenvBool("Logging", true)
|
2013-10-27 07:13:45 +00:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
log.Fatalf("Unable to spawn the test daemon: %s", err)
|
2013-06-24 21:59:37 +00:00
|
|
|
}
|
|
|
|
}()
|
2014-02-16 04:49:50 +00:00
|
|
|
|
2013-06-25 01:22:02 +00:00
|
|
|
// Give some time to ListenAndServer to actually start
|
2013-10-16 20:10:20 +00:00
|
|
|
// FIXME: use inmem transports instead of tcp
|
2013-06-25 01:22:02 +00:00
|
|
|
time.Sleep(time.Second)
|
2014-02-16 04:49:50 +00:00
|
|
|
|
|
|
|
if err := eng.Job("acceptconnections").Run(); err != nil {
|
|
|
|
log.Fatalf("Unable to accept connections for test api: %s", err)
|
|
|
|
}
|
2013-02-13 22:01:44 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 01:32:21 +00:00
|
|
|
// FIXME: test that ImagePull(json=true) send correct json output
|
|
|
|
|
2013-11-14 06:10:20 +00:00
|
|
|
func GetTestImage(runtime *docker.Runtime) *docker.Image {
|
|
|
|
imgs, err := runtime.Graph().Map()
|
2013-02-27 01:45:46 +00:00
|
|
|
if err != nil {
|
2013-11-30 00:53:20 +00:00
|
|
|
log.Fatalf("Unable to get the test image: %s", err)
|
2013-02-27 01:45:46 +00:00
|
|
|
}
|
2013-09-01 03:31:21 +00:00
|
|
|
for _, image := range imgs {
|
|
|
|
if image.ID == unitTestImageID {
|
|
|
|
return image
|
2013-06-28 00:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-14 06:10:20 +00:00
|
|
|
log.Fatalf("Test image %v not found in %s: %s", unitTestImageID, runtime.Graph().Root, imgs)
|
2013-10-09 13:47:49 +00:00
|
|
|
return nil
|
2013-02-27 01:45:46 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 00:47:23 +00:00
|
|
|
func TestRuntimeCreate(t *testing.T) {
|
2013-07-12 00:59:25 +00:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer nuke(runtime)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
// Make sure we start we 0 containers
|
2013-03-21 07:41:15 +00:00
|
|
|
if len(runtime.List()) != 0 {
|
|
|
|
t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
2013-05-07 18:18:13 +00:00
|
|
|
|
2014-02-12 04:04:39 +00:00
|
|
|
container, _, err := runtime.Create(&runconfig.Config{
|
2013-06-04 18:00:22 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 19:39:09 +00:00
|
|
|
Cmd: []string{"ls", "-al"},
|
2013-03-23 19:16:58 +00:00
|
|
|
},
|
2013-10-28 23:58:59 +00:00
|
|
|
"",
|
2013-01-19 00:13:39 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2013-03-21 07:41:15 +00:00
|
|
|
if err := runtime.Destroy(container); err != nil {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Make sure we can find the newly created container with List()
|
2013-03-21 07:41:15 +00:00
|
|
|
if len(runtime.List()) != 1 {
|
|
|
|
t.Errorf("Expected 1 container, %v found", len(runtime.List()))
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the container List() returns is the right one
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.List()[0].ID != container.ID {
|
2013-03-21 07:41:15 +00:00
|
|
|
t.Errorf("Unexpected container %v returned by List", runtime.List()[0])
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we can get the container with Get()
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container.ID) == nil {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Unable to get newly created container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it is the right container
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container.ID) != container {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Get() returned the wrong container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure Exists returns it as existing
|
2013-06-04 18:00:22 +00:00
|
|
|
if !runtime.Exists(container.ID) {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Exists() returned false for a newly created container")
|
|
|
|
}
|
2013-05-07 18:18:13 +00:00
|
|
|
|
2013-11-10 21:50:34 +00:00
|
|
|
// Test that conflict error displays correct details
|
|
|
|
testContainer, _, _ := runtime.Create(
|
2014-02-12 04:04:39 +00:00
|
|
|
&runconfig.Config{
|
2013-11-10 21:50:34 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"ls", "-al"},
|
|
|
|
},
|
|
|
|
"conflictname",
|
|
|
|
)
|
2014-02-12 04:04:39 +00:00
|
|
|
if _, _, err := runtime.Create(&runconfig.Config{Image: GetTestImage(runtime).ID, Cmd: []string{"ls", "-al"}}, testContainer.Name); err == nil || !strings.Contains(err.Error(), utils.TruncateID(testContainer.ID)) {
|
2013-11-10 21:50:34 +00:00
|
|
|
t.Fatalf("Name conflict error doesn't include the correct short id. Message was: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:25:15 +00:00
|
|
|
// Make sure create with bad parameters returns an error
|
2014-02-12 04:04:39 +00:00
|
|
|
if _, _, err = runtime.Create(&runconfig.Config{Image: GetTestImage(runtime).ID}, ""); err == nil {
|
2013-05-07 18:18:13 +00:00
|
|
|
t.Fatal("Builder.Create should throw an error when Cmd is missing")
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:25:15 +00:00
|
|
|
if _, _, err := runtime.Create(
|
2014-02-12 04:04:39 +00:00
|
|
|
&runconfig.Config{
|
2013-06-04 18:00:22 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-07 18:18:13 +00:00
|
|
|
Cmd: []string{},
|
|
|
|
},
|
2013-10-28 23:58:59 +00:00
|
|
|
"",
|
2013-10-09 01:42:53 +00:00
|
|
|
); err == nil {
|
2013-05-07 18:18:13 +00:00
|
|
|
t.Fatal("Builder.Create should throw an error when Cmd is empty")
|
|
|
|
}
|
2013-10-03 14:46:07 +00:00
|
|
|
|
2014-02-12 04:04:39 +00:00
|
|
|
config := &runconfig.Config{
|
2013-10-03 14:46:07 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"/bin/ls"},
|
|
|
|
PortSpecs: []string{"80"},
|
|
|
|
}
|
2013-10-28 23:58:59 +00:00
|
|
|
container, _, err = runtime.Create(config, "")
|
2013-10-03 14:46:07 +00:00
|
|
|
|
2013-10-05 02:25:15 +00:00
|
|
|
_, err = runtime.Commit(container, "testrepo", "testtag", "", "", config)
|
2013-10-03 14:46:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2013-10-30 18:13:10 +00:00
|
|
|
|
|
|
|
// test expose 80:8000
|
2014-02-12 04:04:39 +00:00
|
|
|
container, warnings, err := runtime.Create(&runconfig.Config{
|
2013-10-30 18:13:10 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"ls", "-al"},
|
|
|
|
PortSpecs: []string{"80:8000"},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-30 20:18:48 +00:00
|
|
|
if warnings == nil || len(warnings) != 1 {
|
2013-10-30 18:13:10 +00:00
|
|
|
t.Error("Expected a warning, got none")
|
|
|
|
}
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDestroy(t *testing.T) {
|
2013-07-12 00:59:25 +00:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer nuke(runtime)
|
2013-10-09 01:42:53 +00:00
|
|
|
|
2014-02-12 04:04:39 +00:00
|
|
|
container, _, err := runtime.Create(&runconfig.Config{
|
2013-06-04 18:00:22 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 19:39:09 +00:00
|
|
|
Cmd: []string{"ls", "-al"},
|
2013-10-28 23:58:59 +00:00
|
|
|
}, "")
|
2013-01-19 00:13:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Destroy
|
2013-03-21 07:41:15 +00:00
|
|
|
if err := runtime.Destroy(container); err != nil {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2013-03-21 07:41:15 +00:00
|
|
|
// Make sure runtime.Exists() behaves correctly
|
|
|
|
if runtime.Exists("test_destroy") {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Exists() returned true")
|
|
|
|
}
|
|
|
|
|
2013-03-21 07:41:15 +00:00
|
|
|
// Make sure runtime.List() doesn't list the destroyed container
|
|
|
|
if len(runtime.List()) != 0 {
|
|
|
|
t.Errorf("Expected 0 container, %v found", len(runtime.List()))
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 07:41:15 +00:00
|
|
|
// Make sure runtime.Get() refuses to return the unexisting container
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container.ID) != nil {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Unable to get newly created container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test double destroy
|
2013-03-21 07:41:15 +00:00
|
|
|
if err := runtime.Destroy(container); err == nil {
|
2013-01-19 00:13:39 +00:00
|
|
|
// It should have failed
|
|
|
|
t.Errorf("Double destroy did not fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2013-07-12 00:59:25 +00:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer nuke(runtime)
|
2013-04-24 22:35:28 +00:00
|
|
|
|
2013-11-14 20:33:15 +00:00
|
|
|
container1, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime.Destroy(container1)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-11-14 20:33:15 +00:00
|
|
|
container2, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime.Destroy(container2)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-11-14 20:33:15 +00:00
|
|
|
container3, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime.Destroy(container3)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container1.ID) != container1 {
|
|
|
|
t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.ID), container1)
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container2.ID) != container2 {
|
|
|
|
t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.ID), container2)
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container3.ID) != container3 {
|
|
|
|
t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.ID), container3)
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-01-25 22:09:21 +00:00
|
|
|
|
2013-11-14 06:10:20 +00:00
|
|
|
func startEchoServerContainer(t *testing.T, proto string) (*docker.Runtime, *docker.Container, string) {
|
2013-10-09 01:42:53 +00:00
|
|
|
var (
|
2013-11-14 06:10:20 +00:00
|
|
|
err error
|
|
|
|
id string
|
|
|
|
strPort string
|
|
|
|
eng = NewTestEngine(t)
|
|
|
|
runtime = mkRuntimeFromEngine(eng, t)
|
|
|
|
port = 5554
|
2014-02-12 00:48:44 +00:00
|
|
|
p nat.Port
|
2013-10-09 01:42:53 +00:00
|
|
|
)
|
2013-11-14 06:10:20 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
runtime.Nuke()
|
|
|
|
}
|
|
|
|
}()
|
2013-10-09 01:42:53 +00:00
|
|
|
|
2013-05-30 13:45:39 +00:00
|
|
|
for {
|
|
|
|
port += 1
|
2013-06-11 22:46:23 +00: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-12 00:48:44 +00:00
|
|
|
ep := make(map[nat.Port]struct{}, 1)
|
|
|
|
p = nat.Port(fmt.Sprintf("%s/%s", strPort, proto))
|
2013-10-05 02:25:15 +00:00
|
|
|
ep[p] = struct{}{}
|
|
|
|
|
2013-11-14 06:10:20 +00: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)
|
2013-11-20 07:37:03 +00:00
|
|
|
jobCreate.Stdout.AddString(&id)
|
2013-11-14 06:10:20 +00:00
|
|
|
if err := jobCreate.Run(); err != nil {
|
2013-05-30 13:45:39 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 06:10:20 +00:00
|
|
|
// FIXME: this relies on the undocumented behavior of runtime.Create
|
|
|
|
// which will return a nil error AND container if the exposed ports
|
|
|
|
// are invalid. That behavior should be fixed!
|
|
|
|
if id != "" {
|
2013-10-05 02:25:15 +00:00
|
|
|
break
|
|
|
|
}
|
2013-10-24 22:09:01 +00:00
|
|
|
t.Logf("Port %v already in use, trying another one", strPort)
|
2013-05-30 13:45:39 +00:00
|
|
|
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
2013-11-14 06:10:20 +00:00
|
|
|
|
|
|
|
jobStart := eng.Job("start", id)
|
2014-02-12 00:48:44 +00:00
|
|
|
portBindings := make(map[nat.Port][]nat.PortBinding)
|
|
|
|
portBindings[p] = []nat.PortBinding{
|
2013-10-05 02:25:15 +00:00
|
|
|
{},
|
|
|
|
}
|
2013-11-14 06:10:20 +00:00
|
|
|
if err := jobStart.SetenvJson("PortsBindings", portBindings); err != nil {
|
2013-06-11 22:46:23 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 06:10:20 +00:00
|
|
|
if err := jobStart.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
container := runtime.Get(id)
|
|
|
|
if container == nil {
|
|
|
|
t.Fatalf("Couldn't fetch test container %s", id)
|
|
|
|
}
|
2013-04-23 17:28:40 +00:00
|
|
|
|
|
|
|
setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
|
2013-11-21 20:21:03 +00:00
|
|
|
for !container.State.IsRunning() {
|
2013-04-23 17:28:40 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-07-02 10:47:37 +00:00
|
|
|
// Even if the state is running, lets give some time to lxc to spawn the process
|
|
|
|
container.WaitTimeout(500 * time.Millisecond)
|
|
|
|
|
2013-10-05 02:25:15 +00:00
|
|
|
strPort = container.NetworkSettings.Ports[p][0].HostPort
|
2013-06-11 22:46:23 +00:00
|
|
|
return runtime, container, strPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a container with a TCP port allocated, and test that it can receive connections on localhost
|
|
|
|
func TestAllocateTCPPortLocalhost(t *testing.T) {
|
|
|
|
runtime, container, port := startEchoServerContainer(t, "tcp")
|
|
|
|
defer nuke(runtime)
|
|
|
|
defer container.Kill()
|
|
|
|
|
2013-07-11 01:02:41 +00: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 22:46:23 +00:00
|
|
|
|
2013-07-11 01:02:41 +00: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 22:46:23 +00:00
|
|
|
}
|
2013-07-11 01:02:41 +00:00
|
|
|
|
|
|
|
t.Fatal("No reply from the container")
|
2013-06-11 22:46:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 01:02:41 +00:00
|
|
|
// Run a container with an UDP port allocated, and test that it can receive connections on localhost
|
2013-06-11 22:46:23 +00:00
|
|
|
func TestAllocateUDPPortLocalhost(t *testing.T) {
|
|
|
|
runtime, container, port := startEchoServerContainer(t, "udp")
|
|
|
|
defer nuke(runtime)
|
|
|
|
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-11 01:02:41 +00: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 22:46:23 +00:00
|
|
|
_, err := conn.Write(input.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-11 01:02:41 +00:00
|
|
|
conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
|
2013-06-11 22:46:23 +00: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-20 03:46:07 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 22:09:21 +00:00
|
|
|
func TestRestore(t *testing.T) {
|
2013-11-14 06:10:20 +00:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
runtime1 := mkRuntimeFromEngine(eng, t)
|
|
|
|
defer runtime1.Nuke()
|
2013-02-27 01:45:46 +00:00
|
|
|
// Create a container with one instance of docker
|
2013-11-14 20:33:15 +00:00
|
|
|
container1, _, _ := mkContainer(runtime1, []string{"_", "ls", "-al"}, t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime1.Destroy(container1)
|
2013-04-01 03:14:54 +00:00
|
|
|
|
|
|
|
// Create a second container meant to be killed
|
2013-11-14 20:33:15 +00:00
|
|
|
container2, _, _ := mkContainer(runtime1, []string{"-i", "_", "/bin/cat"}, t)
|
2013-04-02 14:13:42 +00:00
|
|
|
defer runtime1.Destroy(container2)
|
2013-04-01 03:14:54 +00:00
|
|
|
|
|
|
|
// Start the container non blocking
|
2013-10-31 21:58:43 +00:00
|
|
|
if err := container2.Start(); err != nil {
|
2013-04-01 03:14:54 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-11-21 20:21:03 +00:00
|
|
|
if !container2.State.IsRunning() {
|
2013-06-04 18:00:22 +00:00
|
|
|
t.Fatalf("Container %v should appear as running but isn't", container2.ID)
|
2013-04-02 14:01:43 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 03:14:54 +00:00
|
|
|
// Simulate a crash/manual quit of dockerd: process dies, states stays 'Running'
|
2013-04-02 17:06:49 +00:00
|
|
|
cStdin, _ := container2.StdinPipe()
|
|
|
|
cStdin.Close()
|
2013-04-05 03:32:44 +00:00
|
|
|
if err := container2.WaitTimeout(2 * time.Second); err != nil {
|
2013-04-03 17:48:02 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-21 20:21:03 +00:00
|
|
|
container2.State.SetRunning(42)
|
2013-04-02 17:06:49 +00:00
|
|
|
container2.ToDisk()
|
2013-04-01 03:14:54 +00:00
|
|
|
|
|
|
|
if len(runtime1.List()) != 2 {
|
|
|
|
t.Errorf("Expected 2 container, %v found", len(runtime1.List()))
|
2013-01-26 00:20:34 +00:00
|
|
|
}
|
|
|
|
if err := container1.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
2013-01-25 22:09:21 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 20:21:03 +00:00
|
|
|
if !container2.State.IsRunning() {
|
2013-06-04 18:00:22 +00:00
|
|
|
t.Fatalf("Container %v should appear as running but isn't", container2.ID)
|
2013-04-02 14:01:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 00:20:34 +00:00
|
|
|
// Here are are simulating a docker restart - that is, reloading all containers
|
|
|
|
// from scratch
|
2014-02-24 19:22:48 +00:00
|
|
|
eng = newTestEngine(t, false, eng.Root())
|
2013-11-14 06:10:20 +00:00
|
|
|
runtime2 := mkRuntimeFromEngine(eng, t)
|
2013-04-01 03:14:54 +00:00
|
|
|
if len(runtime2.List()) != 2 {
|
|
|
|
t.Errorf("Expected 2 container, %v found", len(runtime2.List()))
|
|
|
|
}
|
|
|
|
runningCount := 0
|
|
|
|
for _, c := range runtime2.List() {
|
2013-11-21 20:21:03 +00:00
|
|
|
if c.State.IsRunning() {
|
2013-06-04 18:00:22 +00:00
|
|
|
t.Errorf("Running container found: %v (%v)", c.ID, c.Path)
|
2013-04-01 03:14:54 +00:00
|
|
|
runningCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if runningCount != 0 {
|
|
|
|
t.Fatalf("Expected 0 container alive, %d found", runningCount)
|
2013-01-25 22:09:21 +00:00
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
container3 := runtime2.Get(container1.ID)
|
2013-04-02 14:13:42 +00:00
|
|
|
if container3 == nil {
|
2013-01-26 00:20:34 +00:00
|
|
|
t.Fatal("Unable to Get container")
|
|
|
|
}
|
2013-04-02 14:13:42 +00:00
|
|
|
if err := container3.Run(); err != nil {
|
2013-01-26 00:20:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-21 20:21:03 +00:00
|
|
|
container2.State.SetStopped(0)
|
2013-01-25 22:09:21 +00:00
|
|
|
}
|
2013-10-05 02:25:15 +00:00
|
|
|
|
|
|
|
func TestReloadContainerLinks(t *testing.T) {
|
2013-11-14 06:10:20 +00:00
|
|
|
root, err := newTestDirectory(unitTestStoreBase)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-24 19:22:48 +00:00
|
|
|
// FIXME: here we don't use NewTestEngine because it calls initserver with Autorestart=false,
|
|
|
|
// and we want to set it to true.
|
|
|
|
|
|
|
|
eng := newTestEngine(t, true, root)
|
2013-11-14 06:10:20 +00:00
|
|
|
|
|
|
|
runtime1 := mkRuntimeFromEngine(eng, t)
|
2013-10-05 02:25:15 +00:00
|
|
|
defer nuke(runtime1)
|
|
|
|
// Create a container with one instance of docker
|
2013-11-14 20:33:15 +00:00
|
|
|
container1, _, _ := mkContainer(runtime1, []string{"-i", "_", "/bin/sh"}, t)
|
2013-10-05 02:25:15 +00:00
|
|
|
defer runtime1.Destroy(container1)
|
|
|
|
|
|
|
|
// Create a second container meant to be killed
|
2013-11-14 20:33:15 +00:00
|
|
|
container2, _, _ := mkContainer(runtime1, []string{"-i", "_", "/bin/cat"}, t)
|
2013-10-05 02:25:15 +00:00
|
|
|
defer runtime1.Destroy(container2)
|
|
|
|
|
|
|
|
// Start the container non blocking
|
2013-10-31 21:58:43 +00:00
|
|
|
if err := container2.Start(); err != nil {
|
2013-10-05 02:25:15 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Add a link to container 2
|
2013-11-14 06:10:20 +00:00
|
|
|
// FIXME @shykes: setting hostConfig.Links seems redundant with calling RegisterLink().
|
|
|
|
// Why do we need it @crosbymichael?
|
|
|
|
// container1.hostConfig.Links = []string{"/" + container2.ID + ":first"}
|
2013-10-28 23:58:59 +00:00
|
|
|
if err := runtime1.RegisterLink(container1, container2, "first"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-31 21:58:43 +00:00
|
|
|
if err := container1.Start(); err != nil {
|
2013-10-05 02:25:15 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-11-21 20:21:03 +00:00
|
|
|
if !container2.State.IsRunning() {
|
2013-10-05 02:25:15 +00:00
|
|
|
t.Fatalf("Container %v should appear as running but isn't", container2.ID)
|
|
|
|
}
|
|
|
|
|
2013-11-21 20:21:03 +00:00
|
|
|
if !container1.State.IsRunning() {
|
2013-10-21 22:42:27 +00:00
|
|
|
t.Fatalf("Container %s should appear as running but isn't", container1.ID)
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(runtime1.List()) != 2 {
|
|
|
|
t.Errorf("Expected 2 container, %v found", len(runtime1.List()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here are are simulating a docker restart - that is, reloading all containers
|
|
|
|
// from scratch
|
2014-02-24 19:22:48 +00:00
|
|
|
eng = newTestEngine(t, false, root)
|
2013-11-14 06:10:20 +00:00
|
|
|
runtime2 := mkRuntimeFromEngine(eng, t)
|
2013-10-05 02:25:15 +00:00
|
|
|
if len(runtime2.List()) != 2 {
|
|
|
|
t.Errorf("Expected 2 container, %v found", len(runtime2.List()))
|
|
|
|
}
|
|
|
|
runningCount := 0
|
|
|
|
for _, c := range runtime2.List() {
|
2013-11-21 20:21:03 +00:00
|
|
|
if c.State.IsRunning() {
|
2013-10-05 02:25:15 +00:00
|
|
|
runningCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if runningCount != 2 {
|
|
|
|
t.Fatalf("Expected 2 container alive, %d found", runningCount)
|
|
|
|
}
|
|
|
|
|
2013-11-14 06:10:20 +00:00
|
|
|
// FIXME: we no longer test if containers were registered in the right order,
|
|
|
|
// because there is no public
|
2013-10-05 02:25:15 +00:00
|
|
|
// Make sure container 2 ( the child of container 1 ) was registered and started first
|
|
|
|
// with the runtime
|
2013-11-14 06:10:20 +00:00
|
|
|
//
|
|
|
|
containers := runtime2.List()
|
|
|
|
if len(containers) == 0 {
|
|
|
|
t.Fatalf("Runtime has no containers")
|
|
|
|
}
|
|
|
|
first := containers[0]
|
|
|
|
if first.ID != container2.ID {
|
2013-10-05 02:25:15 +00:00
|
|
|
t.Fatalf("Container 2 %s should be registered first in the runtime", container2.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the link is still registered in the runtime
|
2013-11-14 06:10:20 +00:00
|
|
|
if c := runtime2.Get(container1.Name); c == nil {
|
|
|
|
t.Fatal("Named container is no longer registered after restart")
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultContainerName(t *testing.T) {
|
2013-10-28 02:20:00 +00:00
|
|
|
eng := NewTestEngine(t)
|
2013-11-14 06:10:20 +00:00
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
2013-10-05 02:25:15 +00:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
2014-02-12 04:04:39 +00:00
|
|
|
config, _, _, err := runconfig.Parse([]string{unitTestImageID, "echo test"}, nil)
|
2013-10-05 02:25:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-10-28 02:20:00 +00:00
|
|
|
container := runtime.Get(createNamedTestContainer(eng, config, t, "some_name"))
|
2013-10-05 02:25:15 +00:00
|
|
|
containerID := container.ID
|
|
|
|
|
2013-10-28 23:58:59 +00:00
|
|
|
if container.Name != "/some_name" {
|
|
|
|
t.Fatalf("Expect /some_name got %s", container.Name)
|
|
|
|
}
|
|
|
|
|
2013-11-14 06:10:20 +00:00
|
|
|
if c := runtime.Get("/some_name"); c == nil {
|
|
|
|
t.Fatalf("Couldn't retrieve test container as /some_name")
|
|
|
|
} else if c.ID != containerID {
|
|
|
|
t.Fatalf("Container /some_name has ID %s instead of %s", c.ID, containerID)
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 23:58:59 +00:00
|
|
|
func TestRandomContainerName(t *testing.T) {
|
2013-10-28 02:20:00 +00:00
|
|
|
eng := NewTestEngine(t)
|
2013-11-14 06:10:20 +00:00
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
2013-10-05 02:25:15 +00:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
2014-02-12 04:04:39 +00:00
|
|
|
config, _, _, err := runconfig.Parse([]string{GetTestImage(runtime).ID, "echo test"}, nil)
|
2013-10-05 02:25:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-10-28 02:20:00 +00:00
|
|
|
container := runtime.Get(createTestContainer(eng, config, t))
|
2013-10-05 02:25:15 +00:00
|
|
|
containerID := container.ID
|
|
|
|
|
2013-10-28 23:58:59 +00:00
|
|
|
if container.Name == "" {
|
|
|
|
t.Fatalf("Expected not empty container name")
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
|
2013-11-14 06:10:20 +00:00
|
|
|
if c := runtime.Get(container.Name); c == nil {
|
|
|
|
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-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 00:57:14 +00:00
|
|
|
func TestContainerNameValidation(t *testing.T) {
|
|
|
|
eng := NewTestEngine(t)
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
Name string
|
|
|
|
Valid bool
|
|
|
|
}{
|
|
|
|
{"abc-123_AAA.1", true},
|
|
|
|
{"\000asdf", false},
|
|
|
|
} {
|
2014-02-12 04:04:39 +00:00
|
|
|
config, _, _, err := runconfig.Parse([]string{unitTestImageID, "echo test"}, nil)
|
2013-12-18 00:57:14 +00:00
|
|
|
if err != nil {
|
|
|
|
if !test.Valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var shortID string
|
|
|
|
job := eng.Job("create", test.Name)
|
|
|
|
if err := job.ImportEnv(config); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
job.Stdout.AddString(&shortID)
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
if !test.Valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
container := runtime.Get(shortID)
|
|
|
|
|
|
|
|
if container.Name != "/"+test.Name {
|
|
|
|
t.Fatalf("Expect /%s got %s", test.Name, container.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c := runtime.Get("/" + test.Name); c == nil {
|
|
|
|
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-05 02:25:15 +00:00
|
|
|
func TestLinkChildContainer(t *testing.T) {
|
2013-10-28 02:20:00 +00:00
|
|
|
eng := NewTestEngine(t)
|
2013-11-14 06:10:20 +00:00
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
2013-10-05 02:25:15 +00:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
2014-02-12 04:04:39 +00:00
|
|
|
config, _, _, err := runconfig.Parse([]string{unitTestImageID, "echo test"}, nil)
|
2013-10-05 02:25:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-10-28 02:20:00 +00:00
|
|
|
container := runtime.Get(createNamedTestContainer(eng, config, t, "/webapp"))
|
2013-10-05 02:25:15 +00:00
|
|
|
|
|
|
|
webapp, err := runtime.GetByName("/webapp")
|
|
|
|
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-02-12 04:04:39 +00:00
|
|
|
config, _, _, err = runconfig.Parse([]string{GetTestImage(runtime).ID, "echo test"}, nil)
|
2013-10-05 02:25:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-10-28 02:20:00 +00:00
|
|
|
childContainer := runtime.Get(createTestContainer(eng, config, t))
|
2013-10-05 02:25:15 +00:00
|
|
|
|
2013-10-28 23:58:59 +00:00
|
|
|
if err := runtime.RegisterLink(webapp, childContainer, "db"); err != nil {
|
2013-10-05 02:25:15 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the child by it's new name
|
|
|
|
db, err := runtime.GetByName("/webapp/db")
|
|
|
|
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-28 02:20:00 +00:00
|
|
|
eng := NewTestEngine(t)
|
2013-11-14 06:10:20 +00:00
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
2013-10-05 02:25:15 +00:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
2014-02-12 04:04:39 +00:00
|
|
|
config, _, _, err := runconfig.Parse([]string{unitTestImageID, "echo test"}, nil)
|
2013-10-05 02:25:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-10-28 02:20:00 +00:00
|
|
|
container := runtime.Get(createNamedTestContainer(eng, config, t, "/webapp"))
|
2013-10-05 02:25:15 +00:00
|
|
|
|
|
|
|
webapp, err := runtime.GetByName("/webapp")
|
|
|
|
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-02-12 04:04:39 +00:00
|
|
|
config, _, _, err = runconfig.Parse([]string{unitTestImageID, "echo test"}, nil)
|
2013-10-05 02:25:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-10-28 02:20:00 +00:00
|
|
|
childContainer := runtime.Get(createTestContainer(eng, config, t))
|
2013-10-05 02:25:15 +00:00
|
|
|
|
2013-10-28 23:58:59 +00:00
|
|
|
if err := runtime.RegisterLink(webapp, childContainer, "db"); err != nil {
|
2013-10-05 02:25:15 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
children, err := runtime.Children("/webapp")
|
|
|
|
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 17:20:14 +00:00
|
|
|
|
|
|
|
func TestDestroyWithInitLayer(t *testing.T) {
|
|
|
|
runtime := mkRuntime(t)
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
2014-02-12 04:04:39 +00:00
|
|
|
container, _, err := runtime.Create(&runconfig.Config{
|
2013-12-03 17:20:14 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"ls", "-al"},
|
|
|
|
}, "")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Destroy
|
|
|
|
if err := runtime.Destroy(container); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure runtime.Exists() behaves correctly
|
|
|
|
if runtime.Exists("test_destroy") {
|
|
|
|
t.Fatalf("Exists() returned true")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure runtime.List() doesn't list the destroyed container
|
|
|
|
if len(runtime.List()) != 0 {
|
|
|
|
t.Fatalf("Expected 0 container, %v found", len(runtime.List()))
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := runtime.Graph().Driver()
|
|
|
|
|
|
|
|
// Make sure that the container does not exist in the driver
|
|
|
|
if _, err := driver.Get(container.ID); err == nil {
|
|
|
|
t.Fatal("Conttainer should not exist in the driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the init layer is removed from the driver
|
|
|
|
if _, err := driver.Get(fmt.Sprintf("%s-init", container.ID)); err == nil {
|
|
|
|
t.Fatal("Container's init layer should not exist in the driver")
|
|
|
|
}
|
|
|
|
}
|