lumberjack_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. package lumberjack
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. "time"
  12. "github.com/BurntSushi/toml"
  13. "gopkg.in/yaml.v2"
  14. )
  15. // !!!NOTE!!!
  16. //
  17. // Running these tests in parallel will almost certainly cause sporadic (or even
  18. // regular) failures, because they're all messing with the same global variable
  19. // that controls the logic's mocked time.Now. So... don't do that.
  20. // Since all the tests uses the time to determine filenames etc, we need to
  21. // control the wall clock as much as possible, which means having a wall clock
  22. // that doesn't change unless we want it to.
  23. var fakeCurrentTime = time.Now()
  24. func fakeTime() time.Time {
  25. return fakeCurrentTime
  26. }
  27. func TestNewFile(t *testing.T) {
  28. currentTime = fakeTime
  29. dir := makeTempDir("TestNewFile", t)
  30. defer os.RemoveAll(dir)
  31. l := &Logger{
  32. Filename: logFile(dir),
  33. }
  34. defer l.Close()
  35. b := []byte("boo!")
  36. n, err := l.Write(b)
  37. isNil(err, t)
  38. equals(len(b), n, t)
  39. existsWithContent(logFile(dir), b, t)
  40. fileCount(dir, 1, t)
  41. }
  42. func TestOpenExisting(t *testing.T) {
  43. currentTime = fakeTime
  44. dir := makeTempDir("TestOpenExisting", t)
  45. defer os.RemoveAll(dir)
  46. filename := logFile(dir)
  47. data := []byte("foo!")
  48. err := ioutil.WriteFile(filename, data, 0644)
  49. isNil(err, t)
  50. existsWithContent(filename, data, t)
  51. l := &Logger{
  52. Filename: filename,
  53. }
  54. defer l.Close()
  55. b := []byte("boo!")
  56. n, err := l.Write(b)
  57. isNil(err, t)
  58. equals(len(b), n, t)
  59. // make sure the file got appended
  60. existsWithContent(filename, append(data, b...), t)
  61. // make sure no other files were created
  62. fileCount(dir, 1, t)
  63. }
  64. func TestWriteTooLong(t *testing.T) {
  65. currentTime = fakeTime
  66. megabyte = 1
  67. dir := makeTempDir("TestWriteTooLong", t)
  68. defer os.RemoveAll(dir)
  69. l := &Logger{
  70. Filename: logFile(dir),
  71. MaxSize: 5,
  72. }
  73. defer l.Close()
  74. b := []byte("booooooooooooooo!")
  75. n, err := l.Write(b)
  76. notNil(err, t)
  77. equals(0, n, t)
  78. equals(err.Error(),
  79. fmt.Sprintf("write length %d exceeds maximum file size %d", len(b), l.MaxSize), t)
  80. _, err = os.Stat(logFile(dir))
  81. assert(os.IsNotExist(err), t, "File exists, but should not have been created")
  82. }
  83. func TestMakeLogDir(t *testing.T) {
  84. currentTime = fakeTime
  85. dir := time.Now().Format("TestMakeLogDir" + backupTimeFormat)
  86. dir = filepath.Join(os.TempDir(), dir)
  87. defer os.RemoveAll(dir)
  88. filename := logFile(dir)
  89. l := &Logger{
  90. Filename: filename,
  91. }
  92. defer l.Close()
  93. b := []byte("boo!")
  94. n, err := l.Write(b)
  95. isNil(err, t)
  96. equals(len(b), n, t)
  97. existsWithContent(logFile(dir), b, t)
  98. fileCount(dir, 1, t)
  99. }
  100. func TestDefaultFilename(t *testing.T) {
  101. currentTime = fakeTime
  102. dir := os.TempDir()
  103. filename := filepath.Join(dir, filepath.Base(os.Args[0])+"-lumberjack.log")
  104. defer os.Remove(filename)
  105. l := &Logger{}
  106. defer l.Close()
  107. b := []byte("boo!")
  108. n, err := l.Write(b)
  109. isNil(err, t)
  110. equals(len(b), n, t)
  111. existsWithContent(filename, b, t)
  112. }
  113. func TestAutoRotate(t *testing.T) {
  114. currentTime = fakeTime
  115. megabyte = 1
  116. dir := makeTempDir("TestAutoRotate", t)
  117. defer os.RemoveAll(dir)
  118. filename := logFile(dir)
  119. l := &Logger{
  120. Filename: filename,
  121. MaxSize: 10,
  122. }
  123. defer l.Close()
  124. b := []byte("boo!")
  125. n, err := l.Write(b)
  126. isNil(err, t)
  127. equals(len(b), n, t)
  128. existsWithContent(filename, b, t)
  129. fileCount(dir, 1, t)
  130. newFakeTime()
  131. b2 := []byte("foooooo!")
  132. n, err = l.Write(b2)
  133. isNil(err, t)
  134. equals(len(b2), n, t)
  135. // the old logfile should be moved aside and the main logfile should have
  136. // only the last write in it.
  137. existsWithContent(filename, b2, t)
  138. // the backup file will use the current fake time and have the old contents.
  139. existsWithContent(backupFile(dir), b, t)
  140. fileCount(dir, 2, t)
  141. }
  142. func TestFirstWriteRotate(t *testing.T) {
  143. currentTime = fakeTime
  144. megabyte = 1
  145. dir := makeTempDir("TestFirstWriteRotate", t)
  146. defer os.RemoveAll(dir)
  147. filename := logFile(dir)
  148. l := &Logger{
  149. Filename: filename,
  150. MaxSize: 10,
  151. }
  152. defer l.Close()
  153. start := []byte("boooooo!")
  154. err := ioutil.WriteFile(filename, start, 0600)
  155. isNil(err, t)
  156. newFakeTime()
  157. // this would make us rotate
  158. b := []byte("fooo!")
  159. n, err := l.Write(b)
  160. isNil(err, t)
  161. equals(len(b), n, t)
  162. existsWithContent(filename, b, t)
  163. existsWithContent(backupFile(dir), start, t)
  164. fileCount(dir, 2, t)
  165. }
  166. func TestMaxBackups(t *testing.T) {
  167. currentTime = fakeTime
  168. megabyte = 1
  169. dir := makeTempDir("TestMaxBackups", t)
  170. defer os.RemoveAll(dir)
  171. filename := logFile(dir)
  172. l := &Logger{
  173. Filename: filename,
  174. MaxSize: 10,
  175. MaxBackups: 1,
  176. }
  177. defer l.Close()
  178. b := []byte("boo!")
  179. n, err := l.Write(b)
  180. isNil(err, t)
  181. equals(len(b), n, t)
  182. existsWithContent(filename, b, t)
  183. fileCount(dir, 1, t)
  184. newFakeTime()
  185. // this will put us over the max
  186. b2 := []byte("foooooo!")
  187. n, err = l.Write(b2)
  188. isNil(err, t)
  189. equals(len(b2), n, t)
  190. // this will use the new fake time
  191. secondFilename := backupFile(dir)
  192. existsWithContent(secondFilename, b, t)
  193. // make sure the old file still exists with the same content.
  194. existsWithContent(filename, b2, t)
  195. fileCount(dir, 2, t)
  196. newFakeTime()
  197. // this will make us rotate again
  198. b3 := []byte("baaaaaar!")
  199. n, err = l.Write(b3)
  200. isNil(err, t)
  201. equals(len(b3), n, t)
  202. // this will use the new fake time
  203. thirdFilename := backupFile(dir)
  204. existsWithContent(thirdFilename, b2, t)
  205. existsWithContent(filename, b3, t)
  206. // we need to wait a little bit since the files get deleted on a different
  207. // goroutine.
  208. <-time.After(time.Millisecond * 10)
  209. // should only have two files in the dir still
  210. fileCount(dir, 2, t)
  211. // second file name should still exist
  212. existsWithContent(thirdFilename, b2, t)
  213. // should have deleted the first backup
  214. notExist(secondFilename, t)
  215. // now test that we don't delete directories or non-logfile files
  216. newFakeTime()
  217. // create a file that is close to but different from the logfile name.
  218. // It shouldn't get caught by our deletion filters.
  219. notlogfile := logFile(dir) + ".foo"
  220. err = ioutil.WriteFile(notlogfile, []byte("data"), 0644)
  221. isNil(err, t)
  222. // Make a directory that exactly matches our log file filters... it still
  223. // shouldn't get caught by the deletion filter since it's a directory.
  224. notlogfiledir := backupFile(dir)
  225. err = os.Mkdir(notlogfiledir, 0700)
  226. isNil(err, t)
  227. newFakeTime()
  228. // this will use the new fake time
  229. fourthFilename := backupFile(dir)
  230. // Create a log file that is/was being compressed - this should
  231. // not be counted since both the compressed and the uncompressed
  232. // log files still exist.
  233. compLogFile := fourthFilename + compressSuffix
  234. err = ioutil.WriteFile(compLogFile, []byte("compress"), 0644)
  235. isNil(err, t)
  236. // this will make us rotate again
  237. b4 := []byte("baaaaaaz!")
  238. n, err = l.Write(b4)
  239. isNil(err, t)
  240. equals(len(b4), n, t)
  241. existsWithContent(fourthFilename, b3, t)
  242. existsWithContent(fourthFilename+compressSuffix, []byte("compress"), t)
  243. // we need to wait a little bit since the files get deleted on a different
  244. // goroutine.
  245. <-time.After(time.Millisecond * 10)
  246. // We should have four things in the directory now - the 2 log files, the
  247. // not log file, and the directory
  248. fileCount(dir, 5, t)
  249. // third file name should still exist
  250. existsWithContent(filename, b4, t)
  251. existsWithContent(fourthFilename, b3, t)
  252. // should have deleted the first filename
  253. notExist(thirdFilename, t)
  254. // the not-a-logfile should still exist
  255. exists(notlogfile, t)
  256. // the directory
  257. exists(notlogfiledir, t)
  258. }
  259. func TestCleanupExistingBackups(t *testing.T) {
  260. // test that if we start with more backup files than we're supposed to have
  261. // in total, that extra ones get cleaned up when we rotate.
  262. currentTime = fakeTime
  263. megabyte = 1
  264. dir := makeTempDir("TestCleanupExistingBackups", t)
  265. defer os.RemoveAll(dir)
  266. // make 3 backup files
  267. data := []byte("data")
  268. backup := backupFile(dir)
  269. err := ioutil.WriteFile(backup, data, 0644)
  270. isNil(err, t)
  271. newFakeTime()
  272. backup = backupFile(dir)
  273. err = ioutil.WriteFile(backup+compressSuffix, data, 0644)
  274. isNil(err, t)
  275. newFakeTime()
  276. backup = backupFile(dir)
  277. err = ioutil.WriteFile(backup, data, 0644)
  278. isNil(err, t)
  279. // now create a primary log file with some data
  280. filename := logFile(dir)
  281. err = ioutil.WriteFile(filename, data, 0644)
  282. isNil(err, t)
  283. l := &Logger{
  284. Filename: filename,
  285. MaxSize: 10,
  286. MaxBackups: 1,
  287. }
  288. defer l.Close()
  289. newFakeTime()
  290. b2 := []byte("foooooo!")
  291. n, err := l.Write(b2)
  292. isNil(err, t)
  293. equals(len(b2), n, t)
  294. // we need to wait a little bit since the files get deleted on a different
  295. // goroutine.
  296. <-time.After(time.Millisecond * 10)
  297. // now we should only have 2 files left - the primary and one backup
  298. fileCount(dir, 2, t)
  299. }
  300. func TestMaxAge(t *testing.T) {
  301. currentTime = fakeTime
  302. megabyte = 1
  303. dir := makeTempDir("TestMaxAge", t)
  304. defer os.RemoveAll(dir)
  305. filename := logFile(dir)
  306. l := &Logger{
  307. Filename: filename,
  308. MaxSize: 10,
  309. MaxAge: 1,
  310. }
  311. defer l.Close()
  312. b := []byte("boo!")
  313. n, err := l.Write(b)
  314. isNil(err, t)
  315. equals(len(b), n, t)
  316. existsWithContent(filename, b, t)
  317. fileCount(dir, 1, t)
  318. // two days later
  319. newFakeTime()
  320. b2 := []byte("foooooo!")
  321. n, err = l.Write(b2)
  322. isNil(err, t)
  323. equals(len(b2), n, t)
  324. existsWithContent(backupFile(dir), b, t)
  325. // we need to wait a little bit since the files get deleted on a different
  326. // goroutine.
  327. <-time.After(10 * time.Millisecond)
  328. // We should still have 2 log files, since the most recent backup was just
  329. // created.
  330. fileCount(dir, 2, t)
  331. existsWithContent(filename, b2, t)
  332. // we should have deleted the old file due to being too old
  333. existsWithContent(backupFile(dir), b, t)
  334. // two days later
  335. newFakeTime()
  336. b3 := []byte("baaaaar!")
  337. n, err = l.Write(b3)
  338. isNil(err, t)
  339. equals(len(b3), n, t)
  340. existsWithContent(backupFile(dir), b2, t)
  341. // we need to wait a little bit since the files get deleted on a different
  342. // goroutine.
  343. <-time.After(10 * time.Millisecond)
  344. // We should have 2 log files - the main log file, and the most recent
  345. // backup. The earlier backup is past the cutoff and should be gone.
  346. fileCount(dir, 2, t)
  347. existsWithContent(filename, b3, t)
  348. // we should have deleted the old file due to being too old
  349. existsWithContent(backupFile(dir), b2, t)
  350. }
  351. func TestOldLogFiles(t *testing.T) {
  352. currentTime = fakeTime
  353. megabyte = 1
  354. dir := makeTempDir("TestOldLogFiles", t)
  355. defer os.RemoveAll(dir)
  356. filename := logFile(dir)
  357. data := []byte("data")
  358. err := ioutil.WriteFile(filename, data, 07)
  359. isNil(err, t)
  360. // This gives us a time with the same precision as the time we get from the
  361. // timestamp in the name.
  362. t1, err := time.Parse(backupTimeFormat, fakeTime().UTC().Format(backupTimeFormat))
  363. isNil(err, t)
  364. backup := backupFile(dir)
  365. err = ioutil.WriteFile(backup, data, 07)
  366. isNil(err, t)
  367. newFakeTime()
  368. t2, err := time.Parse(backupTimeFormat, fakeTime().UTC().Format(backupTimeFormat))
  369. isNil(err, t)
  370. backup2 := backupFile(dir)
  371. err = ioutil.WriteFile(backup2, data, 07)
  372. isNil(err, t)
  373. l := &Logger{Filename: filename}
  374. files, err := l.oldLogFiles()
  375. isNil(err, t)
  376. equals(2, len(files), t)
  377. // should be sorted by newest file first, which would be t2
  378. equals(t2, files[0].timestamp, t)
  379. equals(t1, files[1].timestamp, t)
  380. }
  381. func TestTimeFromName(t *testing.T) {
  382. l := &Logger{Filename: "/var/log/myfoo/foo.log"}
  383. prefix, ext := l.prefixAndExt()
  384. tests := []struct {
  385. filename string
  386. want time.Time
  387. wantErr bool
  388. }{
  389. {"foo-2014-05-04T14-44-33.555.log", time.Date(2014, 5, 4, 14, 44, 33, 555000000, time.UTC), false},
  390. {"foo-2014-05-04T14-44-33.555", time.Time{}, true},
  391. {"2014-05-04T14-44-33.555.log", time.Time{}, true},
  392. {"foo.log", time.Time{}, true},
  393. }
  394. for _, test := range tests {
  395. got, err := l.timeFromName(test.filename, prefix, ext)
  396. equals(got, test.want, t)
  397. equals(err != nil, test.wantErr, t)
  398. }
  399. }
  400. func TestLocalTime(t *testing.T) {
  401. currentTime = fakeTime
  402. megabyte = 1
  403. dir := makeTempDir("TestLocalTime", t)
  404. defer os.RemoveAll(dir)
  405. l := &Logger{
  406. Filename: logFile(dir),
  407. MaxSize: 10,
  408. LocalTime: true,
  409. }
  410. defer l.Close()
  411. b := []byte("boo!")
  412. n, err := l.Write(b)
  413. isNil(err, t)
  414. equals(len(b), n, t)
  415. b2 := []byte("fooooooo!")
  416. n2, err := l.Write(b2)
  417. isNil(err, t)
  418. equals(len(b2), n2, t)
  419. existsWithContent(logFile(dir), b2, t)
  420. existsWithContent(backupFileLocal(dir), b, t)
  421. }
  422. func TestRotate(t *testing.T) {
  423. currentTime = fakeTime
  424. dir := makeTempDir("TestRotate", t)
  425. defer os.RemoveAll(dir)
  426. filename := logFile(dir)
  427. l := &Logger{
  428. Filename: filename,
  429. MaxBackups: 1,
  430. MaxSize: 100, // megabytes
  431. }
  432. defer l.Close()
  433. b := []byte("boo!")
  434. n, err := l.Write(b)
  435. isNil(err, t)
  436. equals(len(b), n, t)
  437. existsWithContent(filename, b, t)
  438. fileCount(dir, 1, t)
  439. newFakeTime()
  440. err = l.Rotate()
  441. isNil(err, t)
  442. // we need to wait a little bit since the files get deleted on a different
  443. // goroutine.
  444. <-time.After(10 * time.Millisecond)
  445. filename2 := backupFile(dir)
  446. existsWithContent(filename2, b, t)
  447. existsWithContent(filename, []byte{}, t)
  448. fileCount(dir, 2, t)
  449. newFakeTime()
  450. err = l.Rotate()
  451. isNil(err, t)
  452. // we need to wait a little bit since the files get deleted on a different
  453. // goroutine.
  454. <-time.After(10 * time.Millisecond)
  455. filename3 := backupFile(dir)
  456. existsWithContent(filename3, []byte{}, t)
  457. existsWithContent(filename, []byte{}, t)
  458. fileCount(dir, 2, t)
  459. b2 := []byte("foooooo!")
  460. n, err = l.Write(b2)
  461. isNil(err, t)
  462. equals(len(b2), n, t)
  463. // this will use the new fake time
  464. existsWithContent(filename, b2, t)
  465. }
  466. func TestCompressOnRotate(t *testing.T) {
  467. currentTime = fakeTime
  468. megabyte = 1
  469. dir := makeTempDir("TestCompressOnRotate", t)
  470. defer os.RemoveAll(dir)
  471. filename := logFile(dir)
  472. l := &Logger{
  473. Compress: true,
  474. Filename: filename,
  475. MaxSize: 10,
  476. }
  477. defer l.Close()
  478. b := []byte("boo!")
  479. n, err := l.Write(b)
  480. isNil(err, t)
  481. equals(len(b), n, t)
  482. existsWithContent(filename, b, t)
  483. fileCount(dir, 1, t)
  484. newFakeTime()
  485. err = l.Rotate()
  486. isNil(err, t)
  487. // the old logfile should be moved aside and the main logfile should have
  488. // nothing in it.
  489. existsWithContent(filename, []byte{}, t)
  490. // we need to wait a little bit since the files get compressed on a different
  491. // goroutine.
  492. <-time.After(300 * time.Millisecond)
  493. // a compressed version of the log file should now exist and the original
  494. // should have been removed.
  495. bc := new(bytes.Buffer)
  496. gz := gzip.NewWriter(bc)
  497. _, err = gz.Write(b)
  498. isNil(err, t)
  499. err = gz.Close()
  500. isNil(err, t)
  501. existsWithContent(backupFile(dir)+compressSuffix, bc.Bytes(), t)
  502. notExist(backupFile(dir), t)
  503. fileCount(dir, 2, t)
  504. }
  505. func TestCompressOnResume(t *testing.T) {
  506. currentTime = fakeTime
  507. megabyte = 1
  508. dir := makeTempDir("TestCompressOnResume", t)
  509. defer os.RemoveAll(dir)
  510. filename := logFile(dir)
  511. l := &Logger{
  512. Compress: true,
  513. Filename: filename,
  514. MaxSize: 10,
  515. }
  516. defer l.Close()
  517. // Create a backup file and empty "compressed" file.
  518. filename2 := backupFile(dir)
  519. b := []byte("foo!")
  520. err := ioutil.WriteFile(filename2, b, 0644)
  521. isNil(err, t)
  522. err = ioutil.WriteFile(filename2+compressSuffix, []byte{}, 0644)
  523. isNil(err, t)
  524. newFakeTime()
  525. b2 := []byte("boo!")
  526. n, err := l.Write(b2)
  527. isNil(err, t)
  528. equals(len(b2), n, t)
  529. existsWithContent(filename, b2, t)
  530. // we need to wait a little bit since the files get compressed on a different
  531. // goroutine.
  532. <-time.After(300 * time.Millisecond)
  533. // The write should have started the compression - a compressed version of
  534. // the log file should now exist and the original should have been removed.
  535. bc := new(bytes.Buffer)
  536. gz := gzip.NewWriter(bc)
  537. _, err = gz.Write(b)
  538. isNil(err, t)
  539. err = gz.Close()
  540. isNil(err, t)
  541. existsWithContent(filename2+compressSuffix, bc.Bytes(), t)
  542. notExist(filename2, t)
  543. fileCount(dir, 2, t)
  544. }
  545. func TestJson(t *testing.T) {
  546. data := []byte(`
  547. {
  548. "filename": "foo",
  549. "maxsize": 5,
  550. "maxage": 10,
  551. "maxbackups": 3,
  552. "localtime": true,
  553. "compress": true
  554. }`[1:])
  555. l := Logger{}
  556. err := json.Unmarshal(data, &l)
  557. isNil(err, t)
  558. equals("foo", l.Filename, t)
  559. equals(5, l.MaxSize, t)
  560. equals(10, l.MaxAge, t)
  561. equals(3, l.MaxBackups, t)
  562. equals(true, l.LocalTime, t)
  563. equals(true, l.Compress, t)
  564. }
  565. func TestYaml(t *testing.T) {
  566. data := []byte(`
  567. filename: foo
  568. maxsize: 5
  569. maxage: 10
  570. maxbackups: 3
  571. localtime: true
  572. compress: true`[1:])
  573. l := Logger{}
  574. err := yaml.Unmarshal(data, &l)
  575. isNil(err, t)
  576. equals("foo", l.Filename, t)
  577. equals(5, l.MaxSize, t)
  578. equals(10, l.MaxAge, t)
  579. equals(3, l.MaxBackups, t)
  580. equals(true, l.LocalTime, t)
  581. equals(true, l.Compress, t)
  582. }
  583. func TestToml(t *testing.T) {
  584. data := `
  585. filename = "foo"
  586. maxsize = 5
  587. maxage = 10
  588. maxbackups = 3
  589. localtime = true
  590. compress = true`[1:]
  591. l := Logger{}
  592. md, err := toml.Decode(data, &l)
  593. isNil(err, t)
  594. equals("foo", l.Filename, t)
  595. equals(5, l.MaxSize, t)
  596. equals(10, l.MaxAge, t)
  597. equals(3, l.MaxBackups, t)
  598. equals(true, l.LocalTime, t)
  599. equals(true, l.Compress, t)
  600. equals(0, len(md.Undecoded()), t)
  601. }
  602. // makeTempDir creates a file with a semi-unique name in the OS temp directory.
  603. // It should be based on the name of the test, to keep parallel tests from
  604. // colliding, and must be cleaned up after the test is finished.
  605. func makeTempDir(name string, t testing.TB) string {
  606. dir := time.Now().Format(name + backupTimeFormat)
  607. dir = filepath.Join(os.TempDir(), dir)
  608. isNilUp(os.Mkdir(dir, 0700), t, 1)
  609. return dir
  610. }
  611. // existsWithContent checks that the given file exists and has the correct content.
  612. func existsWithContent(path string, content []byte, t testing.TB) {
  613. info, err := os.Stat(path)
  614. isNilUp(err, t, 1)
  615. equalsUp(int64(len(content)), info.Size(), t, 1)
  616. b, err := ioutil.ReadFile(path)
  617. isNilUp(err, t, 1)
  618. equalsUp(content, b, t, 1)
  619. }
  620. // logFile returns the log file name in the given directory for the current fake
  621. // time.
  622. func logFile(dir string) string {
  623. return filepath.Join(dir, "foobar.log")
  624. }
  625. func backupFile(dir string) string {
  626. return filepath.Join(dir, "foobar-"+fakeTime().UTC().Format(backupTimeFormat)+".log")
  627. }
  628. func backupFileLocal(dir string) string {
  629. return filepath.Join(dir, "foobar-"+fakeTime().Format(backupTimeFormat)+".log")
  630. }
  631. // logFileLocal returns the log file name in the given directory for the current
  632. // fake time using the local timezone.
  633. func logFileLocal(dir string) string {
  634. return filepath.Join(dir, fakeTime().Format(backupTimeFormat))
  635. }
  636. // fileCount checks that the number of files in the directory is exp.
  637. func fileCount(dir string, exp int, t testing.TB) {
  638. files, err := ioutil.ReadDir(dir)
  639. isNilUp(err, t, 1)
  640. // Make sure no other files were created.
  641. equalsUp(exp, len(files), t, 1)
  642. }
  643. // newFakeTime sets the fake "current time" to two days later.
  644. func newFakeTime() {
  645. fakeCurrentTime = fakeCurrentTime.Add(time.Hour * 24 * 2)
  646. }
  647. func notExist(path string, t testing.TB) {
  648. _, err := os.Stat(path)
  649. assertUp(os.IsNotExist(err), t, 1, "expected to get os.IsNotExist, but instead got %v", err)
  650. }
  651. func exists(path string, t testing.TB) {
  652. _, err := os.Stat(path)
  653. assertUp(err == nil, t, 1, "expected file to exist, but got error from os.Stat: %v", err)
  654. }