2015-05-21 19:38:07 -05:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
2015-05-25 22:44:47 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"strings"
|
2015-05-21 19:38:07 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware/git"
|
|
|
|
"github.com/mholt/caddy/middleware/git/gittest"
|
|
|
|
)
|
|
|
|
|
|
|
|
// init sets the OS used to fakeOS
|
|
|
|
func init() {
|
|
|
|
git.SetOS(gittest.FakeOS)
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:44:47 -05:00
|
|
|
func check(t *testing.T, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no errors, but got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 19:38:07 -05:00
|
|
|
func TestGit(t *testing.T) {
|
|
|
|
c := newTestController(`git git@github.com:mholt/caddy.git`)
|
|
|
|
|
|
|
|
mid, err := Git(c)
|
2015-05-25 22:44:47 -05:00
|
|
|
check(t, err)
|
2015-05-21 19:38:07 -05:00
|
|
|
if mid != nil {
|
|
|
|
t.Fatal("Git middleware is a background service and expected to be nil.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:44:47 -05:00
|
|
|
func TestIntervals(t *testing.T) {
|
|
|
|
tests := []string{
|
|
|
|
`git git@github.com:user/repo { interval 10 }`,
|
|
|
|
`git git@github.com:user/repo { interval 5 }`,
|
|
|
|
`git git@github.com:user/repo { interval 2 }`,
|
|
|
|
`git git@github.com:user/repo { interval 1 }`,
|
|
|
|
`git git@github.com: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
|
2015-05-28 02:21:10 -05:00
|
|
|
gittest.Sleep(time.Millisecond * 100)
|
2015-05-25 22:44:47 -05:00
|
|
|
|
|
|
|
// switch logger to test file
|
|
|
|
logFile := gittest.Open("file")
|
|
|
|
git.Logger = log.New(logFile, "", 0)
|
|
|
|
|
|
|
|
// sleep for the interval
|
2015-05-28 02:21:10 -05:00
|
|
|
gittest.Sleep(repo.Interval)
|
2015-05-25 22:44:47 -05:00
|
|
|
|
|
|
|
// 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
|
2015-05-26 14:18:19 -05:00
|
|
|
git.Services.Stop(repo.URL, 1)
|
2015-05-25 22:44:47 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-21 19:38:07 -05:00
|
|
|
func TestGitParse(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
shouldErr bool
|
|
|
|
expected *git.Repo
|
|
|
|
}{
|
|
|
|
{`git git@github.com:user/repo`, false, &git.Repo{
|
2015-05-24 21:52:34 -05:00
|
|
|
URL: "https://github.com/user/repo.git",
|
2015-05-21 19:38:07 -05:00
|
|
|
}},
|
|
|
|
{`git github.com/user/repo`, false, &git.Repo{
|
2015-05-24 21:52:34 -05:00
|
|
|
URL: "https://github.com/user/repo.git",
|
2015-05-21 19:38:07 -05:00
|
|
|
}},
|
|
|
|
{`git git@github.com/user/repo`, true, nil},
|
|
|
|
{`git http://github.com/user/repo`, false, &git.Repo{
|
2015-05-24 21:52:34 -05:00
|
|
|
URL: "https://github.com/user/repo.git",
|
2015-05-21 19:38:07 -05:00
|
|
|
}},
|
|
|
|
{`git https://github.com/user/repo`, false, &git.Repo{
|
2015-05-24 21:52:34 -05:00
|
|
|
URL: "https://github.com/user/repo.git",
|
2015-05-21 19:38:07 -05:00
|
|
|
}},
|
|
|
|
{`git http://github.com/user/repo {
|
|
|
|
key ~/.key
|
|
|
|
}`, false, &git.Repo{
|
|
|
|
KeyPath: "~/.key",
|
2015-05-24 21:52:34 -05:00
|
|
|
URL: "git@github.com:user/repo.git",
|
2015-05-21 19:38:07 -05:00
|
|
|
}},
|
|
|
|
{`git git@github.com:user/repo {
|
|
|
|
key ~/.key
|
|
|
|
}`, false, &git.Repo{
|
|
|
|
KeyPath: "~/.key",
|
2015-05-24 21:52:34 -05:00
|
|
|
URL: "git@github.com:user/repo.git",
|
2015-05-21 19:38:07 -05:00
|
|
|
}},
|
|
|
|
{`git `, true, nil},
|
|
|
|
{`git {
|
|
|
|
}`, true, nil},
|
|
|
|
{`git {
|
|
|
|
repo git@github.com:user/repo.git`, true, nil},
|
|
|
|
{`git {
|
|
|
|
repo git@github.com:user/repo
|
|
|
|
key ~/.key
|
|
|
|
}`, false, &git.Repo{
|
|
|
|
KeyPath: "~/.key",
|
2015-05-24 21:52:34 -05:00
|
|
|
URL: "git@github.com:user/repo.git",
|
2015-05-21 19:38:07 -05:00
|
|
|
}},
|
|
|
|
{`git {
|
|
|
|
repo git@github.com:user/repo
|
|
|
|
key ~/.key
|
|
|
|
interval 600
|
|
|
|
}`, false, &git.Repo{
|
|
|
|
KeyPath: "~/.key",
|
2015-05-24 21:52:34 -05:00
|
|
|
URL: "git@github.com:user/repo.git",
|
2015-05-21 19:38:07 -05:00
|
|
|
Interval: time.Second * 600,
|
|
|
|
}},
|
|
|
|
{`git {
|
|
|
|
repo git@github.com:user/repo
|
|
|
|
branch dev
|
|
|
|
}`, false, &git.Repo{
|
|
|
|
Branch: "dev",
|
2015-05-24 21:52:34 -05:00
|
|
|
URL: "https://github.com/user/repo.git",
|
2015-05-21 19:38:07 -05:00
|
|
|
}},
|
|
|
|
{`git {
|
|
|
|
key ~/.key
|
|
|
|
}`, true, nil},
|
|
|
|
{`git {
|
|
|
|
repo git@github.com:user/repo
|
|
|
|
key ~/.key
|
|
|
|
then echo hello world
|
|
|
|
}`, false, &git.Repo{
|
|
|
|
KeyPath: "~/.key",
|
2015-05-24 21:52:34 -05:00
|
|
|
URL: "git@github.com:user/repo.git",
|
2015-05-21 19:38:07 -05:00
|
|
|
Then: "echo hello world",
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
c := newTestController(test.input)
|
|
|
|
repo, err := gitParse(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
|
|
|
|
}
|
|
|
|
if !reposEqual(test.expected, repo) {
|
|
|
|
t.Errorf("Test %v expects %v but found %v", i, test.expected, repo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func reposEqual(expected, repo *git.Repo) bool {
|
|
|
|
if expected == nil {
|
|
|
|
return repo == nil
|
|
|
|
}
|
|
|
|
if expected.Branch != "" && expected.Branch != repo.Branch {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if expected.Host != "" && expected.Host != repo.Host {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if expected.Interval != 0 && expected.Interval != repo.Interval {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if expected.KeyPath != "" && expected.KeyPath != repo.KeyPath {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if expected.Path != "" && expected.Path != repo.Path {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if expected.Then != "" && expected.Then != repo.Then {
|
|
|
|
return false
|
|
|
|
}
|
2015-05-24 21:52:34 -05:00
|
|
|
if expected.URL != "" && expected.URL != repo.URL {
|
2015-05-21 19:38:07 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|