2013-01-19 00:13:39 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-01-22 20:12:16 +00:00
|
|
|
"bytes"
|
2013-01-19 00:13:39 +00:00
|
|
|
"io/ioutil"
|
2013-01-22 20:12:16 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2013-01-19 00:13:39 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFilesystem(t *testing.T) {
|
|
|
|
rootfs, err := ioutil.TempDir("", "docker-test-root")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rwpath, err := ioutil.TempDir("", "docker-test-rw")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-01-22 20:12:16 +00:00
|
|
|
filesystem := newFilesystem(rootfs, rwpath, []string{"/var/lib/docker/images/ubuntu"})
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
if err := filesystem.Umount(); err == nil {
|
|
|
|
t.Errorf("Umount succeeded even though the filesystem was not mounted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := filesystem.Mount(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-01-22 20:12:16 +00:00
|
|
|
if err := filesystem.Mount(); err == nil {
|
|
|
|
t.Errorf("Double mount succeeded")
|
|
|
|
}
|
|
|
|
|
2013-01-19 00:13:39 +00:00
|
|
|
if err := filesystem.Umount(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := filesystem.Umount(); err == nil {
|
|
|
|
t.Errorf("Umount succeeded even though the filesystem was already umounted")
|
|
|
|
}
|
|
|
|
}
|
2013-01-22 20:12:16 +00:00
|
|
|
|
|
|
|
func TestFilesystemMultiLayer(t *testing.T) {
|
|
|
|
// Create a fake layer
|
|
|
|
fakeLayer, err := ioutil.TempDir("", "docker-layer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
data := []byte("hello world")
|
|
|
|
if err := ioutil.WriteFile(path.Join(fakeLayer, "test_file"), data, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the layered filesystem and add our fake layer on top
|
|
|
|
rootfs, err := ioutil.TempDir("", "docker-test-root")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rwpath, err := ioutil.TempDir("", "docker-test-rw")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
filesystem := newFilesystem(rootfs, rwpath, []string{"/var/lib/docker/images/ubuntu", fakeLayer})
|
|
|
|
|
|
|
|
// Mount it
|
|
|
|
if err := filesystem.Mount(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := filesystem.Umount(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Check to see whether we can access our fake layer
|
|
|
|
if _, err := os.Stat(path.Join(rootfs, "test_file")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fsdata, err := ioutil.ReadFile(path.Join(rootfs, "test_file"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(data, fsdata) {
|
|
|
|
t.Error(string(fsdata))
|
|
|
|
}
|
|
|
|
}
|