linux_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // +build linux
  2. package lumberjack
  3. import (
  4. "os"
  5. "syscall"
  6. "testing"
  7. "time"
  8. )
  9. func TestMaintainMode(t *testing.T) {
  10. currentTime = fakeTime
  11. dir := makeTempDir("TestMaintainMode", t)
  12. defer os.RemoveAll(dir)
  13. filename := logFile(dir)
  14. mode := os.FileMode(0600)
  15. f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, mode)
  16. isNil(err, t)
  17. f.Close()
  18. l := &Logger{
  19. Filename: filename,
  20. MaxBackups: 1,
  21. MaxSize: 100, // megabytes
  22. }
  23. defer l.Close()
  24. b := []byte("boo!")
  25. n, err := l.Write(b)
  26. isNil(err, t)
  27. equals(len(b), n, t)
  28. newFakeTime()
  29. err = l.Rotate()
  30. isNil(err, t)
  31. filename2 := backupFile(dir)
  32. info, err := os.Stat(filename)
  33. isNil(err, t)
  34. info2, err := os.Stat(filename2)
  35. isNil(err, t)
  36. equals(mode, info.Mode(), t)
  37. equals(mode, info2.Mode(), t)
  38. }
  39. func TestMaintainOwner(t *testing.T) {
  40. fakeFS := newFakeFS()
  41. os_Chown = fakeFS.Chown
  42. os_Stat = fakeFS.Stat
  43. defer func() {
  44. os_Chown = os.Chown
  45. os_Stat = os.Stat
  46. }()
  47. currentTime = fakeTime
  48. dir := makeTempDir("TestMaintainOwner", t)
  49. defer os.RemoveAll(dir)
  50. filename := logFile(dir)
  51. f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0644)
  52. isNil(err, t)
  53. f.Close()
  54. l := &Logger{
  55. Filename: filename,
  56. MaxBackups: 1,
  57. MaxSize: 100, // megabytes
  58. }
  59. defer l.Close()
  60. b := []byte("boo!")
  61. n, err := l.Write(b)
  62. isNil(err, t)
  63. equals(len(b), n, t)
  64. newFakeTime()
  65. err = l.Rotate()
  66. isNil(err, t)
  67. equals(555, fakeFS.files[filename].uid, t)
  68. equals(666, fakeFS.files[filename].gid, t)
  69. }
  70. func TestCompressMaintainMode(t *testing.T) {
  71. currentTime = fakeTime
  72. dir := makeTempDir("TestCompressMaintainMode", t)
  73. defer os.RemoveAll(dir)
  74. filename := logFile(dir)
  75. mode := os.FileMode(0600)
  76. f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, mode)
  77. isNil(err, t)
  78. f.Close()
  79. l := &Logger{
  80. Compress: true,
  81. Filename: filename,
  82. MaxBackups: 1,
  83. MaxSize: 100, // megabytes
  84. }
  85. defer l.Close()
  86. b := []byte("boo!")
  87. n, err := l.Write(b)
  88. isNil(err, t)
  89. equals(len(b), n, t)
  90. newFakeTime()
  91. err = l.Rotate()
  92. isNil(err, t)
  93. // we need to wait a little bit since the files get compressed on a different
  94. // goroutine.
  95. <-time.After(10 * time.Millisecond)
  96. // a compressed version of the log file should now exist with the correct
  97. // mode.
  98. filename2 := backupFile(dir)
  99. info, err := os.Stat(filename)
  100. isNil(err, t)
  101. info2, err := os.Stat(filename2+compressSuffix)
  102. isNil(err, t)
  103. equals(mode, info.Mode(), t)
  104. equals(mode, info2.Mode(), t)
  105. }
  106. func TestCompressMaintainOwner(t *testing.T) {
  107. fakeFS := newFakeFS()
  108. os_Chown = fakeFS.Chown
  109. os_Stat = fakeFS.Stat
  110. defer func() {
  111. os_Chown = os.Chown
  112. os_Stat = os.Stat
  113. }()
  114. currentTime = fakeTime
  115. dir := makeTempDir("TestCompressMaintainOwner", t)
  116. defer os.RemoveAll(dir)
  117. filename := logFile(dir)
  118. f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0644)
  119. isNil(err, t)
  120. f.Close()
  121. l := &Logger{
  122. Compress: true,
  123. Filename: filename,
  124. MaxBackups: 1,
  125. MaxSize: 100, // megabytes
  126. }
  127. defer l.Close()
  128. b := []byte("boo!")
  129. n, err := l.Write(b)
  130. isNil(err, t)
  131. equals(len(b), n, t)
  132. newFakeTime()
  133. err = l.Rotate()
  134. isNil(err, t)
  135. // we need to wait a little bit since the files get compressed on a different
  136. // goroutine.
  137. <-time.After(10 * time.Millisecond)
  138. // a compressed version of the log file should now exist with the correct
  139. // owner.
  140. filename2 := backupFile(dir)
  141. equals(555, fakeFS.files[filename2+compressSuffix].uid, t)
  142. equals(666, fakeFS.files[filename2+compressSuffix].gid, t)
  143. }
  144. type fakeFile struct {
  145. uid int
  146. gid int
  147. }
  148. type fakeFS struct {
  149. files map[string]fakeFile
  150. }
  151. func newFakeFS() *fakeFS {
  152. return &fakeFS{files: make(map[string]fakeFile)}
  153. }
  154. func (fs *fakeFS) Chown(name string, uid, gid int) error {
  155. fs.files[name] = fakeFile{uid: uid, gid: gid}
  156. return nil
  157. }
  158. func (fs *fakeFS) Stat(name string) (os.FileInfo, error) {
  159. info, err := os.Stat(name)
  160. if err != nil {
  161. return nil, err
  162. }
  163. stat := info.Sys().(*syscall.Stat_t)
  164. stat.Uid = 555
  165. stat.Gid = 666
  166. return info, nil
  167. }