a27b4b8c |
package docker
import ( |
911925b5 |
"fmt" |
e7077320 |
"github.com/dotcloud/docker/utils" |
97a82094 |
"io" |
a27b4b8c |
"io/ioutil" |
2a53717e |
"log" |
911925b5 |
"net" |
a27b4b8c |
"os" |
2839a590 |
"os/user" |
2a53717e |
"strconv"
"strings" |
8f9e4542 |
"sync" |
a27b4b8c |
"testing" |
791ca6fd |
"time" |
a27b4b8c |
)
|
d01b5894 |
const unitTestImageName string = "docker-ut" |
45a89457 |
const unitTestImageId string = "e9aa60c60128cad1" |
ca6cd5b5 |
const unitTestStoreBase string = "/var/lib/docker/unit-tests" |
2ebf3464 |
|
b8547f31 |
func nuke(runtime *Runtime) error { |
8f9e4542 |
var wg sync.WaitGroup
for _, container := range runtime.List() {
wg.Add(1) |
1fc55c2b |
go func(c *Container) {
c.Kill() |
cab31fd5 |
wg.Done() |
1fc55c2b |
}(container) |
8f9e4542 |
}
wg.Wait() |
b8547f31 |
return os.RemoveAll(runtime.root) |
e0b09d49 |
}
|
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() { |
dd84ba34 |
// Hack to run sys init during unit testing |
e7077320 |
if utils.SelfPath() == "/sbin/init" { |
58a22942 |
SysInit() |
de753d5a |
return |
58a22942 |
} |
dd84ba34 |
|
2839a590 |
if usr, err := user.Current(); err != nil {
panic(err)
} else if usr.Uid != "0" {
panic("docker tests needs to be run as root")
}
|
ee298d14 |
NetworkBridgeIface = "testdockbr0"
|
de753d5a |
// Make it our Store root |
50144aeb |
runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false) |
de753d5a |
if err != nil {
panic(err)
} |
04cd20fa |
|
de753d5a |
// Create the "Server"
srv := &Server{ |
fe204e6f |
runtime: runtime,
enableCors: false,
lock: &sync.Mutex{},
pullingPool: make(map[string]struct{}),
pushingPool: make(map[string]struct{}), |
de753d5a |
}
// Retrieve the Image |
62c78696 |
if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, utils.NewStreamFormatter(false), nil); err != nil { |
de753d5a |
panic(err) |
dd84ba34 |
} |
58a22942 |
}
|
bb4b35a8 |
// FIXME: test that ImagePull(json=true) send correct json output
|
b8547f31 |
func newTestRuntime() (*Runtime, error) { |
a27b4b8c |
root, err := ioutil.TempDir("", "docker-test")
if err != nil {
return nil, err
} |
de753d5a |
if err := os.Remove(root); err != nil {
return nil, err
} |
6ae38001 |
if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil { |
de753d5a |
return nil, err
}
|
50144aeb |
runtime, err := NewRuntimeFromDirectory(root, false) |
a27b4b8c |
if err != nil {
return nil, err
} |
9042535f |
runtime.UpdateCapabilities(true) |
b8547f31 |
return runtime, nil |
a27b4b8c |
}
|
ef711962 |
func GetTestImage(runtime *Runtime) *Image { |
b8547f31 |
imgs, err := runtime.graph.All() |
2ebf3464 |
if err != nil {
panic(err)
} else if len(imgs) < 1 {
panic("GASP")
}
return imgs[0]
}
|
ef711962 |
func TestRuntimeCreate(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
a27b4b8c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
a27b4b8c |
// Make sure we start we 0 containers |
b8547f31 |
if len(runtime.List()) != 0 {
t.Errorf("Expected 0 containers, %v found", len(runtime.List())) |
a27b4b8c |
} |
42b1ea48 |
builder := NewBuilder(runtime)
container, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"ls", "-al"}, |
031f91df |
}, |
a27b4b8c |
)
if err != nil {
t.Fatal(err)
}
defer func() { |
b8547f31 |
if err := runtime.Destroy(container); err != nil { |
a27b4b8c |
t.Error(err)
}
}()
// Make sure we can find the newly created container with List() |
b8547f31 |
if len(runtime.List()) != 1 {
t.Errorf("Expected 1 container, %v found", len(runtime.List())) |
a27b4b8c |
}
// Make sure the container List() returns is the right one |
fd224ee5 |
if runtime.List()[0].ID != container.ID { |
b8547f31 |
t.Errorf("Unexpected container %v returned by List", runtime.List()[0]) |
a27b4b8c |
}
// Make sure we can get the container with Get() |
fd224ee5 |
if runtime.Get(container.ID) == nil { |
a27b4b8c |
t.Errorf("Unable to get newly created container")
}
// Make sure it is the right container |
fd224ee5 |
if runtime.Get(container.ID) != container { |
a27b4b8c |
t.Errorf("Get() returned the wrong container")
}
// Make sure Exists returns it as existing |
fd224ee5 |
if !runtime.Exists(container.ID) { |
a27b4b8c |
t.Errorf("Exists() returned false for a newly created container")
} |
42b1ea48 |
// Make sure crete with bad parameters returns an error
_, err = builder.Create(
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
42b1ea48 |
},
)
if err == nil {
t.Fatal("Builder.Create should throw an error when Cmd is missing")
}
_, err = builder.Create(
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
42b1ea48 |
Cmd: []string{},
},
)
if err == nil {
t.Fatal("Builder.Create should throw an error when Cmd is empty")
} |
a27b4b8c |
}
func TestDestroy(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
a27b4b8c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"ls", "-al"}, |
031f91df |
}, |
a27b4b8c |
)
if err != nil {
t.Fatal(err)
}
// Destroy |
b8547f31 |
if err := runtime.Destroy(container); err != nil { |
a27b4b8c |
t.Error(err)
}
|
b8547f31 |
// Make sure runtime.Exists() behaves correctly
if runtime.Exists("test_destroy") { |
a27b4b8c |
t.Errorf("Exists() returned true")
}
|
b8547f31 |
// 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())) |
a27b4b8c |
}
|
b8547f31 |
// Make sure runtime.Get() refuses to return the unexisting container |
fd224ee5 |
if runtime.Get(container.ID) != nil { |
a27b4b8c |
t.Errorf("Unable to get newly created container")
}
// Make sure the container root directory does not exist anymore |
7c57a4cf |
_, err = os.Stat(container.root) |
a27b4b8c |
if err == nil || !os.IsNotExist(err) {
t.Errorf("Container root directory still exists after destroy")
}
// Test double destroy |
b8547f31 |
if err := runtime.Destroy(container); err == nil { |
a27b4b8c |
// It should have failed
t.Errorf("Double destroy did not fail")
}
}
func TestGet(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
a27b4b8c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
builder := NewBuilder(runtime)
container1, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"ls", "-al"}, |
031f91df |
}, |
a27b4b8c |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container1) |
a27b4b8c |
|
ff95f2b0 |
container2, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"ls", "-al"}, |
031f91df |
}, |
a27b4b8c |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container2) |
a27b4b8c |
|
ff95f2b0 |
container3, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"ls", "-al"}, |
031f91df |
}, |
a27b4b8c |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container3) |
a27b4b8c |
|
fd224ee5 |
if runtime.Get(container1.ID) != container1 {
t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.ID), container1) |
a27b4b8c |
}
|
fd224ee5 |
if runtime.Get(container2.ID) != container2 {
t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.ID), container2) |
a27b4b8c |
}
|
fd224ee5 |
if runtime.Get(container3.ID) != container3 {
t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.ID), container3) |
a27b4b8c |
}
} |
2193b0c9 |
|
2a53717e |
func findAvailalblePort(runtime *Runtime, port int) (*Container, error) {
strPort := strconv.Itoa(port) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
2a53717e |
Cmd: []string{"sh", "-c", "echo well hello there | nc -l -p " + strPort},
PortSpecs: []string{strPort}, |
911925b5 |
},
)
if err != nil { |
2a53717e |
return nil, err |
911925b5 |
}
if err := container.Start(); err != nil { |
2a53717e |
if strings.Contains(err.Error(), "address already in use") {
return nil, nil
}
return nil, err
}
return container, nil
}
// Run a container with a TCP port allocated, and test that it can receive connections on localhost
func TestAllocatePortLocalhost(t *testing.T) {
runtime, err := newTestRuntime()
if err != nil { |
911925b5 |
t.Fatal(err)
} |
2a53717e |
port := 5554
var container *Container
for {
port += 1
log.Println("Trying port", port)
t.Log("Trying port", port)
container, err = findAvailalblePort(runtime, port)
if container != nil {
break
}
if err != nil {
t.Fatal(err)
}
log.Println("Port", port, "already in use")
t.Log("Port", port, "already in use")
}
|
911925b5 |
defer container.Kill() |
5a02c9ba |
setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
for {
if container.State.Running {
break
}
time.Sleep(10 * time.Millisecond)
}
})
|
911925b5 |
conn, err := net.Dial("tcp",
fmt.Sprintf( |
2a53717e |
"localhost:%s", container.NetworkSettings.PortMapping[strconv.Itoa(port)], |
911925b5 |
),
)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
output, err := ioutil.ReadAll(conn)
if err != nil {
t.Fatal(err)
}
if string(output) != "well hello there\n" {
t.Fatalf("Received wrong output from network connection: should be '%s', not '%s'",
"well hello there\n",
string(output),
)
} |
5a02c9ba |
container.Wait() |
911925b5 |
}
|
2193b0c9 |
func TestRestore(t *testing.T) { |
de753d5a |
|
2193b0c9 |
root, err := ioutil.TempDir("", "docker-test")
if err != nil {
t.Fatal(err)
} |
de753d5a |
if err := os.Remove(root); err != nil {
t.Fatal(err)
} |
6ae38001 |
if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil { |
de753d5a |
t.Fatal(err)
} |
6513a1d9 |
|
50144aeb |
runtime1, err := NewRuntimeFromDirectory(root, false) |
2193b0c9 |
if err != nil {
t.Fatal(err)
} |
2ebf3464 |
|
ff95f2b0 |
builder := NewBuilder(runtime1)
|
2ebf3464 |
// Create a container with one instance of docker |
ff95f2b0 |
container1, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime1).ID, |
6ce64e84 |
Cmd: []string{"ls", "-al"}, |
031f91df |
}, |
2193b0c9 |
) |
6513a1d9 |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime1.Destroy(container1) |
8c36e692 |
// Create a second container meant to be killed |
ff95f2b0 |
container2, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime1).ID, |
8c36e692 |
Cmd: []string{"/bin/cat"},
OpenStdin: true,
},
)
if err != nil {
t.Fatal(err)
} |
02c211a0 |
defer runtime1.Destroy(container2) |
8c36e692 |
// Start the container non blocking |
02c211a0 |
if err := container2.Start(); err != nil { |
8c36e692 |
t.Fatal(err)
}
|
02c211a0 |
if !container2.State.Running { |
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() |
bae6f958 |
if err := container2.WaitTimeout(2 * time.Second); err != nil { |
ad0183e4 |
t.Fatal(err)
} |
02c211a0 |
container2.State.Running = true |
791ca6fd |
container2.ToDisk() |
8c36e692 |
if len(runtime1.List()) != 2 {
t.Errorf("Expected 2 container, %v found", len(runtime1.List())) |
6513a1d9 |
}
if err := container1.Run(); err != nil {
t.Fatal(err) |
2193b0c9 |
}
|
02c211a0 |
if !container2.State.Running { |
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 |
50144aeb |
runtime2, err := NewRuntimeFromDirectory(root, false) |
2193b0c9 |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime2) |
8c36e692 |
if len(runtime2.List()) != 2 {
t.Errorf("Expected 2 container, %v found", len(runtime2.List()))
}
runningCount := 0
for _, c := range runtime2.List() {
if c.State.Running { |
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 |
} |
fd224ee5 |
container3 := runtime2.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)
} |
b76b329e |
container2.State.Running = false |
2193b0c9 |
} |