-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
223 lines (200 loc) · 6.97 KB
/
tests.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
217
218
219
220
221
222
223
import collections
import contextlib
import tempfile
import textwrap
from unittest import mock
from unittest import TestCase
from cron_install import Command, OptionError
@contextlib.contextmanager
def cronfile(content):
with tempfile.NamedTemporaryFile(mode="w+t", prefix="test_cron_install") as f:
f.write(content)
f.flush()
yield f.name
FakeResult = collections.namedtuple("FakeResult", "returncode stdout")
@contextlib.contextmanager
def cron_mock(cron_returns=["", ""]):
results = [FakeResult(0, s.encode("utf-8")) for s in cron_returns]
with mock.patch("subprocess.run", side_effect=results) as mocked:
yield mocked
class CronInstallTestCase(TestCase):
def installed_crontab(self, mocked):
# in our scenario this should always be the input of last call
last_call = mocked.call_args_list[-1]
self.assertEqual(last_call[0][0][0], "crontab")
self.assertEqual(last_call[0][0][-1], "-")
return last_call[1]["input"].decode("utf-8")
def test_remove_and_cronpath(self):
with self.assertRaises(OptionError) as e:
Command(marker="MARK", cron_path="/tmp/cron", remove=True)
self.assertEqual(str(e.exception), "Do not use a cronpath with remove option")
def test_neither_remove_nor_cronpath(self):
with self.assertRaises(OptionError) as e:
Command(marker="MARK",)
self.assertEqual(
str(e.exception), "Provide a file to install in crontab, or activate remove option",
)
def test_simple_install(self):
my_cron = textwrap.dedent(
"""
# task 1
22 22 * * * /usr/local/bin/test.1
# task 1
*/10 * * * * /usr/local/bin/test.2
"""
)
with cronfile(my_cron) as fpath, cron_mock() as mocked:
cmd = Command(marker="MARK", cron_path=fpath, substitutions={})
cmd.run()
self.assertEqual(
self.installed_crontab(mocked), "\n# START MARK\n" + my_cron + "# END MARK",
)
def test_install_with_user(self):
with cronfile("\n#nothing\n") as fpath, cron_mock() as mocked:
cmd = Command(marker="MARK", cron_path=fpath, substitutions={}, user="bob")
cmd.run()
self.assertEqual(mocked.call_count, 2)
# command always called with "-u bob"
for args in mocked.call_args_list:
self.assertIn("-u bob", " ".join(args[0][0]))
def test_interpolation(self):
my_cron = textwrap.dedent(
"""
# task 1
22 22 * * * /usr/local/bin/$TEST_PROGRAM $TEST_PATH $$TEST_ENV
"""
)
interpolated = textwrap.dedent(
"""
# task 1
22 22 * * * /usr/local/bin/foo bar/baz $TEST_ENV
"""
)
env = {"TEST_PROGRAM": "foo", "TEST_PATH": "bar/baz", "TEST_ENV": "NO!"}
with cronfile(my_cron) as fpath, cron_mock() as mocked:
cmd = Command(marker="INTERPOL", cron_path=fpath, substitutions=env)
cmd.run()
self.assertEqual(
self.installed_crontab(mocked),
"\n# START INTERPOL\n" + interpolated + "# END INTERPOL",
)
def test_interpolation_variable_miss(self):
my_cron = textwrap.dedent(
"""
# $UNKNOWN
"""
)
env = {}
with cronfile(my_cron) as fpath, cron_mock():
cmd = Command(marker="X", cron_path=fpath, substitutions=env)
with self.assertRaises(KeyError) as e:
cmd.run()
self.assertIn("UNKNOWN", str(e.exception))
def test_update(self):
old_cron = textwrap.dedent(
"""
# START MARK
# old task
22 22 * * * /usr/local/bin/old_prog foo
22 24 * * * /usr/local/bin/old_prog bar
# END MARK
# unrelated
0 23 1 * * /usr/bin/mail spam
"""
)
my_cron = textwrap.dedent(
"""
# new task
22 22 * * * /usr/local/bin/new_prog baz
"""
)
expected = textwrap.dedent(
"""
# unrelated
0 23 1 * * /usr/bin/mail spam
# START MARK
# new task
22 22 * * * /usr/local/bin/new_prog baz
# END MARK
"""
).rstrip()
with cronfile(my_cron) as fpath, cron_mock([old_cron, ""]) as mocked:
cmd = Command(marker="MARK", cron_path=fpath, substitutions={})
cmd.run()
self.assertEqual(
self.installed_crontab(mocked), expected,
)
def test_update_interpolation(self):
"""Test that updating and interpolation do not mess with unrelated lines
"""
old_cron = textwrap.dedent(
"""
# START MARK
22 22 * * * /usr/local/bin/old_prog foo
# END MARK
# unrelated
0 23 1 * * /usr/bin/mail $SPAM $FOO
"""
)
my_cron = textwrap.dedent(
"""
22 22 * * * /usr/local/bin/new_prog $ARGS $FOO
"""
)
expected = textwrap.dedent(
"""
# unrelated
0 23 1 * * /usr/bin/mail $SPAM $FOO
# START MARK
22 22 * * * /usr/local/bin/new_prog baz foo
# END MARK
"""
).rstrip()
env = {"ARGS": "baz", "FOO": "foo"}
with cronfile(my_cron) as fpath, cron_mock([old_cron, ""]) as mocked:
cmd = Command(marker="MARK", cron_path=fpath, substitutions=env)
cmd.run()
self.assertEqual(
self.installed_crontab(mocked), expected,
)
def test_remove(self):
old_cron = textwrap.dedent(
"""
# START MARK
22 22 * * * /usr/local/bin/old_prog foo
# END MARK
# unrelated
0 23 1 * * /usr/bin/mail $SPAM $FOO
"""
)
expected = textwrap.dedent(
"""
# unrelated
0 23 1 * * /usr/bin/mail $SPAM $FOO
"""
)
with cron_mock([old_cron, ""]) as mocked:
cmd = Command(marker="MARK", remove=True)
cmd.run()
self.assertEqual(
self.installed_crontab(mocked), expected,
)
def test_remove_no_entry(self):
old_cron = textwrap.dedent(
"""
# unrelated
0 23 1 * * /usr/bin/mail $SPAM $FOO
"""
)
expected = textwrap.dedent(
"""
# unrelated
0 23 1 * * /usr/bin/mail $SPAM $FOO
"""
)
with cron_mock([old_cron, ""]) as mocked:
cmd = Command(marker="MARK", remove=True)
cmd.run()
self.assertEqual(
self.installed_crontab(mocked), expected,
)