Exemplo n.º 1
0
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 {
		SetLogger(gittest.NewLogger(gittest.Open("file")))
		c1 := setup.NewTestController(test)
		git, err := parse(c1)
		check(t, err)
		repo := git.Repo(0)

		c2 := setup.NewTestController(test)
		_, err = Setup(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")
		SetLogger(gittest.NewLogger(logFile))

		// 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
		Services.Stop(repo.URL, 1)

	}

}
Exemplo n.º 2
0
func TestGitSetup(t *testing.T) {
	c := setup.NewTestController(`git [email protected]:mholt/caddy.git`)

	mid, err := Setup(c)
	check(t, err)
	if mid != nil {
		t.Fatal("Git middleware is a background service and expected to be nil.")
	}
}
Exemplo n.º 3
0
func TestGitParse(t *testing.T) {
	tests := []struct {
		input     string
		shouldErr bool
		expected  *Repo
	}{
		{`git [email protected]:user/repo`, false, &Repo{
			URL: "https://github.com/user/repo.git",
		}},
		{`git github.com/user/repo`, false, &Repo{
			URL: "https://github.com/user/repo.git",
		}},
		{`git [email protected]/user/repo`, true, nil},
		{`git http://github.com/user/repo`, false, &Repo{
			URL: "https://github.com/user/repo.git",
		}},
		{`git https://github.com/user/repo`, false, &Repo{
			URL: "https://github.com/user/repo.git",
		}},
		{`git http://github.com/user/repo {
			key ~/.key
		}`, false, &Repo{
			KeyPath: "~/.key",
			URL:     "[email protected]:user/repo.git",
		}},
		{`git [email protected]:user/repo {
			key ~/.key
		}`, false, &Repo{
			KeyPath: "~/.key",
			URL:     "[email protected]:user/repo.git",
		}},
		{`git `, true, nil},
		{`git {
		}`, true, nil},
		{`git {
		repo [email protected]:user/repo.git`, true, nil},
		{`git {
		repo [email protected]:user/repo
		key ~/.key
		}`, false, &Repo{
			KeyPath: "~/.key",
			URL:     "[email protected]:user/repo.git",
		}},
		{`git {
		repo [email protected]:user/repo
		key ~/.key
		interval 600
		}`, false, &Repo{
			KeyPath:  "~/.key",
			URL:      "[email protected]:user/repo.git",
			Interval: time.Second * 600,
		}},
		{`git {
		repo [email protected]:user/repo
		branch dev
		}`, false, &Repo{
			Branch: "dev",
			URL:    "https://github.com/user/repo.git",
		}},
		{`git {
		key ~/.key
		}`, true, nil},
		{`git {
		repo [email protected]:user/repo
		key ~/.key
		then echo hello world
		}`, false, &Repo{
			KeyPath: "~/.key",
			URL:     "[email protected]:user/repo.git",
			Then:    "echo hello world",
		}},
	}

	for i, test := range tests {
		c := setup.NewTestController(test.input)
		git, err := parse(c)
		if !test.shouldErr && err != nil {
			t.Errorf("Test %v should not error but found %v", i, err)
			continue
		}
		if test.shouldErr && err == nil {
			t.Errorf("Test %v should error but found nil", i)
			continue
		}
		repo := git.Repo(0)
		if !reposEqual(test.expected, repo) {
			t.Errorf("Test %v expects %v but found %v", i, test.expected, repo)
		}
	}
}