mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Add test coverage for devicemapper driver.go
This commit is contained in:
parent
80e7319558
commit
6b3dd02bb8
2 changed files with 264 additions and 6 deletions
|
@ -22,7 +22,7 @@ type Driver struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(home string) (graphdriver.Driver, error) {
|
func Init(home string) (graphdriver.Driver, error) {
|
||||||
deviceSet, err := NewDeviceSet(home);
|
deviceSet, err := NewDeviceSet(home)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -61,17 +61,17 @@ func (d *Driver) Size(id string) (int64, error) {
|
||||||
return -1, fmt.Errorf("Not implemented")
|
return -1, fmt.Errorf("Not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) mount(id, mp string) error {
|
func (d *Driver) mount(id, mountPoint string) error {
|
||||||
// Create the target directories if they don't exist
|
// Create the target directories if they don't exist
|
||||||
if err := os.MkdirAll(mp, 0755); err != nil && !os.IsExist(err) {
|
if err := os.MkdirAll(mountPoint, 0755); err != nil && !os.IsExist(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// If mountpoint is already mounted, do nothing
|
// If mountpoint is already mounted, do nothing
|
||||||
if mounted, err := Mounted(mp); err != nil {
|
if mounted, err := Mounted(mountPoint); err != nil {
|
||||||
return fmt.Errorf("Error checking mountpoint: %s", err)
|
return fmt.Errorf("Error checking mountpoint: %s", err)
|
||||||
} else if mounted {
|
} else if mounted {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Mount the device
|
// Mount the device
|
||||||
return d.DeviceSet.MountDevice(id, mp, false)
|
return d.DeviceSet.MountDevice(id, mountPoint, false)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,18 @@ package devmapper
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Reduce the size the the base fs and loopback for the tests
|
||||||
|
DefaultDataLoopbackSize = 300 * 1024 * 1024
|
||||||
|
DefaultMetaDataLoopbackSize = 200 * 1024 * 1024
|
||||||
|
DefaultBaseFsSize = 300 * 1024 * 1024
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func mkTestDirectory(t *testing.T) string {
|
func mkTestDirectory(t *testing.T) string {
|
||||||
dir, err := ioutil.TempDir("", "docker-test-devmapper-")
|
dir, err := ioutil.TempDir("", "docker-test-devmapper-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -14,6 +23,20 @@ func mkTestDirectory(t *testing.T) string {
|
||||||
return dir
|
return dir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newDriver(t *testing.T) *Driver {
|
||||||
|
home := mkTestDirectory(t)
|
||||||
|
d, err := Init(home)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return d.(*Driver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanup(d *Driver) {
|
||||||
|
d.Cleanup()
|
||||||
|
os.RemoveAll(d.home)
|
||||||
|
}
|
||||||
|
|
||||||
func TestInit(t *testing.T) {
|
func TestInit(t *testing.T) {
|
||||||
home := mkTestDirectory(t)
|
home := mkTestDirectory(t)
|
||||||
defer os.RemoveAll(home)
|
defer os.RemoveAll(home)
|
||||||
|
@ -22,11 +45,11 @@ func TestInit(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
return
|
|
||||||
if err := driver.Cleanup(); err != nil {
|
if err := driver.Cleanup(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
id := "foo"
|
id := "foo"
|
||||||
if err := driver.Create(id, ""); err != nil {
|
if err := driver.Create(id, ""); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -41,3 +64,238 @@ func TestInit(t *testing.T) {
|
||||||
t.Fatalf("Get(%V) did not return a directory", id)
|
t.Fatalf("Get(%V) did not return a directory", id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDriverName(t *testing.T) {
|
||||||
|
d := newDriver(t)
|
||||||
|
defer cleanup(d)
|
||||||
|
|
||||||
|
if d.String() != "devicemapper" {
|
||||||
|
t.Fatalf("Expected driver name to be devicemapper got %s", d.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDriverCreate(t *testing.T) {
|
||||||
|
d := newDriver(t)
|
||||||
|
defer cleanup(d)
|
||||||
|
|
||||||
|
if err := d.Create("1", ""); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDriverRemove(t *testing.T) {
|
||||||
|
d := newDriver(t)
|
||||||
|
defer cleanup(d)
|
||||||
|
|
||||||
|
if err := d.Create("1", ""); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := d.Remove("1"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCleanup(t *testing.T) {
|
||||||
|
d := newDriver(t)
|
||||||
|
defer os.RemoveAll(d.home)
|
||||||
|
|
||||||
|
mountPoints := make([]string, 2)
|
||||||
|
|
||||||
|
if err := d.Create("1", ""); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Mount the id
|
||||||
|
p, err := d.Get("1")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
mountPoints[0] = p
|
||||||
|
|
||||||
|
if err := d.Create("2", "1"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
p, err = d.Get("2")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
mountPoints[1] = p
|
||||||
|
|
||||||
|
// Ensure that all the mount points are currently mounted
|
||||||
|
for _, p := range mountPoints {
|
||||||
|
if mounted, err := Mounted(p); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if !mounted {
|
||||||
|
t.Fatalf("Expected %s to be mounted", p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that devices are active
|
||||||
|
for _, p := range []string{"1", "2"} {
|
||||||
|
if !d.HasActivatedDevice(p) {
|
||||||
|
t.Fatalf("Expected %s to have an active device", p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := d.Cleanup(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that all the mount points are no longer mounted
|
||||||
|
for _, p := range mountPoints {
|
||||||
|
if mounted, err := Mounted(p); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if mounted {
|
||||||
|
t.Fatalf("Expected %s to not be mounted", p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that devices are no longer activated
|
||||||
|
for _, p := range []string{"1", "2"} {
|
||||||
|
if d.HasActivatedDevice(p) {
|
||||||
|
t.Fatalf("Expected %s not be an active device", p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNotMounted(t *testing.T) {
|
||||||
|
d := newDriver(t)
|
||||||
|
defer cleanup(d)
|
||||||
|
|
||||||
|
if err := d.Create("1", ""); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mounted, err := Mounted(path.Join(d.home, "mnt", "1"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if mounted {
|
||||||
|
t.Fatal("Id 1 should not be mounted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMounted(t *testing.T) {
|
||||||
|
d := newDriver(t)
|
||||||
|
defer cleanup(d)
|
||||||
|
|
||||||
|
if err := d.Create("1", ""); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := d.Get("1"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mounted, err := Mounted(path.Join(d.home, "mnt", "1"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !mounted {
|
||||||
|
t.Fatal("Id 1 should be mounted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInitCleanedDriver(t *testing.T) {
|
||||||
|
d := newDriver(t)
|
||||||
|
|
||||||
|
if err := d.Create("1", ""); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := d.Get("1"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := d.Cleanup(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
driver, err := Init(d.home)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
d = driver.(*Driver)
|
||||||
|
defer cleanup(d)
|
||||||
|
|
||||||
|
if _, err := d.Get("1"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMountMountedDriver(t *testing.T) {
|
||||||
|
d := newDriver(t)
|
||||||
|
defer cleanup(d)
|
||||||
|
|
||||||
|
if err := d.Create("1", ""); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perform get on same id to ensure that it will
|
||||||
|
// not be mounted twice
|
||||||
|
if _, err := d.Get("1"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := d.Get("1"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetReturnsValidDevice(t *testing.T) {
|
||||||
|
d := newDriver(t)
|
||||||
|
defer cleanup(d)
|
||||||
|
|
||||||
|
if err := d.Create("1", ""); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !d.HasDevice("1") {
|
||||||
|
t.Fatalf("Expected id 1 to be in device set")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := d.Get("1"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !d.HasActivatedDevice("1") {
|
||||||
|
t.Fatalf("Expected id 1 to be activated")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !d.HasInitializedDevice("1") {
|
||||||
|
t.Fatalf("Expected id 1 to be initialized")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDriverGetSize(t *testing.T) {
|
||||||
|
t.Skipf("Size is currently not implemented")
|
||||||
|
|
||||||
|
d := newDriver(t)
|
||||||
|
defer cleanup(d)
|
||||||
|
|
||||||
|
if err := d.Create("1", ""); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mountPoint, err := d.Get("1")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
size := int64(1024)
|
||||||
|
|
||||||
|
f, err := os.Create(path.Join(mountPoint, "test_file"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := f.Truncate(size); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
diffSize, err := d.Size("1")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if diffSize != size {
|
||||||
|
t.Fatalf("Expected size %d got %d", size, diffSize)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue