123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- package worker
- import (
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "testing"
- . "github.com/smartystreets/goconvey/convey"
- )
- func TestConfig(t *testing.T) {
- var cfgBlob = `
- [global]
- name = "test_worker"
- log_dir = "/var/log/tunasync/{{.Name}}"
- mirror_dir = "/data/mirrors"
- concurrent = 10
- interval = 240
- retry = 3
- [manager]
- api_base = "https://127.0.0.1:5000"
- token = "some_token"
- [server]
- hostname = "worker1.example.com"
- listen_addr = "127.0.0.1"
- listen_port = 6000
- ssl_cert = "/etc/tunasync.d/worker1.cert"
- ssl_key = "/etc/tunasync.d/worker1.key"
- [[mirrors]]
- name = "AOSP"
- provider = "command"
- upstream = "https://aosp.google.com/"
- interval = 720
- retry = 2
- mirror_dir = "/data/git/AOSP"
- exec_on_success = [
- "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'"
- ]
- [mirrors.env]
- REPO = "/usr/local/bin/aosp-repo"
- [[mirrors]]
- name = "debian"
- provider = "two-stage-rsync"
- stage1_profile = "debian"
- upstream = "rsync://ftp.debian.org/debian/"
- use_ipv6 = true
- [[mirrors]]
- name = "fedora"
- provider = "rsync"
- upstream = "rsync://ftp.fedoraproject.org/fedora/"
- use_ipv6 = true
- exclude_file = "/etc/tunasync.d/fedora-exclude.txt"
- exec_on_failure = [
- "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'"
- ]
- `
- Convey("When giving invalid file", t, func() {
- cfg, err := LoadConfig("/path/to/invalid/file")
- So(err, ShouldNotBeNil)
- So(cfg, ShouldBeNil)
- })
- Convey("Everything should work on valid config file", t, func() {
- tmpfile, err := ioutil.TempFile("", "tunasync")
- So(err, ShouldEqual, nil)
- defer os.Remove(tmpfile.Name())
- tmpDir, err := ioutil.TempDir("", "tunasync")
- So(err, ShouldBeNil)
- defer os.RemoveAll(tmpDir)
- incSection := fmt.Sprintf(
- "\n[include]\n"+
- "include_mirrors = \"%s/*.conf\"",
- tmpDir,
- )
- curCfgBlob := cfgBlob + incSection
- err = ioutil.WriteFile(tmpfile.Name(), []byte(curCfgBlob), 0644)
- So(err, ShouldEqual, nil)
- defer tmpfile.Close()
- incBlob1 := `
- [[mirrors]]
- name = "debian-cd"
- provider = "two-stage-rsync"
- stage1_profile = "debian"
- use_ipv6 = true
- [[mirrors]]
- name = "debian-security"
- provider = "two-stage-rsync"
- stage1_profile = "debian"
- use_ipv6 = true
- `
- incBlob2 := `
- [[mirrors]]
- name = "ubuntu"
- provider = "two-stage-rsync"
- stage1_profile = "debian"
- use_ipv6 = true
- `
- err = ioutil.WriteFile(filepath.Join(tmpDir, "debian.conf"), []byte(incBlob1), 0644)
- So(err, ShouldEqual, nil)
- err = ioutil.WriteFile(filepath.Join(tmpDir, "ubuntu.conf"), []byte(incBlob2), 0644)
- So(err, ShouldEqual, nil)
- cfg, err := LoadConfig(tmpfile.Name())
- So(err, ShouldBeNil)
- So(cfg.Global.Name, ShouldEqual, "test_worker")
- So(cfg.Global.Interval, ShouldEqual, 240)
- So(cfg.Global.Retry, ShouldEqual, 3)
- So(cfg.Global.MirrorDir, ShouldEqual, "/data/mirrors")
- So(cfg.Manager.APIBase, ShouldEqual, "https://127.0.0.1:5000")
- So(cfg.Server.Hostname, ShouldEqual, "worker1.example.com")
- m := cfg.Mirrors[0]
- So(m.Name, ShouldEqual, "AOSP")
- So(m.MirrorDir, ShouldEqual, "/data/git/AOSP")
- So(m.Provider, ShouldEqual, provCommand)
- So(m.Interval, ShouldEqual, 720)
- So(m.Retry, ShouldEqual, 2)
- So(m.Env["REPO"], ShouldEqual, "/usr/local/bin/aosp-repo")
- m = cfg.Mirrors[1]
- So(m.Name, ShouldEqual, "debian")
- So(m.MirrorDir, ShouldEqual, "")
- So(m.Provider, ShouldEqual, provTwoStageRsync)
- m = cfg.Mirrors[2]
- So(m.Name, ShouldEqual, "fedora")
- So(m.MirrorDir, ShouldEqual, "")
- So(m.Provider, ShouldEqual, provRsync)
- So(m.ExcludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
- m = cfg.Mirrors[3]
- So(m.Name, ShouldEqual, "debian-cd")
- So(m.MirrorDir, ShouldEqual, "")
- So(m.Provider, ShouldEqual, provTwoStageRsync)
- m = cfg.Mirrors[4]
- So(m.Name, ShouldEqual, "debian-security")
- m = cfg.Mirrors[5]
- So(m.Name, ShouldEqual, "ubuntu")
- So(len(cfg.Mirrors), ShouldEqual, 6)
- })
- Convey("Everything should work on nested config file", t, func() {
- tmpfile, err := ioutil.TempFile("", "tunasync")
- So(err, ShouldEqual, nil)
- defer os.Remove(tmpfile.Name())
- tmpDir, err := ioutil.TempDir("", "tunasync")
- So(err, ShouldBeNil)
- defer os.RemoveAll(tmpDir)
- incSection := fmt.Sprintf(
- "\n[include]\n"+
- "include_mirrors = \"%s/*.conf\"",
- tmpDir,
- )
- curCfgBlob := cfgBlob + incSection
- err = ioutil.WriteFile(tmpfile.Name(), []byte(curCfgBlob), 0644)
- So(err, ShouldEqual, nil)
- defer tmpfile.Close()
- incBlob1 := `
- [[mirrors]]
- name = "ipv6s"
- use_ipv6 = true
- [[mirrors.mirrors]]
- name = "debians"
- mirror_subdir = "debian"
- provider = "two-stage-rsync"
- stage1_profile = "debian"
- [[mirrors.mirrors.mirrors]]
- name = "debian-security"
- upstream = "rsync://test.host/debian-security/"
- [[mirrors.mirrors.mirrors]]
- name = "ubuntu"
- stage1_profile = "ubuntu"
- upstream = "rsync://test.host2/ubuntu/"
- [[mirrors.mirrors]]
- name = "debian-cd"
- provider = "rsync"
- upstream = "rsync://test.host3/debian-cd/"
- `
- err = ioutil.WriteFile(filepath.Join(tmpDir, "nest.conf"), []byte(incBlob1), 0644)
- So(err, ShouldEqual, nil)
- cfg, err := LoadConfig(tmpfile.Name())
- So(err, ShouldBeNil)
- So(cfg.Global.Name, ShouldEqual, "test_worker")
- So(cfg.Global.Interval, ShouldEqual, 240)
- So(cfg.Global.Retry, ShouldEqual, 3)
- So(cfg.Global.MirrorDir, ShouldEqual, "/data/mirrors")
- So(cfg.Manager.APIBase, ShouldEqual, "https://127.0.0.1:5000")
- So(cfg.Server.Hostname, ShouldEqual, "worker1.example.com")
- m := cfg.Mirrors[0]
- So(m.Name, ShouldEqual, "AOSP")
- So(m.MirrorDir, ShouldEqual, "/data/git/AOSP")
- So(m.Provider, ShouldEqual, provCommand)
- So(m.Interval, ShouldEqual, 720)
- So(m.Retry, ShouldEqual, 2)
- So(m.Env["REPO"], ShouldEqual, "/usr/local/bin/aosp-repo")
- m = cfg.Mirrors[1]
- So(m.Name, ShouldEqual, "debian")
- So(m.MirrorDir, ShouldEqual, "")
- So(m.Provider, ShouldEqual, provTwoStageRsync)
- m = cfg.Mirrors[2]
- So(m.Name, ShouldEqual, "fedora")
- So(m.MirrorDir, ShouldEqual, "")
- So(m.Provider, ShouldEqual, provRsync)
- So(m.ExcludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
- m = cfg.Mirrors[3]
- So(m.Name, ShouldEqual, "debian-security")
- So(m.MirrorDir, ShouldEqual, "")
- So(m.Provider, ShouldEqual, provTwoStageRsync)
- So(m.UseIPv6, ShouldEqual, true)
- So(m.Stage1Profile, ShouldEqual, "debian")
- m = cfg.Mirrors[4]
- So(m.Name, ShouldEqual, "ubuntu")
- So(m.MirrorDir, ShouldEqual, "")
- So(m.Provider, ShouldEqual, provTwoStageRsync)
- So(m.UseIPv6, ShouldEqual, true)
- So(m.Stage1Profile, ShouldEqual, "ubuntu")
- m = cfg.Mirrors[5]
- So(m.Name, ShouldEqual, "debian-cd")
- So(m.UseIPv6, ShouldEqual, true)
- So(m.Provider, ShouldEqual, provRsync)
- So(len(cfg.Mirrors), ShouldEqual, 6)
- })
- Convey("Providers can be inited from a valid config file", t, func() {
- tmpfile, err := ioutil.TempFile("", "tunasync")
- So(err, ShouldEqual, nil)
- defer os.Remove(tmpfile.Name())
- err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644)
- So(err, ShouldEqual, nil)
- defer tmpfile.Close()
- cfg, err := LoadConfig(tmpfile.Name())
- So(err, ShouldBeNil)
- providers := map[string]mirrorProvider{}
- for _, m := range cfg.Mirrors {
- p := newMirrorProvider(m, cfg)
- providers[p.Name()] = p
- }
- p := providers["AOSP"]
- So(p.Name(), ShouldEqual, "AOSP")
- So(p.LogDir(), ShouldEqual, "/var/log/tunasync/AOSP")
- So(p.LogFile(), ShouldEqual, "/var/log/tunasync/AOSP/latest.log")
- _, ok := p.(*cmdProvider)
- So(ok, ShouldBeTrue)
- for _, hook := range p.Hooks() {
- switch h := hook.(type) {
- case *execPostHook:
- So(h.command, ShouldResemble, []string{"bash", "-c", `echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status`})
- }
- }
- p = providers["debian"]
- So(p.Name(), ShouldEqual, "debian")
- So(p.LogDir(), ShouldEqual, "/var/log/tunasync/debian")
- So(p.LogFile(), ShouldEqual, "/var/log/tunasync/debian/latest.log")
- r2p, ok := p.(*twoStageRsyncProvider)
- So(ok, ShouldBeTrue)
- So(r2p.stage1Profile, ShouldEqual, "debian")
- So(r2p.WorkingDir(), ShouldEqual, "/data/mirrors/debian")
- p = providers["fedora"]
- So(p.Name(), ShouldEqual, "fedora")
- So(p.LogDir(), ShouldEqual, "/var/log/tunasync/fedora")
- So(p.LogFile(), ShouldEqual, "/var/log/tunasync/fedora/latest.log")
- rp, ok := p.(*rsyncProvider)
- So(ok, ShouldBeTrue)
- So(rp.WorkingDir(), ShouldEqual, "/data/mirrors/fedora")
- So(rp.excludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
- })
- Convey("MirrorSubdir should work", t, func() {
- tmpfile, err := ioutil.TempFile("", "tunasync")
- So(err, ShouldEqual, nil)
- defer os.Remove(tmpfile.Name())
- cfgBlob1 := `
- [global]
- name = "test_worker"
- log_dir = "/var/log/tunasync/{{.Name}}"
- mirror_dir = "/data/mirrors"
- concurrent = 10
- interval = 240
- retry = 3
- [manager]
- api_base = "https://127.0.0.1:5000"
- token = "some_token"
- [server]
- hostname = "worker1.example.com"
- listen_addr = "127.0.0.1"
- listen_port = 6000
- ssl_cert = "/etc/tunasync.d/worker1.cert"
- ssl_key = "/etc/tunasync.d/worker1.key"
- [[mirrors]]
- name = "ipv6s"
- use_ipv6 = true
- [[mirrors.mirrors]]
- name = "debians"
- mirror_subdir = "debian"
- provider = "two-stage-rsync"
- stage1_profile = "debian"
- [[mirrors.mirrors.mirrors]]
- name = "debian-security"
- upstream = "rsync://test.host/debian-security/"
- [[mirrors.mirrors.mirrors]]
- name = "ubuntu"
- stage1_profile = "ubuntu"
- upstream = "rsync://test.host2/ubuntu/"
- [[mirrors.mirrors]]
- name = "debian-cd"
- provider = "rsync"
- upstream = "rsync://test.host3/debian-cd/"
- `
- err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob1), 0644)
- So(err, ShouldEqual, nil)
- defer tmpfile.Close()
- cfg, err := LoadConfig(tmpfile.Name())
- So(err, ShouldBeNil)
- providers := map[string]mirrorProvider{}
- for _, m := range cfg.Mirrors {
- p := newMirrorProvider(m, cfg)
- providers[p.Name()] = p
- }
- p := providers["debian-security"]
- So(p.Name(), ShouldEqual, "debian-security")
- So(p.LogDir(), ShouldEqual, "/var/log/tunasync/debian-security")
- So(p.LogFile(), ShouldEqual, "/var/log/tunasync/debian-security/latest.log")
- r2p, ok := p.(*twoStageRsyncProvider)
- So(ok, ShouldBeTrue)
- So(r2p.stage1Profile, ShouldEqual, "debian")
- So(r2p.WorkingDir(), ShouldEqual, "/data/mirrors/debian/debian-security")
- p = providers["ubuntu"]
- So(p.Name(), ShouldEqual, "ubuntu")
- So(p.LogDir(), ShouldEqual, "/var/log/tunasync/ubuntu")
- So(p.LogFile(), ShouldEqual, "/var/log/tunasync/ubuntu/latest.log")
- r2p, ok = p.(*twoStageRsyncProvider)
- So(ok, ShouldBeTrue)
- So(r2p.stage1Profile, ShouldEqual, "ubuntu")
- So(r2p.WorkingDir(), ShouldEqual, "/data/mirrors/debian/ubuntu")
- p = providers["debian-cd"]
- So(p.Name(), ShouldEqual, "debian-cd")
- So(p.LogDir(), ShouldEqual, "/var/log/tunasync/debian-cd")
- So(p.LogFile(), ShouldEqual, "/var/log/tunasync/debian-cd/latest.log")
- rp, ok := p.(*rsyncProvider)
- So(ok, ShouldBeTrue)
- So(rp.WorkingDir(), ShouldEqual, "/data/mirrors/debian-cd")
- })
- }
|