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
This commit is contained in:
Andrey Kurilin 2020-03-22 00:44:08 +02:00
parent 4a466f2bc5
commit 30d5a8ed04
18 changed files with 163 additions and 195 deletions

View File

@ -45,6 +45,11 @@ Deprecated
* Module *rally.common.sshutils* is deprecated. Use *rally.utils.sshutils* * Module *rally.common.sshutils* is deprecated. Use *rally.utils.sshutils*
instead. 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 * All modules from *rally.plugins.common.contexts* are deprecated. Use
*rally.plugins.task.contexts* instead. *rally.plugins.task.contexts* instead.

View File

@ -25,10 +25,9 @@ import jsonschema
from rally.cli import cliutils from rally.cli import cliutils
from rally.cli import envutils 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 logging
from rally.common import utils from rally.common import utils
from rally.common import yamlutils as yaml
from rally.env import env_mgr from rally.env import env_mgr
from rally import exceptions from rally import exceptions
from rally import plugins from rally import plugins
@ -326,10 +325,10 @@ class DeploymentCommands(object):
return 1 return 1
print("Using deployment: %s" % deployment["uuid"]) print("Using deployment: %s" % deployment["uuid"])
fileutils.update_globals_file(envutils.ENV_DEPLOYMENT, envutils.update_globals_file(envutils.ENV_DEPLOYMENT,
deployment["uuid"]) deployment["uuid"])
fileutils.update_globals_file(envutils.ENV_ENV, envutils.update_globals_file(envutils.ENV_ENV,
deployment["uuid"]) deployment["uuid"])
if "openstack" in deployment["credentials"]: if "openstack" in deployment["credentials"]:
creds = deployment["credentials"]["openstack"][0] creds = deployment["credentials"]["openstack"][0]

View File

@ -21,8 +21,7 @@ import traceback
from rally.cli import cliutils from rally.cli import cliutils
from rally.cli import envutils from rally.cli import envutils
from rally.common import fileutils from rally.cli import yamlutils as yaml
from rally.common import yamlutils as yaml
from rally.env import env_mgr from rally.env import env_mgr
from rally import exceptions from rally import exceptions
@ -336,4 +335,4 @@ class EnvCommands(object):
def _use(self, env_uuid, to_json): def _use(self, env_uuid, to_json):
_print("Using environment: %s" % 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)

View File

@ -28,11 +28,10 @@ import jsonschema
from rally.cli import cliutils from rally.cli import cliutils
from rally.cli import envutils 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 logging
from rally.common import utils as rutils from rally.common import utils as rutils
from rally.common import version from rally.common import version
from rally.common import yamlutils as yaml
from rally import consts from rally import consts
from rally import exceptions from rally import exceptions
from rally import plugins from rally import plugins
@ -926,7 +925,7 @@ class TaskCommands(object):
print("Using task: %s" % task_id) print("Using task: %s" % task_id)
api.task.get(task_id=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, @cliutils.args("--uuid", dest="tasks", nargs="+", type=str,
help="UUIDs of tasks or json reports of tasks") help="UUIDs of tasks or json reports of tasks")

View File

@ -25,9 +25,8 @@ import webbrowser
from rally.cli import cliutils from rally.cli import cliutils
from rally.cli import envutils 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 logging
from rally.common import yamlutils as yaml
from rally import exceptions from rally import exceptions
from rally import plugins from rally import plugins
@ -151,7 +150,7 @@ class VerifyCommands(object):
def use_verifier(self, api, verifier_id): def use_verifier(self, api, verifier_id):
"""Choose a verifier to use for the future operations.""" """Choose a verifier to use for the future operations."""
verifier = api.verifier.get(verifier_id=verifier_id) 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 " print("Using verifier '%s' (UUID=%s) as the default verifier "
"for the future CLI operations." "for the future CLI operations."
% (verifier["name"], verifier["uuid"])) % (verifier["name"], verifier["uuid"]))
@ -536,7 +535,7 @@ class VerifyCommands(object):
verification = api.verification.get( verification = api.verification.get(
verification_uuid=verification_uuid) verification_uuid=verification_uuid)
fileutils.update_globals_file( envutils.update_globals_file(
envutils.ENV_VERIFICATION, verification["uuid"]) envutils.ENV_VERIFICATION, verification["uuid"])
print("Using verification (UUID=%s) as the default verification " print("Using verification (UUID=%s) as the default verification "
"for the future operations." % verification["uuid"]) "for the future operations." % verification["uuid"])

View File

@ -17,7 +17,6 @@ import os
import decorator import decorator
from rally.common import fileutils
from rally import exceptions from rally import exceptions
PATH_GLOBALS = "~/.rally/globals" 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" 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): def clear_global(global_key):
path = os.path.expanduser(PATH_GLOBALS) path = os.path.expanduser(PATH_GLOBALS)
if os.path.exists(path): 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: if global_key in os.environ:
os.environ.pop(global_key) os.environ.pop(global_key)
@ -46,7 +113,7 @@ def clear_env():
def get_global(global_key, do_raise=False): def get_global(global_key, do_raise=False):
if global_key not in os.environ: 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) value = os.environ.get(global_key)
if not value and do_raise: if not value and do_raise:
raise exceptions.InvalidArgumentsException("%s env is missing" raise exceptions.InvalidArgumentsException("%s env is missing"

View File

@ -15,75 +15,14 @@
import os import os
import tempfile import tempfile
import warnings
import zipfile import zipfile
def _read_env_file(path, except_env=None): warnings.warn(
"""Read the environment variable file. f"Module `{__name__}` is deprecated since Rally v3.0.0 and may be "
f"removed in further releases."
: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 pack_dir(source_directory, zip_name=None): def pack_dir(source_directory, zip_name=None):

View File

@ -156,6 +156,7 @@ class RAMInt(object):
self.__int.value = 0 self.__int.value = 0
@logging.log_deprecated("it was an inner helper.", rally_version="3.0.0")
def get_method_class(func): def get_method_class(func):
"""Return the class that defined the given method. """Return the class that defined the given method.
@ -178,6 +179,7 @@ def get_method_class(func):
return None return None
@logging.log_deprecated("it was an inner helper.", rally_version="3.0.0")
def first_index(lst, predicate): def first_index(lst, predicate):
"""Return the index of the first element that matches a predicate. """Return the index of the first element that matches a predicate.
@ -212,6 +214,7 @@ def retry(times, func, *args, **kwargs):
raise raise
@logging.log_deprecated("it is openstack specific.", rally_version="3.0.0")
def iterate_per_tenants(users): def iterate_per_tenants(users):
"""Iterate of a single arbitrary user from each tenant """Iterate of a single arbitrary user from each tenant
@ -425,6 +428,7 @@ def make_name_matcher(*names):
return CustomNameMatcher return CustomNameMatcher
@logging.log_deprecated("it was an inner helper.", rally_version="3.0.0")
def merge(length, *sources): def merge(length, *sources):
"""Merge lists of lists. """Merge lists of lists.

View File

@ -243,8 +243,8 @@ class DeploymentCommandsTestCase(test.TestCase):
@mock.patch("os.remove") @mock.patch("os.remove")
@mock.patch("os.symlink") @mock.patch("os.symlink")
@mock.patch("os.path.exists", return_value=True) @mock.patch("os.path.exists", return_value=True)
@mock.patch("rally.common.fileutils.update_env_file") @mock.patch("rally.cli.envutils._update_env_file")
def test_use(self, mock_update_env_file, mock_path_exists, def test_use(self, mock__update_env_file, mock_path_exists,
mock_symlink, mock_remove): mock_symlink, mock_remove):
deployment_id = "593b683c-4b16-4b2b-a56b-e162bd60f10b" deployment_id = "593b683c-4b16-4b2b-a56b-e162bd60f10b"
self.fake_api.deployment.get.return_value = { self.fake_api.deployment.get.return_value = {
@ -262,7 +262,7 @@ class DeploymentCommandsTestCase(test.TestCase):
create=True) as mock_file: create=True) as mock_file:
self.deployment.use(self.fake_api, deployment_id) self.deployment.use(self.fake_api, deployment_id)
self.assertEqual(3, mock_path_exists.call_count) 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"), mock.call(os.path.expanduser("~/.rally/globals"),
"RALLY_DEPLOYMENT", "%s\n" % deployment_id), "RALLY_DEPLOYMENT", "%s\n" % deployment_id),
mock.call(os.path.expanduser("~/.rally/globals"), mock.call(os.path.expanduser("~/.rally/globals"),
@ -286,8 +286,8 @@ class DeploymentCommandsTestCase(test.TestCase):
@mock.patch("os.remove") @mock.patch("os.remove")
@mock.patch("os.symlink") @mock.patch("os.symlink")
@mock.patch("os.path.exists", return_value=True) @mock.patch("os.path.exists", return_value=True)
@mock.patch("rally.common.fileutils.update_env_file") @mock.patch("rally.cli.envutils._update_env_file")
def test_use_with_v3_auth(self, mock_update_env_file, mock_path_exists, def test_use_with_v3_auth(self, mock__update_env_file, mock_path_exists,
mock_symlink, mock_remove): mock_symlink, mock_remove):
deployment_id = "593b683c-4b16-4b2b-a56b-e162bd60f10b" deployment_id = "593b683c-4b16-4b2b-a56b-e162bd60f10b"
@ -308,7 +308,7 @@ class DeploymentCommandsTestCase(test.TestCase):
create=True) as mock_file: create=True) as mock_file:
self.deployment.use(self.fake_api, deployment_id) self.deployment.use(self.fake_api, deployment_id)
self.assertEqual(3, mock_path_exists.call_count) 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"), mock.call(os.path.expanduser("~/.rally/globals"),
"RALLY_DEPLOYMENT", "%s\n" % deployment_id), "RALLY_DEPLOYMENT", "%s\n" % deployment_id),
mock.call(os.path.expanduser("~/.rally/globals"), mock.call(os.path.expanduser("~/.rally/globals"),
@ -334,7 +334,7 @@ class DeploymentCommandsTestCase(test.TestCase):
@mock.patch("rally.cli.commands.deployment.DeploymentCommands." @mock.patch("rally.cli.commands.deployment.DeploymentCommands."
"_update_openrc_deployment_file") "_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, def test_use_by_name(self, mock_update_globals_file,
mock__update_openrc_deployment_file): mock__update_openrc_deployment_file):
fake_credentials = {"admin": "foo_admin", "users": ["foo_user"]} fake_credentials = {"admin": "foo_admin", "users": ["foo_user"]}

View File

@ -494,7 +494,7 @@ class EnvCommandsTestCase(test.TestCase):
mock__use.assert_has_calls( mock__use.assert_has_calls(
[mock.call("aa", False), mock.call("bb", True)]) [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") @mock.patch("rally.cli.commands.env.print")
def test__use(self, mock_print, mock_update_globals_file): def test__use(self, mock_print, mock_update_globals_file):
self.env._use("aa", True) self.env._use("aa", True)

View File

@ -26,7 +26,7 @@ import rally
from rally import api from rally import api
from rally.cli import cliutils from rally.cli import cliutils
from rally.cli.commands import task 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 consts
from rally import exceptions from rally import exceptions
from tests.unit import fakes from tests.unit import fakes
@ -859,7 +859,7 @@ class TaskCommandsTestCase(test.TestCase):
deployment="deployment", deployment="deployment",
config=mock__load_and_validate_task.return_value) 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): def test_use(self, mock__rewrite_env_file):
task_id = "80422553-5774-44bd-98ac-38bd8c7a0feb" task_id = "80422553-5774-44bd-98ac-38bd8c7a0feb"
self.task.use(self.fake_api, task_id) self.task.use(self.fake_api, task_id)

View File

@ -129,7 +129,7 @@ class VerifyCommandsTestCase(test.TestCase):
self.fake_api.verifier.list_plugins.assert_called_once_with( self.fake_api.verifier.list_plugins.assert_called_once_with(
platform="some") 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): def test_create_verifier(self, mock_update_globals_file):
self.fake_api.verifier.create.return_value = self.verifier_uuid self.fake_api.verifier.create.return_value = self.verifier_uuid
self.fake_api.verifier.get.return_value = self.verifier_data 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( mock_update_globals_file.assert_called_once_with(
envutils.ENV_VERIFIER, self.verifier_uuid) 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): def test_use_verifier(self, mock_update_globals_file):
self.fake_api.verifier.get.return_value = self.verifier_data self.fake_api.verifier.get.return_value = self.verifier_data
self.verify.use_verifier(self.fake_api, self.verifier_uuid) 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( self.fake_api.verifier.delete_extension.assert_called_once_with(
verifier_id=self.verifier_uuid, name="ext_name") 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") @mock.patch("rally.cli.commands.verify.os.path.exists")
def test_start(self, mock_exists, mock_update_globals_file): def test_start(self, mock_exists, mock_update_globals_file):
self.verify.start(self.fake_api, self.verifier_uuid, self.verify.start(self.fake_api, self.verifier_uuid,
@ -462,7 +462,7 @@ class VerifyCommandsTestCase(test.TestCase):
self.assertFalse(mock_update_globals_file.called) self.assertFalse(mock_update_globals_file.called)
@mock.patch("rally.cli.commands.verify.os.path.exists") @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, def test_start_on_unfinished_deployment(self, mock_update_globals_file,
mock_exists): mock_exists):
deployment_id = self.deployment_uuid deployment_id = self.deployment_uuid
@ -476,7 +476,7 @@ class VerifyCommandsTestCase(test.TestCase):
1, self.verify.start(self.fake_api, 1, self.verify.start(self.fake_api,
self.deployment_uuid, deployment_id)) 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): def test_use(self, mock_update_globals_file):
self.fake_api.verification.get.return_value = self.verification_data self.fake_api.verification.get.return_value = self.verification_data
self.verify.use(self.fake_api, self.verification_uuid) 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( mock_update_globals_file.assert_called_once_with(
envutils.ENV_VERIFICATION, self.verification_uuid) 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): def test_rerun(self, mock_update_globals_file):
self.fake_api.verification.rerun.return_value = { self.fake_api.verification.rerun.return_value = {
"verification": self.verification_data, "verification": self.verification_data,

View File

@ -24,6 +24,27 @@ from tests.unit import test
class EnvUtilsTestCase(test.TestCase): 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): def test_default_from_global(self):
@envutils.default_from_global("test_arg_name", @envutils.default_from_global("test_arg_name",
@ -46,18 +67,18 @@ class EnvUtilsTestCase(test.TestCase):
self.assertEqual("my_deployment_id", deployment_id) self.assertEqual("my_deployment_id", deployment_id)
@mock.patch.dict(os.environ, values={}, clear=True) @mock.patch.dict(os.environ, values={}, clear=True)
@mock.patch("rally.cli.envutils.fileutils.load_env_file") @mock.patch("rally.cli.envutils._load_env_file")
def test_get_deployment_id_with_exception(self, mock_load_env_file): def test_get_deployment_id_with_exception(self, mock__load_env_file):
self.assertRaises(exceptions.InvalidArgumentsException, self.assertRaises(exceptions.InvalidArgumentsException,
envutils.get_global, envutils.ENV_DEPLOYMENT, True) 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")) "~/.rally/globals"))
@mock.patch.dict(os.environ, values={}, clear=True) @mock.patch.dict(os.environ, values={}, clear=True)
@mock.patch("rally.cli.envutils.fileutils.load_env_file") @mock.patch("rally.cli.envutils._load_env_file")
def test_get_deployment_id_with_none(self, mock_load_env_file): def test_get_deployment_id_with_none(self, mock__load_env_file):
self.assertIsNone(envutils.get_global(envutils.ENV_DEPLOYMENT)) 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")) "~/.rally/globals"))
@mock.patch.dict(os.environ, values={envutils.ENV_TASK: "my_task_id"}, @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)) self.assertEqual("my_task_id", envutils.get_global(envutils.ENV_TASK))
@mock.patch.dict(os.environ, values={}, clear=True) @mock.patch.dict(os.environ, values={}, clear=True)
@mock.patch("rally.cli.envutils.fileutils.load_env_file") @mock.patch("rally.cli.envutils._load_env_file")
def test_get_task_id_with_exception(self, mock_load_env_file): def test_get_task_id_with_exception(self, mock__load_env_file):
self.assertRaises(exceptions.InvalidArgumentsException, self.assertRaises(exceptions.InvalidArgumentsException,
envutils.get_global, envutils.ENV_TASK, True) 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")) "~/.rally/globals"))
@mock.patch.dict(os.environ, values={}, clear=True) @mock.patch.dict(os.environ, values={}, clear=True)
@mock.patch("rally.cli.envutils.fileutils.load_env_file") @mock.patch("rally.cli.envutils._load_env_file")
def test_get_task_id_with_none(self, mock_load_env_file): def test_get_task_id_with_none(self, mock__load_env_file):
self.assertIsNone(envutils.get_global("RALLY_TASK")) 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")) "~/.rally/globals"))
@mock.patch.dict(os.environ, @mock.patch.dict(os.environ,
values={envutils.ENV_DEPLOYMENT: "test_deployment_id"}, values={envutils.ENV_DEPLOYMENT: "test_deployment_id"},
clear=True) clear=True)
@mock.patch("os.path.exists") @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) 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) 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") "~/.rally/globals"), envutils.ENV_DEPLOYMENT, "\n")
self.assertEqual({}, os.environ) self.assertEqual({}, os.environ)
@ -97,8 +118,8 @@ class EnvUtilsTestCase(test.TestCase):
envutils.ENV_TASK: "test_task_id"}, envutils.ENV_TASK: "test_task_id"},
clear=True) clear=True)
@mock.patch("os.path.exists") @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) 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() envutils.clear_env()
self.assertEqual({}, os.environ) self.assertEqual({}, os.environ)

View File

@ -12,7 +12,7 @@
from yaml import constructor from yaml import constructor
from rally.common import yamlutils from rally.cli import yamlutils
from tests.unit import test from tests.unit import test

View File

@ -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()])

View File

@ -241,7 +241,7 @@ class FuncMockArgsDecoratorsChecker(ast.NodeVisitor):
self.generic_visit(node) self.generic_visit(node)
def check_name(self, arg, dec_vars): 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): def visit_FunctionDef(self, node):
self.generic_visit(node) self.generic_visit(node)
@ -263,19 +263,19 @@ class FuncMockArgsDecoratorsChecker(ast.NodeVisitor):
shortest_name = sorted_by_len.pop() shortest_name = sorted_by_len.pop()
if len(shortest_name) <= self.SHORTEST_VARIANT_LEN_LIMIT: if len(shortest_name) <= self.SHORTEST_VARIANT_LEN_LIMIT:
error_msgs.append( error_msgs.append(
("Argument '%(arg)s' misnamed; should be either " (f"Argument 'mock_{arg}' misnamed; should be "
"of %(dec)s that is derived from the mock " f"either of {dec_vars} that is derived from the "
"decorator args.\n") % {"arg": arg, f"mock decorator args.\n")
"dec": dec_vars}) )
elif not arg: elif not arg:
error_msgs.append( error_msgs.append(
"Missing or malformed argument for %s decorator." f"Missing or malformed argument for {dec_vars} "
% dec_vars) f"decorator.")
mismatched = True mismatched = True
elif not dec_vars: elif not dec_vars:
error_msgs.append( error_msgs.append(
"Missing or malformed decorator for '%s' argument." f"Missing or malformed decorator for 'mock_{arg}' "
% arg) f"argument.")
mismatched = True mismatched = True
if error_msgs: if error_msgs:

View File

@ -337,7 +337,7 @@ def test_func(self, mock_args, mock_args2, mock_some_longer_args):
{ {
"lineno": lineno, "lineno": lineno,
"messages": [ "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 " "either of %s that is derived from the mock decorator "
"args.\n" % variants "args.\n" % variants
], ],
@ -377,10 +377,10 @@ def test_func(self, mock_args, mock_args2, mock_some_longer_args):
{ {
"lineno": lineno, "lineno": lineno,
"messages": [ "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 " "either of %s that is derived from the mock decorator "
"args.\n" % variants, "args.\n" % variants,
"Missing or malformed decorator for 'mismatched' " "Missing or malformed decorator for 'mock_mismatched' "
"argument." "argument."
], ],
"args": self.visitor._get_mock_args.return_value, "args": self.visitor._get_mock_args.return_value,