1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
//go:build linux
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package devmapper
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/pkg/testutil"
"github.com/containerd/containerd/v2/plugins/snapshots/devmapper/dmsetup"
"github.com/containerd/log"
"github.com/docker/go-units"
"github.com/stretchr/testify/assert"
)
const (
thinDevice1 = "thin-1"
thinDevice2 = "thin-2"
snapDevice1 = "snap-1"
device1Size = 1000000
device2Size = 2000000
testsPrefix = "devmapper-snapshotter-tests-"
)
// TestPoolDevice runs integration tests for pool device.
// The following scenario implemented:
// - Create pool device with name 'test-pool-device'
// - Create two thin volumes 'thin-1' and 'thin-2'
// - Write ext4 file system on 'thin-1' and make sure it'errs moutable
// - Write v1 test file on 'thin-1' volume
// - Take 'thin-1' snapshot 'snap-1'
// - Change v1 file to v2 on 'thin-1'
// - Mount 'snap-1' and make sure test file is v1
// - Unmount volumes and remove all devices
func TestPoolDevice(t *testing.T) {
testutil.RequiresRoot(t)
assert.NoError(t, log.SetLevel("debug"))
ctx := context.Background()
tempDir := t.TempDir()
_, loopDataDevice := createLoopbackDevice(t, tempDir)
_, loopMetaDevice := createLoopbackDevice(t, tempDir)
poolName := fmt.Sprintf("test-pool-device-%d", time.Now().Nanosecond())
err := dmsetup.CreatePool(poolName, loopDataDevice, loopMetaDevice, 64*1024/dmsetup.SectorSize)
assert.Nil(t, err, "failed to create pool %q", poolName)
defer func() {
// Detach loop devices and remove images
err := mount.DetachLoopDevice(loopDataDevice, loopMetaDevice)
assert.NoError(t, err)
}()
config := &Config{
PoolName: poolName,
RootPath: tempDir,
BaseImageSize: "16mb",
BaseImageSizeBytes: 16 * 1024 * 1024,
DiscardBlocks: true,
}
pool, err := NewPoolDevice(ctx, config)
assert.Nil(t, err, "can't create device pool")
assert.True(t, pool != nil)
defer func() {
err := pool.RemovePool(ctx)
assert.Nil(t, err, "can't close device pool")
}()
// Create thin devices
t.Run("CreateThinDevice", func(t *testing.T) {
testCreateThinDevice(t, pool)
})
// Make ext4 filesystem on 'thin-1'
t.Run("MakeFileSystem", func(t *testing.T) {
testMakeFileSystem(t, pool)
})
// Mount 'thin-1' and write v1 test file on 'thin-1' device
err = mount.WithTempMount(ctx, getMounts(thinDevice1), func(thin1MountPath string) error {
// Write v1 test file on 'thin-1' device
thin1TestFilePath := filepath.Join(thin1MountPath, "TEST")
err := os.WriteFile(thin1TestFilePath, []byte("test file (v1)"), 0700)
assert.Nil(t, err, "failed to write test file v1 on '%s' volume", thinDevice1)
return nil
})
// Take snapshot of 'thin-1'
t.Run("CreateSnapshotDevice", func(t *testing.T) {
testCreateSnapshot(t, pool)
})
// Update TEST file on 'thin-1' to v2
err = mount.WithTempMount(ctx, getMounts(thinDevice1), func(thin1MountPath string) error {
thin1TestFilePath := filepath.Join(thin1MountPath, "TEST")
err = os.WriteFile(thin1TestFilePath, []byte("test file (v2)"), 0700)
assert.Nil(t, err, "failed to write test file v2 on 'thin-1' volume after taking snapshot")
return nil
})
assert.NoError(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
fileData, err := os.ReadFile(filepath.Join(snap1MountPath, "TEST"))
assert.Nil(t, err, "couldn't read test file from '%s' device", snapDevice1)
assert.Equal(t, "test file (v1)", string(fileData), "test file content is invalid on snapshot")
return nil
})
assert.NoError(t, err)
t.Run("DeactivateDevice", func(t *testing.T) {
testDeactivateThinDevice(t, pool)
})
t.Run("RemoveDevice", func(t *testing.T) {
testRemoveThinDevice(t, pool)
})
t.Run("rollbackActivate", func(t *testing.T) {
testCreateThinDevice(t, pool)
ctx := context.Background()
snapDevice := "snap2"
err := pool.CreateSnapshotDevice(ctx, thinDevice1, snapDevice, device1Size)
assert.NoError(t, err)
info, err := pool.metadata.GetDevice(ctx, snapDevice)
assert.NoError(t, err)
// Simulate a case that the device cannot be activated.
err = pool.DeactivateDevice(ctx, info.Name, false, false)
assert.NoError(t, err)
err = pool.rollbackActivate(ctx, info, err)
assert.NoError(t, err)
})
}
func TestPoolDeviceMarkFaulty(t *testing.T) {
store := createStore(t)
defer cleanupStore(t, store)
err := store.AddDevice(testCtx, &DeviceInfo{Name: "1", State: Unknown})
assert.NoError(t, err)
// Note: do not use 'Activated' here because pool.ensureDeviceStates() will
// try to activate the real dm device, which will fail on a faked device.
err = store.AddDevice(testCtx, &DeviceInfo{Name: "2", State: Deactivated})
assert.NoError(t, err)
pool := &PoolDevice{metadata: store}
err = pool.ensureDeviceStates(testCtx)
assert.NoError(t, err)
called := 0
err = pool.metadata.WalkDevices(testCtx, func(info *DeviceInfo) error {
called++
switch called {
case 1:
assert.Equal(t, Faulty, info.State)
assert.Equal(t, "1", info.Name)
case 2:
assert.Equal(t, Deactivated, info.State)
assert.Equal(t, "2", info.Name)
default:
t.Error("unexpected walk call")
}
return nil
})
assert.NoError(t, err)
assert.Equal(t, 2, called)
}
func testCreateThinDevice(t *testing.T, pool *PoolDevice) {
ctx := context.Background()
err := pool.CreateThinDevice(ctx, thinDevice1, device1Size)
assert.Nil(t, err, "can't create first thin device")
err = pool.CreateThinDevice(ctx, thinDevice1, device1Size)
assert.True(t, err != nil, "device pool allows duplicated device names")
err = pool.CreateThinDevice(ctx, thinDevice2, device2Size)
assert.Nil(t, err, "can't create second thin device")
deviceInfo1, err := pool.metadata.GetDevice(ctx, thinDevice1)
assert.NoError(t, err)
deviceInfo2, err := pool.metadata.GetDevice(ctx, thinDevice2)
assert.NoError(t, err)
assert.True(t, deviceInfo1.DeviceID != deviceInfo2.DeviceID, "assigned device ids should be different")
usage, err := pool.GetUsage(thinDevice1)
assert.NoError(t, err)
assert.Equal(t, usage, int64(0))
}
func testMakeFileSystem(t *testing.T, pool *PoolDevice) {
devicePath := dmsetup.GetFullDevicePath(thinDevice1)
args := []string{
"-E",
"nodiscard,lazy_itable_init=0,lazy_journal_init=0",
devicePath,
}
output, err := exec.Command("mkfs.ext4", args...).CombinedOutput()
assert.Nil(t, err, "failed to make filesystem on '%s': %s", thinDevice1, string(output))
usage, err := pool.GetUsage(thinDevice1)
assert.NoError(t, err)
assert.True(t, usage > 0)
}
func testCreateSnapshot(t *testing.T, pool *PoolDevice) {
err := pool.CreateSnapshotDevice(context.Background(), thinDevice1, snapDevice1, device1Size)
assert.Nil(t, err, "failed to create snapshot from '%s' volume", thinDevice1)
}
func testDeactivateThinDevice(t *testing.T, pool *PoolDevice) {
deviceList := []string{
thinDevice2,
snapDevice1,
}
for _, deviceName := range deviceList {
assert.True(t, pool.IsActivated(deviceName))
err := pool.DeactivateDevice(context.Background(), deviceName, false, true)
assert.Nil(t, err, "failed to remove '%s'", deviceName)
assert.False(t, pool.IsActivated(deviceName))
}
}
func testRemoveThinDevice(t *testing.T, pool *PoolDevice) {
err := pool.RemoveDevice(testCtx, thinDevice1)
assert.Nil(t, err, "should delete thin device from pool")
err = pool.RemoveDevice(testCtx, thinDevice2)
assert.Nil(t, err, "should delete thin device from pool")
}
func getMounts(thinDeviceName string) []mount.Mount {
return []mount.Mount{
{
Source: dmsetup.GetFullDevicePath(thinDeviceName),
Type: "ext4",
},
}
}
func createLoopbackDevice(t *testing.T, dir string) (string, string) {
file, err := os.CreateTemp(dir, testsPrefix)
assert.NoError(t, err)
size, err := units.RAMInBytes("128Mb")
assert.NoError(t, err)
err = file.Truncate(size)
assert.NoError(t, err)
err = file.Close()
assert.NoError(t, err)
imagePath := file.Name()
loopDevice, err := mount.AttachLoopDevice(imagePath)
assert.NoError(t, err)
return imagePath, loopDevice
}
|