test failure induced by reading system config file

run run_tests.sh or tox to test the glance code, it will get
following failure:
test_get_bind_addr_default_value
test_get_path_non_exist

Analysis
--------
The root cause is resided in "glance/tests/unit/api/test_cmd.py".
In TestGlanceApiCmd.setUp, the sys.argv is overrided to ['glance-api'].
Since sys.argv is "global" variable, and this test entry it run
firstly by nosetests while running tests in "glance/tests/unit"(since
nosetests search test items alphabetically), the other test entries
will inherit the sys.argv value ['glance-api']. Finally the system
config file "/etc/glance/glance-api.conf" is read in "setUp" via
oslo.config.cfg:ConfigOpts.__call__ -> _pre_setup -> find_config_files.
CONF.bind_port and CONF.bind_host is set to the value reading from
system config file: /etc/glance/glance-api.conf.

A simplied call backtrace:
TestGlanceApiCmd.setUp -->  sys.argv = ['glance-api']
 ->JSONRequestDeserializerTest.setUp
  ->glance/common/config.py:parse_args
   ->oslo.config.cfg:ConfigOpts.__call__
    ->CONF.bind_port CONF.bind_host is set

wsgi.get_bind_addr() will read the CONF.bind_port and CONF.bind_host
to return value. if system config file is not read, CONF.bind_host
is set to "0.0.0.0", CONF.bind_port is not set. wsgi.get_bind_addr()
will return the expected value. This is the reason that individual
test of test_get_bind_addr_default_value got passed, but failed while
testing all.

The failure of test_get_path_non_exist is induced by same reason.

Test case should clear up the setting in tearDown(), especially the
"global" setting, e.g. sys.argv.

Solution
--------
The solution is simple:
In class TestGlanceApiCmd, add a private variable to backup the
sys.argv in setUp(), and restore it in tearDown().

Test method
-----------
$ ./run_tests.sh

$ tox -evenv -- nosetests \
--tests=glance.tests.unit.common.test_config:TestPasteApp

tox -evenv -- nosetests
--tests=glance.tests.unit.common.test_wsgi:JSONRequestDeserializerTest

Fixes Bug #1204186

Change-Id: I2dac8e506fa794d1faa9509258936d78e2f981d8
This commit is contained in:
Kui Shi 2013-08-24 16:23:51 +08:00
parent b2d5a38cc8
commit de22b642f7
1 changed files with 5 additions and 0 deletions

View File

@ -22,6 +22,9 @@ from glance.tests import utils as test_utils
class TestGlanceApiCmd(test_utils.BaseTestCase):
__argv_backup = None
def _do_nothing(self, *args, **kwargs):
pass
@ -32,6 +35,7 @@ class TestGlanceApiCmd(test_utils.BaseTestCase):
def setUp(self):
super(TestGlanceApiCmd, self).setUp()
self.__argv_backup = sys.argv
sys.argv = ['glance-api']
self.stderr = StringIO.StringIO()
sys.stderr = self.stderr
@ -45,6 +49,7 @@ class TestGlanceApiCmd(test_utils.BaseTestCase):
def tearDown(self):
sys.stderr = sys.__stderr__
sys.argv = self.__argv_backup
super(TestGlanceApiCmd, self).tearDown()
def test_supported_default_store(self):