-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_cronner.py
216 lines (193 loc) · 7.39 KB
/
test_cronner.py
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
from cronner.cronner import Cronner
import contextlib
import os
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import sys
import unittest
import yaml
class TestCronner(unittest.TestCase):
@contextlib.contextmanager
def captureOutput(self, assert_stdout=None, assert_stderr=None):
out, err = sys.stdout, sys.stderr
sys.stdout, sys.stderr = StringIO(), StringIO()
try:
yield
finally:
sys.stdout.seek(0), sys.stderr.seek(0)
actual_stdout, actual_stderr = sys.stdout.read(), sys.stderr.read()
sys.stdout, sys.stderr = out, err
if assert_stdout is not None:
self.assertEqual(actual_stdout, assert_stdout)
if assert_stderr is not None:
self.assertEqual(actual_stderr, assert_stderr)
def test_run(self):
state = {}
cronner = Cronner()
@cronner.register('* * * * *')
def fn():
state['a'] = 1
cronner.run('{}.{}'.format(fn.__module__, fn.__name__))
self.assertEqual(state, {'a': 1})
def test_run_with_args(self):
state = {}
cronner = Cronner()
@cronner.register('* * * * *')
def fn(a, b):
state.update(a=a, b=b)
cronner.run('{}.{}'.format(fn.__module__, fn.__name__), 2, 3)
self.assertEqual(state, {'a': 2, 'b': 3})
def test_crontab_single(self):
cronner = Cronner()
@cronner.register('* * * * *')
def fn():
pass
line = cronner.get_entries()
self.assertEqual(
line.split(),
['*', '*', '*', '*', '*', sys.executable, os.path.abspath(sys.argv[0]), 'run', '{}.{}'.format(fn.__module__, fn.__name__)]
)
def test_crontab_multiple(self):
cronner = Cronner()
@cronner.register('* * * * *')
def fn():
pass
@cronner.register('* * * * *')
def gn():
pass
lines = cronner.get_entries().split('\n')
self.assertEqual(
sorted(line.split() for line in lines),
sorted([
['*', '*', '*', '*', '*', sys.executable, os.path.abspath(sys.argv[0]), 'run', '{}.{}'.format(fn.__module__, fn.__name__)],
['*', '*', '*', '*', '*', sys.executable, os.path.abspath(sys.argv[0]), 'run', '{}.{}'.format(gn.__module__, gn.__name__)]
])
)
def test_custom_serializer(self):
cronner = Cronner()
cronner.configure(serializer=lambda _: 'custom_template')
@cronner.register('* * * * *')
def fn():
pass
line = cronner.get_entries()
self.assertEqual(line, 'custom_template')
def test_template_vars(self):
cronner = Cronner()
cronner.configure(serializer=lambda es: '\n'.join(e['var'] for e in es))
@cronner.register('* * * * *', template_vars={'var': 'template_var'})
def fn():
pass
line = cronner.get_entries()
self.assertEqual(line, 'template_var')
def test_custom_template(self):
template = '\n'.join(['concurrencyPolicy: Allow',
'namespace: test',
'restartPolicy: Never',
'image: 000000000000.dkr.ecr.us-east-1.amazonaws.com/non-existent-image',
'labelKey: kronjob/job.test-template',
'memoryRequest: 4096Mi',
'memoryLimit: 6144Mi',
'nodeSelector:',
' group: jobs',
'env:',
' - name: ENV',
' value: test'
])
template_yaml = yaml.load(template, Loader=yaml.FullLoader)
cronner = Cronner()
cronner.configure(kronjob_template=template)
@cronner.register('* * * * *')
def fn():
pass
spec = yaml.load(cronner.get_entries(), Loader=yaml.FullLoader)
self.assertListEqual([spec[k] for k in sorted(template_yaml.keys())], [template_yaml[k] for k in sorted(template_yaml.keys())])
self.assertEqual(spec['jobs'][0]['schedule'], '* * * * *')
self.assertEqual(spec['jobs'][0]['name'], '{}.{}'.format(fn.__module__, fn.__name__).lower().replace('_', '-').strip('-'))
self.assertEqual(spec['jobs'][0]['command'], [sys.executable, os.path.abspath(sys.argv[0]), 'run', '{}.{}'.format(fn.__module__, fn.__name__)])
def test_template_vars_custom_template(self):
template = '\n'.join(['concurrencyPolicy: Allow',
'namespace: test',
'restartPolicy: Never',
'image: 000000000000.dkr.ecr.us-east-1.amazonaws.com/non-existent-image',
'labelKey: kronjob/job.test-template',
'memoryRequest: 4096Mi',
'memoryLimit: 6144Mi',
'nodeSelector:',
' group: jobs',
'env:',
' - name: ENV',
' value: test'
])
template_yaml = yaml.load(template, Loader=yaml.FullLoader)
cronner = Cronner()
cronner.configure(kronjob_template=template)
@cronner.register('* * * * *', template_vars={'foo':'bar'})
def fn():
pass
spec = yaml.load(cronner.get_entries(), Loader=yaml.FullLoader)
self.assertEqual(spec['jobs'][0]['foo'], 'bar')
def test_main_run(self):
cronner = Cronner()
@cronner.register('* * * * *')
def fn(*args):
print('+'.join(args))
with self.captureOutput(assert_stdout='a+b+c\n'):
cronner.main(['run', '{}.{}'.format(fn.__module__, fn.__name__), '--params', 'a', 'b', 'c'])
def test_main_gen_cfg(self):
cronner = Cronner()
cronner.configure(serializer=lambda es: '\n'.join('{}'.format(e['schedule']) for e in es))
@cronner.register('* * * * *')
def fn():
pass
with self.captureOutput(assert_stdout='* * * * *\n'):
cronner.main(['gen-cfg'])
def test_main_help(self):
cronner = Cronner()
@cronner.register('* * * * *')
def fn():
pass
with self.captureOutput():
try:
cronner.main(['--help'])
except SystemExit as e:
self.assertEqual(e.code, 0)
def test_main_unknown_input(self):
cronner = Cronner()
@cronner.register('* * * * *')
def fn():
pass
with self.captureOutput():
try:
cronner.main(['unknown-input'])
except SystemExit as e:
self.assertTrue(e.code > 0)
def test_main_no_input(self):
cronner = Cronner()
@cronner.register('* * * * *')
def fn():
pass
with self.captureOutput():
try:
cronner.main([])
except SystemExit as e:
self.assertTrue(e.code > 0)
def test_name_collision(self):
cronner = Cronner()
def get_f1():
def f():
pass
return f
def get_f2():
def f():
pass
return f
f1 = get_f1()
f2 = get_f2()
self.assertEqual(f1.__name__, f2.__name__) # Both their names are 'f'
self.assertNotEqual(f1, f2) # But they are different
cronner.register('* * * * *')(f1) # This should be fine
cronner.register('* * * * *')(f1) # Can register the same function again
# However, it should fail if we try to register another function with the same name
self.assertRaises(Exception, lambda: cronner.register('* * * * *')(f2))