Ubuntu Pastebin

Paste from smoser at Thu, 7 Dec 2017 22:28:51 +0000

Download as text
 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
diff --git a/cloudinit/cmd/tests/test_clean.py b/cloudinit/cmd/tests/test_clean.py
index af438aab..cc195131 100644
--- a/cloudinit/cmd/tests/test_clean.py
+++ b/cloudinit/cmd/tests/test_clean.py
@@ -151,7 +151,7 @@ class TestClean(CiTestCase):
                  'sys.argv': {'new': ['clean', '--logs']}},
                 clean.main)
 
-        self.assertEqual(0, context_manager.exception.code)
+        self.assertRaisesCodeEqual(0, context_manager.exception)
         self.assertFalse(
             os.path.exists(self.log1), 'Unexpected log {0}'.format(self.log1))
 
diff --git a/cloudinit/cmd/tests/test_status.py b/cloudinit/cmd/tests/test_status.py
index 8ec9b5bc..5463fca2 100644
--- a/cloudinit/cmd/tests/test_status.py
+++ b/cloudinit/cmd/tests/test_status.py
@@ -347,7 +347,7 @@ class TestStatus(CiTestCase):
                      '_is_cloudinit_disabled': (False, ''),
                      'Init': {'side_effect': self.init_class}},
                     status.main)
-        self.assertEqual(0, context_manager.exception.code)
+        self.assertRaisesCodeEqual(0, context_manager.exception)
         self.assertEqual('status: running\n', m_stdout.getvalue())
 
 # vi: ts=4 expandtab syntax=python
diff --git a/cloudinit/tests/helpers.py b/cloudinit/tests/helpers.py
index feb884ab..9a509215 100644
--- a/cloudinit/tests/helpers.py
+++ b/cloudinit/tests/helpers.py
@@ -158,6 +158,18 @@ class CiTestCase(TestCase):
             dir = self.tmp_dir()
         return os.path.normpath(os.path.abspath(os.path.join(dir, path)))
 
+    def assertRaisesCodeEqual(self, expected, found):
+        """Handle centos6 having different context manager for assertRaises.
+            with assertRaises(Exception) as e:
+                raise Exception("BOO")
+
+            centos6 will have e.exception as an integer.
+            anything nwere will have it as something with a '.code'"""
+        if isinstance(found, int):
+            self.assertEqual(expected, found)
+        else:
+            self.assertEqual(expected, found.code)
+
 
 class ResourceUsingTestCase(CiTestCase):
 
@@ -395,4 +407,12 @@ if not hasattr(mock.Mock, 'assert_not_called'):
     mock.Mock.assert_not_called = __mock_assert_not_called
 
 
+# older unittest2.TestCase (centos6) do not have assertRaisesRegex
+# And setting assertRaisesRegex to assertRaisesRegexp causes
+# https://github.com/PyCQA/pylint/issues/1653 . So the workaround.
+if not hasattr(unittest2.TestCase, 'assertRaisesRegex'):
+    def _tricky(*args, **kwargs):
+        return unittest2.TestCase.assertRaisesRegexp
+    unittest2.TestCase.assertRaisesRegex = _tricky
+
 # vi: ts=4 expandtab
Download as text