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.

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