123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package worker
- import (
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "testing"
- . "github.com/smartystreets/goconvey/convey"
- )
- func TestRsyncProvider(t *testing.T) {
- Convey("Rsync Provider should work", t, func() {
- c := rsyncConfig{
- name: "tuna",
- upstreamURL: "rsync://rsync.tuna.moe/tuna/",
- workingDir: "/srv/mirror/production/tuna",
- logDir: "/var/log/tunasync",
- logFile: "tuna.log",
- useIPv6: true,
- interval: 600,
- }
- provider, err := newRsyncProvider(c)
- So(err, ShouldBeNil)
- So(provider.Name(), ShouldEqual, c.name)
- So(provider.WorkingDir(), ShouldEqual, c.workingDir)
- So(provider.LogDir(), ShouldEqual, c.logDir)
- So(provider.LogFile(), ShouldEqual, c.logFile)
- So(provider.Interval(), ShouldEqual, c.interval)
- Convey("When entering a context (auto exit)", func() {
- func() {
- ctx := provider.EnterContext()
- defer provider.ExitContext()
- So(provider.WorkingDir(), ShouldEqual, c.workingDir)
- newWorkingDir := "/srv/mirror/working/tuna"
- ctx.Set(_WorkingDirKey, newWorkingDir)
- So(provider.WorkingDir(), ShouldEqual, newWorkingDir)
- }()
- Convey("After context is done", func() {
- So(provider.WorkingDir(), ShouldEqual, c.workingDir)
- })
- })
- Convey("When entering a context (manually exit)", func() {
- ctx := provider.EnterContext()
- So(provider.WorkingDir(), ShouldEqual, c.workingDir)
- newWorkingDir := "/srv/mirror/working/tuna"
- ctx.Set(_WorkingDirKey, newWorkingDir)
- So(provider.WorkingDir(), ShouldEqual, newWorkingDir)
- Convey("After context is done", func() {
- provider.ExitContext()
- So(provider.WorkingDir(), ShouldEqual, c.workingDir)
- })
- })
- })
- }
- func TestCmdProvider(t *testing.T) {
- Convey("Command Provider should work", t, func() {
- tmpDir, err := ioutil.TempDir("", "tunasync")
- defer os.RemoveAll(tmpDir)
- So(err, ShouldBeNil)
- scriptFile := filepath.Join(tmpDir, "cmd.sh")
- tmpFile := filepath.Join(tmpDir, "log_file")
- c := cmdConfig{
- name: "tuna-cmd",
- upstreamURL: "http://mirrors.tuna.moe/",
- command: "bash " + scriptFile,
- workingDir: tmpDir,
- logDir: tmpDir,
- logFile: tmpFile,
- interval: 600,
- }
- provider, err := newCmdProvider(c)
- So(err, ShouldBeNil)
- So(provider.Name(), ShouldEqual, c.name)
- So(provider.WorkingDir(), ShouldEqual, c.workingDir)
- So(provider.LogDir(), ShouldEqual, c.logDir)
- So(provider.LogFile(), ShouldEqual, c.logFile)
- So(provider.Interval(), ShouldEqual, c.interval)
- Convey("Let's try to run a simple command", func() {
- scriptContent := `#!/bin/bash
- echo $TUNASYNC_WORKING_DIR
- echo $TUNASYNC_MIRROR_NAME
- echo $TUNASYNC_UPSTREAM_URL
- echo $TUNASYNC_LOG_FILE
- `
- exceptedOutput := fmt.Sprintf(
- "%s\n%s\n%s\n%s\n",
- provider.WorkingDir(),
- provider.Name(),
- provider.upstreamURL,
- provider.LogFile(),
- )
- err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
- So(err, ShouldBeNil)
- readedScriptContent, err := ioutil.ReadFile(scriptFile)
- So(err, ShouldBeNil)
- So(readedScriptContent, ShouldResemble, []byte(scriptContent))
- err = provider.Run()
- So(err, ShouldBeNil)
- err = provider.cmd.Wait()
- So(err, ShouldBeNil)
- loggedContent, err := ioutil.ReadFile(provider.LogFile())
- So(err, ShouldBeNil)
- So(string(loggedContent), ShouldEqual, exceptedOutput)
- })
- Convey("If a command fails", func() {
- scriptContent := `exit 1`
- err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755)
- So(err, ShouldBeNil)
- readedScriptContent, err := ioutil.ReadFile(scriptFile)
- So(err, ShouldBeNil)
- So(readedScriptContent, ShouldResemble, []byte(scriptContent))
- err = provider.Run()
- So(err, ShouldBeNil)
- err = provider.cmd.Wait()
- So(err, ShouldNotBeNil)
- })
- })
- }
|