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.6 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. defer os.Chdir(origDir)
  17. // First, create a temporary directory and move into it
  18. dir, err := ioutil.TempDir("", "sighup_test")
  19. require.NoError(t, err)
  20. t.Cleanup(func() {
  21. os.RemoveAll(dir)
  22. })
  23. err = os.Chdir(dir)
  24. require.NoError(t, err)
  25. // Create an AutoFile in the temporary directory
  26. name := "sighup_test"
  27. af, err := OpenAutoFile(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. err = os.Rename(name, name+"_old")
  37. require.NoError(t, err)
  38. // Move into a different temporary directory
  39. otherDir, err := ioutil.TempDir("", "sighup_test_other")
  40. require.NoError(t, err)
  41. defer os.RemoveAll(otherDir)
  42. err = os.Chdir(otherDir)
  43. require.NoError(t, err)
  44. // Send SIGHUP to self.
  45. err = syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
  46. require.NoError(t, err)
  47. // Wait a bit... signals are not handled synchronously.
  48. time.Sleep(time.Millisecond * 10)
  49. // Write more to the file.
  50. _, err = af.Write([]byte("Line 3\n"))
  51. require.NoError(t, err)
  52. _, err = af.Write([]byte("Line 4\n"))
  53. require.NoError(t, err)
  54. err = af.Close()
  55. require.NoError(t, err)
  56. // Both files should exist
  57. if body := tmos.MustReadFile(filepath.Join(dir, name+"_old")); string(body) != "Line 1\nLine 2\n" {
  58. t.Errorf("unexpected body %s", body)
  59. }
  60. if body := tmos.MustReadFile(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 := ioutil.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 := ioutil.TempFile("", "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. // First, create an AutoFile writing to a tempfile dir
  94. f, err := ioutil.TempFile("", "sighup_test")
  95. require.NoError(t, err)
  96. err = f.Close()
  97. require.NoError(t, err)
  98. // Here is the actual AutoFile.
  99. af, err := OpenAutoFile(f.Name())
  100. require.NoError(t, err)
  101. // 1. Empty file
  102. size, err := af.Size()
  103. require.Zero(t, size)
  104. require.NoError(t, err)
  105. // 2. Not empty file
  106. data := []byte("Maniac\n")
  107. _, err = af.Write(data)
  108. require.NoError(t, err)
  109. size, err = af.Size()
  110. require.EqualValues(t, len(data), size)
  111. require.NoError(t, err)
  112. // 3. Not existing file
  113. err = af.Close()
  114. require.NoError(t, err)
  115. err = os.Remove(f.Name())
  116. require.NoError(t, err)
  117. size, err = af.Size()
  118. require.EqualValues(t, 0, size, "Expected a new file to be empty")
  119. require.NoError(t, err)
  120. // Cleanup
  121. _ = os.Remove(f.Name())
  122. }