devmapper: proper cleanup in pool device test

Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
This commit is contained in:
Maksym Pavlenko 2019-02-22 11:26:14 -08:00
parent 734989c2a0
commit 208957ba3c
No known key found for this signature in database
GPG Key ID: BDA48CBFE7A0FC14

View File

@ -28,6 +28,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/pkg/testutil" "github.com/containerd/containerd/pkg/testutil"
"github.com/containerd/containerd/snapshots/devmapper/dmsetup" "github.com/containerd/containerd/snapshots/devmapper/dmsetup"
"github.com/containerd/containerd/snapshots/devmapper/losetup" "github.com/containerd/containerd/snapshots/devmapper/losetup"
@ -107,13 +108,10 @@ func TestPoolDevice(t *testing.T) {
}) })
// Mount 'thin-1' // Mount 'thin-1'
thin1MountPath := tempMountPath(t) err = mount.WithTempMount(ctx, getMounts(thinDevice1), func(thin1MountPath string) error {
output, err := exec.Command("mount", dmsetup.GetFullDevicePath(thinDevice1), thin1MountPath).CombinedOutput()
assert.NilError(t, err, "failed to mount '%s': %s", thinDevice1, string(output))
// Write v1 test file on 'thin-1' device // Write v1 test file on 'thin-1' device
thin1TestFilePath := filepath.Join(thin1MountPath, "TEST") thin1TestFilePath := filepath.Join(thin1MountPath, "TEST")
err = ioutil.WriteFile(thin1TestFilePath, []byte("test file (v1)"), 0700) err := ioutil.WriteFile(thin1TestFilePath, []byte("test file (v1)"), 0700)
assert.NilError(t, err, "failed to write test file v1 on '%s' volume", thinDevice1) assert.NilError(t, err, "failed to write test file v1 on '%s' volume", thinDevice1)
// Take snapshot of 'thin-1' // Take snapshot of 'thin-1'
@ -125,19 +123,22 @@ func TestPoolDevice(t *testing.T) {
err = ioutil.WriteFile(thin1TestFilePath, []byte("test file (v2)"), 0700) err = ioutil.WriteFile(thin1TestFilePath, []byte("test file (v2)"), 0700)
assert.NilError(t, err, "failed to write test file v2 on 'thin-1' volume after taking snapshot") assert.NilError(t, err, "failed to write test file v2 on 'thin-1' volume after taking snapshot")
// Mount 'snap-1' and make sure TEST file is v1 return nil
snap1MountPath := tempMountPath(t) })
output, err = exec.Command("mount", dmsetup.GetFullDevicePath(snapDevice1), snap1MountPath).CombinedOutput()
assert.NilError(t, err, "failed to mount '%s' device: %s", snapDevice1, string(output))
assert.NilError(t, err)
// Mount 'snap-1' and make sure TEST file is v1
err = mount.WithTempMount(ctx, getMounts(snapDevice1), func(snap1MountPath string) error {
// Read test file from snapshot device and make sure it's v1 // Read test file from snapshot device and make sure it's v1
fileData, err := ioutil.ReadFile(filepath.Join(snap1MountPath, "TEST")) fileData, err := ioutil.ReadFile(filepath.Join(snap1MountPath, "TEST"))
assert.NilError(t, err, "couldn't read test file from '%s' device", snapDevice1) assert.NilError(t, err, "couldn't read test file from '%s' device", snapDevice1)
assert.Assert(t, string(fileData) == "test file (v1)", "test file content is invalid on snapshot") assert.Equal(t, "test file (v1)", string(fileData), "test file content is invalid on snapshot")
// Unmount devices before removing return nil
output, err = exec.Command("umount", thin1MountPath, snap1MountPath).CombinedOutput() })
assert.NilError(t, err, "failed to unmount devices: %s", string(output))
assert.NilError(t, err)
t.Run("DeactivateDevice", func(t *testing.T) { t.Run("DeactivateDevice", func(t *testing.T) {
testDeactivateThinDevice(t, pool) testDeactivateThinDevice(t, pool)
@ -207,11 +208,13 @@ func testRemoveThinDevice(t *testing.T, pool *PoolDevice) {
assert.NilError(t, err, "should delete thin device from pool") assert.NilError(t, err, "should delete thin device from pool")
} }
func tempMountPath(t *testing.T) string { func getMounts(thinDeviceName string) []mount.Mount {
path, err := ioutil.TempDir("", "devmapper-snapshotter-mount-") return []mount.Mount{
assert.NilError(t, err, "failed to get temp directory for mount") {
Source: dmsetup.GetFullDevicePath(thinDeviceName),
return path Type: "ext4",
},
}
} }
func createLoopbackDevice(t *testing.T, dir string) (string, string) { func createLoopbackDevice(t *testing.T, dir string) (string, string) {