func TestIntervals(t *testing.T) { tests := []string{ `git [email protected]:user/repo { interval 10 }`, `git [email protected]:user/repo { interval 5 }`, `git [email protected]:user/repo { interval 2 }`, `git [email protected]:user/repo { interval 1 }`, `git [email protected]:user/repo { interval 6 }`, } for i, test := range tests { git.Logger = nil c1 := newTestController(test) repo, err := gitParse(c1) check(t, err) c2 := newTestController(test) _, err = Git(c2) check(t, err) // start startup services err = c2.Startup[0]() check(t, err) // wait for first background pull gittest.Sleep(time.Millisecond * 100) // switch logger to test file logFile := gittest.Open("file") git.Logger = log.New(logFile, "", 0) // sleep for the interval gittest.Sleep(repo.Interval) // get log output out, err := ioutil.ReadAll(logFile) check(t, err) // if greater than minimum interval if repo.Interval >= time.Second*5 { expected := `https://github.com/user/repo.git pulled. No new changes.` // ensure pull is done by tracing the output if expected != strings.TrimSpace(string(out)) { t.Errorf("Test %v: Expected %v found %v", i, expected, string(out)) } } else { // ensure pull is ignored by confirming no output if string(out) != "" { t.Errorf("Test %v: Expected no output but found %v", i, string(out)) } } // stop background thread monitor git.Services.Stop(repo.URL, 1) } }
func TestGit(t *testing.T) { // prepare repos := []*Repo{ nil, &Repo{Path: "gitdir", URL: "success.git"}, } for _, r := range repos { repo := createRepo(r) err := repo.Prepare() check(t, err) } // pull with success logFile := gittest.Open("file") SetLogger(log.New(logFile, "", 0)) tests := []struct { repo *Repo output string }{ { &Repo{Path: "gitdir", URL: "[email protected]:user/repo.git", KeyPath: "~/.key", Then: "echo Hello"}, `[email protected]:user/repo.git pulled. Command echo Hello successful. `, }, { &Repo{Path: "gitdir", URL: "https://github.com/user/repo.git", Then: "echo Hello"}, `https://github.com/user/repo.git pulled. Command echo Hello successful. `, }, { &Repo{URL: "[email protected]:user/repo"}, `[email protected]:user/repo pulled. `, }, } for i, test := range tests { gittest.CmdOutput = test.repo.URL test.repo = createRepo(test.repo) err := test.repo.Prepare() check(t, err) err = test.repo.Pull() check(t, err) out, err := ioutil.ReadAll(logFile) check(t, err) if test.output != string(out) { t.Errorf("Pull with Success %v: Expected %v found %v", i, test.output, string(out)) } } // pull with error repos = []*Repo{ &Repo{Path: "gitdir", URL: "http://github.com:u/repo.git"}, &Repo{Path: "gitdir", URL: "https://github.com/user/repo.git", Then: "echo Hello"}, &Repo{Path: "gitdir"}, &Repo{Path: "gitdir", KeyPath: ".key"}, } gittest.CmdOutput = "[email protected]:u1/repo.git" for i, repo := range repos { repo = createRepo(repo) err := repo.Prepare() if err == nil { t.Errorf("Pull with Error %v: Error expected but not found %v", i, err) continue } expected := "another git repo '[email protected]:u1/repo.git' exists at gitdir" if expected != err.Error() { t.Errorf("Pull with Error %v: Expected %v found %v", i, expected, err.Error()) } } // timeout checks timeoutTests := []struct { repo *Repo shouldPull bool }{ {&Repo{Interval: time.Millisecond * 4900}, false}, {&Repo{Interval: time.Millisecond * 1}, false}, {&Repo{Interval: time.Second * 5}, true}, {&Repo{Interval: time.Second * 10}, true}, } for i, r := range timeoutTests { r.repo = createRepo(r.repo) err := r.repo.Prepare() check(t, err) err = r.repo.Pull() check(t, err) before := r.repo.lastPull gittest.Sleep(r.repo.Interval) err = r.repo.Pull() after := r.repo.lastPull check(t, err) expected := after.After(before) if expected != r.shouldPull { t.Errorf("Pull with Error %v: Expected %v found %v", i, expected, r.shouldPull) } } }