Merge lp:~doanac/utah/unit-test-regressions into lp:utah
- unit-test-regressions
- Merge into dev
Proposed by
Andy Doan
Status: | Merged |
---|---|
Merged at revision: | 842 |
Proposed branch: | lp:~doanac/utah/unit-test-regressions |
Merge into: | lp:utah |
Prerequisite: | lp:~nuclearbob/utah/fix-recent-issues |
Diff against target: |
346 lines (+64/-163) 3 files modified
utah/client/tests/common.py (+14/-13) utah/client/tests/test_runner.py (+8/-15) utah/client/tests/test_state_agent.py (+42/-135) |
To merge this branch: | bzr merge lp:~doanac/utah/unit-test-regressions |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Max Brustkern (community) | Approve | ||
Joe Talbott (community) | Approve | ||
Review via email: mp+154808@code.launchpad.net |
Commit message
Description of the change
This fixes some stuff in our client unit tests
To post a comment you must log in.
Revision history for this message
Max Brustkern (nuclearbob) wrote : | # |
Revision history for this message
Joe Talbott (joetalbott) wrote : | # |
L177 seems backwards.
L46 don't we still need to raise an exception if non-root, setUp and cleanUp aren't guaranteed to be called sequentially.
Other than that, LGTM
Revision history for this message
Andy Doan (doanac) wrote : | # |
177 is backwards, must have mis-merged stuff.
joe for line 46: I don't think you have to check for root in teardown, because setup will have failed if the user isn't root and thus those directories won't exist for cleanup
Revision history for this message
Joe Talbott (joetalbott) wrote : | # |
L46: makes sense to me.
LGTM.
review:
Approve
Revision history for this message
Max Brustkern (nuclearbob) wrote : | # |
I like it. I'll go ahead and merge.
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'utah/client/tests/common.py' |
2 | --- utah/client/tests/common.py 2013-03-21 21:24:22 +0000 |
3 | +++ utah/client/tests/common.py 2013-03-21 21:24:22 +0000 |
4 | @@ -59,8 +59,7 @@ |
5 | |
6 | master_runlist = os.path.join(UTAH_DIR, MASTER_RUNLIST) |
7 | master_runlist_bak = master_runlist + ".bak" |
8 | -examples_dir = os.path.join(UTAH_DIR, 'testsuites', 'examples') |
9 | -#examples_source = os.path.join(UTAH_CLIENT, 'examples') |
10 | +ts_dir = os.path.join(UTAH_DIR, 'testsuites') |
11 | examples_source = os.path.join(get_module_path(), 'client', 'examples') |
12 | |
13 | |
14 | @@ -72,15 +71,21 @@ |
15 | |
16 | # If we're not root don't bother |
17 | if os.geteuid() != 0 and os.getuid() != 0: |
18 | - return |
19 | + raise RuntimeError('These tests must be run as root') |
20 | |
21 | # save a copy of the master runlist |
22 | if os.path.exists(master_runlist): |
23 | os.rename(master_runlist, master_runlist_bak) |
24 | |
25 | - # Copy the examples directory into the working directory |
26 | - if not os.path.exists(examples_dir): |
27 | - shutil.copytree(examples_source, examples_dir) |
28 | + if os.path.exists(ts_dir): |
29 | + shutil.rmtree(ts_dir) |
30 | + for x in os.listdir(examples_source): |
31 | + p = os.path.join(examples_source, x) |
32 | + if os.path.isdir(p): |
33 | + # NOTE: the runner expects the directory tree for test suite to |
34 | + # look like: |
35 | + # /var/lib/utah/testsuites/<tsname>/<tsname>/tslist.run |
36 | + shutil.copytree(p, os.path.join(ts_dir, x, x)) |
37 | |
38 | with open(master_runlist, 'w') as fp: |
39 | fp.write(master_runlist_content) |
40 | @@ -91,18 +96,14 @@ |
41 | Cleanup after ourselves. |
42 | """ |
43 | |
44 | - # If we're not root don't bother |
45 | - if os.geteuid() != 0 and os.getuid() != 0: |
46 | - return |
47 | - |
48 | # restore the copy of the master runlist |
49 | if os.path.exists(master_runlist_bak): |
50 | os.rename(master_runlist_bak, master_runlist) |
51 | - else: |
52 | + elif os.path.exists(master_runlist): |
53 | os.remove(master_runlist) |
54 | |
55 | - if os.path.exists(examples_dir): |
56 | - shutil.rmtree(examples_dir) |
57 | + if os.path.exists(ts_dir): |
58 | + shutil.rmtree(ts_dir) |
59 | |
60 | |
61 | def _get_partial_state_file(filename): |
62 | |
63 | === modified file 'utah/client/tests/test_runner.py' |
64 | --- utah/client/tests/test_runner.py 2013-03-21 21:24:22 +0000 |
65 | +++ utah/client/tests/test_runner.py 2013-03-21 21:24:22 +0000 |
66 | @@ -58,28 +58,21 @@ |
67 | raise |
68 | |
69 | def test_fetch_failed(self): |
70 | - print "Starting test" |
71 | - # exit 2 should be flagged as an error. |
72 | - bad_runlist_content = ('---\n' |
73 | - '- name: fake_tests\n' |
74 | - ' fetch_method: bzr\n' |
75 | - ' fetch_location: /dev/null\n') |
76 | + bad_runlist_content = """--- |
77 | +testsuites: |
78 | + - name: fake_tests |
79 | + fetch_method: bzr |
80 | + fetch_location: /dev/null |
81 | +""" |
82 | runlist = '/tmp/master_fetch_fails.run' |
83 | |
84 | - self.assertFalse(os.path.exists(runlist), runlist) |
85 | - |
86 | with open(runlist, 'w') as fp: |
87 | fp.write(bad_runlist_content) |
88 | |
89 | runner = Runner(install_type='desktop', result_class=self.result_class, |
90 | state_agent=self.state_agent, runlist=runlist) |
91 | |
92 | - # remove the runlist as early as possible to avoid leaving it |
93 | - # laying around if this test fails. |
94 | - try: |
95 | - os.remove(runlist) |
96 | - except OSError: |
97 | - pass |
98 | + os.remove(runlist) |
99 | |
100 | runner.run() |
101 | print runner.report() |
102 | @@ -98,7 +91,7 @@ |
103 | for suite in self.runner.suites: |
104 | self.assertTrue(suite.is_done()) |
105 | |
106 | - self.assertEqual(retcode, ReturnCodes.ERROR) |
107 | + self.assertEqual(retcode, ReturnCodes.FAIL) |
108 | |
109 | def test_missing_testdir(self): |
110 | """ |
111 | |
112 | === modified file 'utah/client/tests/test_state_agent.py' |
113 | --- utah/client/tests/test_state_agent.py 2013-03-21 21:24:22 +0000 |
114 | +++ utah/client/tests/test_state_agent.py 2013-03-21 21:24:22 +0000 |
115 | @@ -14,19 +14,16 @@ |
116 | # with this program. If not, see <http://www.gnu.org/licenses/>. |
117 | |
118 | import os |
119 | -import shutil |
120 | import unittest |
121 | import yaml |
122 | |
123 | from utah.client.state_agent import StateAgentYAML |
124 | from utah.client.runner import Runner |
125 | from utah.client.result import ResultYAML |
126 | -from utah.client.common import UTAH_DIR |
127 | |
128 | -from .common import ( # NOQA |
129 | +from utah.client.tests.common import ( |
130 | setUp, # Used by nosetests |
131 | tearDown, # Used by nosetests |
132 | - get_module_path, |
133 | partial_state_file_content, |
134 | partial_state_file_content_run_all, |
135 | partial_state_file_content_done_all_failed, |
136 | @@ -40,32 +37,21 @@ |
137 | """ |
138 | self.state_file = '/tmp/state.yaml' |
139 | |
140 | - # Fail if there is already a test state file. |
141 | - self.assertFalse( |
142 | - os.path.exists(self.state_file), |
143 | - "state file (%s) already exists" % self.state_file) |
144 | + if os.path.exists(self.state_file): |
145 | + os.unlink(self.state_file) |
146 | |
147 | self.state_agent = StateAgentYAML(state_file=self.state_file) |
148 | self.runner = Runner( |
149 | install_type="desktop", result_class=ResultYAML, |
150 | state_agent=self.state_agent) |
151 | - self.partial_state_file_content = partial_state_file_content |
152 | - self.partial_state_file_content_run_all = \ |
153 | - partial_state_file_content_run_all |
154 | - self.partial_state_file_content_done_all_failed = \ |
155 | - partial_state_file_content_done_all_failed |
156 | |
157 | def tearDown(self): |
158 | """ |
159 | Clean up the test state file. |
160 | """ |
161 | - try: |
162 | + |
163 | + if os.path.exists(self.state_file): |
164 | os.remove(self.state_file) |
165 | - except OSError as e: |
166 | - if e.errno == 2: # does not exist |
167 | - pass |
168 | - else: |
169 | - raise |
170 | |
171 | def test_creation(self): |
172 | self.runner.run() |
173 | @@ -99,136 +85,57 @@ |
174 | self.assertEqual(self.runner.count_suites(), suite_count) |
175 | self.assertEqual(self.runner.count_tests(), test_count) |
176 | |
177 | - with open(self.state_file, 'r') as fp: |
178 | - state_data = yaml.load(fp) |
179 | + with open(self.state_file, 'r') as f: |
180 | + state_data = yaml.load(f) |
181 | |
182 | self.assertEqual(state_data['status'], 'DONE') |
183 | |
184 | + def _test_partial(self, content, next_test, passes, fails, errors): |
185 | + with open(self.state_file, 'w') as f: |
186 | + f.write(content) |
187 | + |
188 | + # we must call the common setUp again since our object's setUp() |
189 | + # creates a Runner object which will delete everything under |
190 | + # /var/lib/utah/testsuites |
191 | + setUp() |
192 | + state_agent = StateAgentYAML(state_file=self.state_file) |
193 | + runner = Runner(install_type="desktop", |
194 | + state_agent=state_agent, |
195 | + result_class=ResultYAML, |
196 | + resume=True) |
197 | + |
198 | + self.assertEqual(runner.status, 'RUN') |
199 | + self.assertEqual(runner.count_suites(), 3) |
200 | + self.assertEqual(runner.count_tests(), 5) |
201 | + |
202 | + if next_test: |
203 | + next_test_obj = runner.get_next_test() |
204 | + self.assertEqual(next_test_obj.name, next_test) |
205 | + |
206 | + runner.run() |
207 | + |
208 | + self.assertEqual(runner.passes, passes) |
209 | + self.assertEqual(runner.failures, fails) |
210 | + self.assertEqual(runner.errors, errors) |
211 | + t = passes + fails + errors |
212 | + self.assertEqual(runner.passes + runner.failures + runner.errors, t) |
213 | + |
214 | def test_load_state_partial(self): |
215 | """ |
216 | Test that a partially run state file can be resumed. |
217 | """ |
218 | - with open(self.state_file, 'w') as fp: |
219 | - fp.write(self.partial_state_file_content) |
220 | - |
221 | - # Need to set up utah_tests since we're simulating a restart |
222 | - test_src = os.path.join(get_module_path(), 'client', |
223 | - 'examples', 'utah_tests') |
224 | - test_dir = os.path.join(UTAH_DIR, 'testsuites', |
225 | - 'utah_tests', 'utah_tests') |
226 | - if not os.path.exists(test_dir): |
227 | - shutil.copytree(test_src, test_dir) |
228 | - |
229 | - # Need to set up utah_tests_sample since we're simulating a restart |
230 | - test_src = os.path.join(get_module_path(), 'client', |
231 | - 'examples', 'utah_tests_sample') |
232 | - test_dir = os.path.join(UTAH_DIR, 'testsuites', |
233 | - 'utah_tests_sample', 'utah_tests_sample') |
234 | - if not os.path.exists(test_dir): |
235 | - shutil.copytree(test_src, test_dir) |
236 | - |
237 | - state_agent = StateAgentYAML(state_file=self.state_file) |
238 | - runner = Runner( |
239 | - install_type="desktop", |
240 | - state_agent=state_agent, |
241 | - result_class=ResultYAML, |
242 | - resume=True) |
243 | - |
244 | - self.assertEqual(runner.status, 'RUN') |
245 | - self.assertEqual(runner.count_suites(), 3) |
246 | - self.assertEqual(runner.count_tests(), 5) |
247 | - |
248 | - next_test = runner.get_next_test() |
249 | - |
250 | - self.assertEqual(next_test.name, 'test_two') |
251 | - |
252 | - runner.run() |
253 | - |
254 | - self.assertEqual(runner.passes, 4) |
255 | - self.assertEqual(runner.failures, 1) |
256 | - self.assertEqual(runner.errors, 0) |
257 | - self.assertEqual(runner.passes + runner.failures + runner.errors, 5) |
258 | + self._test_partial(partial_state_file_content, 'test_two', 4, 1, 0) |
259 | |
260 | def test_load_state_partial_run_all(self): |
261 | """ |
262 | Test partially run state file - run all jobs. |
263 | """ |
264 | - with open(self.state_file, 'w') as fp: |
265 | - fp.write(self.partial_state_file_content_run_all) |
266 | - |
267 | - # Need to set up utah_tests since we're simulating a restart |
268 | - test_src = os.path.join(get_module_path(), 'client', |
269 | - 'examples', 'utah_tests') |
270 | - test_dir = os.path.join(UTAH_DIR, 'testsuites', |
271 | - 'utah_tests', 'utah_tests') |
272 | - if not os.path.exists(test_dir): |
273 | - shutil.copytree(test_src, test_dir) |
274 | - |
275 | - # Need to set up utah_tests_sample since we're simulating a restart |
276 | - test_src = os.path.join(get_module_path(), 'client', |
277 | - 'examples', 'utah_tests_sample') |
278 | - test_dir = os.path.join(UTAH_DIR, 'testsuites', |
279 | - 'utah_tests_sample', 'utah_tests_sample') |
280 | - if not os.path.exists(test_dir): |
281 | - shutil.copytree(test_src, test_dir) |
282 | - |
283 | - state_agent = StateAgentYAML(state_file=self.state_file) |
284 | - runner = Runner(install_type="desktop", |
285 | - state_agent=state_agent, |
286 | - result_class=ResultYAML, |
287 | - resume=True) |
288 | - |
289 | - self.assertEqual(runner.status, 'RUN') |
290 | - self.assertEqual(runner.count_suites(), 3) |
291 | - self.assertEqual(runner.count_tests(), 5) |
292 | - |
293 | - next_test = runner.get_next_test() |
294 | - |
295 | - self.assertEqual(next_test.name, 'test_one') |
296 | - |
297 | - runner.run() |
298 | - |
299 | - self.assertEqual(runner.passes, 3) |
300 | - self.assertEqual(runner.failures, 0) |
301 | - self.assertEqual(runner.errors, 2) |
302 | - self.assertEqual(runner.passes + runner.failures + runner.errors, 5) |
303 | + self._test_partial( |
304 | + partial_state_file_content_run_all, 'test_one', 3, 2, 0) |
305 | |
306 | def test_load_state_partial_done_all_failed(self): |
307 | """ |
308 | Test partially run state file - all jobs failed. |
309 | """ |
310 | - with open(self.state_file, 'w') as fp: |
311 | - fp.write(self.partial_state_file_content_done_all_failed) |
312 | - |
313 | - # Need to set up utah_tests since we're simulating a restart |
314 | - test_src = os.path.join(get_module_path(), 'client', |
315 | - 'examples', 'utah_tests') |
316 | - test_dir = os.path.join(UTAH_DIR, 'testsuites', |
317 | - 'utah_tests', 'utah_tests') |
318 | - if not os.path.exists(test_dir): |
319 | - shutil.copytree(test_src, test_dir) |
320 | - |
321 | - # Need to set up utah_tests_sample since we're simulating a restart |
322 | - test_src = os.path.join(get_module_path(), 'client', |
323 | - 'examples', 'utah_tests_sample') |
324 | - test_dir = os.path.join(UTAH_DIR, 'testsuites', |
325 | - 'utah_tests_sample', 'utah_tests_sample') |
326 | - if not os.path.exists(test_dir): |
327 | - shutil.copytree(test_src, test_dir) |
328 | - |
329 | - state_agent = StateAgentYAML(state_file=self.state_file) |
330 | - runner = Runner(install_type="desktop", |
331 | - state_agent=state_agent, |
332 | - result_class=ResultYAML, |
333 | - resume=True) |
334 | - |
335 | - self.assertEqual(runner.status, 'RUN') |
336 | - self.assertEqual(runner.count_suites(), 3) |
337 | - self.assertEqual(runner.count_tests(), 5) |
338 | - |
339 | - runner.run() |
340 | - |
341 | - self.assertEqual(runner.passes, 0) |
342 | - self.assertEqual(runner.failures, 5) |
343 | - self.assertEqual(runner.errors, 0) |
344 | - self.assertEqual(runner.passes + runner.failures + runner.errors, 5) |
345 | + self._test_partial( |
346 | + partial_state_file_content_done_all_failed, None, 0, 5, 0) |
On 177-181, is there a reason the with was dumped?
Other than that, this looks good to me.