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