| ... | ... |
@@ -22,7 +22,7 @@ type Driver struct {
|
| 22 | 22 |
} |
| 23 | 23 |
|
| 24 | 24 |
func Init(home string) (graphdriver.Driver, error) {
|
| 25 |
- deviceSet, err := NewDeviceSet(home); |
|
| 25 |
+ deviceSet, err := NewDeviceSet(home) |
|
| 26 | 26 |
if err != nil {
|
| 27 | 27 |
return nil, err |
| 28 | 28 |
} |
| ... | ... |
@@ -61,17 +61,17 @@ func (d *Driver) Size(id string) (int64, error) {
|
| 61 | 61 |
return -1, fmt.Errorf("Not implemented")
|
| 62 | 62 |
} |
| 63 | 63 |
|
| 64 |
-func (d *Driver) mount(id, mp string) error {
|
|
| 64 |
+func (d *Driver) mount(id, mountPoint string) error {
|
|
| 65 | 65 |
// Create the target directories if they don't exist |
| 66 |
- if err := os.MkdirAll(mp, 0755); err != nil && !os.IsExist(err) {
|
|
| 66 |
+ if err := os.MkdirAll(mountPoint, 0755); err != nil && !os.IsExist(err) {
|
|
| 67 | 67 |
return err |
| 68 | 68 |
} |
| 69 | 69 |
// If mountpoint is already mounted, do nothing |
| 70 |
- if mounted, err := Mounted(mp); err != nil {
|
|
| 70 |
+ if mounted, err := Mounted(mountPoint); err != nil {
|
|
| 71 | 71 |
return fmt.Errorf("Error checking mountpoint: %s", err)
|
| 72 | 72 |
} else if mounted {
|
| 73 | 73 |
return nil |
| 74 | 74 |
} |
| 75 | 75 |
// Mount the device |
| 76 |
- return d.DeviceSet.MountDevice(id, mp, false) |
|
| 76 |
+ return d.DeviceSet.MountDevice(id, mountPoint, false) |
|
| 77 | 77 |
} |
| ... | ... |
@@ -3,9 +3,18 @@ package devmapper |
| 3 | 3 |
import ( |
| 4 | 4 |
"io/ioutil" |
| 5 | 5 |
"os" |
| 6 |
+ "path" |
|
| 6 | 7 |
"testing" |
| 7 | 8 |
) |
| 8 | 9 |
|
| 10 |
+func init() {
|
|
| 11 |
+ // Reduce the size the the base fs and loopback for the tests |
|
| 12 |
+ DefaultDataLoopbackSize = 300 * 1024 * 1024 |
|
| 13 |
+ DefaultMetaDataLoopbackSize = 200 * 1024 * 1024 |
|
| 14 |
+ DefaultBaseFsSize = 300 * 1024 * 1024 |
|
| 15 |
+ |
|
| 16 |
+} |
|
| 17 |
+ |
|
| 9 | 18 |
func mkTestDirectory(t *testing.T) string {
|
| 10 | 19 |
dir, err := ioutil.TempDir("", "docker-test-devmapper-")
|
| 11 | 20 |
if err != nil {
|
| ... | ... |
@@ -14,6 +23,20 @@ func mkTestDirectory(t *testing.T) string {
|
| 14 | 14 |
return dir |
| 15 | 15 |
} |
| 16 | 16 |
|
| 17 |
+func newDriver(t *testing.T) *Driver {
|
|
| 18 |
+ home := mkTestDirectory(t) |
|
| 19 |
+ d, err := Init(home) |
|
| 20 |
+ if err != nil {
|
|
| 21 |
+ t.Fatal(err) |
|
| 22 |
+ } |
|
| 23 |
+ return d.(*Driver) |
|
| 24 |
+} |
|
| 25 |
+ |
|
| 26 |
+func cleanup(d *Driver) {
|
|
| 27 |
+ d.Cleanup() |
|
| 28 |
+ os.RemoveAll(d.home) |
|
| 29 |
+} |
|
| 30 |
+ |
|
| 17 | 31 |
func TestInit(t *testing.T) {
|
| 18 | 32 |
home := mkTestDirectory(t) |
| 19 | 33 |
defer os.RemoveAll(home) |
| ... | ... |
@@ -22,11 +45,11 @@ func TestInit(t *testing.T) {
|
| 22 | 22 |
t.Fatal(err) |
| 23 | 23 |
} |
| 24 | 24 |
defer func() {
|
| 25 |
- return |
|
| 26 | 25 |
if err := driver.Cleanup(); err != nil {
|
| 27 | 26 |
t.Fatal(err) |
| 28 | 27 |
} |
| 29 | 28 |
}() |
| 29 |
+ |
|
| 30 | 30 |
id := "foo" |
| 31 | 31 |
if err := driver.Create(id, ""); err != nil {
|
| 32 | 32 |
t.Fatal(err) |
| ... | ... |
@@ -41,3 +64,238 @@ func TestInit(t *testing.T) {
|
| 41 | 41 |
t.Fatalf("Get(%V) did not return a directory", id)
|
| 42 | 42 |
} |
| 43 | 43 |
} |
| 44 |
+ |
|
| 45 |
+func TestDriverName(t *testing.T) {
|
|
| 46 |
+ d := newDriver(t) |
|
| 47 |
+ defer cleanup(d) |
|
| 48 |
+ |
|
| 49 |
+ if d.String() != "devicemapper" {
|
|
| 50 |
+ t.Fatalf("Expected driver name to be devicemapper got %s", d.String())
|
|
| 51 |
+ } |
|
| 52 |
+} |
|
| 53 |
+ |
|
| 54 |
+func TestDriverCreate(t *testing.T) {
|
|
| 55 |
+ d := newDriver(t) |
|
| 56 |
+ defer cleanup(d) |
|
| 57 |
+ |
|
| 58 |
+ if err := d.Create("1", ""); err != nil {
|
|
| 59 |
+ t.Fatal(err) |
|
| 60 |
+ } |
|
| 61 |
+} |
|
| 62 |
+ |
|
| 63 |
+func TestDriverRemove(t *testing.T) {
|
|
| 64 |
+ d := newDriver(t) |
|
| 65 |
+ defer cleanup(d) |
|
| 66 |
+ |
|
| 67 |
+ if err := d.Create("1", ""); err != nil {
|
|
| 68 |
+ t.Fatal(err) |
|
| 69 |
+ } |
|
| 70 |
+ |
|
| 71 |
+ if err := d.Remove("1"); err != nil {
|
|
| 72 |
+ t.Fatal(err) |
|
| 73 |
+ } |
|
| 74 |
+} |
|
| 75 |
+ |
|
| 76 |
+func TestCleanup(t *testing.T) {
|
|
| 77 |
+ d := newDriver(t) |
|
| 78 |
+ defer os.RemoveAll(d.home) |
|
| 79 |
+ |
|
| 80 |
+ mountPoints := make([]string, 2) |
|
| 81 |
+ |
|
| 82 |
+ if err := d.Create("1", ""); err != nil {
|
|
| 83 |
+ t.Fatal(err) |
|
| 84 |
+ } |
|
| 85 |
+ // Mount the id |
|
| 86 |
+ p, err := d.Get("1")
|
|
| 87 |
+ if err != nil {
|
|
| 88 |
+ t.Fatal(err) |
|
| 89 |
+ } |
|
| 90 |
+ mountPoints[0] = p |
|
| 91 |
+ |
|
| 92 |
+ if err := d.Create("2", "1"); err != nil {
|
|
| 93 |
+ t.Fatal(err) |
|
| 94 |
+ } |
|
| 95 |
+ |
|
| 96 |
+ p, err = d.Get("2")
|
|
| 97 |
+ if err != nil {
|
|
| 98 |
+ t.Fatal(err) |
|
| 99 |
+ } |
|
| 100 |
+ mountPoints[1] = p |
|
| 101 |
+ |
|
| 102 |
+ // Ensure that all the mount points are currently mounted |
|
| 103 |
+ for _, p := range mountPoints {
|
|
| 104 |
+ if mounted, err := Mounted(p); err != nil {
|
|
| 105 |
+ t.Fatal(err) |
|
| 106 |
+ } else if !mounted {
|
|
| 107 |
+ t.Fatalf("Expected %s to be mounted", p)
|
|
| 108 |
+ } |
|
| 109 |
+ } |
|
| 110 |
+ |
|
| 111 |
+ // Ensure that devices are active |
|
| 112 |
+ for _, p := range []string{"1", "2"} {
|
|
| 113 |
+ if !d.HasActivatedDevice(p) {
|
|
| 114 |
+ t.Fatalf("Expected %s to have an active device", p)
|
|
| 115 |
+ } |
|
| 116 |
+ } |
|
| 117 |
+ |
|
| 118 |
+ if err := d.Cleanup(); err != nil {
|
|
| 119 |
+ t.Fatal(err) |
|
| 120 |
+ } |
|
| 121 |
+ |
|
| 122 |
+ // Ensure that all the mount points are no longer mounted |
|
| 123 |
+ for _, p := range mountPoints {
|
|
| 124 |
+ if mounted, err := Mounted(p); err != nil {
|
|
| 125 |
+ t.Fatal(err) |
|
| 126 |
+ } else if mounted {
|
|
| 127 |
+ t.Fatalf("Expected %s to not be mounted", p)
|
|
| 128 |
+ } |
|
| 129 |
+ } |
|
| 130 |
+ |
|
| 131 |
+ // Ensure that devices are no longer activated |
|
| 132 |
+ for _, p := range []string{"1", "2"} {
|
|
| 133 |
+ if d.HasActivatedDevice(p) {
|
|
| 134 |
+ t.Fatalf("Expected %s not be an active device", p)
|
|
| 135 |
+ } |
|
| 136 |
+ } |
|
| 137 |
+} |
|
| 138 |
+ |
|
| 139 |
+func TestNotMounted(t *testing.T) {
|
|
| 140 |
+ d := newDriver(t) |
|
| 141 |
+ defer cleanup(d) |
|
| 142 |
+ |
|
| 143 |
+ if err := d.Create("1", ""); err != nil {
|
|
| 144 |
+ t.Fatal(err) |
|
| 145 |
+ } |
|
| 146 |
+ |
|
| 147 |
+ mounted, err := Mounted(path.Join(d.home, "mnt", "1")) |
|
| 148 |
+ if err != nil {
|
|
| 149 |
+ t.Fatal(err) |
|
| 150 |
+ } |
|
| 151 |
+ if mounted {
|
|
| 152 |
+ t.Fatal("Id 1 should not be mounted")
|
|
| 153 |
+ } |
|
| 154 |
+} |
|
| 155 |
+ |
|
| 156 |
+func TestMounted(t *testing.T) {
|
|
| 157 |
+ d := newDriver(t) |
|
| 158 |
+ defer cleanup(d) |
|
| 159 |
+ |
|
| 160 |
+ if err := d.Create("1", ""); err != nil {
|
|
| 161 |
+ t.Fatal(err) |
|
| 162 |
+ } |
|
| 163 |
+ if _, err := d.Get("1"); err != nil {
|
|
| 164 |
+ t.Fatal(err) |
|
| 165 |
+ } |
|
| 166 |
+ |
|
| 167 |
+ mounted, err := Mounted(path.Join(d.home, "mnt", "1")) |
|
| 168 |
+ if err != nil {
|
|
| 169 |
+ t.Fatal(err) |
|
| 170 |
+ } |
|
| 171 |
+ if !mounted {
|
|
| 172 |
+ t.Fatal("Id 1 should be mounted")
|
|
| 173 |
+ } |
|
| 174 |
+} |
|
| 175 |
+ |
|
| 176 |
+func TestInitCleanedDriver(t *testing.T) {
|
|
| 177 |
+ d := newDriver(t) |
|
| 178 |
+ |
|
| 179 |
+ if err := d.Create("1", ""); err != nil {
|
|
| 180 |
+ t.Fatal(err) |
|
| 181 |
+ } |
|
| 182 |
+ if _, err := d.Get("1"); err != nil {
|
|
| 183 |
+ t.Fatal(err) |
|
| 184 |
+ } |
|
| 185 |
+ |
|
| 186 |
+ if err := d.Cleanup(); err != nil {
|
|
| 187 |
+ t.Fatal(err) |
|
| 188 |
+ } |
|
| 189 |
+ |
|
| 190 |
+ driver, err := Init(d.home) |
|
| 191 |
+ if err != nil {
|
|
| 192 |
+ t.Fatal(err) |
|
| 193 |
+ } |
|
| 194 |
+ d = driver.(*Driver) |
|
| 195 |
+ defer cleanup(d) |
|
| 196 |
+ |
|
| 197 |
+ if _, err := d.Get("1"); err != nil {
|
|
| 198 |
+ t.Fatal(err) |
|
| 199 |
+ } |
|
| 200 |
+} |
|
| 201 |
+ |
|
| 202 |
+func TestMountMountedDriver(t *testing.T) {
|
|
| 203 |
+ d := newDriver(t) |
|
| 204 |
+ defer cleanup(d) |
|
| 205 |
+ |
|
| 206 |
+ if err := d.Create("1", ""); err != nil {
|
|
| 207 |
+ t.Fatal(err) |
|
| 208 |
+ } |
|
| 209 |
+ |
|
| 210 |
+ // Perform get on same id to ensure that it will |
|
| 211 |
+ // not be mounted twice |
|
| 212 |
+ if _, err := d.Get("1"); err != nil {
|
|
| 213 |
+ t.Fatal(err) |
|
| 214 |
+ } |
|
| 215 |
+ if _, err := d.Get("1"); err != nil {
|
|
| 216 |
+ t.Fatal(err) |
|
| 217 |
+ } |
|
| 218 |
+} |
|
| 219 |
+ |
|
| 220 |
+func TestGetReturnsValidDevice(t *testing.T) {
|
|
| 221 |
+ d := newDriver(t) |
|
| 222 |
+ defer cleanup(d) |
|
| 223 |
+ |
|
| 224 |
+ if err := d.Create("1", ""); err != nil {
|
|
| 225 |
+ t.Fatal(err) |
|
| 226 |
+ } |
|
| 227 |
+ |
|
| 228 |
+ if !d.HasDevice("1") {
|
|
| 229 |
+ t.Fatalf("Expected id 1 to be in device set")
|
|
| 230 |
+ } |
|
| 231 |
+ |
|
| 232 |
+ if _, err := d.Get("1"); err != nil {
|
|
| 233 |
+ t.Fatal(err) |
|
| 234 |
+ } |
|
| 235 |
+ |
|
| 236 |
+ if !d.HasActivatedDevice("1") {
|
|
| 237 |
+ t.Fatalf("Expected id 1 to be activated")
|
|
| 238 |
+ } |
|
| 239 |
+ |
|
| 240 |
+ if !d.HasInitializedDevice("1") {
|
|
| 241 |
+ t.Fatalf("Expected id 1 to be initialized")
|
|
| 242 |
+ } |
|
| 243 |
+} |
|
| 244 |
+ |
|
| 245 |
+func TestDriverGetSize(t *testing.T) {
|
|
| 246 |
+ t.Skipf("Size is currently not implemented")
|
|
| 247 |
+ |
|
| 248 |
+ d := newDriver(t) |
|
| 249 |
+ defer cleanup(d) |
|
| 250 |
+ |
|
| 251 |
+ if err := d.Create("1", ""); err != nil {
|
|
| 252 |
+ t.Fatal(err) |
|
| 253 |
+ } |
|
| 254 |
+ |
|
| 255 |
+ mountPoint, err := d.Get("1")
|
|
| 256 |
+ if err != nil {
|
|
| 257 |
+ t.Fatal(err) |
|
| 258 |
+ } |
|
| 259 |
+ |
|
| 260 |
+ size := int64(1024) |
|
| 261 |
+ |
|
| 262 |
+ f, err := os.Create(path.Join(mountPoint, "test_file")) |
|
| 263 |
+ if err != nil {
|
|
| 264 |
+ t.Fatal(err) |
|
| 265 |
+ } |
|
| 266 |
+ if err := f.Truncate(size); err != nil {
|
|
| 267 |
+ t.Fatal(err) |
|
| 268 |
+ } |
|
| 269 |
+ f.Close() |
|
| 270 |
+ |
|
| 271 |
+ diffSize, err := d.Size("1")
|
|
| 272 |
+ if err != nil {
|
|
| 273 |
+ t.Fatal(err) |
|
| 274 |
+ } |
|
| 275 |
+ if diffSize != size {
|
|
| 276 |
+ t.Fatalf("Expected size %d got %d", size, diffSize)
|
|
| 277 |
+ } |
|
| 278 |
+} |