Merge "Use fixtures and addCleanup instead of tearDown"
This commit is contained in:
@ -14,6 +14,7 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
import fixtures
|
||||||
import mock
|
import mock
|
||||||
import os
|
import os
|
||||||
import testtools
|
import testtools
|
||||||
@ -161,6 +162,23 @@ def fake_execute(shell, cmd):
|
|||||||
return shell.run(cmd.split())
|
return shell.run(cmd.split())
|
||||||
|
|
||||||
|
|
||||||
|
class EnvFixture(fixtures.Fixture):
|
||||||
|
"""Environment Fixture.
|
||||||
|
|
||||||
|
This fixture replaces os.environ with provided env or an empty env.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, env=None):
|
||||||
|
self.new_env = env or {}
|
||||||
|
|
||||||
|
def _setUp(self):
|
||||||
|
self.orig_env, os.environ = os.environ, self.new_env
|
||||||
|
self.addCleanup(self.revert)
|
||||||
|
|
||||||
|
def revert(self):
|
||||||
|
os.environ = self.orig_env
|
||||||
|
|
||||||
|
|
||||||
class TestShell(utils.TestCase):
|
class TestShell(utils.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -168,12 +186,9 @@ class TestShell(utils.TestCase):
|
|||||||
patch = "openstackclient.shell.OpenStackShell.run_subcommand"
|
patch = "openstackclient.shell.OpenStackShell.run_subcommand"
|
||||||
self.cmd_patch = mock.patch(patch)
|
self.cmd_patch = mock.patch(patch)
|
||||||
self.cmd_save = self.cmd_patch.start()
|
self.cmd_save = self.cmd_patch.start()
|
||||||
|
self.addCleanup(self.cmd_patch.stop)
|
||||||
self.app = mock.Mock("Test Shell")
|
self.app = mock.Mock("Test Shell")
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
super(TestShell, self).tearDown()
|
|
||||||
self.cmd_patch.stop()
|
|
||||||
|
|
||||||
def _assert_initialize_app_arg(self, cmd_options, default_args):
|
def _assert_initialize_app_arg(self, cmd_options, default_args):
|
||||||
"""Check the args passed to initialize_app()
|
"""Check the args passed to initialize_app()
|
||||||
|
|
||||||
@ -285,11 +300,7 @@ class TestShellHelp(TestShell):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestShellHelp, self).setUp()
|
super(TestShellHelp, self).setUp()
|
||||||
self.orig_env, os.environ = os.environ, {}
|
self.useFixture(EnvFixture())
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
super(TestShellHelp, self).tearDown()
|
|
||||||
os.environ = self.orig_env
|
|
||||||
|
|
||||||
@testtools.skip("skip until bug 1444983 is resolved")
|
@testtools.skip("skip until bug 1444983 is resolved")
|
||||||
def test_help_options(self):
|
def test_help_options(self):
|
||||||
@ -310,11 +321,7 @@ class TestShellOptions(TestShell):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestShellOptions, self).setUp()
|
super(TestShellOptions, self).setUp()
|
||||||
self.orig_env, os.environ = os.environ, {}
|
self.useFixture(EnvFixture())
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
super(TestShellOptions, self).tearDown()
|
|
||||||
os.environ = self.orig_env
|
|
||||||
|
|
||||||
def _test_options_init_app(self, test_opts):
|
def _test_options_init_app(self, test_opts):
|
||||||
for opt in test_opts.keys():
|
for opt in test_opts.keys():
|
||||||
@ -402,11 +409,7 @@ class TestShellTokenAuthEnv(TestShell):
|
|||||||
"OS_TOKEN": DEFAULT_TOKEN,
|
"OS_TOKEN": DEFAULT_TOKEN,
|
||||||
"OS_AUTH_URL": DEFAULT_AUTH_URL,
|
"OS_AUTH_URL": DEFAULT_AUTH_URL,
|
||||||
}
|
}
|
||||||
self.orig_env, os.environ = os.environ, env.copy()
|
self.useFixture(EnvFixture(env.copy()))
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
super(TestShellTokenAuthEnv, self).tearDown()
|
|
||||||
os.environ = self.orig_env
|
|
||||||
|
|
||||||
def test_env(self):
|
def test_env(self):
|
||||||
flag = ""
|
flag = ""
|
||||||
@ -450,11 +453,7 @@ class TestShellTokenEndpointAuthEnv(TestShell):
|
|||||||
"OS_TOKEN": DEFAULT_TOKEN,
|
"OS_TOKEN": DEFAULT_TOKEN,
|
||||||
"OS_URL": DEFAULT_SERVICE_URL,
|
"OS_URL": DEFAULT_SERVICE_URL,
|
||||||
}
|
}
|
||||||
self.orig_env, os.environ = os.environ, env.copy()
|
self.useFixture(EnvFixture(env.copy()))
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
super(TestShellTokenEndpointAuthEnv, self).tearDown()
|
|
||||||
os.environ = self.orig_env
|
|
||||||
|
|
||||||
def test_env(self):
|
def test_env(self):
|
||||||
flag = ""
|
flag = ""
|
||||||
@ -501,11 +500,7 @@ class TestShellCli(TestShell):
|
|||||||
"OS_VOLUME_API_VERSION": DEFAULT_VOLUME_API_VERSION,
|
"OS_VOLUME_API_VERSION": DEFAULT_VOLUME_API_VERSION,
|
||||||
"OS_NETWORK_API_VERSION": DEFAULT_NETWORK_API_VERSION,
|
"OS_NETWORK_API_VERSION": DEFAULT_NETWORK_API_VERSION,
|
||||||
}
|
}
|
||||||
self.orig_env, os.environ = os.environ, env.copy()
|
self.useFixture(EnvFixture(env.copy()))
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
super(TestShellCli, self).tearDown()
|
|
||||||
os.environ = self.orig_env
|
|
||||||
|
|
||||||
def test_shell_args_no_options(self):
|
def test_shell_args_no_options(self):
|
||||||
_shell = make_shell()
|
_shell = make_shell()
|
||||||
@ -719,11 +714,7 @@ class TestShellCliEnv(TestShell):
|
|||||||
env = {
|
env = {
|
||||||
'OS_REGION_NAME': 'occ-env',
|
'OS_REGION_NAME': 'occ-env',
|
||||||
}
|
}
|
||||||
self.orig_env, os.environ = os.environ, env.copy()
|
self.useFixture(EnvFixture(env.copy()))
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
super(TestShellCliEnv, self).tearDown()
|
|
||||||
os.environ = self.orig_env
|
|
||||||
|
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
|
||||||
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")
|
||||||
|
Reference in New Issue
Block a user