gitee.com/bomy/docker.git@v1.13.1/pkg/fsutils/fsutils_linux_test.go (about)

     1  // +build linux
     2  
     3  package fsutils
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"syscall"
    10  	"testing"
    11  )
    12  
    13  func testSupportsDType(t *testing.T, expected bool, mkfsCommand string, mkfsArg ...string) {
    14  	// check whether mkfs is installed
    15  	if _, err := exec.LookPath(mkfsCommand); err != nil {
    16  		t.Skipf("%s not installed: %v", mkfsCommand, err)
    17  	}
    18  
    19  	// create a sparse image
    20  	imageSize := int64(32 * 1024 * 1024)
    21  	imageFile, err := ioutil.TempFile("", "fsutils-image")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	imageFileName := imageFile.Name()
    26  	defer os.Remove(imageFileName)
    27  	if _, err = imageFile.Seek(imageSize-1, 0); err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	if _, err = imageFile.Write([]byte{0}); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	if err = imageFile.Close(); err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	// create a mountpoint
    38  	mountpoint, err := ioutil.TempDir("", "fsutils-mountpoint")
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	defer os.RemoveAll(mountpoint)
    43  
    44  	// format the image
    45  	args := append(mkfsArg, imageFileName)
    46  	t.Logf("Executing `%s %v`", mkfsCommand, args)
    47  	out, err := exec.Command(mkfsCommand, args...).CombinedOutput()
    48  	if len(out) > 0 {
    49  		t.Log(string(out))
    50  	}
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	// loopback-mount the image.
    56  	// for ease of setting up loopback device, we use os/exec rather than syscall.Mount
    57  	out, err = exec.Command("mount", "-o", "loop", imageFileName, mountpoint).CombinedOutput()
    58  	if len(out) > 0 {
    59  		t.Log(string(out))
    60  	}
    61  	if err != nil {
    62  		t.Skip("skipping the test because mount failed")
    63  	}
    64  	defer func() {
    65  		if err := syscall.Unmount(mountpoint, 0); err != nil {
    66  			t.Fatal(err)
    67  		}
    68  	}()
    69  
    70  	// check whether it supports d_type
    71  	result, err := SupportsDType(mountpoint)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	t.Logf("Supports d_type: %v", result)
    76  	if result != expected {
    77  		t.Fatalf("expected %v, got %v", expected, result)
    78  	}
    79  }
    80  
    81  func TestSupportsDTypeWithFType0XFS(t *testing.T) {
    82  	testSupportsDType(t, false, "mkfs.xfs", "-m", "crc=0", "-n", "ftype=0")
    83  }
    84  
    85  func TestSupportsDTypeWithFType1XFS(t *testing.T) {
    86  	testSupportsDType(t, true, "mkfs.xfs", "-m", "crc=0", "-n", "ftype=1")
    87  }
    88  
    89  func TestSupportsDTypeWithExt4(t *testing.T) {
    90  	testSupportsDType(t, true, "mkfs.ext4")
    91  }