From 486937907a5dec6f1f227a1bc3330cad061ff437 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Mon, 1 Sep 2014 13:03:27 -0700 Subject: [PATCH] Prevent SystemExits when running tests 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 | 7 ++++ neutron/tests/unit/test_basetestcase.py | 43 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 neutron/tests/unit/test_basetestcase.py diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 72c81beb4..e34dcc38b 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -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 index 000000000..40b5c43e0 --- /dev/null +++ b/neutron/tests/unit/test_basetestcase.py @@ -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) -- 2.45.2