-
Notifications
You must be signed in to change notification settings - Fork 2
/
job_test.go
174 lines (156 loc) · 5.03 KB
/
job_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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"time"
"io/ioutil"
"os"
"path"
)
func TestPathMatching(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(dir, "0 30 * * * * job name .godoit")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, "0 30 * * * *", job.Spec)
assert.Equal(t, time.UTC, job.Timezone)
assert.Equal(t, 0 * time.Second, job.Timeout)
assert.Equal(t, "job name", job.Name)
assert.Equal(t, dir + "/0 30 * * * * job name .godoit", job.Filepath)
assert.Equal(t, true, job.Enabled)
})
}
func TestWithCronSpecInFile(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(dir, "job name.godoit","#:godoit cronspec 0 30 * * * *")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, "0 30 * * * *", job.Spec)
assert.Equal(t, time.UTC, job.Timezone)
assert.Equal(t, 0 * time.Second, job.Timeout)
assert.Equal(t, "job name", job.Name)
assert.Equal(t, dir + "/job name.godoit", job.Filepath)
assert.Equal(t, true, job.Enabled)
})
}
func TestWithParamsInFile(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(
dir,
"job name.godoit",
"#:godoit cronspec 0 30 * * * *",
"#:godoit timeout 1h30m",
"#:godoit timezone Europe/London")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, "0 30 * * * *", job.Spec)
assert.Equal(t, "Europe/London", job.Timezone.String())
assert.Equal(t, 90 * time.Minute, job.Timeout)
assert.Equal(t, "job name", job.Name)
assert.Equal(t, dir + "/job name.godoit", job.Filepath)
assert.Equal(t, true, job.Enabled)
})
}
func TestPathMatchingWithRepalce(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(dir, "0 0%5 x x x x job name.godoit")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, "0 0/5 * * * *", job.Spec)
assert.Equal(t, "job name", job.Name)
assert.Equal(t, dir + "/0 0%5 x x x x job name.godoit", job.Filepath)
assert.Equal(t, true, job.Enabled)
})
}
func TestPathMatching2(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(dir, "* * * * * * TestScanRemoveJob.godoit")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, true, job.Enabled)
})
}
func TestPathAll(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(dir, "* * * * * * job name.godoit")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, true, job.Enabled)
})
}
func TestHashCommentedOutJobShouldBeDisabled(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(dir, "#0 30 * * * * job name.godoit")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, "0 30 * * * *", job.Spec)
assert.Equal(t, time.UTC, job.Timezone)
assert.Equal(t, 0 * time.Second, job.Timeout)
assert.Equal(t, "job name", job.Name)
assert.Equal(t, dir + "/#0 30 * * * * job name.godoit", job.Filepath)
assert.Equal(t, false, job.Enabled)
})
}
func TestCommentedOutJobShouldBeDisabled(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(dir, "--0 30 * * * * job name.godoit")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, "job name", job.Name)
assert.Equal(t, dir + "/--0 30 * * * * job name.godoit", job.Filepath)
assert.Equal(t, false, job.Enabled)
})
}
func TestIncompleteTaskShouldBeNil(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(dir, "0 30 * * *.godoit")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, false, job.Enabled)
assert.Equal(t, "Missing cronspec", job.Errors[0])
})
}
func TestWithoutNameShouldBeNil(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(dir, "0 30 * * * *.godoit")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, "Missing cronspec", job.Errors[0])
})
}
func TestInvalidTParams(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(
dir,
"test.godoit",
"#:godoit timeout ghgh",
"#:godoit timezone ohh",
"#:godoit cronspec ahh")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, "Invalid timeout: 'ghgh'", job.Errors[0])
assert.Equal(t, "Invalid timezone: 'ohh'", job.Errors[1])
assert.Equal(t, "Invalid cronspec: 'ahh'", job.Errors[2])
})
}
func TestDuplicateCronSpec(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(
dir,
"0 30 * * * * test.godoit",
"#:godoit cronspec 2 45 * * * *")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, "Cronspec in filename and as comment", job.Errors[0])
})
}
func TestUnknownParam(t *testing.T) {
withDir(func(dir string) {
job := createTestJob(dir, "0 30 * * * * test.godoit","#:godoit blahh")
assert.NotNil(t, job, "Failed to parse job")
assert.Equal(t, "Invalid parameter 'blahh'", job.Errors[0])
})
}
type withDirFunc func(dir string)
func withDir(aFunc withDirFunc) {
dir, _ := ioutil.TempDir("", "")
defer os.RemoveAll(dir)
aFunc(dir)
}
func createTestJob(dir string, file string, lines ...string) *Job {
f, _ := os.Create(path.Join(dir, file))
for _, line := range lines {
f.WriteString(line)
f.WriteString("\n")
}
f.Close()
return ParseJobFile(dir, file)
}