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