Don't change global environment in functionaltests

Change-Id: Ifab9260fd8d195a61e6b22ce54e9fe2f3015d523
This commit is contained in:
Andrey Kurilin 2015-06-18 16:03:02 +03:00
parent 0e4bdf1fe6
commit 15ff4afeb5
3 changed files with 107 additions and 103 deletions

View File

@ -17,9 +17,6 @@ import json
import re import re
import unittest import unittest
import mock
from rally.cli import envutils
from tests.functional import utils from tests.functional import utils
@ -30,15 +27,15 @@ class DeploymentTestCase(unittest.TestCase):
self.rally = utils.Rally() self.rally = utils.Rally()
def test_create_fromenv_list_show(self): def test_create_fromenv_list_show(self):
with mock.patch.dict("os.environ", utils.TEST_ENV): self.rally.env.update(utils.TEST_ENV)
self.rally("deployment create --name t_create_env --fromenv") self.rally("deployment create --name t_create_env --fromenv")
self.assertIn("t_create_env", self.rally("deployment list")) self.assertIn("t_create_env", self.rally("deployment list"))
self.assertIn(utils.TEST_ENV["OS_AUTH_URL"], self.assertIn(utils.TEST_ENV["OS_AUTH_URL"],
self.rally("deployment show")) self.rally("deployment show"))
def test_create_fromfile(self): def test_create_fromfile(self):
with mock.patch.dict("os.environ", utils.TEST_ENV): self.rally.env.update(utils.TEST_ENV)
self.rally("deployment create --name t_create_env --fromenv") self.rally("deployment create --name t_create_env --fromenv")
with open("/tmp/.tmp.deployment", "w") as f: with open("/tmp/.tmp.deployment", "w") as f:
f.write(self.rally("deployment config")) f.write(self.rally("deployment config"))
self.rally("deployment create --name t_create_file " 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")) self.assertIn("t_create_file", self.rally("deployment list"))
def test_config(self): def test_config(self):
with mock.patch.dict("os.environ", utils.TEST_ENV): self.rally.env.update(utils.TEST_ENV)
self.rally("deployment create --name t_create_env --fromenv") self.rally("deployment create --name t_create_env --fromenv")
config = json.loads(self.rally("deployment config")) config = json.loads(self.rally("deployment config"))
self.assertEqual(utils.TEST_ENV["OS_USERNAME"], self.assertEqual(utils.TEST_ENV["OS_USERNAME"],
config["admin"]["username"]) config["admin"]["username"])
@ -59,8 +56,8 @@ class DeploymentTestCase(unittest.TestCase):
config["auth_url"]) config["auth_url"])
def test_destroy(self): def test_destroy(self):
with mock.patch.dict("os.environ", utils.TEST_ENV): self.rally.env.update(utils.TEST_ENV)
self.rally("deployment create --name t_create_env --fromenv") self.rally("deployment create --name t_create_env --fromenv")
self.assertIn("t_create_env", self.rally("deployment list")) self.assertIn("t_create_env", self.rally("deployment list"))
self.rally("deployment destroy") self.rally("deployment destroy")
self.assertNotIn("t_create_env", self.rally("deployment list")) self.assertNotIn("t_create_env", self.rally("deployment list"))
@ -69,24 +66,25 @@ class DeploymentTestCase(unittest.TestCase):
self.assertTrue(self.rally("deployment check")) self.assertTrue(self.rally("deployment check"))
def test_check_fail(self): def test_check_fail(self):
with mock.patch.dict("os.environ", utils.TEST_ENV): self.rally.env.update(utils.TEST_ENV)
self.rally("deployment create --name t_create_env --fromenv") self.rally("deployment create --name t_create_env --fromenv")
self.assertRaises(utils.RallyCliError, self.rally, self.assertRaises(utils.RallyCliError, self.rally,
("deployment check")) ("deployment check"))
def test_recreate(self): def test_recreate(self):
with mock.patch.dict("os.environ", utils.TEST_ENV): self.rally.env.update(utils.TEST_ENV)
self.rally("deployment create --name t_create_env --fromenv") self.rally("deployment create --name t_create_env --fromenv")
self.rally("deployment recreate --deployment t_create_env") self.rally("deployment recreate --deployment t_create_env")
self.assertIn("t_create_env", self.rally("deployment list")) self.assertIn("t_create_env", self.rally("deployment list"))
def test_use(self): def test_use(self):
with mock.patch.dict("os.environ", utils.TEST_ENV): self.rally.env.update(utils.TEST_ENV)
output = self.rally( output = self.rally(
"deployment create --name t_create_env1 --fromenv") "deployment create --name t_create_env1 --fromenv")
uuid = re.search(r"Using deployment: (?P<uuid>[0-9a-f\-]{36})", uuid = re.search(r"Using deployment: (?P<uuid>[0-9a-f\-]{36})",
output).group("uuid") output).group("uuid")
self.rally("deployment create --name t_create_env2 --fromenv") self.rally("deployment create --name t_create_env2 --fromenv")
self.rally("deployment use --deployment %s" % uuid) self.rally("deployment use --deployment %s" % uuid)
current_deployment = envutils.get_global("RALLY_DEPLOYMENT") current_deployment = utils.get_global("RALLY_DEPLOYMENT",
self.assertEqual(uuid, current_deployment) self.rally.env)
self.assertEqual(uuid, current_deployment)

View File

@ -20,7 +20,6 @@ import unittest
import mock import mock
from rally.cli import envutils
from tests.functional import utils from tests.functional import utils
@ -276,8 +275,7 @@ class TaskTestCase(unittest.TestCase):
def test_validate_is_invalid(self): def test_validate_is_invalid(self):
rally = utils.Rally() rally = utils.Rally()
with mock.patch.dict("os.environ", utils.TEST_ENV): deployment_id = utils.get_global("RALLY_DEPLOYMENT", rally.env)
deployment_id = envutils.get_global("RALLY_DEPLOYMENT")
cfg = {"invalid": "config"} cfg = {"invalid": "config"}
config = utils.TaskConfig(cfg) config = utils.TaskConfig(cfg)
self.assertRaises(utils.RallyCliError, self.assertRaises(utils.RallyCliError,
@ -289,64 +287,61 @@ class TaskTestCase(unittest.TestCase):
def test_start(self): def test_start(self):
rally = utils.Rally() rally = utils.Rally()
with mock.patch.dict("os.environ", utils.TEST_ENV): deployment_id = utils.get_global("RALLY_DEPLOYMENT", rally.env)
deployment_id = envutils.get_global("RALLY_DEPLOYMENT") cfg = self._get_sample_task_config()
cfg = self._get_sample_task_config() config = utils.TaskConfig(cfg)
config = utils.TaskConfig(cfg) output = rally(("task start --task %(task_file)s "
output = rally(("task start --task %(task_file)s " "--deployment %(deployment_id)s") %
"--deployment %(deployment_id)s") % {"task_file": config.filename,
{"task_file": config.filename, "deployment_id": deployment_id})
"deployment_id": deployment_id})
result = re.search( result = re.search(
r"(?P<task_id>[0-9a-f\-]{36}): started", output) r"(?P<task_id>[0-9a-f\-]{36}): started", output)
self.assertIsNotNone(result) self.assertIsNotNone(result)
def test_validate_with_plugin_paths(self): def test_validate_with_plugin_paths(self):
rally = utils.Rally() rally = utils.Rally()
with mock.patch.dict("os.environ", utils.TEST_ENV): plugin_paths = ("tests/functional/extra/fake_dir1/,"
plugin_paths = ("tests/functional/extra/fake_dir1/," "tests/functional/extra/fake_dir2/")
"tests/functional/extra/fake_dir2/") task_file = "tests/functional/extra/test_fake_scenario.json"
task_file = "tests/functional/extra/test_fake_scenario.json" output = rally(("--plugin-paths %(plugin_paths)s "
output = rally(("--plugin-paths %(plugin_paths)s " "task validate --task %(task_file)s") %
"task validate --task %(task_file)s") % {"task_file": task_file,
{"task_file": task_file, "plugin_paths": plugin_paths})
"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/" plugin_paths = ("tests/functional/extra/fake_dir1/"
"fake_plugin1.py," "fake_plugin1.py,"
"tests/functional/extra/fake_dir2/" "tests/functional/extra/fake_dir2/"
"fake_plugin2.py") "fake_plugin2.py")
task_file = "tests/functional/extra/test_fake_scenario.json" task_file = "tests/functional/extra/test_fake_scenario.json"
output = rally(("--plugin-paths %(plugin_paths)s " output = rally(("--plugin-paths %(plugin_paths)s "
"task validate --task %(task_file)s") % "task validate --task %(task_file)s") %
{"task_file": task_file, {"task_file": task_file,
"plugin_paths": plugin_paths}) "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/," plugin_paths = ("tests/functional/extra/fake_dir1/,"
"tests/functional/extra/fake_dir2/" "tests/functional/extra/fake_dir2/"
"fake_plugin2.py") "fake_plugin2.py")
task_file = "tests/functional/extra/test_fake_scenario.json" task_file = "tests/functional/extra/test_fake_scenario.json"
output = rally(("--plugin-paths %(plugin_paths)s " output = rally(("--plugin-paths %(plugin_paths)s "
"task validate --task %(task_file)s") % "task validate --task %(task_file)s") %
{"task_file": task_file, {"task_file": task_file,
"plugin_paths": plugin_paths}) "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): def _test_start_abort_on_sla_failure_success(self, cfg, times):
rally = utils.Rally() rally = utils.Rally()
with mock.patch.dict("os.environ", utils.TEST_ENV): deployment_id = utils.get_global("RALLY_DEPLOYMENT", rally.env)
deployment_id = envutils.get_global("RALLY_DEPLOYMENT") config = utils.TaskConfig(cfg)
config = utils.TaskConfig(cfg) rally(("task start --task %(task_file)s "
rally(("task start --task %(task_file)s " "--deployment %(deployment_id)s --abort-on-sla-failure") %
"--deployment %(deployment_id)s --abort-on-sla-failure") % {"task_file": config.filename,
{"task_file": config.filename, "deployment_id": deployment_id})
"deployment_id": deployment_id}) results = json.loads(rally("task results"))
results = json.loads(rally("task results"))
iterations_completed = len(results[0]["result"]) iterations_completed = len(results[0]["result"])
self.assertEqual(times, iterations_completed) self.assertEqual(times, iterations_completed)
@ -414,14 +409,13 @@ class TaskTestCase(unittest.TestCase):
def _test_start_abort_on_sla_failure(self, cfg, times): def _test_start_abort_on_sla_failure(self, cfg, times):
rally = utils.Rally() rally = utils.Rally()
with mock.patch.dict("os.environ", utils.TEST_ENV): deployment_id = utils.get_global("RALLY_DEPLOYMENT", rally.env)
deployment_id = envutils.get_global("RALLY_DEPLOYMENT") config = utils.TaskConfig(cfg)
config = utils.TaskConfig(cfg) rally(("task start --task %(task_file)s "
rally(("task start --task %(task_file)s " "--deployment %(deployment_id)s --abort-on-sla-failure") %
"--deployment %(deployment_id)s --abort-on-sla-failure") % {"task_file": config.filename,
{"task_file": config.filename, "deployment_id": deployment_id})
"deployment_id": deployment_id}) results = json.loads(rally("task results"))
results = json.loads(rally("task results"))
iterations_completed = len(results[0]["result"]) iterations_completed = len(results[0]["result"])
self.assertTrue(iterations_completed < times) self.assertTrue(iterations_completed < times)
@ -555,19 +549,18 @@ class TaskTestCase(unittest.TestCase):
def test_use(self): def test_use(self):
rally = utils.Rally() rally = utils.Rally()
with mock.patch.dict("os.environ", utils.TEST_ENV): deployment_id = utils.get_global("RALLY_DEPLOYMENT", rally.env)
deployment_id = envutils.get_global("RALLY_DEPLOYMENT") config = utils.TaskConfig(self._get_sample_task_config())
config = utils.TaskConfig(self._get_sample_task_config()) output = rally(("task start --task %(task_file)s "
output = rally(("task start --task %(task_file)s " "--deployment %(deployment_id)s") %
"--deployment %(deployment_id)s") % {"task_file": config.filename,
{"task_file": config.filename, "deployment_id": deployment_id})
"deployment_id": deployment_id}) result = re.search(
result = re.search( r"(?P<uuid>[0-9a-f\-]{36}): started", output)
r"(?P<uuid>[0-9a-f\-]{36}): started", output) uuid = result.group("uuid")
uuid = result.group("uuid") rally("task use --task %s" % uuid)
rally("task use --task %s" % uuid) current_task = utils.get_global("RALLY_TASK", rally.env)
current_task = envutils.get_global("RALLY_TASK") self.assertEqual(uuid, current_task)
self.assertEqual(uuid, current_task)
class SLATestCase(unittest.TestCase): class SLATestCase(unittest.TestCase):

View File

@ -13,17 +13,19 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from oslo_utils import encodeutils import copy
from six.moves import configparser
import inspect import inspect
import json import json
import os import os
import pwd
import shutil import shutil
import subprocess import subprocess
import tempfile import tempfile
from oslo_utils import encodeutils
from six.moves import configparser
TEST_ENV = { TEST_ENV = {
"OS_USERNAME": "admin", "OS_USERNAME": "admin",
"OS_PASSWORD": "admin", "OS_PASSWORD": "admin",
@ -71,14 +73,15 @@ class Rally(object):
""" """
def __init__(self, fake=False): 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): if not os.path.exists(DEPLOYMENT_FILE):
subprocess.call("rally deployment config > %s" % DEPLOYMENT_FILE, subprocess.call("rally deployment config > %s" % DEPLOYMENT_FILE,
shell=True) 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() 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: if "RCI_KEEP_DB" not in os.environ:
config_filename = os.path.join(self.tmp_dir, "conf") config_filename = os.path.join(self.tmp_dir, "conf")
@ -90,10 +93,10 @@ class Rally(object):
config.write(conf) config.write(conf)
self.args = ["rally", "--config-file", config_filename] self.args = ["rally", "--config-file", config_filename]
subprocess.call(["rally-manage", "--config-file", config_filename, subprocess.call(["rally-manage", "--config-file", config_filename,
"db", "recreate"]) "db", "recreate"], env=self.env)
else: else:
self.args = ["rally"] 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", self.reports_root = os.environ.get("REPORTS_ROOT",
"rally-cli-output-files") "rally-cli-output-files")
@ -169,7 +172,7 @@ class Rally(object):
cmd = cmd.split(" ") cmd = cmd.split(" ")
try: try:
output = encodeutils.safe_decode(subprocess.check_output( 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 write_report:
if not report_path: if not report_path:
@ -185,3 +188,13 @@ class Rally(object):
return output return output
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
raise RallyCliError(e.returncode, e.output) 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 ""