lumberjack_test.go 18 KB

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