a27b4b8c |
package docker
import ( |
fac0d87d |
"bytes" |
911925b5 |
"fmt" |
97a82094 |
"io" |
2a53717e |
"log" |
911925b5 |
"net" |
b00d5f01 |
"net/url" |
a27b4b8c |
"os" |
21161dbd |
"path/filepath" |
4c2b9d73 |
"runtime" |
2a53717e |
"strconv"
"strings" |
ce9e50f4 |
"syscall" |
a27b4b8c |
"testing" |
791ca6fd |
"time" |
e304e3a6 |
"github.com/dotcloud/docker/daemon"
"github.com/dotcloud/docker/engine"
"github.com/dotcloud/docker/image"
"github.com/dotcloud/docker/nat"
"github.com/dotcloud/docker/runconfig"
"github.com/dotcloud/docker/sysinit"
"github.com/dotcloud/docker/utils" |
a27b4b8c |
)
|
a749fb21 |
const ( |
c000cb64 |
unitTestImageName = "docker-test-image"
unitTestImageID = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
unitTestImageIDShort = "83599e29c455"
unitTestNetworkBridge = "testdockbr0"
unitTestStoreBase = "/var/lib/docker/unit-tests"
testDaemonAddr = "127.0.0.1:4270"
testDaemonProto = "tcp"
testDaemonHttpsProto = "tcp"
testDaemonHttpsAddr = "localhost:4271"
testDaemonRogueHttpsAddr = "localhost:4272" |
a749fb21 |
) |
2ebf3464 |
|
fd9ad1a1 |
var ( |
359b7df5 |
// FIXME: globalDaemon is deprecated by globalEngine. All tests should be converted.
globalDaemon *daemon.Daemon |
c000cb64 |
globalEngine *engine.Engine
globalHttpsEngine *engine.Engine
globalRogueHttpsEngine *engine.Engine
startFds int
startGoroutines int |
fd9ad1a1 |
) |
5190f7f3 |
|
359b7df5 |
// FIXME: nuke() is deprecated by Daemon.Nuke()
func nuke(daemon *daemon.Daemon) error {
return daemon.Nuke() |
e0b09d49 |
}
|
c001a5af |
// FIXME: cleanup and nuke are redundant.
func cleanup(eng *engine.Engine, t *testing.T) error { |
359b7df5 |
daemon := mkDaemonFromEngine(eng, t)
for _, container := range daemon.List() { |
5190f7f3 |
container.Kill() |
359b7df5 |
daemon.Destroy(container) |
5190f7f3 |
} |
17a806c8 |
job := eng.Job("images")
images, err := job.Stdout.AddTable() |
5190f7f3 |
if err != nil { |
17a806c8 |
t.Fatal(err) |
5190f7f3 |
} |
17a806c8 |
if err := job.Run(); err != nil {
t.Fatal(err)
}
for _, image := range images.Data { |
54072dbb |
if image.Get("Id") != unitTestImageID { |
b881a6c3 |
eng.Job("image_delete", image.Get("Id")).Run() |
5190f7f3 |
}
}
return nil
}
|
97a82094 |
func layerArchive(tarfile string) (io.Reader, error) { |
2ebf3464 |
// FIXME: need to close f somewhere
f, err := os.Open(tarfile)
if err != nil {
return nil, err
}
return f, nil
} |
dd84ba34 |
|
58a22942 |
func init() { |
a9373137 |
// 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") |
740a97f1 |
os.Setenv("TEST", "1")
|
dd84ba34 |
// Hack to run sys init during unit testing |
de083400 |
if selfPath := utils.SelfPath(); strings.Contains(selfPath, ".dockerinit") { |
db999667 |
sysinit.SysInit() |
de753d5a |
return |
58a22942 |
} |
dd84ba34 |
|
ce9e50f4 |
if uid := syscall.Geteuid(); uid != 0 { |
ba9aef6f |
log.Fatal("docker tests need to be run as root") |
2839a590 |
}
|
21161dbd |
// 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()
}
|
359b7df5 |
// Setup the base daemon, which will be duplicated for each test. |
240d5b3f |
// (no tests are run directly in the base)
setupBaseImage() |
04cd20fa |
|
359b7df5 |
// Create the "global daemon" with a long-running daemons for integration tests |
240d5b3f |
spawnGlobalDaemon() |
c000cb64 |
spawnLegitHttpsDaemon()
spawnRogueHttpsDaemon() |
4c2b9d73 |
startFds, startGoroutines = utils.GetTotalUsedFds(), runtime.NumGoroutine() |
240d5b3f |
}
func setupBaseImage() { |
92c3927b |
eng := newTestEngine(log.New(os.Stderr, "", 0), false, unitTestStoreBase) |
730d9ba1 |
job := eng.Job("image_inspect", unitTestImageName) |
fc2f9988 |
img, _ := job.Stdout.AddEnv() |
e43ef364 |
// If the unit test is not found, try to download it. |
730d9ba1 |
if err := job.Run(); err != nil || img.Get("Id") != unitTestImageID { |
e43ef364 |
// Retrieve the Image |
9dcbdbc4 |
job = eng.Job("pull", unitTestImageName)
job.Stdout.Add(utils.NopWriteCloser(os.Stdout))
if err := job.Run(); err != nil { |
402a58a7 |
log.Fatalf("Unable to pull the test image: %s", err) |
e43ef364 |
} |
dd84ba34 |
} |
240d5b3f |
}
func spawnGlobalDaemon() { |
359b7df5 |
if globalDaemon != nil {
utils.Debugf("Global daemon already exists. Skipping.") |
240d5b3f |
return
} |
5a85456d |
t := log.New(os.Stderr, "", 0)
eng := NewTestEngine(t) |
c001a5af |
globalEngine = eng |
359b7df5 |
globalDaemon = mkDaemonFromEngine(eng, t) |
240d5b3f |
|
873a5aa8 |
// Spawn a Daemon
go func() { |
240d5b3f |
utils.Debugf("Spawning global daemon for integration tests") |
5a85456d |
listenURL := &url.URL{ |
b00d5f01 |
Scheme: testDaemonProto,
Host: testDaemonAddr, |
5a85456d |
}
job := eng.Job("serveapi", listenURL.String()) |
17a806c8 |
job.SetenvBool("Logging", true) |
5a85456d |
if err := job.Run(); err != nil {
log.Fatalf("Unable to spawn the test daemon: %s", err) |
873a5aa8 |
}
}() |
778f1bf6 |
|
5190f7f3 |
// Give some time to ListenAndServer to actually start |
240d5b3f |
// FIXME: use inmem transports instead of tcp |
5190f7f3 |
time.Sleep(time.Second) |
778f1bf6 |
if err := eng.Job("acceptconnections").Run(); err != nil {
log.Fatalf("Unable to accept connections for test api: %s", err)
} |
58a22942 |
}
|
c000cb64 |
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 {
t := log.New(os.Stderr, "", 0)
root, err := newTestDirectory(unitTestStoreBase)
if err != nil {
t.Fatal(err)
}
// 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)
// Spawn a Daemon
go func() {
utils.Debugf("Spawning https daemon for integration tests")
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
}
|
bb4b35a8 |
// FIXME: test that ImagePull(json=true) send correct json output
|
359b7df5 |
func GetTestImage(daemon *daemon.Daemon) *image.Image {
imgs, err := daemon.Graph().Map() |
2ebf3464 |
if err != nil { |
a6c9a332 |
log.Fatalf("Unable to get the test image: %s", err) |
2ebf3464 |
} |
1fca99ad |
for _, image := range imgs {
if image.ID == unitTestImageID {
return image |
fe014a8e |
}
} |
359b7df5 |
log.Fatalf("Test image %v not found in %s: %s", unitTestImageID, daemon.Graph().Root, imgs) |
823174de |
return nil |
2ebf3464 |
}
|
359b7df5 |
func TestDaemonCreate(t *testing.T) {
daemon := mkDaemon(t)
defer nuke(daemon) |
a27b4b8c |
// Make sure we start we 0 containers |
359b7df5 |
if len(daemon.List()) != 0 {
t.Errorf("Expected 0 containers, %v found", len(daemon.List())) |
a27b4b8c |
} |
42b1ea48 |
|
359b7df5 |
container, _, err := daemon.Create(&runconfig.Config{
Image: GetTestImage(daemon).ID, |
6ce64e84 |
Cmd: []string{"ls", "-al"}, |
031f91df |
}, |
0d292440 |
"", |
a27b4b8c |
)
if err != nil {
t.Fatal(err)
}
defer func() { |
359b7df5 |
if err := daemon.Destroy(container); err != nil { |
a27b4b8c |
t.Error(err)
}
}()
// Make sure we can find the newly created container with List() |
359b7df5 |
if len(daemon.List()) != 1 {
t.Errorf("Expected 1 container, %v found", len(daemon.List())) |
a27b4b8c |
}
// Make sure the container List() returns is the right one |
359b7df5 |
if daemon.List()[0].ID != container.ID {
t.Errorf("Unexpected container %v returned by List", daemon.List()[0]) |
a27b4b8c |
}
// Make sure we can get the container with Get() |
359b7df5 |
if daemon.Get(container.ID) == nil { |
a27b4b8c |
t.Errorf("Unable to get newly created container")
}
// Make sure it is the right container |
359b7df5 |
if daemon.Get(container.ID) != container { |
a27b4b8c |
t.Errorf("Get() returned the wrong container")
}
// Make sure Exists returns it as existing |
359b7df5 |
if !daemon.Exists(container.ID) { |
a27b4b8c |
t.Errorf("Exists() returned false for a newly created container")
} |
42b1ea48 |
|
3c67a284 |
// Test that conflict error displays correct details |
359b7df5 |
testContainer, _, _ := daemon.Create( |
6393c383 |
&runconfig.Config{ |
359b7df5 |
Image: GetTestImage(daemon).ID, |
3c67a284 |
Cmd: []string{"ls", "-al"},
},
"conflictname",
) |
359b7df5 |
if _, _, err := daemon.Create(&runconfig.Config{Image: GetTestImage(daemon).ID, Cmd: []string{"ls", "-al"}}, testContainer.Name); err == nil || !strings.Contains(err.Error(), utils.TruncateID(testContainer.ID)) { |
3c67a284 |
t.Fatalf("Name conflict error doesn't include the correct short id. Message was: %s", err.Error())
}
|
1cbdaeba |
// Make sure create with bad parameters returns an error |
359b7df5 |
if _, _, err = daemon.Create(&runconfig.Config{Image: GetTestImage(daemon).ID}, ""); err == nil { |
42b1ea48 |
t.Fatal("Builder.Create should throw an error when Cmd is missing")
}
|
359b7df5 |
if _, _, err := daemon.Create( |
6393c383 |
&runconfig.Config{ |
359b7df5 |
Image: GetTestImage(daemon).ID, |
42b1ea48 |
Cmd: []string{},
}, |
0d292440 |
"", |
3e014aa6 |
); err == nil { |
42b1ea48 |
t.Fatal("Builder.Create should throw an error when Cmd is empty")
} |
1328be7c |
|
6393c383 |
config := &runconfig.Config{ |
359b7df5 |
Image: GetTestImage(daemon).ID, |
1328be7c |
Cmd: []string{"/bin/ls"},
PortSpecs: []string{"80"},
} |
359b7df5 |
container, _, err = daemon.Create(config, "") |
1328be7c |
|
17d870be |
_, err = daemon.Commit(container, "testrepo", "testtag", "", "", true, config) |
1328be7c |
if err != nil {
t.Error(err)
} |
b2503a72 |
// test expose 80:8000 |
359b7df5 |
container, warnings, err := daemon.Create(&runconfig.Config{
Image: GetTestImage(daemon).ID, |
b2503a72 |
Cmd: []string{"ls", "-al"},
PortSpecs: []string{"80:8000"},
},
"",
)
if err != nil {
t.Fatal(err)
} |
1c7ca51f |
if warnings == nil || len(warnings) != 1 { |
b2503a72 |
t.Error("Expected a warning, got none")
} |
a27b4b8c |
}
func TestDestroy(t *testing.T) { |
359b7df5 |
daemon := mkDaemon(t)
defer nuke(daemon) |
3e014aa6 |
|
359b7df5 |
container, _, err := daemon.Create(&runconfig.Config{
Image: GetTestImage(daemon).ID, |
6ce64e84 |
Cmd: []string{"ls", "-al"}, |
0d292440 |
}, "") |
a27b4b8c |
if err != nil {
t.Fatal(err)
}
// Destroy |
359b7df5 |
if err := daemon.Destroy(container); err != nil { |
a27b4b8c |
t.Error(err)
}
|
359b7df5 |
// Make sure daemon.Exists() behaves correctly
if daemon.Exists("test_destroy") { |
a27b4b8c |
t.Errorf("Exists() returned true")
}
|
359b7df5 |
// 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())) |
a27b4b8c |
}
|
359b7df5 |
// Make sure daemon.Get() refuses to return the unexisting container
if daemon.Get(container.ID) != nil { |
a27b4b8c |
t.Errorf("Unable to get newly created container")
}
// Test double destroy |
359b7df5 |
if err := daemon.Destroy(container); err == nil { |
a27b4b8c |
// It should have failed
t.Errorf("Double destroy did not fail")
}
}
func TestGet(t *testing.T) { |
359b7df5 |
daemon := mkDaemon(t)
defer nuke(daemon) |
ff95f2b0 |
|
359b7df5 |
container1, _, _ := mkContainer(daemon, []string{"_", "ls", "-al"}, t)
defer daemon.Destroy(container1) |
a27b4b8c |
|
359b7df5 |
container2, _, _ := mkContainer(daemon, []string{"_", "ls", "-al"}, t)
defer daemon.Destroy(container2) |
a27b4b8c |
|
359b7df5 |
container3, _, _ := mkContainer(daemon, []string{"_", "ls", "-al"}, t)
defer daemon.Destroy(container3) |
a27b4b8c |
|
359b7df5 |
if daemon.Get(container1.ID) != container1 {
t.Errorf("Get(test1) returned %v while expecting %v", daemon.Get(container1.ID), container1) |
a27b4b8c |
}
|
359b7df5 |
if daemon.Get(container2.ID) != container2 {
t.Errorf("Get(test2) returned %v while expecting %v", daemon.Get(container2.ID), container2) |
a27b4b8c |
}
|
359b7df5 |
if daemon.Get(container3.ID) != container3 {
t.Errorf("Get(test3) returned %v while expecting %v", daemon.Get(container3.ID), container3) |
a27b4b8c |
}
} |
2193b0c9 |
|
359b7df5 |
func startEchoServerContainer(t *testing.T, proto string) (*daemon.Daemon, *daemon.Container, string) { |
3e014aa6 |
var ( |
e304e3a6 |
err error
id string
outputBuffer = bytes.NewBuffer(nil)
strPort string
eng = NewTestEngine(t)
daemon = mkDaemonFromEngine(eng, t)
port = 5554
p nat.Port |
3e014aa6 |
) |
c001a5af |
defer func() {
if err != nil { |
359b7df5 |
daemon.Nuke() |
c001a5af |
}
}() |
3e014aa6 |
|
2a53717e |
for {
port += 1 |
fac0d87d |
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))
} |
3ecd8ff0 |
ep := make(map[nat.Port]struct{}, 1)
p = nat.Port(fmt.Sprintf("%s/%s", strPort, proto)) |
1cbdaeba |
ep[p] = struct{}{}
|
c001a5af |
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) |
e304e3a6 |
jobCreate.Stdout.Add(outputBuffer) |
c001a5af |
if err := jobCreate.Run(); err != nil { |
2a53717e |
t.Fatal(err)
} |
e304e3a6 |
id = engine.Tail(outputBuffer, 1) |
359b7df5 |
// FIXME: this relies on the undocumented behavior of daemon.Create |
c001a5af |
// which will return a nil error AND container if the exposed ports
// are invalid. That behavior should be fixed!
if id != "" { |
1cbdaeba |
break
} |
acc2aec9 |
t.Logf("Port %v already in use, trying another one", strPort) |
2a53717e |
|
1cbdaeba |
} |
c001a5af |
jobStart := eng.Job("start", id) |
3ecd8ff0 |
portBindings := make(map[nat.Port][]nat.PortBinding)
portBindings[p] = []nat.PortBinding{ |
1cbdaeba |
{},
} |
c001a5af |
if err := jobStart.SetenvJson("PortsBindings", portBindings); err != nil { |
fac0d87d |
t.Fatal(err)
} |
c001a5af |
if err := jobStart.Run(); err != nil {
t.Fatal(err)
}
|
359b7df5 |
container := daemon.Get(id) |
c001a5af |
if container == nil {
t.Fatalf("Couldn't fetch test container %s", id)
} |
5a02c9ba |
setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() { |
33e70864 |
for !container.State.IsRunning() { |
5a02c9ba |
time.Sleep(10 * time.Millisecond)
}
})
|
ebba0a60 |
// Even if the state is running, lets give some time to lxc to spawn the process |
57d86a56 |
container.State.WaitStop(500 * time.Millisecond) |
ebba0a60 |
|
1cbdaeba |
strPort = container.NetworkSettings.Ports[p][0].HostPort |
359b7df5 |
return daemon, container, strPort |
fac0d87d |
}
// Run a container with a TCP port allocated, and test that it can receive connections on localhost
func TestAllocateTCPPortLocalhost(t *testing.T) { |
359b7df5 |
daemon, container, port := startEchoServerContainer(t, "tcp")
defer nuke(daemon) |
fac0d87d |
defer container.Kill()
|
5a411fa3 |
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() |
fac0d87d |
|
5a411fa3 |
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
} |
fac0d87d |
} |
5a411fa3 |
t.Fatal("No reply from the container") |
fac0d87d |
}
|
5a411fa3 |
// Run a container with an UDP port allocated, and test that it can receive connections on localhost |
fac0d87d |
func TestAllocateUDPPortLocalhost(t *testing.T) { |
359b7df5 |
daemon, container, port := startEchoServerContainer(t, "udp")
defer nuke(daemon) |
fac0d87d |
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) |
5a411fa3 |
// 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++ { |
fac0d87d |
_, err := conn.Write(input.Bytes())
if err != nil {
t.Fatal(err)
} |
5a411fa3 |
conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond)) |
fac0d87d |
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") |
911925b5 |
}
|
2193b0c9 |
func TestRestore(t *testing.T) { |
c001a5af |
eng := NewTestEngine(t) |
359b7df5 |
daemon1 := mkDaemonFromEngine(eng, t)
defer daemon1.Nuke() |
2ebf3464 |
// Create a container with one instance of docker |
359b7df5 |
container1, _, _ := mkContainer(daemon1, []string{"_", "ls", "-al"}, t)
defer daemon1.Destroy(container1) |
8c36e692 |
// Create a second container meant to be killed |
359b7df5 |
container2, _, _ := mkContainer(daemon1, []string{"-i", "_", "/bin/cat"}, t)
defer daemon1.Destroy(container2) |
8c36e692 |
// Start the container non blocking |
31638ab2 |
if err := container2.Start(); err != nil { |
8c36e692 |
t.Fatal(err)
}
|
33e70864 |
if !container2.State.IsRunning() { |
fd224ee5 |
t.Fatalf("Container %v should appear as running but isn't", container2.ID) |
c780ff5a |
}
|
8c36e692 |
// Simulate a crash/manual quit of dockerd: process dies, states stays 'Running' |
791ca6fd |
cStdin, _ := container2.StdinPipe()
cStdin.Close() |
57d86a56 |
if _, err := container2.State.WaitStop(2 * time.Second); err != nil { |
ad0183e4 |
t.Fatal(err)
} |
33e70864 |
container2.State.SetRunning(42) |
791ca6fd |
container2.ToDisk() |
8c36e692 |
|
359b7df5 |
if len(daemon1.List()) != 2 {
t.Errorf("Expected 2 container, %v found", len(daemon1.List())) |
6513a1d9 |
}
if err := container1.Run(); err != nil {
t.Fatal(err) |
2193b0c9 |
}
|
33e70864 |
if !container2.State.IsRunning() { |
fd224ee5 |
t.Fatalf("Container %v should appear as running but isn't", container2.ID) |
c780ff5a |
}
|
6513a1d9 |
// Here are are simulating a docker restart - that is, reloading all containers
// from scratch |
87e8d775 |
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())) |
8c36e692 |
}
runningCount := 0 |
359b7df5 |
for _, c := range daemon2.List() { |
33e70864 |
if c.State.IsRunning() { |
fd224ee5 |
t.Errorf("Running container found: %v (%v)", c.ID, c.Path) |
8c36e692 |
runningCount++
}
}
if runningCount != 0 {
t.Fatalf("Expected 0 container alive, %d found", runningCount) |
2193b0c9 |
} |
359b7df5 |
container3 := daemon2.Get(container1.ID) |
02c211a0 |
if container3 == nil { |
6513a1d9 |
t.Fatal("Unable to Get container")
} |
02c211a0 |
if err := container3.Run(); err != nil { |
6513a1d9 |
t.Fatal(err)
} |
33e70864 |
container2.State.SetStopped(0) |
2193b0c9 |
} |
1cbdaeba |
func TestDefaultContainerName(t *testing.T) { |
e5f8ab61 |
eng := NewTestEngine(t) |
359b7df5 |
daemon := mkDaemonFromEngine(eng, t)
defer nuke(daemon) |
1cbdaeba |
|
6393c383 |
config, _, _, err := runconfig.Parse([]string{unitTestImageID, "echo test"}, nil) |
1cbdaeba |
if err != nil {
t.Fatal(err)
}
|
359b7df5 |
container := daemon.Get(createNamedTestContainer(eng, config, t, "some_name")) |
1cbdaeba |
containerID := container.ID
|
0d292440 |
if container.Name != "/some_name" {
t.Fatalf("Expect /some_name got %s", container.Name)
}
|
359b7df5 |
if c := daemon.Get("/some_name"); c == nil { |
c001a5af |
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) |
1cbdaeba |
}
}
|
0d292440 |
func TestRandomContainerName(t *testing.T) { |
e5f8ab61 |
eng := NewTestEngine(t) |
359b7df5 |
daemon := mkDaemonFromEngine(eng, t)
defer nuke(daemon) |
1cbdaeba |
|
359b7df5 |
config, _, _, err := runconfig.Parse([]string{GetTestImage(daemon).ID, "echo test"}, nil) |
1cbdaeba |
if err != nil {
t.Fatal(err)
}
|
359b7df5 |
container := daemon.Get(createTestContainer(eng, config, t)) |
1cbdaeba |
containerID := container.ID
|
0d292440 |
if container.Name == "" {
t.Fatalf("Expected not empty container name") |
1cbdaeba |
}
|
359b7df5 |
if c := daemon.Get(container.Name); c == nil { |
c001a5af |
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) |
1cbdaeba |
}
}
|
c06ab5f9 |
func TestContainerNameValidation(t *testing.T) {
eng := NewTestEngine(t) |
359b7df5 |
daemon := mkDaemonFromEngine(eng, t)
defer nuke(daemon) |
c06ab5f9 |
for _, test := range []struct {
Name string
Valid bool
}{
{"abc-123_AAA.1", true},
{"\000asdf", false},
} { |
6393c383 |
config, _, _, err := runconfig.Parse([]string{unitTestImageID, "echo test"}, nil) |
c06ab5f9 |
if err != nil {
if !test.Valid {
continue
}
t.Fatal(err)
}
|
e304e3a6 |
var outputBuffer = bytes.NewBuffer(nil) |
c06ab5f9 |
job := eng.Job("create", test.Name)
if err := job.ImportEnv(config); err != nil {
t.Fatal(err)
} |
e304e3a6 |
job.Stdout.Add(outputBuffer) |
c06ab5f9 |
if err := job.Run(); err != nil {
if !test.Valid {
continue
}
t.Fatal(err)
}
|
e304e3a6 |
container := daemon.Get(engine.Tail(outputBuffer, 1)) |
c06ab5f9 |
if container.Name != "/"+test.Name {
t.Fatalf("Expect /%s got %s", test.Name, container.Name)
}
|
359b7df5 |
if c := daemon.Get("/" + test.Name); c == nil { |
c06ab5f9 |
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)
}
}
}
|
1cbdaeba |
func TestLinkChildContainer(t *testing.T) { |
e5f8ab61 |
eng := NewTestEngine(t) |
359b7df5 |
daemon := mkDaemonFromEngine(eng, t)
defer nuke(daemon) |
1cbdaeba |
|
6393c383 |
config, _, _, err := runconfig.Parse([]string{unitTestImageID, "echo test"}, nil) |
1cbdaeba |
if err != nil {
t.Fatal(err)
}
|
359b7df5 |
container := daemon.Get(createNamedTestContainer(eng, config, t, "/webapp")) |
1cbdaeba |
|
359b7df5 |
webapp, err := daemon.GetByName("/webapp") |
1cbdaeba |
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)
}
|
359b7df5 |
config, _, _, err = runconfig.Parse([]string{GetTestImage(daemon).ID, "echo test"}, nil) |
1cbdaeba |
if err != nil {
t.Fatal(err)
}
|
359b7df5 |
childContainer := daemon.Get(createTestContainer(eng, config, t)) |
1cbdaeba |
|
359b7df5 |
if err := daemon.RegisterLink(webapp, childContainer, "db"); err != nil { |
1cbdaeba |
t.Fatal(err)
}
// Get the child by it's new name |
359b7df5 |
db, err := daemon.GetByName("/webapp/db") |
1cbdaeba |
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) { |
e5f8ab61 |
eng := NewTestEngine(t) |
359b7df5 |
daemon := mkDaemonFromEngine(eng, t)
defer nuke(daemon) |
1cbdaeba |
|
6393c383 |
config, _, _, err := runconfig.Parse([]string{unitTestImageID, "echo test"}, nil) |
1cbdaeba |
if err != nil {
t.Fatal(err)
}
|
359b7df5 |
container := daemon.Get(createNamedTestContainer(eng, config, t, "/webapp")) |
1cbdaeba |
|
359b7df5 |
webapp, err := daemon.GetByName("/webapp") |
1cbdaeba |
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)
}
|
6393c383 |
config, _, _, err = runconfig.Parse([]string{unitTestImageID, "echo test"}, nil) |
1cbdaeba |
if err != nil {
t.Fatal(err)
}
|
359b7df5 |
childContainer := daemon.Get(createTestContainer(eng, config, t)) |
1cbdaeba |
|
359b7df5 |
if err := daemon.RegisterLink(webapp, childContainer, "db"); err != nil { |
1cbdaeba |
t.Fatal(err)
}
|
359b7df5 |
children, err := daemon.Children("/webapp") |
1cbdaeba |
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)
}
}
} |
5976c26c |
func TestDestroyWithInitLayer(t *testing.T) { |
359b7df5 |
daemon := mkDaemon(t)
defer nuke(daemon) |
5976c26c |
|
359b7df5 |
container, _, err := daemon.Create(&runconfig.Config{
Image: GetTestImage(daemon).ID, |
5976c26c |
Cmd: []string{"ls", "-al"},
}, "")
if err != nil {
t.Fatal(err)
}
// Destroy |
359b7df5 |
if err := daemon.Destroy(container); err != nil { |
5976c26c |
t.Fatal(err)
}
|
359b7df5 |
// Make sure daemon.Exists() behaves correctly
if daemon.Exists("test_destroy") { |
5976c26c |
t.Fatalf("Exists() returned true")
}
|
359b7df5 |
// 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())) |
5976c26c |
}
|
359b7df5 |
driver := daemon.Graph().Driver() |
5976c26c |
// Make sure that the container does not exist in the driver |
f0e6e135 |
if _, err := driver.Get(container.ID, ""); err == nil { |
5976c26c |
t.Fatal("Conttainer should not exist in the driver")
}
// Make sure that the init layer is removed from the driver |
f0e6e135 |
if _, err := driver.Get(fmt.Sprintf("%s-init", container.ID), ""); err == nil { |
5976c26c |
t.Fatal("Container's init layer should not exist in the driver")
}
} |