diff --git a/tests/functional/test_cli_deployment.py b/tests/functional/test_cli_deployment.py index 7f7d57d795..2d0f3a1fbc 100644 --- a/tests/functional/test_cli_deployment.py +++ b/tests/functional/test_cli_deployment.py @@ -17,9 +17,6 @@ import json import re import unittest -import mock - -from rally.cli import envutils from tests.functional import utils @@ -30,15 +27,15 @@ class DeploymentTestCase(unittest.TestCase): self.rally = utils.Rally() def test_create_fromenv_list_show(self): - with mock.patch.dict("os.environ", utils.TEST_ENV): - self.rally("deployment create --name t_create_env --fromenv") + self.rally.env.update(utils.TEST_ENV) + self.rally("deployment create --name t_create_env --fromenv") self.assertIn("t_create_env", self.rally("deployment list")) self.assertIn(utils.TEST_ENV["OS_AUTH_URL"], self.rally("deployment show")) def test_create_fromfile(self): - with mock.patch.dict("os.environ", utils.TEST_ENV): - self.rally("deployment create --name t_create_env --fromenv") + self.rally.env.update(utils.TEST_ENV) + self.rally("deployment create --name t_create_env --fromenv") with open("/tmp/.tmp.deployment", "w") as f: f.write(self.rally("deployment config")) self.rally("deployment create --name t_create_file " @@ -46,8 +43,8 @@ class DeploymentTestCase(unittest.TestCase): self.assertIn("t_create_file", self.rally("deployment list")) def test_config(self): - with mock.patch.dict("os.environ", utils.TEST_ENV): - self.rally("deployment create --name t_create_env --fromenv") + self.rally.env.update(utils.TEST_ENV) + self.rally("deployment create --name t_create_env --fromenv") config = json.loads(self.rally("deployment config")) self.assertEqual(utils.TEST_ENV["OS_USERNAME"], config["admin"]["username"]) @@ -59,8 +56,8 @@ class DeploymentTestCase(unittest.TestCase): config["auth_url"]) def test_destroy(self): - with mock.patch.dict("os.environ", utils.TEST_ENV): - self.rally("deployment create --name t_create_env --fromenv") + self.rally.env.update(utils.TEST_ENV) + self.rally("deployment create --name t_create_env --fromenv") self.assertIn("t_create_env", self.rally("deployment list")) self.rally("deployment destroy") self.assertNotIn("t_create_env", self.rally("deployment list")) @@ -69,24 +66,25 @@ class DeploymentTestCase(unittest.TestCase): self.assertTrue(self.rally("deployment check")) def test_check_fail(self): - with mock.patch.dict("os.environ", utils.TEST_ENV): - self.rally("deployment create --name t_create_env --fromenv") + self.rally.env.update(utils.TEST_ENV) + self.rally("deployment create --name t_create_env --fromenv") self.assertRaises(utils.RallyCliError, self.rally, ("deployment check")) def test_recreate(self): - with mock.patch.dict("os.environ", utils.TEST_ENV): - self.rally("deployment create --name t_create_env --fromenv") + self.rally.env.update(utils.TEST_ENV) + self.rally("deployment create --name t_create_env --fromenv") self.rally("deployment recreate --deployment t_create_env") self.assertIn("t_create_env", self.rally("deployment list")) def test_use(self): - with mock.patch.dict("os.environ", utils.TEST_ENV): - output = self.rally( - "deployment create --name t_create_env1 --fromenv") - uuid = re.search(r"Using deployment: (?P[0-9a-f\-]{36})", - output).group("uuid") - self.rally("deployment create --name t_create_env2 --fromenv") - self.rally("deployment use --deployment %s" % uuid) - current_deployment = envutils.get_global("RALLY_DEPLOYMENT") - self.assertEqual(uuid, current_deployment) + self.rally.env.update(utils.TEST_ENV) + output = self.rally( + "deployment create --name t_create_env1 --fromenv") + uuid = re.search(r"Using deployment: (?P[0-9a-f\-]{36})", + output).group("uuid") + self.rally("deployment create --name t_create_env2 --fromenv") + self.rally("deployment use --deployment %s" % uuid) + current_deployment = utils.get_global("RALLY_DEPLOYMENT", + self.rally.env) + self.assertEqual(uuid, current_deployment) diff --git a/tests/functional/test_cli_task.py b/tests/functional/test_cli_task.py index 194910ef92..41bdf321c0 100644 --- a/tests/functional/test_cli_task.py +++ b/tests/functional/test_cli_task.py @@ -20,7 +20,6 @@ import unittest import mock -from rally.cli import envutils from tests.functional import utils @@ -276,8 +275,7 @@ class TaskTestCase(unittest.TestCase): def test_validate_is_invalid(self): rally = utils.Rally() - with mock.patch.dict("os.environ", utils.TEST_ENV): - deployment_id = envutils.get_global("RALLY_DEPLOYMENT") + deployment_id = utils.get_global("RALLY_DEPLOYMENT", rally.env) cfg = {"invalid": "config"} config = utils.TaskConfig(cfg) self.assertRaises(utils.RallyCliError, @@ -289,64 +287,61 @@ class TaskTestCase(unittest.TestCase): def test_start(self): rally = utils.Rally() - with mock.patch.dict("os.environ", utils.TEST_ENV): - deployment_id = envutils.get_global("RALLY_DEPLOYMENT") - cfg = self._get_sample_task_config() - config = utils.TaskConfig(cfg) - output = rally(("task start --task %(task_file)s " - "--deployment %(deployment_id)s") % - {"task_file": config.filename, - "deployment_id": deployment_id}) + deployment_id = utils.get_global("RALLY_DEPLOYMENT", rally.env) + cfg = self._get_sample_task_config() + config = utils.TaskConfig(cfg) + output = rally(("task start --task %(task_file)s " + "--deployment %(deployment_id)s") % + {"task_file": config.filename, + "deployment_id": deployment_id}) result = re.search( r"(?P[0-9a-f\-]{36}): started", output) self.assertIsNotNone(result) def test_validate_with_plugin_paths(self): rally = utils.Rally() - with mock.patch.dict("os.environ", utils.TEST_ENV): - plugin_paths = ("tests/functional/extra/fake_dir1/," - "tests/functional/extra/fake_dir2/") - task_file = "tests/functional/extra/test_fake_scenario.json" - output = rally(("--plugin-paths %(plugin_paths)s " - "task validate --task %(task_file)s") % - {"task_file": task_file, - "plugin_paths": plugin_paths}) + plugin_paths = ("tests/functional/extra/fake_dir1/," + "tests/functional/extra/fake_dir2/") + task_file = "tests/functional/extra/test_fake_scenario.json" + output = rally(("--plugin-paths %(plugin_paths)s " + "task validate --task %(task_file)s") % + {"task_file": task_file, + "plugin_paths": plugin_paths}) - self.assertIn("Task config is valid", output) + self.assertIn("Task config is valid", output) - plugin_paths = ("tests/functional/extra/fake_dir1/" - "fake_plugin1.py," - "tests/functional/extra/fake_dir2/" - "fake_plugin2.py") - task_file = "tests/functional/extra/test_fake_scenario.json" - output = rally(("--plugin-paths %(plugin_paths)s " - "task validate --task %(task_file)s") % - {"task_file": task_file, - "plugin_paths": plugin_paths}) + plugin_paths = ("tests/functional/extra/fake_dir1/" + "fake_plugin1.py," + "tests/functional/extra/fake_dir2/" + "fake_plugin2.py") + task_file = "tests/functional/extra/test_fake_scenario.json" + output = rally(("--plugin-paths %(plugin_paths)s " + "task validate --task %(task_file)s") % + {"task_file": task_file, + "plugin_paths": plugin_paths}) - self.assertIn("Task config is valid", output) + self.assertIn("Task config is valid", output) - plugin_paths = ("tests/functional/extra/fake_dir1/," - "tests/functional/extra/fake_dir2/" - "fake_plugin2.py") - task_file = "tests/functional/extra/test_fake_scenario.json" - output = rally(("--plugin-paths %(plugin_paths)s " - "task validate --task %(task_file)s") % - {"task_file": task_file, - "plugin_paths": plugin_paths}) + plugin_paths = ("tests/functional/extra/fake_dir1/," + "tests/functional/extra/fake_dir2/" + "fake_plugin2.py") + task_file = "tests/functional/extra/test_fake_scenario.json" + output = rally(("--plugin-paths %(plugin_paths)s " + "task validate --task %(task_file)s") % + {"task_file": task_file, + "plugin_paths": plugin_paths}) - self.assertIn("Task config is valid", output) + self.assertIn("Task config is valid", output) def _test_start_abort_on_sla_failure_success(self, cfg, times): rally = utils.Rally() - with mock.patch.dict("os.environ", utils.TEST_ENV): - deployment_id = envutils.get_global("RALLY_DEPLOYMENT") - config = utils.TaskConfig(cfg) - rally(("task start --task %(task_file)s " - "--deployment %(deployment_id)s --abort-on-sla-failure") % - {"task_file": config.filename, - "deployment_id": deployment_id}) - results = json.loads(rally("task results")) + deployment_id = utils.get_global("RALLY_DEPLOYMENT", rally.env) + config = utils.TaskConfig(cfg) + rally(("task start --task %(task_file)s " + "--deployment %(deployment_id)s --abort-on-sla-failure") % + {"task_file": config.filename, + "deployment_id": deployment_id}) + results = json.loads(rally("task results")) iterations_completed = len(results[0]["result"]) self.assertEqual(times, iterations_completed) @@ -414,14 +409,13 @@ class TaskTestCase(unittest.TestCase): def _test_start_abort_on_sla_failure(self, cfg, times): rally = utils.Rally() - with mock.patch.dict("os.environ", utils.TEST_ENV): - deployment_id = envutils.get_global("RALLY_DEPLOYMENT") - config = utils.TaskConfig(cfg) - rally(("task start --task %(task_file)s " - "--deployment %(deployment_id)s --abort-on-sla-failure") % - {"task_file": config.filename, - "deployment_id": deployment_id}) - results = json.loads(rally("task results")) + deployment_id = utils.get_global("RALLY_DEPLOYMENT", rally.env) + config = utils.TaskConfig(cfg) + rally(("task start --task %(task_file)s " + "--deployment %(deployment_id)s --abort-on-sla-failure") % + {"task_file": config.filename, + "deployment_id": deployment_id}) + results = json.loads(rally("task results")) iterations_completed = len(results[0]["result"]) self.assertTrue(iterations_completed < times) @@ -555,19 +549,18 @@ class TaskTestCase(unittest.TestCase): def test_use(self): rally = utils.Rally() - with mock.patch.dict("os.environ", utils.TEST_ENV): - deployment_id = envutils.get_global("RALLY_DEPLOYMENT") - config = utils.TaskConfig(self._get_sample_task_config()) - output = rally(("task start --task %(task_file)s " - "--deployment %(deployment_id)s") % - {"task_file": config.filename, - "deployment_id": deployment_id}) - result = re.search( - r"(?P[0-9a-f\-]{36}): started", output) - uuid = result.group("uuid") - rally("task use --task %s" % uuid) - current_task = envutils.get_global("RALLY_TASK") - self.assertEqual(uuid, current_task) + deployment_id = utils.get_global("RALLY_DEPLOYMENT", rally.env) + config = utils.TaskConfig(self._get_sample_task_config()) + output = rally(("task start --task %(task_file)s " + "--deployment %(deployment_id)s") % + {"task_file": config.filename, + "deployment_id": deployment_id}) + result = re.search( + r"(?P[0-9a-f\-]{36}): started", output) + uuid = result.group("uuid") + rally("task use --task %s" % uuid) + current_task = utils.get_global("RALLY_TASK", rally.env) + self.assertEqual(uuid, current_task) class SLATestCase(unittest.TestCase): diff --git a/tests/functional/utils.py b/tests/functional/utils.py index 1b88f7c2c5..66fb9a93e6 100644 --- a/tests/functional/utils.py +++ b/tests/functional/utils.py @@ -13,17 +13,19 @@ # License for the specific language governing permissions and limitations # under the License. -from oslo_utils import encodeutils -from six.moves import configparser - +import copy import inspect import json import os -import pwd import shutil import subprocess import tempfile + +from oslo_utils import encodeutils +from six.moves import configparser + + TEST_ENV = { "OS_USERNAME": "admin", "OS_PASSWORD": "admin", @@ -71,14 +73,15 @@ class Rally(object): """ def __init__(self, fake=False): - # NOTE(sskripnick): we shoud change home dir to avoid races - # and do not touch any user files in ~/.rally - os.environ["HOME"] = pwd.getpwuid(os.getuid()).pw_dir if not os.path.exists(DEPLOYMENT_FILE): subprocess.call("rally deployment config > %s" % DEPLOYMENT_FILE, shell=True) + + # NOTE(sskripnick): we should change home dir to avoid races + # and do not touch any user files in ~/.rally self.tmp_dir = tempfile.mkdtemp() - os.environ["HOME"] = self.tmp_dir + self.env = copy.deepcopy(os.environ) + self.env["HOME"] = self.tmp_dir if "RCI_KEEP_DB" not in os.environ: config_filename = os.path.join(self.tmp_dir, "conf") @@ -90,10 +93,10 @@ class Rally(object): config.write(conf) self.args = ["rally", "--config-file", config_filename] subprocess.call(["rally-manage", "--config-file", config_filename, - "db", "recreate"]) + "db", "recreate"], env=self.env) else: self.args = ["rally"] - subprocess.call(["rally-manage", "db", "recreate"]) + subprocess.call(["rally-manage", "db", "recreate"], env=self.env) self.reports_root = os.environ.get("REPORTS_ROOT", "rally-cli-output-files") @@ -169,7 +172,7 @@ class Rally(object): cmd = cmd.split(" ") try: output = encodeutils.safe_decode(subprocess.check_output( - self.args + cmd, stderr=subprocess.STDOUT)) + self.args + cmd, stderr=subprocess.STDOUT, env=self.env)) if write_report: if not report_path: @@ -185,3 +188,13 @@ class Rally(object): return output except subprocess.CalledProcessError as e: raise RallyCliError(e.returncode, e.output) + + +def get_global(global_key, env): + home_dir = env.get("HOME") + with open("%s/.rally/globals" % home_dir) as f: + for line in f.readlines(): + if line.startswith("%s=" % global_key): + key, value = line.split("=") + return value.rstrip() + return ""