github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/snapshots/devmapper/losetup/losetup_test.go (about) 1 // +build linux 2 3 /* 4 Copyright The containerd Authors. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 19 package losetup 20 21 import ( 22 "io/ioutil" 23 "os" 24 "testing" 25 26 "github.com/docker/go-units" 27 "golang.org/x/sys/unix" 28 "gotest.tools/v3/assert" 29 is "gotest.tools/v3/assert/cmp" 30 31 "github.com/containerd/containerd/pkg/testutil" 32 ) 33 34 func TestLosetup(t *testing.T) { 35 testutil.RequiresRoot(t) 36 37 var ( 38 imagePath = createSparseImage(t) 39 loopDevice1 string 40 loopDevice2 string 41 ) 42 43 defer func() { 44 err := os.Remove(imagePath) 45 assert.NilError(t, err) 46 }() 47 48 t.Run("AttachLoopDevice", func(t *testing.T) { 49 dev1, err := AttachLoopDevice(imagePath) 50 assert.NilError(t, err) 51 assert.Assert(t, dev1 != "") 52 53 dev2, err := AttachLoopDevice(imagePath) 54 assert.NilError(t, err) 55 assert.Assert(t, dev2 != dev1, "should attach different loop device") 56 57 loopDevice1 = dev1 58 loopDevice2 = dev2 59 }) 60 61 t.Run("AttachEmptyLoopDevice", func(t *testing.T) { 62 _, err := AttachLoopDevice("") 63 assert.Assert(t, err != nil, "shouldn't attach empty path") 64 }) 65 66 t.Run("FindAssociatedLoopDevices", func(t *testing.T) { 67 devices, err := FindAssociatedLoopDevices(imagePath) 68 assert.NilError(t, err) 69 assert.Assert(t, is.Len(devices, 2), "unexpected number of attached devices") 70 assert.Assert(t, is.Contains(devices, loopDevice1)) 71 assert.Assert(t, is.Contains(devices, loopDevice2)) 72 }) 73 74 t.Run("FindAssociatedLoopDevicesForInvalidImage", func(t *testing.T) { 75 devices, err := FindAssociatedLoopDevices("") 76 assert.NilError(t, err) 77 assert.Assert(t, is.Len(devices, 0)) 78 }) 79 80 t.Run("DetachLoopDevice", func(t *testing.T) { 81 err := DetachLoopDevice(loopDevice2) 82 assert.NilError(t, err, "failed to detach %q", loopDevice2) 83 }) 84 85 t.Run("DetachEmptyDevice", func(t *testing.T) { 86 err := DetachLoopDevice("") 87 assert.Assert(t, err != nil, "shouldn't detach empty path") 88 }) 89 90 t.Run("RemoveLoopDevicesAssociatedWithImage", func(t *testing.T) { 91 err := RemoveLoopDevicesAssociatedWithImage(imagePath) 92 assert.NilError(t, err) 93 94 devices, err := FindAssociatedLoopDevices(imagePath) 95 assert.NilError(t, err) 96 assert.Assert(t, is.Len(devices, 0)) 97 }) 98 99 t.Run("RemoveLoopDevicesAssociatedWithInvalidImage", func(t *testing.T) { 100 err := RemoveLoopDevicesAssociatedWithImage("") 101 assert.NilError(t, err) 102 }) 103 104 t.Run("DetachInvalidDevice", func(t *testing.T) { 105 err := DetachLoopDevice("/dev/loop_invalid_idx") 106 assert.Equal(t, unix.ENOENT, err) 107 }) 108 } 109 110 func createSparseImage(t *testing.T) string { 111 file, err := ioutil.TempFile("", "losetup-tests-") 112 assert.NilError(t, err) 113 114 size, err := units.RAMInBytes("16Mb") 115 assert.NilError(t, err) 116 117 err = file.Truncate(size) 118 assert.NilError(t, err) 119 120 err = file.Close() 121 assert.NilError(t, err) 122 123 return file.Name() 124 }