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
This commit is contained in:
Kevin Benton 2014-09-01 13:03:27 -07:00
parent db312a2886
commit 4d19bd9592
2 changed files with 50 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import logging as std_logging
import os import os
import os.path import os.path
import sys import sys
import traceback
import eventlet.timeout import eventlet.timeout
import fixtures import fixtures
@ -147,6 +148,12 @@ class BaseTestCase(testtools.TestCase):
raise self.skipException('XML Testing Skipped in Py26') raise self.skipException('XML Testing Skipped in Py26')
self.setup_config() 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): def setup_config(self):
"""Tests that need a non-default config can override this method.""" """Tests that need a non-default config can override this method."""

View File

@ -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)