]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Prevent SystemExits when running tests
authorKevin Benton <blak111@gmail.com>
Mon, 1 Sep 2014 20:03:27 +0000 (13:03 -0700)
committerKevin Benton <blak111@gmail.com>
Mon, 1 Sep 2014 22:05:16 +0000 (15:05 -0700)
Adds a check for SystemExit exceptions during tests
to convert them into a test failure rather than an exit.
Includes the traceback in the failure so the source of
the exit can be located.

Related-Bug: #1364171
Change-Id: I2c7d9010ebf935d39ed58fe7a6fc4a1a867e2548

neutron/tests/base.py
neutron/tests/unit/test_basetestcase.py [new file with mode: 0644]

index 72c81beb4e63d084d112807b967bcb50577e9851..e34dcc38b776ed6b4ba5ca9aad869ee08d88d310 100644 (file)
@@ -20,6 +20,7 @@ import logging as std_logging
 import os
 import os.path
 import sys
+import traceback
 
 import eventlet.timeout
 import fixtures
@@ -147,6 +148,12 @@ class BaseTestCase(testtools.TestCase):
             raise self.skipException('XML Testing Skipped in Py26')
 
         self.setup_config()
+        self.addOnException(self.check_for_systemexit)
+
+    def check_for_systemexit(self, exc_info):
+        if isinstance(exc_info[1], SystemExit):
+            self.fail("A SystemExit was raised during the test. %s"
+                      % traceback.format_exception(*exc_info))
 
     def setup_config(self):
         """Tests that need a non-default config can override this method."""
diff --git a/neutron/tests/unit/test_basetestcase.py b/neutron/tests/unit/test_basetestcase.py
new file mode 100644 (file)
index 0000000..40b5c43
--- /dev/null
@@ -0,0 +1,43 @@
+# Copyright 2014 OpenStack Foundation
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+"""Tests to test the test framework"""
+
+import sys
+
+from neutron.tests import base
+
+
+class SytemExitTestCase(base.BaseTestCase):
+
+    def setUp(self):
+        def _fail_SystemExit(exc_info):
+            if isinstance(exc_info[1], SystemExit):
+                self.fail("A SystemExit was allowed out")
+        super(SytemExitTestCase, self).setUp()
+        # add the handler last so reaching it means the handler in BaseTestCase
+        # didn't do it's job
+        self.addOnException(_fail_SystemExit)
+
+    def run(self, *args, **kwargs):
+        exc = self.assertRaises(AssertionError,
+                                super(SytemExitTestCase, self).run,
+                                *args, **kwargs)
+        # this message should be generated when SystemExit is raised by a test
+        self.assertIn('A SystemExit was raised during the test.', str(exc))
+
+    def test_system_exit(self):
+        # this should generate a failure that mentions SystemExit was used
+        sys.exit(1)