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.

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