From 30d5a8ed044af7e140d814110d1d834d42ea9e73 Mon Sep 17 00:00:00 2001 From: Andrey Kurilin Date: Sun, 22 Mar 2020 00:44:08 +0200 Subject: [PATCH] Deprecate rally.common.yamlutils and rally.common.fileutils * rally.common.fileutils contains stuff that related only to rally.cli.envutils + openstack related helper * rally.common.yamlutils was designed for CLI purpose and should not be places under rally.common. Change-Id: I9d639d2baa9298a26e7670c5b4b356a8b56583a2 --- CHANGELOG.rst | 5 ++ rally/cli/commands/deployment.py | 11 ++- rally/cli/commands/env.py | 5 +- rally/cli/commands/task.py | 5 +- rally/cli/commands/verify.py | 7 +- rally/cli/envutils.py | 73 +++++++++++++++++++- rally/{common => cli}/yamlutils.py | 0 rally/common/fileutils.py | 71 ++----------------- rally/common/utils.py | 4 ++ tests/unit/cli/commands/test_deployment.py | 14 ++-- tests/unit/cli/commands/test_env.py | 2 +- tests/unit/cli/commands/test_task.py | 4 +- tests/unit/cli/commands/test_verify.py | 12 ++-- tests/unit/cli/test_envutils.py | 55 ++++++++++----- tests/unit/{common => cli}/test_yamlutils.py | 2 +- tests/unit/common/test_fileutils.py | 64 ----------------- tests/unit/test_mock.py | 18 ++--- tests/unit/test_test_mock.py | 6 +- 18 files changed, 163 insertions(+), 195 deletions(-) rename rally/{common => cli}/yamlutils.py (100%) rename tests/unit/{common => cli}/test_yamlutils.py (97%) delete mode 100644 tests/unit/common/test_fileutils.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e6d0548a6e..d6fe9a1e66 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -45,6 +45,11 @@ Deprecated * Module *rally.common.sshutils* is deprecated. Use *rally.utils.sshutils* instead. +* Module *rally.common.yamlutils* is deprecated. It was designed for CLI usage + and moves to right place. + +* Module *rally.common.fileutils* is deprecated. + * All modules from *rally.plugins.common.contexts* are deprecated. Use *rally.plugins.task.contexts* instead. diff --git a/rally/cli/commands/deployment.py b/rally/cli/commands/deployment.py index ff7c2a8078..655495c9ac 100644 --- a/rally/cli/commands/deployment.py +++ b/rally/cli/commands/deployment.py @@ -25,10 +25,9 @@ import jsonschema from rally.cli import cliutils from rally.cli import envutils -from rally.common import fileutils +from rally.cli import yamlutils as yaml from rally.common import logging from rally.common import utils -from rally.common import yamlutils as yaml from rally.env import env_mgr from rally import exceptions from rally import plugins @@ -326,10 +325,10 @@ class DeploymentCommands(object): return 1 print("Using deployment: %s" % deployment["uuid"]) - fileutils.update_globals_file(envutils.ENV_DEPLOYMENT, - deployment["uuid"]) - fileutils.update_globals_file(envutils.ENV_ENV, - deployment["uuid"]) + envutils.update_globals_file(envutils.ENV_DEPLOYMENT, + deployment["uuid"]) + envutils.update_globals_file(envutils.ENV_ENV, + deployment["uuid"]) if "openstack" in deployment["credentials"]: creds = deployment["credentials"]["openstack"][0] diff --git a/rally/cli/commands/env.py b/rally/cli/commands/env.py index 519830ad48..9e5014d0c9 100644 --- a/rally/cli/commands/env.py +++ b/rally/cli/commands/env.py @@ -21,8 +21,7 @@ import traceback from rally.cli import cliutils from rally.cli import envutils -from rally.common import fileutils -from rally.common import yamlutils as yaml +from rally.cli import yamlutils as yaml from rally.env import env_mgr from rally import exceptions @@ -336,4 +335,4 @@ class EnvCommands(object): def _use(self, env_uuid, to_json): _print("Using environment: %s" % env_uuid, to_json) - fileutils.update_globals_file(envutils.ENV_ENV, env_uuid) + envutils.update_globals_file(envutils.ENV_ENV, env_uuid) diff --git a/rally/cli/commands/task.py b/rally/cli/commands/task.py index 590e580be5..18f5ce1117 100644 --- a/rally/cli/commands/task.py +++ b/rally/cli/commands/task.py @@ -28,11 +28,10 @@ import jsonschema from rally.cli import cliutils from rally.cli import envutils -from rally.common import fileutils +from rally.cli import yamlutils as yaml from rally.common import logging from rally.common import utils as rutils from rally.common import version -from rally.common import yamlutils as yaml from rally import consts from rally import exceptions from rally import plugins @@ -926,7 +925,7 @@ class TaskCommands(object): print("Using task: %s" % task_id) api.task.get(task_id=task_id) - fileutils.update_globals_file("RALLY_TASK", task_id) + envutils.update_globals_file("RALLY_TASK", task_id) @cliutils.args("--uuid", dest="tasks", nargs="+", type=str, help="UUIDs of tasks or json reports of tasks") diff --git a/rally/cli/commands/verify.py b/rally/cli/commands/verify.py index 83685d5cf4..540211171b 100644 --- a/rally/cli/commands/verify.py +++ b/rally/cli/commands/verify.py @@ -25,9 +25,8 @@ import webbrowser from rally.cli import cliutils from rally.cli import envutils -from rally.common import fileutils +from rally.cli import yamlutils as yaml from rally.common import logging -from rally.common import yamlutils as yaml from rally import exceptions from rally import plugins @@ -151,7 +150,7 @@ class VerifyCommands(object): def use_verifier(self, api, verifier_id): """Choose a verifier to use for the future operations.""" verifier = api.verifier.get(verifier_id=verifier_id) - fileutils.update_globals_file(envutils.ENV_VERIFIER, verifier["uuid"]) + envutils.update_globals_file(envutils.ENV_VERIFIER, verifier["uuid"]) print("Using verifier '%s' (UUID=%s) as the default verifier " "for the future CLI operations." % (verifier["name"], verifier["uuid"])) @@ -536,7 +535,7 @@ class VerifyCommands(object): verification = api.verification.get( verification_uuid=verification_uuid) - fileutils.update_globals_file( + envutils.update_globals_file( envutils.ENV_VERIFICATION, verification["uuid"]) print("Using verification (UUID=%s) as the default verification " "for the future operations." % verification["uuid"]) diff --git a/rally/cli/envutils.py b/rally/cli/envutils.py index 4428eab795..1bd8aa533f 100755 --- a/rally/cli/envutils.py +++ b/rally/cli/envutils.py @@ -17,7 +17,6 @@ import os import decorator -from rally.common import fileutils from rally import exceptions PATH_GLOBALS = "~/.rally/globals" @@ -31,10 +30,78 @@ ENVVARS = [ENV_ENV, ENV_DEPLOYMENT, ENV_TASK, ENV_VERIFIER, ENV_VERIFICATION] MSG_MISSING_ARG = "Missing argument: --%(arg_name)s" +def _read_env_file(path, except_env=None): + """Read the environment variable file. + + :param path: the path of the file + :param except_env: the environment variable to avoid in the output + + :returns: the content of the original file except the line starting with + the except_env parameter + """ + output = [] + if os.path.exists(path): + with open(path, "r") as env_file: + content = env_file.readlines() + for line in content: + if except_env is None or not line.startswith("%s=" % + except_env): + output.append(line) + return output + + +def _load_env_file(path): + """Load the environment variable file into os.environ. + + :param path: the path of the file + """ + if os.path.exists(path): + content = _read_env_file(path) + for line in content: + (key, sep, value) = line.partition("=") + os.environ[key] = value.rstrip() + + +def _rewrite_env_file(path, initial_content): + """Rewrite the environment variable file. + + :param path: the path of the file + :param initial_content: the original content of the file + """ + with open(path, "w+") as env_file: + for line in initial_content: + env_file.write(line) + + +def _update_env_file(path, env_key, env_value): + """Update the environment variable file. + + :param path: the path of the file + :param env_key: the key to update + :param env_value: the value of the property to update + """ + output = _read_env_file(path, env_key) + output.append("%s=%s" % (env_key, env_value)) + _rewrite_env_file(path, output) + + +def update_globals_file(key, value): + """Update the globals variables file. + + :param key: the key to update + :param value: the value to update + """ + dir = os.path.expanduser("~/.rally/") + if not os.path.exists(dir): + os.makedirs(dir) + expanded_path = os.path.join(dir, "globals") + _update_env_file(expanded_path, key, "%s\n" % value) + + def clear_global(global_key): path = os.path.expanduser(PATH_GLOBALS) if os.path.exists(path): - fileutils.update_env_file(path, global_key, "\n") + _update_env_file(path, global_key, "\n") if global_key in os.environ: os.environ.pop(global_key) @@ -46,7 +113,7 @@ def clear_env(): def get_global(global_key, do_raise=False): if global_key not in os.environ: - fileutils.load_env_file(os.path.expanduser(PATH_GLOBALS)) + _load_env_file(os.path.expanduser(PATH_GLOBALS)) value = os.environ.get(global_key) if not value and do_raise: raise exceptions.InvalidArgumentsException("%s env is missing" diff --git a/rally/common/yamlutils.py b/rally/cli/yamlutils.py similarity index 100% rename from rally/common/yamlutils.py rename to rally/cli/yamlutils.py diff --git a/rally/common/fileutils.py b/rally/common/fileutils.py index 8f70a01c0a..12cf8869d2 100644 --- a/rally/common/fileutils.py +++ b/rally/common/fileutils.py @@ -15,75 +15,14 @@ import os import tempfile +import warnings import zipfile -def _read_env_file(path, except_env=None): - """Read the environment variable file. - - :param path: the path of the file - :param except_env: the environment variable to avoid in the output - - :returns: the content of the original file except the line starting with - the except_env parameter - """ - output = [] - if os.path.exists(path): - with open(path, "r") as env_file: - content = env_file.readlines() - for line in content: - if except_env is None or not line.startswith("%s=" % - except_env): - output.append(line) - return output - - -def load_env_file(path): - """Load the environment variable file into os.environ. - - :param path: the path of the file - """ - if os.path.exists(path): - content = _read_env_file(path) - for line in content: - (key, sep, value) = line.partition("=") - os.environ[key] = value.rstrip() - - -def _rewrite_env_file(path, initial_content): - """Rewrite the environment variable file. - - :param path: the path of the file - :param initial_content: the original content of the file - """ - with open(path, "w+") as env_file: - for line in initial_content: - env_file.write(line) - - -def update_env_file(path, env_key, env_value): - """Update the environment variable file. - - :param path: the path of the file - :param env_key: the key to update - :param env_value: the value of the property to update - """ - output = _read_env_file(path, env_key) - output.append("%s=%s" % (env_key, env_value)) - _rewrite_env_file(path, output) - - -def update_globals_file(key, value): - """Update the globals variables file. - - :param key: the key to update - :param value: the value to update - """ - dir = os.path.expanduser("~/.rally/") - if not os.path.exists(dir): - os.makedirs(dir) - expanded_path = os.path.join(dir, "globals") - update_env_file(expanded_path, key, "%s\n" % value) +warnings.warn( + f"Module `{__name__}` is deprecated since Rally v3.0.0 and may be " + f"removed in further releases." +) def pack_dir(source_directory, zip_name=None): diff --git a/rally/common/utils.py b/rally/common/utils.py index 35483c35f5..01059000b0 100644 --- a/rally/common/utils.py +++ b/rally/common/utils.py @@ -156,6 +156,7 @@ class RAMInt(object): self.__int.value = 0 +@logging.log_deprecated("it was an inner helper.", rally_version="3.0.0") def get_method_class(func): """Return the class that defined the given method. @@ -178,6 +179,7 @@ def get_method_class(func): return None +@logging.log_deprecated("it was an inner helper.", rally_version="3.0.0") def first_index(lst, predicate): """Return the index of the first element that matches a predicate. @@ -212,6 +214,7 @@ def retry(times, func, *args, **kwargs): raise +@logging.log_deprecated("it is openstack specific.", rally_version="3.0.0") def iterate_per_tenants(users): """Iterate of a single arbitrary user from each tenant @@ -425,6 +428,7 @@ def make_name_matcher(*names): return CustomNameMatcher +@logging.log_deprecated("it was an inner helper.", rally_version="3.0.0") def merge(length, *sources): """Merge lists of lists. diff --git a/tests/unit/cli/commands/test_deployment.py b/tests/unit/cli/commands/test_deployment.py index cfcd5a45b5..0479ce6038 100755 --- a/tests/unit/cli/commands/test_deployment.py +++ b/tests/unit/cli/commands/test_deployment.py @@ -243,8 +243,8 @@ class DeploymentCommandsTestCase(test.TestCase): @mock.patch("os.remove") @mock.patch("os.symlink") @mock.patch("os.path.exists", return_value=True) - @mock.patch("rally.common.fileutils.update_env_file") - def test_use(self, mock_update_env_file, mock_path_exists, + @mock.patch("rally.cli.envutils._update_env_file") + def test_use(self, mock__update_env_file, mock_path_exists, mock_symlink, mock_remove): deployment_id = "593b683c-4b16-4b2b-a56b-e162bd60f10b" self.fake_api.deployment.get.return_value = { @@ -262,7 +262,7 @@ class DeploymentCommandsTestCase(test.TestCase): create=True) as mock_file: self.deployment.use(self.fake_api, deployment_id) self.assertEqual(3, mock_path_exists.call_count) - mock_update_env_file.assert_has_calls([ + mock__update_env_file.assert_has_calls([ mock.call(os.path.expanduser("~/.rally/globals"), "RALLY_DEPLOYMENT", "%s\n" % deployment_id), mock.call(os.path.expanduser("~/.rally/globals"), @@ -286,8 +286,8 @@ class DeploymentCommandsTestCase(test.TestCase): @mock.patch("os.remove") @mock.patch("os.symlink") @mock.patch("os.path.exists", return_value=True) - @mock.patch("rally.common.fileutils.update_env_file") - def test_use_with_v3_auth(self, mock_update_env_file, mock_path_exists, + @mock.patch("rally.cli.envutils._update_env_file") + def test_use_with_v3_auth(self, mock__update_env_file, mock_path_exists, mock_symlink, mock_remove): deployment_id = "593b683c-4b16-4b2b-a56b-e162bd60f10b" @@ -308,7 +308,7 @@ class DeploymentCommandsTestCase(test.TestCase): create=True) as mock_file: self.deployment.use(self.fake_api, deployment_id) self.assertEqual(3, mock_path_exists.call_count) - mock_update_env_file.assert_has_calls([ + mock__update_env_file.assert_has_calls([ mock.call(os.path.expanduser("~/.rally/globals"), "RALLY_DEPLOYMENT", "%s\n" % deployment_id), mock.call(os.path.expanduser("~/.rally/globals"), @@ -334,7 +334,7 @@ class DeploymentCommandsTestCase(test.TestCase): @mock.patch("rally.cli.commands.deployment.DeploymentCommands." "_update_openrc_deployment_file") - @mock.patch("rally.common.fileutils.update_globals_file") + @mock.patch("rally.cli.envutils.update_globals_file") def test_use_by_name(self, mock_update_globals_file, mock__update_openrc_deployment_file): fake_credentials = {"admin": "foo_admin", "users": ["foo_user"]} diff --git a/tests/unit/cli/commands/test_env.py b/tests/unit/cli/commands/test_env.py index c2c48da5f9..baa1e759f1 100644 --- a/tests/unit/cli/commands/test_env.py +++ b/tests/unit/cli/commands/test_env.py @@ -494,7 +494,7 @@ class EnvCommandsTestCase(test.TestCase): mock__use.assert_has_calls( [mock.call("aa", False), mock.call("bb", True)]) - @mock.patch("rally.cli.commands.env.fileutils.update_globals_file") + @mock.patch("rally.cli.commands.env.envutils.update_globals_file") @mock.patch("rally.cli.commands.env.print") def test__use(self, mock_print, mock_update_globals_file): self.env._use("aa", True) diff --git a/tests/unit/cli/commands/test_task.py b/tests/unit/cli/commands/test_task.py index e9df76e6d4..33ad27dc7c 100644 --- a/tests/unit/cli/commands/test_task.py +++ b/tests/unit/cli/commands/test_task.py @@ -26,7 +26,7 @@ import rally from rally import api from rally.cli import cliutils from rally.cli.commands import task -from rally.common import yamlutils as yaml +from rally.cli import yamlutils as yaml from rally import consts from rally import exceptions from tests.unit import fakes @@ -859,7 +859,7 @@ class TaskCommandsTestCase(test.TestCase): deployment="deployment", config=mock__load_and_validate_task.return_value) - @mock.patch("rally.common.fileutils._rewrite_env_file") + @mock.patch("rally.cli.envutils._rewrite_env_file") def test_use(self, mock__rewrite_env_file): task_id = "80422553-5774-44bd-98ac-38bd8c7a0feb" self.task.use(self.fake_api, task_id) diff --git a/tests/unit/cli/commands/test_verify.py b/tests/unit/cli/commands/test_verify.py index 6d5cc25e90..088277c48e 100644 --- a/tests/unit/cli/commands/test_verify.py +++ b/tests/unit/cli/commands/test_verify.py @@ -129,7 +129,7 @@ class VerifyCommandsTestCase(test.TestCase): self.fake_api.verifier.list_plugins.assert_called_once_with( platform="some") - @mock.patch("rally.cli.commands.verify.fileutils.update_globals_file") + @mock.patch("rally.cli.commands.verify.envutils.update_globals_file") def test_create_verifier(self, mock_update_globals_file): self.fake_api.verifier.create.return_value = self.verifier_uuid self.fake_api.verifier.get.return_value = self.verifier_data @@ -146,7 +146,7 @@ class VerifyCommandsTestCase(test.TestCase): mock_update_globals_file.assert_called_once_with( envutils.ENV_VERIFIER, self.verifier_uuid) - @mock.patch("rally.cli.commands.verify.fileutils.update_globals_file") + @mock.patch("rally.cli.commands.verify.envutils.update_globals_file") def test_use_verifier(self, mock_update_globals_file): self.fake_api.verifier.get.return_value = self.verifier_data self.verify.use_verifier(self.fake_api, self.verifier_uuid) @@ -380,7 +380,7 @@ class VerifyCommandsTestCase(test.TestCase): self.fake_api.verifier.delete_extension.assert_called_once_with( verifier_id=self.verifier_uuid, name="ext_name") - @mock.patch("rally.cli.commands.verify.fileutils.update_globals_file") + @mock.patch("rally.cli.commands.verify.envutils.update_globals_file") @mock.patch("rally.cli.commands.verify.os.path.exists") def test_start(self, mock_exists, mock_update_globals_file): self.verify.start(self.fake_api, self.verifier_uuid, @@ -462,7 +462,7 @@ class VerifyCommandsTestCase(test.TestCase): self.assertFalse(mock_update_globals_file.called) @mock.patch("rally.cli.commands.verify.os.path.exists") - @mock.patch("rally.cli.commands.verify.fileutils.update_globals_file") + @mock.patch("rally.cli.commands.verify.envutils.update_globals_file") def test_start_on_unfinished_deployment(self, mock_update_globals_file, mock_exists): deployment_id = self.deployment_uuid @@ -476,7 +476,7 @@ class VerifyCommandsTestCase(test.TestCase): 1, self.verify.start(self.fake_api, self.deployment_uuid, deployment_id)) - @mock.patch("rally.cli.commands.verify.fileutils.update_globals_file") + @mock.patch("rally.cli.commands.verify.envutils.update_globals_file") def test_use(self, mock_update_globals_file): self.fake_api.verification.get.return_value = self.verification_data self.verify.use(self.fake_api, self.verification_uuid) @@ -485,7 +485,7 @@ class VerifyCommandsTestCase(test.TestCase): mock_update_globals_file.assert_called_once_with( envutils.ENV_VERIFICATION, self.verification_uuid) - @mock.patch("rally.cli.commands.verify.fileutils.update_globals_file") + @mock.patch("rally.cli.commands.verify.envutils.update_globals_file") def test_rerun(self, mock_update_globals_file): self.fake_api.verification.rerun.return_value = { "verification": self.verification_data, diff --git a/tests/unit/cli/test_envutils.py b/tests/unit/cli/test_envutils.py index c79c4b649a..efb7667c12 100644 --- a/tests/unit/cli/test_envutils.py +++ b/tests/unit/cli/test_envutils.py @@ -24,6 +24,27 @@ from tests.unit import test class EnvUtilsTestCase(test.TestCase): + @mock.patch("os.path.exists", return_value=True) + @mock.patch.dict("os.environ", values={}, clear=True) + def test_load_env_vile(self, mock_exists): + file_data = "FAKE_ENV=fake_env\n" + with mock.patch("rally.cli.envutils.open", mock.mock_open( + read_data=file_data), create=True) as mock_file: + envutils._load_env_file("path_to_file") + self.assertIn("FAKE_ENV", os.environ) + mock_file.return_value.readlines.assert_called_once_with() + + @mock.patch("os.path.exists", return_value=True) + def test_update_env_file(self, mock_exists): + file_data = "FAKE_ENV=old_value\nFAKE_ENV2=any\n" + with mock.patch("rally.cli.envutils.open", mock.mock_open( + read_data=file_data), create=True) as mock_file: + envutils._update_env_file("path_to_file", "FAKE_ENV", "new_value") + calls = [mock.call("FAKE_ENV2=any\n"), mock.call( + "FAKE_ENV=new_value")] + mock_file.return_value.readlines.assert_called_once_with() + mock_file.return_value.write.assert_has_calls(calls) + def test_default_from_global(self): @envutils.default_from_global("test_arg_name", @@ -46,18 +67,18 @@ class EnvUtilsTestCase(test.TestCase): self.assertEqual("my_deployment_id", deployment_id) @mock.patch.dict(os.environ, values={}, clear=True) - @mock.patch("rally.cli.envutils.fileutils.load_env_file") - def test_get_deployment_id_with_exception(self, mock_load_env_file): + @mock.patch("rally.cli.envutils._load_env_file") + def test_get_deployment_id_with_exception(self, mock__load_env_file): self.assertRaises(exceptions.InvalidArgumentsException, envutils.get_global, envutils.ENV_DEPLOYMENT, True) - mock_load_env_file.assert_called_once_with(os.path.expanduser( + mock__load_env_file.assert_called_once_with(os.path.expanduser( "~/.rally/globals")) @mock.patch.dict(os.environ, values={}, clear=True) - @mock.patch("rally.cli.envutils.fileutils.load_env_file") - def test_get_deployment_id_with_none(self, mock_load_env_file): + @mock.patch("rally.cli.envutils._load_env_file") + def test_get_deployment_id_with_none(self, mock__load_env_file): self.assertIsNone(envutils.get_global(envutils.ENV_DEPLOYMENT)) - mock_load_env_file.assert_called_once_with(os.path.expanduser( + mock__load_env_file.assert_called_once_with(os.path.expanduser( "~/.rally/globals")) @mock.patch.dict(os.environ, values={envutils.ENV_TASK: "my_task_id"}, @@ -66,29 +87,29 @@ class EnvUtilsTestCase(test.TestCase): self.assertEqual("my_task_id", envutils.get_global(envutils.ENV_TASK)) @mock.patch.dict(os.environ, values={}, clear=True) - @mock.patch("rally.cli.envutils.fileutils.load_env_file") - def test_get_task_id_with_exception(self, mock_load_env_file): + @mock.patch("rally.cli.envutils._load_env_file") + def test_get_task_id_with_exception(self, mock__load_env_file): self.assertRaises(exceptions.InvalidArgumentsException, envutils.get_global, envutils.ENV_TASK, True) - mock_load_env_file.assert_called_once_with(os.path.expanduser( + mock__load_env_file.assert_called_once_with(os.path.expanduser( "~/.rally/globals")) @mock.patch.dict(os.environ, values={}, clear=True) - @mock.patch("rally.cli.envutils.fileutils.load_env_file") - def test_get_task_id_with_none(self, mock_load_env_file): + @mock.patch("rally.cli.envutils._load_env_file") + def test_get_task_id_with_none(self, mock__load_env_file): self.assertIsNone(envutils.get_global("RALLY_TASK")) - mock_load_env_file.assert_called_once_with(os.path.expanduser( + mock__load_env_file.assert_called_once_with(os.path.expanduser( "~/.rally/globals")) @mock.patch.dict(os.environ, values={envutils.ENV_DEPLOYMENT: "test_deployment_id"}, clear=True) @mock.patch("os.path.exists") - @mock.patch("rally.cli.envutils.fileutils.update_env_file", + @mock.patch("rally.cli.envutils._update_env_file", return_value=True) - def test_clear_global(self, mock_update_env_file, mock_path_exists): + def test_clear_global(self, mock__update_env_file, mock_path_exists): envutils.clear_global(envutils.ENV_DEPLOYMENT) - mock_update_env_file.assert_called_once_with(os.path.expanduser( + mock__update_env_file.assert_called_once_with(os.path.expanduser( "~/.rally/globals"), envutils.ENV_DEPLOYMENT, "\n") self.assertEqual({}, os.environ) @@ -97,8 +118,8 @@ class EnvUtilsTestCase(test.TestCase): envutils.ENV_TASK: "test_task_id"}, clear=True) @mock.patch("os.path.exists") - @mock.patch("rally.cli.envutils.fileutils.update_env_file", + @mock.patch("rally.cli.envutils._update_env_file", return_value=True) - def test_clear_env(self, mock_update_env_file, mock_path_exists): + def test_clear_env(self, mock__update_env_file, mock_path_exists): envutils.clear_env() self.assertEqual({}, os.environ) diff --git a/tests/unit/common/test_yamlutils.py b/tests/unit/cli/test_yamlutils.py similarity index 97% rename from tests/unit/common/test_yamlutils.py rename to tests/unit/cli/test_yamlutils.py index 9ab4a4e08c..3d3da56fb8 100644 --- a/tests/unit/common/test_yamlutils.py +++ b/tests/unit/cli/test_yamlutils.py @@ -12,7 +12,7 @@ from yaml import constructor -from rally.common import yamlutils +from rally.cli import yamlutils from tests.unit import test diff --git a/tests/unit/common/test_fileutils.py b/tests/unit/common/test_fileutils.py deleted file mode 100644 index 0394e48ae5..0000000000 --- a/tests/unit/common/test_fileutils.py +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright 2013: Mirantis Inc. -# 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. - -import os -from unittest import mock - -from rally.common import fileutils -from tests.unit import test - - -class FileUtilsTestCase(test.TestCase): - - @mock.patch("os.path.exists", return_value=True) - @mock.patch.dict("os.environ", values={}, clear=True) - def test_load_env_vile(self, mock_exists): - file_data = "FAKE_ENV=fake_env\n" - with mock.patch("rally.common.fileutils.open", mock.mock_open( - read_data=file_data), create=True) as mock_file: - fileutils.load_env_file("path_to_file") - self.assertIn("FAKE_ENV", os.environ) - mock_file.return_value.readlines.assert_called_once_with() - - @mock.patch("os.path.exists", return_value=True) - def test_update_env_file(self, mock_exists): - file_data = "FAKE_ENV=old_value\nFAKE_ENV2=any\n" - with mock.patch("rally.common.fileutils.open", mock.mock_open( - read_data=file_data), create=True) as mock_file: - fileutils.update_env_file("path_to_file", "FAKE_ENV", "new_value") - calls = [mock.call("FAKE_ENV2=any\n"), mock.call( - "FAKE_ENV=new_value")] - mock_file.return_value.readlines.assert_called_once_with() - mock_file.return_value.write.assert_has_calls(calls) - - -class PackDirTestCase(test.TestCase): - - @mock.patch("os.walk") - @mock.patch("zipfile.ZipFile") - def test_pack_dir(self, mock_zip_file, mock_walk): - mock_walk.side_effect = [ - [("foo_root", [], ["file1", "file2", "file3"])]] - fileutils.pack_dir("rally-jobs/extra/murano/HelloReporter", - "fake_dir/package.zip") - mock_zip_file.assert_called_once_with("fake_dir/package.zip", - mode="w") - mock_walk.assert_called_once_with( - "rally-jobs/extra/murano/HelloReporter") - mock_zip_file.return_value.assert_has_calls( - [mock.call.write("foo_root/file1", "../../../../foo_root/file1"), - mock.call.write("foo_root/file2", "../../../../foo_root/file2"), - mock.call.write("foo_root/file3", "../../../../foo_root/file3"), - mock.call.close()]) diff --git a/tests/unit/test_mock.py b/tests/unit/test_mock.py index 1a98b1451d..8299e3f4f0 100644 --- a/tests/unit/test_mock.py +++ b/tests/unit/test_mock.py @@ -241,7 +241,7 @@ class FuncMockArgsDecoratorsChecker(ast.NodeVisitor): self.generic_visit(node) def check_name(self, arg, dec_vars): - return (dec_vars is not None and arg in dec_vars) + return dec_vars is not None and arg in dec_vars def visit_FunctionDef(self, node): self.generic_visit(node) @@ -263,19 +263,19 @@ class FuncMockArgsDecoratorsChecker(ast.NodeVisitor): shortest_name = sorted_by_len.pop() if len(shortest_name) <= self.SHORTEST_VARIANT_LEN_LIMIT: error_msgs.append( - ("Argument '%(arg)s' misnamed; should be either " - "of %(dec)s that is derived from the mock " - "decorator args.\n") % {"arg": arg, - "dec": dec_vars}) + (f"Argument 'mock_{arg}' misnamed; should be " + f"either of {dec_vars} that is derived from the " + f"mock decorator args.\n") + ) elif not arg: error_msgs.append( - "Missing or malformed argument for %s decorator." - % dec_vars) + f"Missing or malformed argument for {dec_vars} " + f"decorator.") mismatched = True elif not dec_vars: error_msgs.append( - "Missing or malformed decorator for '%s' argument." - % arg) + f"Missing or malformed decorator for 'mock_{arg}' " + f"argument.") mismatched = True if error_msgs: diff --git a/tests/unit/test_test_mock.py b/tests/unit/test_test_mock.py index e8b81e081d..287e30261d 100644 --- a/tests/unit/test_test_mock.py +++ b/tests/unit/test_test_mock.py @@ -337,7 +337,7 @@ def test_func(self, mock_args, mock_args2, mock_some_longer_args): { "lineno": lineno, "messages": [ - "Argument 'bar_foo_misnamed' misnamed; should be " + "Argument 'mock_bar_foo_misnamed' misnamed; should be " "either of %s that is derived from the mock decorator " "args.\n" % variants ], @@ -377,10 +377,10 @@ def test_func(self, mock_args, mock_args2, mock_some_longer_args): { "lineno": lineno, "messages": [ - "Argument 'bar_foo_misnamed' misnamed; should be " + "Argument 'mock_bar_foo_misnamed' misnamed; should be " "either of %s that is derived from the mock decorator " "args.\n" % variants, - "Missing or malformed decorator for 'mismatched' " + "Missing or malformed decorator for 'mock_mismatched' " "argument." ], "args": self.visitor._get_mock_args.return_value,