diff options
author | Alistair Coles <alistair.coles@hp.com> | 2014-09-25 11:34:48 +0100 |
---|---|---|
committer | Alistair Coles <alistair.coles@hp.com> | 2014-09-25 11:44:39 +0100 |
commit | b13c84b2ef8a7fee31de2a77b66b50370e6968e5 (patch) | |
tree | de99d562d94ef4525a33eac80f678a76bec11114 | |
parent | 8f1b394325804c23ac18e7f9da6cb56a3b95eed2 (diff) | |
download | python-swiftclient-b13c84b2ef8a7fee31de2a77b66b50370e6968e5.tar.gz |
Fix unit tests failing when OS_ env vars are set
tests/unit/test_shell.py:TestParsing tests can fail if
there are OS_* variables set in the environment. There
is already code in the test setUp to remove ST_* variables,
so we should do the same for OS_* variables.
This patch also changes the mechanism used to remove and
then restore any unwanted variables found in os.environ.
The existing setUp() takes a copy of os.environ and then
deletes any ST_* variables in the original. In tearDown() it
sets os.environ as the copy. However, the environ imported
into shell.py remains pointing to the original os.environ
object. So after the first call to tearDown, subsequent
mocking of os.environ has no effect on shell.environ.
This renders some of the tests ineffective e.g.
test_insufficient_env_vars_v3 is not actually setting any
vars in shell.environ.
The issue can be provoked by repeating a test:
nosetests -w tests/unit/ test_shell.py:TestParsing.test_args_v3 \
test_shell.py:TestParsing.test_args_v3
The test will pass first time and fail second time.
Change-Id: I5d100f81115e74878d510326acb5777e6a3626c8
-rw-r--r-- | tests/unit/test_shell.py | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/tests/unit/test_shell.py b/tests/unit/test_shell.py index 80d63d9..543b678 100644 --- a/tests/unit/test_shell.py +++ b/tests/unit/test_shell.py @@ -399,14 +399,15 @@ class TestParsing(unittest.TestCase): def setUp(self): super(TestParsing, self).setUp() - self._orig_environ = os.environ.copy() + self._environ_vars = {} keys = os.environ.keys() for k in keys: - if k in ('ST_KEY', 'ST_USER', 'ST_AUTH'): - del os.environ[k] + if (k in ('ST_KEY', 'ST_USER', 'ST_AUTH') + or k.startswith('OS_')): + self._environ_vars[k] = os.environ.pop(k) def tearDown(self): - os.environ = self._orig_environ + os.environ.update(self._environ_vars) def _make_fake_command(self, result): def fake_command(parser, args, thread_manager): |