fsutils_linux_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //go:build linux
  2. // +build linux
  3. package fsutils // import "github.com/docker/docker/pkg/fsutils"
  4. import (
  5. "os"
  6. "os/exec"
  7. "testing"
  8. "golang.org/x/sys/unix"
  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 := os.CreateTemp("", "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 := os.MkdirTemp("", "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 unix.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 := unix.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. }