Updated mock return values to decorators

Updated the return_value=True/False statements for
mocks to decorators

Change-Id: I4915e11dd4af4776a93d211c4c019213c90c5ff2
This commit is contained in:
Swapnil Kulkarni 2014-07-10 16:11:55 +00:00
parent ae659666e2
commit 1c7f196c63
8 changed files with 35 additions and 63 deletions

View File

@ -36,14 +36,11 @@ class TempestContextTestCase(test.TestCase):
@mock.patch(CONTEXT + ".os.mkdir")
@mock.patch(TEMPEST + ".Tempest.generate_config_file")
@mock.patch(TEMPEST + ".Tempest.is_configured")
@mock.patch(TEMPEST + ".Tempest.is_configured", return_value=True)
@mock.patch(TEMPEST + ".Tempest.install")
@mock.patch(TEMPEST + ".Tempest.is_installed")
@mock.patch(TEMPEST + ".Tempest.is_installed", return_value=True)
def test_setup(self, mock_is_install, mock_install, mock_is_cfg, mock_cfg,
mock_mkdir):
mock_is_install.return_value = True
mock_is_cfg.return_value = True
benchmark = tempest.Tempest(self.context)
benchmark.setup()
@ -54,11 +51,10 @@ class TempestContextTestCase(test.TestCase):
@mock.patch(CONTEXT + ".os.mkdir")
@mock.patch(TEMPEST + ".Tempest.is_configured")
@mock.patch(TEMPEST + ".Tempest.is_installed")
@mock.patch(TEMPEST + ".Tempest.is_installed", return_value=False)
@mock.patch(TEMPEST + ".Tempest.install")
def test_setup_failure_on_tempest_installation(
self, mock_install, mock_is_installed, mock_is_cfg, mock_mkdir):
mock_is_installed.return_value = False
mock_install.side_effect = exceptions.TempestSetupFailure()
benchmark = tempest.Tempest(self.context)
@ -67,13 +63,11 @@ class TempestContextTestCase(test.TestCase):
self.assertEqual(0, mock_is_cfg.call_count)
@mock.patch(CONTEXT + ".os.mkdir")
@mock.patch(TEMPEST + ".Tempest.is_configured")
@mock.patch(TEMPEST + ".Tempest.is_installed")
@mock.patch(TEMPEST + ".Tempest.is_configured", return_value=False)
@mock.patch(TEMPEST + ".Tempest.is_installed", return_value=True)
@mock.patch(TEMPEST + ".Tempest.generate_config_file")
def test_setup_failure_on_tempest_configuration(
self, mock_gen, mock_is_installed, mock_is_cfg, mock_mkdir):
mock_is_installed.return_value = True
mock_is_cfg.return_value = False
mock_gen.side_effect = exceptions.TempestConfigCreationFailure()
benchmark = tempest.Tempest(self.context)
@ -82,13 +76,11 @@ class TempestContextTestCase(test.TestCase):
self.assertEqual(1, mock_is_cfg.call_count)
@mock.patch(CONTEXT + ".os.mkdir")
@mock.patch(TEMPEST + ".Tempest.is_configured")
@mock.patch(TEMPEST + ".Tempest.is_installed")
@mock.patch(TEMPEST + ".Tempest.is_configured", return_value=False)
@mock.patch(TEMPEST + ".Tempest.is_installed", return_value=True)
@mock.patch(TEMPEST + ".Tempest.generate_config_file")
def test_setup_with_no_configuration(
self, mock_gen, mock_is_installed, mock_is_cfg, mock_mkdir):
mock_is_installed.return_value = True
mock_is_cfg.return_value = False
benchmark = tempest.Tempest(self.context)
benchmark.setup()
@ -96,14 +88,13 @@ class TempestContextTestCase(test.TestCase):
self.assertEqual('/dev/null', benchmark.verifier.log_file_raw)
self.assertEqual(1, mock_gen.call_count)
@mock.patch(CONTEXT + ".os.path.exists")
@mock.patch(CONTEXT + ".os.path.exists", return_value=True)
@mock.patch(CONTEXT + ".shutil")
@mock.patch(CONTEXT + ".subprocess")
def test_cleanup(self, mock_sp, mock_shutil, mock_os_path_exists):
benchmark = tempest.Tempest(self.context)
benchmark.verifier = mock.MagicMock()
benchmark.results_dir = "/tmp/path"
mock_os_path_exists.return_value = True
benchmark.cleanup()
@ -114,14 +105,13 @@ class TempestContextTestCase(test.TestCase):
env=benchmark.verifier.env)
mock_shutil.rmtree.assert_called_once_with("/tmp/path")
@mock.patch(CONTEXT + ".os.path.exists")
@mock.patch(CONTEXT + ".os.path.exists", return_value=False)
@mock.patch(CONTEXT + ".shutil")
@mock.patch(CONTEXT + ".subprocess")
def test_cleanup_fail(self, mock_sp, mock_shutil, mock_os_path_exists):
benchmark = tempest.Tempest(self.context)
benchmark.verifier = mock.MagicMock()
benchmark.results_dir = "/tmp/path"
mock_os_path_exists.return_value = False
benchmark.cleanup()
mock_sp.check_call.side_effect = subprocess.CalledProcessError(0, '')
self.assertRaises(subprocess.CalledProcessError, benchmark.cleanup)

View File

@ -51,9 +51,9 @@ class VMScenarioTestCase(test.TestCase):
vm_scenario.wait_for_ssh(ssh)
ssh.wait.assert_called_once_with()
@mock.patch(VMTASKS_UTILS + ".VMScenario.ping_ip_address")
@mock.patch(VMTASKS_UTILS + ".VMScenario.ping_ip_address",
return_value=True)
def test_wait_for_ping(self, mock_ping):
mock_ping.return_value = True
vm_scenario = utils.VMScenario()
vm_scenario.wait_for_ping("1.2.3.4")
self.wait_for.mock.assert_called_once_with("1.2.3.4",

View File

@ -54,7 +54,7 @@ class UseCommandsTestCase(test.TestCase):
@mock.patch('os.remove')
@mock.patch('os.symlink')
@mock.patch(MOD + 'db.deployment_get')
@mock.patch('os.path.exists')
@mock.patch('os.path.exists', return_value=True)
@mock.patch(MOD + 'fileutils.update_env_file')
def test_deployment(self, mock_env, mock_path, mock_deployment,
mock_symlink, mock_remove):
@ -65,7 +65,6 @@ class UseCommandsTestCase(test.TestCase):
'tenant_name': 'fake_tenant_name',
'region_name': None}]}
mock_deployment.return_value = endpoints
mock_path.return_value = True
with mock.patch('rally.cmd.commands.use.open', mock.mock_open(),
create=True) as mock_file:
self.use.deployment(deploy_id)
@ -91,10 +90,9 @@ class UseCommandsTestCase(test.TestCase):
self.assertEqual(1, self.use.deployment(deploy_id))
@mock.patch(MOD + 'fileutils._rewrite_env_file')
@mock.patch(MOD + 'db.task_get')
@mock.patch(MOD + 'db.task_get', return_value=True)
def test_task(self, mock_task, mock_file):
task_id = str(uuid.uuid4())
mock_task.return_value = True
self.use.task(task_id)
mock_file.assert_called_once_with(
os.path.expanduser('~/.rally/globals'),

View File

@ -85,9 +85,9 @@ class EnvUtilsTestCase(test.TestCase):
values={envutils.ENV_DEPLOYMENT: 'test_deployment_id'},
clear=True)
@mock.patch('os.path.exists')
@mock.patch('rally.cmd.envutils.fileutils.update_env_file')
@mock.patch('rally.cmd.envutils.fileutils.update_env_file',
return_value=True)
def test_clear_global(self, mock_file, mock_file_status):
mock_file_status.return_value = True
envutils.clear_global(envutils.ENV_DEPLOYMENT)
mock_file.assert_called_once_with(os.path.expanduser(
'~/.rally/globals'), envutils.ENV_DEPLOYMENT, '\n')
@ -98,8 +98,8 @@ class EnvUtilsTestCase(test.TestCase):
envutils.ENV_TASK: 'test_task_id'},
clear=True)
@mock.patch('os.path.exists')
@mock.patch('rally.cmd.envutils.fileutils.update_env_file')
@mock.patch('rally.cmd.envutils.fileutils.update_env_file',
return_value=True)
def test_clear_env(self, mock_file, mock_file_status):
mock_file_status.return_value = True
envutils.clear_env()
self.assertEqual(os.environ, {})

View File

@ -23,11 +23,10 @@ from tests import test
class FileUtilsTestCase(test.TestCase):
@mock.patch('os.path.exists')
@mock.patch('os.path.exists', return_value=True)
@mock.patch.dict('os.environ', values={}, clear=True)
def test_load_env_vile(self, mock_path):
file_data = ["FAKE_ENV=fake_env\n"]
mock_path.return_value = True
with mock.patch('rally.fileutils.open', mock.mock_open(
read_data=file_data), create=True) as mock_file:
mock_file.return_value.readlines.return_value = file_data
@ -35,10 +34,9 @@ class FileUtilsTestCase(test.TestCase):
self.assertIn('FAKE_ENV', os.environ)
mock_file.return_value.readlines.assert_called_once_with()
@mock.patch('os.path.exists')
@mock.patch('os.path.exists', return_value=True)
def test_update_env_file(self, mock_path):
file_data = ["FAKE_ENV=old_value\n", "FAKE_ENV2=any\n"]
mock_path.return_value = True
with mock.patch('rally.fileutils.open', mock.mock_open(
read_data=file_data), create=True) as mock_file:
mock_file.return_value.readlines.return_value = file_data

View File

@ -173,7 +173,7 @@ class LoadExtraModulesTestCase(test.TestCase):
@mock.patch("rally.utils.imp.load_module")
@mock.patch("rally.utils.imp.find_module")
@mock.patch("rally.utils.os.path.exists")
@mock.patch("rally.utils.os.path.exists", return_value=True)
@mock.patch("rally.utils.os.path.isfile")
@mock.patch("rally.utils.os.listdir")
def test_load_plugins_successfull(self, mock_listdir, mock_isfile,
@ -182,7 +182,6 @@ class LoadExtraModulesTestCase(test.TestCase):
mock_listdir.return_value = ["plugin1.py", "plugin2.py",
"somethingnotpythonmodule",
"somestrangedir.py"]
mock_exists.return_value = True
# check we don't try to load something that is not file
def isfile_side_effect(*args):
@ -211,11 +210,10 @@ class LoadExtraModulesTestCase(test.TestCase):
@mock.patch("rally.utils.imp.load_module")
@mock.patch("rally.utils.imp.find_module")
@mock.patch("rally.utils.os.path")
@mock.patch("rally.utils.os.path.exists", return_value=True)
@mock.patch("rally.utils.os.listdir")
def test_load_plugins_fails(self, mock_oslistdir, mock_ospath,
mock_load_module, mock_find_module):
mock_ospath.exists.return_value = True
mock_oslistdir.return_value = ["somebrokenplugin.py", ]
mock_load_module.side_effect = Exception()
# test no fails if module is broken

View File

@ -30,7 +30,8 @@ class ConfigTestCase(test.TestCase):
@mock.patch("rally.objects.deploy.db.deployment_get")
@mock.patch("rally.osclients.Clients.verified_keystone")
@mock.patch("rally.verification.verifiers.tempest.config.os.path.isfile")
@mock.patch("rally.verification.verifiers.tempest.config.os.path.isfile",
return_value=True)
def setUp(self, mock_isfile, mock_verified_keystone, mock_get):
super(ConfigTestCase, self).setUp()
self.endpoint = {"username": "test",
@ -39,7 +40,6 @@ class ConfigTestCase(test.TestCase):
"auth_url": "http://test/v2.0",
"permission": "admin"}
mock_get.return_value = {"endpoints": [self.endpoint]}
mock_isfile.return_value = True
self.deploy_id = "fake_deploy_id"
self.conf_generator = config.TempestConf(self.deploy_id)
@ -202,10 +202,10 @@ class ConfigTestCase(test.TestCase):
self.conf_generator.conf.get("compute",
"ssh_connect_method"))
@mock.patch("rally.verification.verifiers.tempest.config.os.path.exists")
@mock.patch("rally.verification.verifiers.tempest.config.os.path.exists",
return_value=False)
@mock.patch("rally.verification.verifiers.tempest.config.os.makedirs")
def test__set_default(self, mock_makedirs, mock_exists):
mock_exists.return_value = False
self.conf_generator._set_default()
lock_path = os.path.join(self.conf_generator.data_path, "lock_files_%s"
% self.deploy_id)

View File

@ -41,10 +41,8 @@ class TempestTestCase(test.TestCase):
self.verifier.log_file_raw = '/tmp/subunit.stream'
self.regex = None
@mock.patch('os.path.exists')
@mock.patch('os.path.exists', return_value=True)
def test_is_installed(self, mock_exists):
mock_exists.return_value = True
result = self.verifier.is_installed()
mock_exists.assert_called_once_with(
@ -78,9 +76,8 @@ class TempestTestCase(test.TestCase):
shell=True)
@mock.patch('rally.verification.verifiers.tempest.tempest.shutil')
@mock.patch('os.path.exists')
@mock.patch('os.path.exists', return_value=True)
def test_uninstall(self, mock_exists, mock_shutil):
mock_exists.return_value = True
self.verifier.uninstall()
mock_shutil.rmtree.assert_called_once_with(self.verifier.tempest_path)
@ -126,22 +123,20 @@ class TempestTestCase(test.TestCase):
self.verifier._generate_env()
self.assertEqual(expected_env, self.verifier._env)
@mock.patch('os.path.isdir')
@mock.patch('os.path.isdir', return_value=True)
@mock.patch(TEMPEST_PATH + '.tempest.subprocess')
@testtools.skipIf(sys.version_info < (2, 7), "Incompatible Python Version")
def test__venv_install_when_venv_exists(self, mock_sp, mock_isdir):
mock_isdir.return_value = True
self.verifier._install_venv()
mock_isdir.assert_called_once_with(
os.path.join(self.verifier.tempest_path, '.venv'))
self.assertFalse(mock_sp.called)
@mock.patch('os.path.isdir')
@mock.patch('os.path.isdir', return_value=False)
@mock.patch(TEMPEST_PATH + '.tempest.subprocess.check_call')
@testtools.skipIf(sys.version_info < (2, 7), "Incompatible Python Version")
def test__venv_install_when_venv_not_exist(self, mock_sp, mock_isdir):
mock_isdir.return_value = False
self.verifier._install_venv()
mock_isdir.assert_called_once_with(
@ -153,33 +148,30 @@ class TempestTestCase(test.TestCase):
self.verifier.venv_wrapper, shell=True,
cwd=self.verifier.tempest_path)])
@mock.patch('os.path.isdir')
@mock.patch('os.path.isdir', return_value=False)
@testtools.skipIf(sys.version_info >= (2, 7),
"Incompatible Python Version")
def test__venv_install_for_py26_fails(self, mock_isdir):
mock_isdir.return_value = False
self.assertRaises(exceptions.IncompatiblePythonVersion,
self.verifier._install_venv)
mock_isdir.assert_called_once_with(
os.path.join(self.verifier.tempest_path, '.venv'))
@mock.patch('os.path.isdir')
@mock.patch('os.path.isdir', return_value=True)
@mock.patch(TEMPEST_PATH + '.tempest.subprocess')
def test__initialize_testr_when_testr_already_initialized(
self, mock_sp, mock_isdir):
mock_isdir.return_value = True
self.verifier._initialize_testr()
mock_isdir.assert_called_once_with(
os.path.join(self.verifier.tempest_path, '.testrepository'))
self.assertFalse(mock_sp.called)
@mock.patch('os.path.isdir')
@mock.patch('os.path.isdir', return_value=False)
@mock.patch(TEMPEST_PATH + '.tempest.subprocess.check_call')
def test__initialize_testr_when_testr_not_initialized(
self, mock_sp, mock_isdir):
mock_isdir.return_value = False
self.verifier._initialize_testr()
mock_isdir.assert_called_once_with(
@ -188,20 +180,16 @@ class TempestTestCase(test.TestCase):
'%s testr init' % self.verifier.venv_wrapper, shell=True,
cwd=self.verifier.tempest_path)
@mock.patch('xml.dom.minidom.parse')
@mock.patch('os.path.isfile')
@mock.patch.object(subunit2json, 'main')
@mock.patch('os.path.isfile', return_value=False)
def test__save_results_without_log_file(self, mock_isfile, mock_parse):
mock_isfile.return_value = False
self.verifier._save_results()
mock_isfile.assert_called_once_with(self.verifier.log_file_raw)
self.assertEqual(0, mock_parse.call_count)
@mock.patch('os.path.isfile')
@mock.patch('os.path.isfile', return_value=True)
def test__save_results_with_log_file(self, mock_isfile):
with mock.patch.object(subunit2json, 'main') as mock_main:
mock_isfile.return_value = True
data = {'total': True, 'test_cases': True}
mock_main.return_value = jsonutils.dumps(data)
self.verifier.log_file_raw = os.path.join(