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.

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