-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjobset_test.go
More file actions
262 lines (226 loc) · 8.07 KB
/
jobset_test.go
File metadata and controls
262 lines (226 loc) · 8.07 KB
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
251
252
253
254
255
256
257
258
259
260
package main
import (
"testing"
"log"
"github.com/stretchr/testify/assert"
"os"
"io/ioutil"
"path"
"time"
"sync"
)
var executions = make(map [string]int)
var lock sync.RWMutex
var executor = func(name, path string, timeout time.Duration) {
lock.Lock()
defer lock.Unlock()
log.Printf("Executing %s: %s", name, path)
if _,ok := executions[name]; ok {
executions[name] = executions[name]+1
} else {
executions[name] = 1
}
}
func TestScanEmptyDir(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Scan an empty directory
assertRescanUpdates(t, jobSet, false)
assertRescanUpdates(t, jobSet, false)
})
}
func TestScanDirWithCommentedOutFile(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "--* * * * * * TestScanDirWithCommentedOutFile.godoit")
assertRescanUpdates(t, jobSet, true)
time.Sleep(time.Second * 3)
assertRescanUpdates(t, jobSet, false)
assertNoExecutions(t, "TestScanDirWithCommentedOutFile")
})
}
func TestScanDirWithCommentedOutFile2(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "#* * * * * * TestScanDirWithCommentedOutFile2.godoit")
assertRescanUpdates(t, jobSet, true)
time.Sleep(time.Second * 3)
assertRescanUpdates(t, jobSet, false)
assertNoExecutions(t, "TestScanDirWithCommentedOutFile2")
})
}
func TestScanCreate1SecondJob(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "* * * * * * TestScanCreate1SecondJob.godoit")
assertRescanUpdates(t, jobSet, true)
assertJobCount(t, jobSet, 1)
time.Sleep(time.Second * 6)
assertRescanUpdates(t, jobSet, false)
assertExecutions(t, "TestScanCreate1SecondJob", 5)
})
}
func TestScanCreateJobWithTimeZone(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "TestScanCreateJobWithTimeZone.godoit","#:godoit cronspec * * * * *","#:godoit timezone Europe/Paris")
assertRescanUpdates(t, jobSet, true)
time.Sleep(time.Second * 6)
assertRescanUpdates(t, jobSet, false)
assertExecutions(t, "TestScanCreateJobWithTimeZone", 5)
})
}
func TestScanCreateJobWithTimeZoneAndUTCJob(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "aaTestScanCreateJobWithTimeZone_utc.godoit","#:godoit cronspec * * * * *")
createJob(jobSet, "zzTestScanCreateJobWithTimeZone.godoit","#:godoit cronspec * * * * *","#:godoit timezone Europe/Paris")
createJob(jobSet, "bbTestScanCreateJobWithTimeZone_utc.godoit","#:godoit cronspec * * * * *")
createJob(jobSet, "yyTestScanCreateJobWithTimeZone.godoit","#:godoit cronspec * * * * *","#:godoit timezone Europe/Paris")
assertRescanUpdates(t, jobSet, true)
time.Sleep(time.Second * 6)
assertRescanUpdates(t, jobSet, false)
assertExecutions(t, "zzTestScanCreateJobWithTimeZone", 5)
assertExecutions(t, "aaTestScanCreateJobWithTimeZone_utc", 5)
assertExecutions(t, "yyTestScanCreateJobWithTimeZone", 5)
assertExecutions(t, "bbTestScanCreateJobWithTimeZone_utc", 5)
})
}
func TestScanCreateJobWithTimeout(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "TestScanCreateJobWithTimeout.godoit","#:godoit cronspec * * * * *","#:godoit timeout 3s")
assertRescanUpdates(t, jobSet, true)
time.Sleep(time.Second * 6)
assertRescanUpdates(t, jobSet, false)
assertExecutions(t, "TestScanCreateJobWithTimeout", 5)
})
}
func TestScanCreateJobWithSpecInFile(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "TestScanCreateJobWithSpecInFile.godoit","#:godoit cronspec * * * * * *")
assertRescanUpdates(t, jobSet, true)
assertJobCount(t, jobSet, 1)
time.Sleep(time.Second * 6)
assertRescanUpdates(t, jobSet, false)
assertExecutions(t, "TestScanCreateJobWithSpecInFile", 5)
})
}
func TestReScanJobWithUpdatedSpecInFile(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "TestReScanJobWithUpdatedSpecInFile.godoit","#:godoit cronspec 0 0 12 * * *")
assertRescanUpdates(t, jobSet, true)
assertJobCount(t, jobSet, 1)
time.Sleep(time.Second * 3)
// Should generate no executions - or 1 if we run right on 12:00!
assertRescanUpdates(t, jobSet, false)
// Now update the file
createJob(jobSet, "TestReScanJobWithUpdatedSpecInFile.godoit","#:godoit cronspec * * * * * *")
assertRescanUpdates(t, jobSet, true)
time.Sleep(time.Second * 6)
assertExecutions(t, "TestReScanJobWithUpdatedSpecInFile", 6)
})
}
func TestScanCreateJobWithInvalidSpecInFile(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "TestScanCreateJobWithInvalidSpecInFile.godoit","#:godoit cronspec * * * *")
assertRescanUpdates(t, jobSet, true)
time.Sleep(time.Second * 3)
assertRescanUpdates(t, jobSet, false)
assertNoExecutions(t, "TestScanCreateJobWithInvalidSpecInFile")
})
}
func TestScanRemoveJob(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
createJob(jobSet, "* * * * * * TestScanRemoveJob.godoit")
assertRescanUpdates(t, jobSet, true)
assertJobCount(t, jobSet, 1)
time.Sleep(time.Second * 2)
removeJob(t, jobSet, "* * * * * * TestScanRemoveJob.godoit")
assertRescanUpdates(t, jobSet, true)
assertJobCount(t, jobSet, 0)
time.Sleep(time.Second * 4)
assertExecutions(t, "TestScanRemoveJob", 1)
})
}
func TestScanSwapJobs(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
createJob(jobSet, "* * * * * * TestScanSwapJobs.godoit")
createJob(jobSet, "* * * * * * TestScanSwapJobsX.godoit")
assertRescanUpdates(t, jobSet, true)
jobSet.printJobs()
assertJobCount(t, jobSet, 2)
time.Sleep(time.Second * 2)
createJob(jobSet, "* * * * * * TestScanSwapJobs_Other.godoit")
removeJob(t, jobSet, "* * * * * * TestScanSwapJobs.godoit")
assertRescanUpdates(t, jobSet, true)
jobSet.printJobs()
assertJobCount(t, jobSet, 2)
time.Sleep(time.Second * 5)
assertExecutions(t, "TestScanSwapJobs", 1)
assertExecutions(t, "TestScanSwapJobsX", 5)
assertExecutions(t, "TestScanSwapJobs_Other", 3)
})
}
func TestStatusScript(t *testing.T) {
withJobSet(func(jobSet1 *JobSet) {
createJob(jobSet1, "0 1 * * * * Job 1.godoit")
createJob(jobSet1, "0 2 * * * * Job 2.godoit", "#:godoit timeout 10s", "#:godoit timezone Europe/London")
createJob(jobSet1, "broken job.godoit", "#:godoit wrongparam")
jobSet1.Scan()
jobSetsMap := map[string]*JobSet{
"test_set": jobSet1,
}
statusFunc := StatusReporterFromScript("./test_status.sh", []string{"PATH"}, os.Stdout)
statusFunc(jobSetsMap)
})
}
type withJobSetFunc func(jobSet *JobSet)
func withJobSet(aFunc withJobSetFunc) {
dir,_ := ioutil.TempDir("", "")
defer os.RemoveAll(dir)
jobSet := NewJobSet(executor, dir)
defer jobSet.Stop()
println(dir)
aFunc(jobSet)
}
func createJob(jobSet *JobSet, script string, lines ...string) {
f,_ := os.Create(path.Join(jobSet.directory,script))
for _,line := range lines {
f.WriteString(line)
f.WriteString("\n")
}
f.Close()
}
func removeJob(t *testing.T, jobSet *JobSet, script string) {
if err := os.Remove(path.Join(jobSet.directory,script)); err != nil {
t.Fatalf("Failed to remove job %s, error %s", script, err)
}
}
func assertRescanUpdates(t *testing.T, jobSet *JobSet, expectUpdate bool) {
r:= jobSet.Scan();
assert.Equal(t,expectUpdate, r)
}
func assertJobCount(t *testing.T, jobSet *JobSet, expectedJobs int) {
assert.Equal(t,expectedJobs, len(jobSet.jobs))
}
func assertExecutions(t *testing.T, name string, minCount int) {
lock.RLock()
defer lock.RUnlock()
if actualCount,ok := executions[name]; ok {
assert.True(
t,actualCount >= minCount,
"Number of executions should be at least %n but was %n", minCount, actualCount)
} else {
assert.Fail(t,"No executions found for job %s", name)
}
}
func assertNoExecutions(t *testing.T, name string) {
lock.RLock()
defer lock.RUnlock()
if actualCount,ok := executions[name]; ok {
assert.Fail(t, "Expected no executions of %s but found %n", name, actualCount)
}
}