Skip to content

Commit 754f540

Browse files
smoserblackboxsw
authored andcommitted
tests: run nosetests in cloudinit/ directory, fix py26 fallout.
When we moved some tests to live under cloudinit/ we inadvertantly failed to change all things that would run nose to include that directory. This changes all the 'nose' invocations to consistently run with tests/unittests and cloudinit/. Also, it works around, more correctly this time, a python2.6-ism with the following code: with assertRaises(SystemExit) as cm: sys.exit(2)
1 parent a1ca220 commit 754f540

File tree

6 files changed

+25
-22
lines changed

6 files changed

+25
-22
lines changed

cloudinit/cmd/tests/test_clean.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,11 @@ def test_status_main(self):
165165
wrap_and_call(
166166
'cloudinit.cmd.clean',
167167
{'Init': {'side_effect': self.init_class},
168+
'sys.exit': {'side_effect': self.sys_exit},
168169
'sys.argv': {'new': ['clean', '--logs']}},
169170
clean.main)
170171

171-
self.assertRaisesCodeEqual(0, context_manager.exception.code)
172+
self.assertEqual(0, context_manager.exception.code)
172173
self.assertFalse(
173174
os.path.exists(self.log1), 'Unexpected log {0}'.format(self.log1))
174175

cloudinit/cmd/tests/test_status.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,11 @@ def test_status_main(self):
380380
wrap_and_call(
381381
'cloudinit.cmd.status',
382382
{'sys.argv': {'new': ['status']},
383+
'sys.exit': {'side_effect': self.sys_exit},
383384
'_is_cloudinit_disabled': (False, ''),
384385
'Init': {'side_effect': self.init_class}},
385386
status.main)
386-
self.assertRaisesCodeEqual(0, context_manager.exception.code)
387+
self.assertEqual(0, context_manager.exception.code)
387388
self.assertEqual('status: running\n', m_stdout.getvalue())
388389

389390
# vi: ts=4 expandtab syntax=python

cloudinit/tests/helpers.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,15 @@ def tmp_path(self, path, dir=None):
173173
dir = self.tmp_dir()
174174
return os.path.normpath(os.path.abspath(os.path.join(dir, path)))
175175

176-
def assertRaisesCodeEqual(self, expected, found):
177-
"""Handle centos6 having different context manager for assertRaises.
178-
with assertRaises(Exception) as e:
179-
raise Exception("BOO")
180-
181-
centos6 will have e.exception as an integer.
182-
anything nwere will have it as something with a '.code'"""
183-
if isinstance(found, int):
184-
self.assertEqual(expected, found)
185-
else:
186-
self.assertEqual(expected, found.code)
176+
def sys_exit(self, code):
177+
"""Provide a wrapper around sys.exit for python 2.6
178+
179+
In 2.6, this code would produce 'cm.exception' with value int(2)
180+
rather than the SystemExit that was raised by sys.exit(2).
181+
with assertRaises(SystemExit) as cm:
182+
sys.exit(2)
183+
"""
184+
raise SystemExit(code)
187185

188186

189187
class ResourceUsingTestCase(CiTestCase):

tests/unittests/test_handler/test_schema.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,13 @@ class MainTest(CiTestCase):
336336

337337
def test_main_missing_args(self):
338338
"""Main exits non-zero and reports an error on missing parameters."""
339-
with mock.patch('sys.argv', ['mycmd']):
340-
with mock.patch('sys.stderr', new_callable=StringIO) as m_stderr:
341-
with self.assertRaises(SystemExit) as context_manager:
342-
main()
343-
self.assertEqual('1', str(context_manager.exception))
339+
with mock.patch('sys.exit', side_effect=self.sys_exit):
340+
with mock.patch('sys.argv', ['mycmd']):
341+
with mock.patch('sys.stderr', new_callable=StringIO) as \
342+
m_stderr:
343+
with self.assertRaises(SystemExit) as context_manager:
344+
main()
345+
self.assertEqual(1, context_manager.exception.code)
344346
self.assertEqual(
345347
'Expected either --config-file argument or --doc\n',
346348
m_stderr.getvalue())

tools/run-centos

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ main() {
313313

314314
if [ -n "$unittest" ]; then
315315
debug 1 "running unit tests."
316-
inside_as_cd "$name" "$user" "$cdir" nosetests tests/unittests ||
316+
inside_as_cd "$name" "$user" "$cdir" \
317+
nosetests tests/unittests cloudinit ||
317318
{ errorrc "nosetests failed."; errors=$(($errors+1)); }
318319
fi
319320

tox.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ deps = -r{toxinidir}/test-requirements.txt
4545

4646
[testenv:py26]
4747
deps = -r{toxinidir}/test-requirements.txt
48-
commands = nosetests {posargs:tests/unittests}
48+
commands = nosetests {posargs:tests/unittests cloudinit}
4949
setenv =
5050
LC_ALL = C
5151

@@ -83,7 +83,7 @@ deps =
8383

8484
[testenv:centos6]
8585
basepython = python2.6
86-
commands = nosetests {posargs:tests/unittests}
86+
commands = nosetests {posargs:tests/unittests cloudinit}
8787
deps =
8888
# requirements
8989
argparse==1.2.1
@@ -98,7 +98,7 @@ deps =
9898

9999
[testenv:opensusel42]
100100
basepython = python2.7
101-
commands = nosetests {posargs:tests/unittests}
101+
commands = nosetests {posargs:tests/unittests cloudinit}
102102
deps =
103103
# requirements
104104
argparse==1.3.0

0 commit comments

Comments
 (0)