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.

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