Skip to content

Commit 208957b

Browse files
committed
devmapper: proper cleanup in pool device test
Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent 734989c commit 208957b

1 file changed

Lines changed: 33 additions & 30 deletions

File tree

snapshots/devmapper/pool_device_test.go

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"testing"
2929
"time"
3030

31+
"github.com/containerd/containerd/mount"
3132
"github.com/containerd/containerd/pkg/testutil"
3233
"github.com/containerd/containerd/snapshots/devmapper/dmsetup"
3334
"github.com/containerd/containerd/snapshots/devmapper/losetup"
@@ -107,37 +108,37 @@ func TestPoolDevice(t *testing.T) {
107108
})
108109

109110
// Mount 'thin-1'
110-
thin1MountPath := tempMountPath(t)
111-
output, err := exec.Command("mount", dmsetup.GetFullDevicePath(thinDevice1), thin1MountPath).CombinedOutput()
112-
assert.NilError(t, err, "failed to mount '%s': %s", thinDevice1, string(output))
113-
114-
// Write v1 test file on 'thin-1' device
115-
thin1TestFilePath := filepath.Join(thin1MountPath, "TEST")
116-
err = ioutil.WriteFile(thin1TestFilePath, []byte("test file (v1)"), 0700)
117-
assert.NilError(t, err, "failed to write test file v1 on '%s' volume", thinDevice1)
118-
119-
// Take snapshot of 'thin-1'
120-
t.Run("CreateSnapshotDevice", func(t *testing.T) {
121-
testCreateSnapshot(t, pool)
111+
err = mount.WithTempMount(ctx, getMounts(thinDevice1), func(thin1MountPath string) error {
112+
// Write v1 test file on 'thin-1' device
113+
thin1TestFilePath := filepath.Join(thin1MountPath, "TEST")
114+
err := ioutil.WriteFile(thin1TestFilePath, []byte("test file (v1)"), 0700)
115+
assert.NilError(t, err, "failed to write test file v1 on '%s' volume", thinDevice1)
116+
117+
// Take snapshot of 'thin-1'
118+
t.Run("CreateSnapshotDevice", func(t *testing.T) {
119+
testCreateSnapshot(t, pool)
120+
})
121+
122+
// Update TEST file on 'thin-1' to v2
123+
err = ioutil.WriteFile(thin1TestFilePath, []byte("test file (v2)"), 0700)
124+
assert.NilError(t, err, "failed to write test file v2 on 'thin-1' volume after taking snapshot")
125+
126+
return nil
122127
})
123128

124-
// Update TEST file on 'thin-1' to v2
125-
err = ioutil.WriteFile(thin1TestFilePath, []byte("test file (v2)"), 0700)
126-
assert.NilError(t, err, "failed to write test file v2 on 'thin-1' volume after taking snapshot")
129+
assert.NilError(t, err)
127130

128131
// Mount 'snap-1' and make sure TEST file is v1
129-
snap1MountPath := tempMountPath(t)
130-
output, err = exec.Command("mount", dmsetup.GetFullDevicePath(snapDevice1), snap1MountPath).CombinedOutput()
131-
assert.NilError(t, err, "failed to mount '%s' device: %s", snapDevice1, string(output))
132+
err = mount.WithTempMount(ctx, getMounts(snapDevice1), func(snap1MountPath string) error {
133+
// Read test file from snapshot device and make sure it's v1
134+
fileData, err := ioutil.ReadFile(filepath.Join(snap1MountPath, "TEST"))
135+
assert.NilError(t, err, "couldn't read test file from '%s' device", snapDevice1)
136+
assert.Equal(t, "test file (v1)", string(fileData), "test file content is invalid on snapshot")
132137

133-
// Read test file from snapshot device and make sure it's v1
134-
fileData, err := ioutil.ReadFile(filepath.Join(snap1MountPath, "TEST"))
135-
assert.NilError(t, err, "couldn't read test file from '%s' device", snapDevice1)
136-
assert.Assert(t, string(fileData) == "test file (v1)", "test file content is invalid on snapshot")
138+
return nil
139+
})
137140

138-
// Unmount devices before removing
139-
output, err = exec.Command("umount", thin1MountPath, snap1MountPath).CombinedOutput()
140-
assert.NilError(t, err, "failed to unmount devices: %s", string(output))
141+
assert.NilError(t, err)
141142

142143
t.Run("DeactivateDevice", func(t *testing.T) {
143144
testDeactivateThinDevice(t, pool)
@@ -207,11 +208,13 @@ func testRemoveThinDevice(t *testing.T, pool *PoolDevice) {
207208
assert.NilError(t, err, "should delete thin device from pool")
208209
}
209210

210-
func tempMountPath(t *testing.T) string {
211-
path, err := ioutil.TempDir("", "devmapper-snapshotter-mount-")
212-
assert.NilError(t, err, "failed to get temp directory for mount")
213-
214-
return path
211+
func getMounts(thinDeviceName string) []mount.Mount {
212+
return []mount.Mount{
213+
{
214+
Source: dmsetup.GetFullDevicePath(thinDeviceName),
215+
Type: "ext4",
216+
},
217+
}
215218
}
216219

217220
func createLoopbackDevice(t *testing.T, dir string) (string, string) {

0 commit comments

Comments
 (0)