2
0

loglimit_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package worker
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. . "github.com/smartystreets/goconvey/convey"
  10. . "github.com/tuna/tunasync/internal"
  11. )
  12. func TestLogLimiter(t *testing.T) {
  13. Convey("LogLimiter should work", t, func(ctx C) {
  14. tmpDir, err := ioutil.TempDir("", "tunasync")
  15. tmpLogDir, err := ioutil.TempDir("", "tunasync-log")
  16. defer os.RemoveAll(tmpDir)
  17. defer os.RemoveAll(tmpLogDir)
  18. So(err, ShouldBeNil)
  19. scriptFile := filepath.Join(tmpDir, "cmd.sh")
  20. c := cmdConfig{
  21. name: "tuna-loglimit",
  22. upstreamURL: "http://mirrors.tuna.moe/",
  23. command: scriptFile,
  24. workingDir: tmpDir,
  25. logDir: tmpLogDir,
  26. logFile: filepath.Join(tmpLogDir, "latest.log"),
  27. interval: 600 * time.Second,
  28. }
  29. provider, err := newCmdProvider(c)
  30. So(err, ShouldBeNil)
  31. limiter := newLogLimiter(provider)
  32. provider.AddHook(limiter)
  33. Convey("If logs are created simply", func() {
  34. for i := 0; i < 15; i++ {
  35. fn := filepath.Join(tmpLogDir, fmt.Sprintf("%s-%d.log", provider.Name(), i))
  36. f, _ := os.Create(fn)
  37. // time.Sleep(1 * time.Second)
  38. f.Close()
  39. }
  40. matches, _ := filepath.Glob(filepath.Join(tmpLogDir, "*.log"))
  41. So(len(matches), ShouldEqual, 15)
  42. managerChan := make(chan jobMessage)
  43. semaphore := make(chan empty, 1)
  44. job := newMirrorJob(provider)
  45. scriptContent := `#!/bin/bash
  46. echo $TUNASYNC_WORKING_DIR
  47. echo $TUNASYNC_MIRROR_NAME
  48. echo $TUNASYNC_UPSTREAM_URL
  49. echo $TUNASYNC_LOG_FILE
  50. `
  51. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  52. So(err, ShouldBeNil)
  53. go job.Run(managerChan, semaphore)
  54. job.ctrlChan <- jobStart
  55. msg := <-managerChan
  56. So(msg.status, ShouldEqual, PreSyncing)
  57. msg = <-managerChan
  58. So(msg.status, ShouldEqual, Syncing)
  59. logFile := provider.LogFile()
  60. msg = <-managerChan
  61. So(msg.status, ShouldEqual, Success)
  62. job.ctrlChan <- jobDisable
  63. So(logFile, ShouldNotEqual, provider.LogFile())
  64. matches, _ = filepath.Glob(filepath.Join(tmpLogDir, "*.log"))
  65. So(len(matches), ShouldEqual, 10)
  66. expectedOutput := fmt.Sprintf(
  67. "%s\n%s\n%s\n%s\n",
  68. provider.WorkingDir(),
  69. provider.Name(),
  70. provider.upstreamURL,
  71. logFile,
  72. )
  73. loggedContent, err := ioutil.ReadFile(filepath.Join(provider.LogDir(), "latest"))
  74. So(err, ShouldBeNil)
  75. So(string(loggedContent), ShouldEqual, expectedOutput)
  76. })
  77. Convey("If job failed simply", func() {
  78. managerChan := make(chan jobMessage)
  79. semaphore := make(chan empty, 1)
  80. job := newMirrorJob(provider)
  81. scriptContent := `#!/bin/bash
  82. echo $TUNASYNC_WORKING_DIR
  83. echo $TUNASYNC_MIRROR_NAME
  84. echo $TUNASYNC_UPSTREAM_URL
  85. echo $TUNASYNC_LOG_FILE
  86. sleep 5
  87. `
  88. err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
  89. So(err, ShouldBeNil)
  90. go job.Run(managerChan, semaphore)
  91. job.ctrlChan <- jobStart
  92. msg := <-managerChan
  93. So(msg.status, ShouldEqual, PreSyncing)
  94. msg = <-managerChan
  95. So(msg.status, ShouldEqual, Syncing)
  96. logFile := provider.LogFile()
  97. time.Sleep(1 * time.Second)
  98. job.ctrlChan <- jobStop
  99. msg = <-managerChan
  100. So(msg.status, ShouldEqual, Failed)
  101. job.ctrlChan <- jobDisable
  102. <-job.disabled
  103. So(logFile, ShouldNotEqual, provider.LogFile())
  104. expectedOutput := fmt.Sprintf(
  105. "%s\n%s\n%s\n%s\n",
  106. provider.WorkingDir(),
  107. provider.Name(),
  108. provider.upstreamURL,
  109. logFile,
  110. )
  111. loggedContent, err := ioutil.ReadFile(filepath.Join(provider.LogDir(), "latest"))
  112. So(err, ShouldBeNil)
  113. So(string(loggedContent), ShouldEqual, expectedOutput)
  114. loggedContent, err = ioutil.ReadFile(logFile + ".fail")
  115. So(err, ShouldBeNil)
  116. So(string(loggedContent), ShouldEqual, expectedOutput)
  117. })
  118. })
  119. }