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.

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