fsutils_linux_test.go 2.1 KB

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