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.

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