123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package worker
- import (
- "fmt"
- "os"
- "path/filepath"
- "testing"
- "time"
- . "github.com/smartystreets/goconvey/convey"
- . "github.com/tuna/tunasync/internal"
- )
- func TestLogLimiter(t *testing.T) {
- Convey("LogLimiter should work", t, func(ctx C) {
- tmpDir, _ := os.MkdirTemp("", "tunasync")
- tmpLogDir, err := os.MkdirTemp("", "tunasync-log")
- defer os.RemoveAll(tmpDir)
- defer os.RemoveAll(tmpLogDir)
- So(err, ShouldBeNil)
- scriptFile := filepath.Join(tmpDir, "cmd.sh")
- c := cmdConfig{
- name: "tuna-loglimit",
- upstreamURL: "http://mirrors.tuna.moe/",
- command: scriptFile,
- workingDir: tmpDir,
- logDir: tmpLogDir,
- logFile: filepath.Join(tmpLogDir, "latest.log"),
- interval: 600 * time.Second,
- }
- provider, err := newCmdProvider(c)
- So(err, ShouldBeNil)
- limiter := newLogLimiter(provider)
- provider.AddHook(limiter)
- Convey("If logs are created simply", func() {
- for i := 0; i < 15; i++ {
- fn := filepath.Join(tmpLogDir, fmt.Sprintf("%s-%d.log", provider.Name(), i))
- f, _ := os.Create(fn)
- // time.Sleep(1 * time.Second)
- f.Close()
- }
- matches, _ := filepath.Glob(filepath.Join(tmpLogDir, "*.log"))
- So(len(matches), ShouldEqual, 15)
- managerChan := make(chan jobMessage)
- semaphore := make(chan empty, 1)
- job := newMirrorJob(provider)
- scriptContent := `#!/bin/bash
- echo $TUNASYNC_WORKING_DIR
- echo $TUNASYNC_MIRROR_NAME
- echo $TUNASYNC_UPSTREAM_URL
- echo $TUNASYNC_LOG_FILE
- `
- err = os.WriteFile(scriptFile, []byte(scriptContent), 0755)
- So(err, ShouldBeNil)
- go job.Run(managerChan, semaphore)
- job.ctrlChan <- jobStart
- msg := <-managerChan
- So(msg.status, ShouldEqual, PreSyncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- logFile := provider.LogFile()
- msg = <-managerChan
- So(msg.status, ShouldEqual, Success)
- job.ctrlChan <- jobDisable
- So(logFile, ShouldNotEqual, provider.LogFile())
- matches, _ = filepath.Glob(filepath.Join(tmpLogDir, "*.log"))
- So(len(matches), ShouldEqual, 10)
- expectedOutput := fmt.Sprintf(
- "%s\n%s\n%s\n%s\n",
- provider.WorkingDir(),
- provider.Name(),
- provider.upstreamURL,
- logFile,
- )
- loggedContent, err := os.ReadFile(filepath.Join(provider.LogDir(), "latest"))
- So(err, ShouldBeNil)
- So(string(loggedContent), ShouldEqual, expectedOutput)
- })
- Convey("If job failed simply", func() {
- managerChan := make(chan jobMessage)
- semaphore := make(chan empty, 1)
- job := newMirrorJob(provider)
- scriptContent := `#!/bin/bash
- echo $TUNASYNC_WORKING_DIR
- echo $TUNASYNC_MIRROR_NAME
- echo $TUNASYNC_UPSTREAM_URL
- echo $TUNASYNC_LOG_FILE
- sleep 5
- `
- err = os.WriteFile(scriptFile, []byte(scriptContent), 0755)
- So(err, ShouldBeNil)
- go job.Run(managerChan, semaphore)
- job.ctrlChan <- jobStart
- msg := <-managerChan
- So(msg.status, ShouldEqual, PreSyncing)
- msg = <-managerChan
- So(msg.status, ShouldEqual, Syncing)
- logFile := provider.LogFile()
- time.Sleep(1 * time.Second)
- job.ctrlChan <- jobStop
- msg = <-managerChan
- So(msg.status, ShouldEqual, Failed)
- job.ctrlChan <- jobDisable
- <-job.disabled
- So(logFile, ShouldNotEqual, provider.LogFile())
- expectedOutput := fmt.Sprintf(
- "%s\n%s\n%s\n%s\n",
- provider.WorkingDir(),
- provider.Name(),
- provider.upstreamURL,
- logFile,
- )
- loggedContent, err := os.ReadFile(filepath.Join(provider.LogDir(), "latest"))
- So(err, ShouldBeNil)
- So(string(loggedContent), ShouldEqual, expectedOutput)
- loggedContent, err = os.ReadFile(logFile + ".fail")
- So(err, ShouldBeNil)
- So(string(loggedContent), ShouldEqual, expectedOutput)
- })
- })
- }
|