-
Notifications
You must be signed in to change notification settings - Fork 43
/
setup_test.go
250 lines (230 loc) · 5.81 KB
/
setup_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package git
import (
"fmt"
"io/ioutil"
"strings"
"testing"
"time"
"github.com/abiosoft/caddy-git/gittest"
"github.com/caddyserver/caddy"
)
// init sets the OS used to fakeOS
func init() {
SetOS(gittest.FakeOS)
}
func TestGitSetup(t *testing.T) {
c := caddy.NewTestController("http", `git ssh://[email protected]:mholt/caddy.git`)
err := setup(c)
check(t, err)
}
func TestGitParse(t *testing.T) {
tests := []struct {
input string
shouldErr bool
expected *Repo
}{
{`git [email protected]:user/repo {
args --depth 1
key ~/.key
}`, false, &Repo{
URL: "ssh://[email protected]:user/repo",
CloneArgs: []string{"--depth", "1"},
}},
{`git user:[email protected]/user/repo.git {
args --depth 1
}`, false, &Repo{
URL: "https://user:[email protected]/user/repo.git",
CloneArgs: []string{"--depth", "1"},
}},
{`git {
repo ssh://[email protected]:user/repo
key ~/.key
}`, false, &Repo{
KeyPath: "~/.key",
URL: "ssh://[email protected]:user/repo",
}},
{`git {
repo ssh://[email protected]:user/repo.git
key ~/.key
interval 600
}`, false, &Repo{
KeyPath: "~/.key",
URL: "ssh://[email protected]:user/repo.git",
Interval: time.Second * 600,
}},
{`git {
key ~/.key
}`, true, nil},
{`git {
repo ssh://[email protected]:user/repo
key ~/.key
then echo hello world
}`, false, &Repo{
KeyPath: "~/.key",
URL: "ssh://[email protected]:user/repo",
Then: []Then{NewThen("echo", "hello world")},
}},
{`git https://[email protected]/user/repo.git`, false, &Repo{
URL: "https://[email protected]/user/repo.git",
}},
{`git ssh://[email protected]:user/repo.git {
key ~/.key
}`, false, &Repo{
KeyPath: "~/.key",
URL: "ssh://[email protected]:user/repo.git",
}},
{`git ssh://[email protected]:user/repo.git {
key ~/.key
}`, false, &Repo{
KeyPath: "~/.key",
URL: "ssh://[email protected]:user/repo.git",
}},
{`git ssh://[email protected]:2222/user/repo.git {
key ~/.key
}`, false, &Repo{
KeyPath: "~/.key",
URL: "ssh://[email protected]:2222/user/repo.git",
}},
{`git ssh://[email protected]:2222/user/repo.git {
key ~/.key
hook_type gogs
}`, false, &Repo{
KeyPath: "~/.key",
URL: "ssh://[email protected]:2222/user/repo.git",
Hook: HookConfig{
Type: "gogs",
},
}},
{`git ssh://[email protected]:2222/user/repo.git {
key ~/.key
hook /webhook
hook_type gogs
}`, false, &Repo{
KeyPath: "~/.key",
URL: "ssh://[email protected]:2222/user/repo.git",
Hook: HookConfig{
URL: "/webhook",
Type: "gogs",
},
}},
{`git ssh://[email protected]:2222/user/repo.git {
key ~/.key
hook /webhook some-secrets
hook_type gogs
}`, false, &Repo{
KeyPath: "~/.key",
URL: "ssh://[email protected]:2222/user/repo.git",
Hook: HookConfig{
URL: "/webhook",
Secret: "some-secrets",
Type: "gogs",
},
}},
}
for i, test := range tests {
c := caddy.NewTestController("http", 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)
}
}
}
func TestIntervals(t *testing.T) {
tests := []string{
`git user:[email protected]/user/repo.git { interval 10 }`,
`git user:[email protected]/user/repo.git { interval 1 }`,
}
for i, test := range tests {
SetLogger(gittest.NewLogger(gittest.Open("file")))
c1 := caddy.NewTestController("http", test)
git, err := parse(c1)
check(t, err)
repo := git.Repo(0)
c2 := caddy.NewTestController("http", test)
err = setup(c2)
check(t, err)
// start startup services
err = func() error {
// Start service routine in background
Start(repo)
// Do a pull right away to return error
return repo.Pull()
}()
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://[email protected]/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(string(repo.URL), 1)
}
}
func reposEqual(expected, repo *Repo) bool {
thenStr := func(then []Then) string {
var str []string
for _, t := range then {
str = append(str, t.Command())
}
return fmt.Sprint(str)
}
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 != nil && thenStr(expected.Then) != thenStr(repo.Then) {
return false
}
if expected.URL != "" && expected.URL != repo.URL {
return false
}
if fmt.Sprint(expected.Hook) != fmt.Sprint(repo.Hook) {
return false
}
if fmt.Sprint(expected.CloneArgs) != fmt.Sprint(repo.CloneArgs) {
return false
}
return true
}