github.com/demonoid81/containerd@v1.3.4/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/containerd/containerd/pkg/testutil"
    27  	"github.com/docker/go-units"
    28  	"gotest.tools/assert"
    29  	is "gotest.tools/assert/cmp"
    30  )
    31  
    32  func TestLosetup(t *testing.T) {
    33  	testutil.RequiresRoot(t)
    34  
    35  	var (
    36  		imagePath   = createSparseImage(t)
    37  		loopDevice1 string
    38  		loopDevice2 string
    39  	)
    40  
    41  	defer func() {
    42  		err := os.Remove(imagePath)
    43  		assert.NilError(t, err)
    44  	}()
    45  
    46  	t.Run("AttachLoopDevice", func(t *testing.T) {
    47  		dev1, err := AttachLoopDevice(imagePath)
    48  		assert.NilError(t, err)
    49  		assert.Assert(t, dev1 != "")
    50  
    51  		dev2, err := AttachLoopDevice(imagePath)
    52  		assert.NilError(t, err)
    53  		assert.Assert(t, dev2 != dev1, "should attach different loop device")
    54  
    55  		loopDevice1 = dev1
    56  		loopDevice2 = dev2
    57  	})
    58  
    59  	t.Run("AttachEmptyLoopDevice", func(t *testing.T) {
    60  		_, err := AttachLoopDevice("")
    61  		assert.Assert(t, err != nil, "shouldn't attach empty path")
    62  	})
    63  
    64  	t.Run("FindAssociatedLoopDevices", func(t *testing.T) {
    65  		devices, err := FindAssociatedLoopDevices(imagePath)
    66  		assert.NilError(t, err)
    67  		assert.Assert(t, is.Len(devices, 2), "unexpected number of attached devices")
    68  		assert.Assert(t, is.Contains(devices, loopDevice1))
    69  		assert.Assert(t, is.Contains(devices, loopDevice2))
    70  	})
    71  
    72  	t.Run("FindAssociatedLoopDevicesForInvalidImage", func(t *testing.T) {
    73  		devices, err := FindAssociatedLoopDevices("")
    74  		assert.NilError(t, err)
    75  		assert.Assert(t, is.Len(devices, 0))
    76  	})
    77  
    78  	t.Run("DetachLoopDevice", func(t *testing.T) {
    79  		err := DetachLoopDevice(loopDevice2)
    80  		assert.NilError(t, err, "failed to detach %q", loopDevice2)
    81  	})
    82  
    83  	t.Run("DetachEmptyDevice", func(t *testing.T) {
    84  		err := DetachLoopDevice("")
    85  		assert.Assert(t, err != nil, "shouldn't detach empty path")
    86  	})
    87  
    88  	t.Run("RemoveLoopDevicesAssociatedWithImage", func(t *testing.T) {
    89  		err := RemoveLoopDevicesAssociatedWithImage(imagePath)
    90  		assert.NilError(t, err)
    91  
    92  		devices, err := FindAssociatedLoopDevices(imagePath)
    93  		assert.NilError(t, err)
    94  		assert.Assert(t, is.Len(devices, 0))
    95  	})
    96  
    97  	t.Run("RemoveLoopDevicesAssociatedWithInvalidImage", func(t *testing.T) {
    98  		err := RemoveLoopDevicesAssociatedWithImage("")
    99  		assert.NilError(t, err)
   100  	})
   101  }
   102  
   103  func createSparseImage(t *testing.T) string {
   104  	file, err := ioutil.TempFile("", "losetup-tests-")
   105  	assert.NilError(t, err)
   106  
   107  	size, err := units.RAMInBytes("16Mb")
   108  	assert.NilError(t, err)
   109  
   110  	err = file.Truncate(size)
   111  	assert.NilError(t, err)
   112  
   113  	err = file.Close()
   114  	assert.NilError(t, err)
   115  
   116  	return file.Name()
   117  }