You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package autofile
  2. import (
  3. "context"
  4. "os"
  5. "path/filepath"
  6. "syscall"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestSIGHUP(t *testing.T) {
  13. ctx, cancel := context.WithCancel(context.Background())
  14. defer cancel()
  15. origDir, err := os.Getwd()
  16. require.NoError(t, err)
  17. t.Cleanup(func() {
  18. if err := os.Chdir(origDir); err != nil {
  19. t.Error(err)
  20. }
  21. })
  22. // First, create a temporary directory and move into it
  23. dir, err := os.MkdirTemp("", "sighup_test")
  24. require.NoError(t, err)
  25. t.Cleanup(func() {
  26. _ = os.RemoveAll(dir)
  27. })
  28. require.NoError(t, os.Chdir(dir))
  29. // Create an AutoFile in the temporary directory
  30. name := "sighup_test"
  31. af, err := OpenAutoFile(ctx, name)
  32. require.NoError(t, err)
  33. require.True(t, filepath.IsAbs(af.Path))
  34. // Write to the file.
  35. _, err = af.Write([]byte("Line 1\n"))
  36. require.NoError(t, err)
  37. _, err = af.Write([]byte("Line 2\n"))
  38. require.NoError(t, err)
  39. // Move the file over
  40. require.NoError(t, os.Rename(name, name+"_old"))
  41. // Move into a different temporary directory
  42. otherDir, err := os.MkdirTemp("", "sighup_test_other")
  43. require.NoError(t, err)
  44. t.Cleanup(func() { os.RemoveAll(otherDir) })
  45. require.NoError(t, os.Chdir(otherDir))
  46. // Send SIGHUP to self.
  47. require.NoError(t, syscall.Kill(syscall.Getpid(), syscall.SIGHUP))
  48. // Wait a bit... signals are not handled synchronously.
  49. time.Sleep(time.Millisecond * 10)
  50. // Write more to the file.
  51. _, err = af.Write([]byte("Line 3\n"))
  52. require.NoError(t, err)
  53. _, err = af.Write([]byte("Line 4\n"))
  54. require.NoError(t, err)
  55. require.NoError(t, af.Close())
  56. // Both files should exist
  57. if body := mustReadFile(t, filepath.Join(dir, name+"_old")); string(body) != "Line 1\nLine 2\n" {
  58. t.Errorf("unexpected body %s", body)
  59. }
  60. if body := mustReadFile(t, filepath.Join(dir, name)); string(body) != "Line 3\nLine 4\n" {
  61. t.Errorf("unexpected body %s", body)
  62. }
  63. // The current directory should be empty
  64. files, err := os.ReadDir(".")
  65. require.NoError(t, err)
  66. assert.Empty(t, files)
  67. }
  68. // // Manually modify file permissions, close, and reopen using autofile:
  69. // // We expect the file permissions to be changed back to the intended perms.
  70. // func TestOpenAutoFilePerms(t *testing.T) {
  71. // file, err := os.CreateTemp("", "permission_test")
  72. // require.NoError(t, err)
  73. // err = file.Close()
  74. // require.NoError(t, err)
  75. // name := file.Name()
  76. // // open and change permissions
  77. // af, err := OpenAutoFile(name)
  78. // require.NoError(t, err)
  79. // err = af.file.Chmod(0755)
  80. // require.NoError(t, err)
  81. // err = af.Close()
  82. // require.NoError(t, err)
  83. // // reopen and expect an ErrPermissionsChanged as Cause
  84. // af, err = OpenAutoFile(name)
  85. // require.Error(t, err)
  86. // if e, ok := err.(*errors.ErrPermissionsChanged); ok {
  87. // t.Logf("%v", e)
  88. // } else {
  89. // t.Errorf("unexpected error %v", e)
  90. // }
  91. // }
  92. func TestAutoFileSize(t *testing.T) {
  93. ctx, cancel := context.WithCancel(context.Background())
  94. defer cancel()
  95. // First, create an AutoFile writing to a tempfile dir
  96. f, err := os.CreateTemp("", "sighup_test")
  97. require.NoError(t, err)
  98. require.NoError(t, f.Close())
  99. // Here is the actual AutoFile.
  100. af, err := OpenAutoFile(ctx, f.Name())
  101. require.NoError(t, err)
  102. // 1. Empty file
  103. size, err := af.Size()
  104. require.Zero(t, size)
  105. require.NoError(t, err)
  106. // 2. Not empty file
  107. data := []byte("Maniac\n")
  108. _, err = af.Write(data)
  109. require.NoError(t, err)
  110. size, err = af.Size()
  111. require.EqualValues(t, len(data), size)
  112. require.NoError(t, err)
  113. // 3. Not existing file
  114. require.NoError(t, af.closeFile())
  115. require.NoError(t, os.Remove(f.Name()))
  116. size, err = af.Size()
  117. require.EqualValues(t, 0, size, "Expected a new file to be empty")
  118. require.NoError(t, err)
  119. // Cleanup
  120. t.Cleanup(func() { os.Remove(f.Name()) })
  121. }
  122. func mustReadFile(t *testing.T, filePath string) []byte {
  123. fileBytes, err := os.ReadFile(filePath)
  124. require.NoError(t, err)
  125. return fileBytes
  126. }