From 2135dd981e8e45fdcb96fcfcfdfdedbc1fb88ead Mon Sep 17 00:00:00 2001 From: Amy Ge Date: Thu, 20 Jul 2017 10:02:35 -0700 Subject: [PATCH] Fix assertRaise cli output to only generate one file per test Previously, assertRaise call generates separate output file from each test function in rally-cli-output-files. It's better to get the method_name in __init__ to avoid the situation where some method/call generate new frames. Also I added check when after getting method_name to make sure rally instance is generated in each test function instead of being generated once in setup(). Change-Id: Ia7b1608a3abd91caeefbaa021276d7b1a056fa16 --- tests/functional/test_cli_deployment.py | 109 +++++++++++++----------- tests/functional/test_cli_plugin.py | 22 +++-- tests/functional/utils.py | 27 +++--- 3 files changed, 89 insertions(+), 69 deletions(-) diff --git a/tests/functional/test_cli_deployment.py b/tests/functional/test_cli_deployment.py index 62ce47258c..d8c27289d2 100644 --- a/tests/functional/test_cli_deployment.py +++ b/tests/functional/test_cli_deployment.py @@ -24,28 +24,30 @@ class DeploymentTestCase(unittest.TestCase): def setUp(self): super(DeploymentTestCase, self).setUp() - self.rally = utils.Rally() def test_create_fromenv_list_show(self): - 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")) + rally = utils.Rally() + rally.env.update(utils.TEST_ENV) + rally("deployment create --name t_create_env --fromenv") + self.assertIn("t_create_env", rally("deployment list")) self.assertIn(utils.TEST_ENV["OS_AUTH_URL"], - self.rally("deployment show")) + rally("deployment show")) def test_create_fromfile(self): - self.rally.env.update(utils.TEST_ENV) - self.rally("deployment create --name t_create_env --fromenv") + rally = utils.Rally() + rally.env.update(utils.TEST_ENV) + 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 " - "--filename /tmp/.tmp.deployment") - self.assertIn("t_create_file", self.rally("deployment list")) + f.write(rally("deployment config")) + rally("deployment create --name t_create_file " + "--filename /tmp/.tmp.deployment") + self.assertIn("t_create_file", rally("deployment list")) def test_config(self): - self.rally.env.update(utils.TEST_ENV) - self.rally("deployment create --name t_create_env --fromenv") - config = json.loads(self.rally("deployment config")) + rally = utils.Rally() + rally.env.update(utils.TEST_ENV) + rally("deployment create --name t_create_env --fromenv") + config = json.loads(rally("deployment config")) self.assertIn("creds", config) self.assertIn("openstack", config["creds"]) oscreds = config["creds"]["openstack"] @@ -65,37 +67,39 @@ class DeploymentTestCase(unittest.TestCase): oscreds["auth_url"]) def test_destroy(self): - 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")) + rally = utils.Rally() + rally.env.update(utils.TEST_ENV) + rally("deployment create --name t_create_env --fromenv") + self.assertIn("t_create_env", rally("deployment list")) + rally("deployment destroy") + self.assertNotIn("t_create_env", rally("deployment list")) def test_check_success(self): - self.assertTrue(self.rally("deployment check")) + rally = utils.Rally() + self.assertTrue(rally("deployment check")) def test_check_fail(self): - self.rally.env.update(utils.TEST_ENV) - self.rally("deployment create --name t_create_env --fromenv") - self.assertRaises(utils.RallyCliError, self.rally, - ("deployment check")) + rally = utils.Rally() + rally.env.update(utils.TEST_ENV) + rally("deployment create --name t_create_env --fromenv") + self.assertRaises(utils.RallyCliError, rally, "deployment check") def test_check_debug(self): - self.rally.env.update(utils.TEST_ENV) - self.rally("deployment create --name t_create_env --fromenv") - config = json.loads(self.rally("deployment config")) + rally = utils.Rally() + rally.env.update(utils.TEST_ENV) + rally("deployment create --name t_create_env --fromenv") + config = json.loads(rally("deployment config")) config["creds"]["openstack"]["admin"]["password"] = "fakepassword" file = utils.JsonTempFile(config) - self.rally("deployment create --name t_create_file_debug " - "--filename %s" % file.filename) - self.assertIn("t_create_file_debug", self.rally("deployment list")) + rally("deployment create --name t_create_file_debug " + "--filename %s" % file.filename) + self.assertIn("t_create_file_debug", rally("deployment list")) self.assertEqual(config, - json.loads(self.rally("deployment config"))) - self.assertRaises(utils.RallyCliError, self.rally, - ("deployment check")) + json.loads(rally("deployment config"))) + self.assertRaises(utils.RallyCliError, rally, "deployment check") try: - self.rally("--debug deployment check") + rally("--debug deployment check") except utils.RallyCliError as e: self.assertIn( "[-] Unable to authenticate for user %(username)s in" @@ -115,32 +119,35 @@ class DeploymentTestCase(unittest.TestCase): " authentication info") def test_recreate(self): - 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")) + rally = utils.Rally() + rally.env.update(utils.TEST_ENV) + rally("deployment create --name t_create_env --fromenv") + rally("deployment recreate --deployment t_create_env") + self.assertIn("t_create_env", rally("deployment list")) def test_recreate_from_file(self): - self.rally.env.update(utils.TEST_ENV) - self.rally("deployment create --name t_create_env --fromenv") - config = json.loads(self.rally("deployment config")) + rally = utils.Rally() + rally.env.update(utils.TEST_ENV) + rally("deployment create --name t_create_env --fromenv") + config = json.loads(rally("deployment config")) config["creds"]["openstack"]["auth_url"] = "http://foo/" file = utils.JsonTempFile(config) - self.rally("deployment recreate --deployment t_create_env " - "--filename %s" % file.filename) - self.assertIn("t_create_env", self.rally("deployment list")) + rally("deployment recreate --deployment t_create_env " + "--filename %s" % file.filename) + self.assertIn("t_create_env", rally("deployment list")) self.assertEqual(config, - json.loads(self.rally("deployment config"))) - self.assertIn("http://foo/", self.rally("deployment show")) + json.loads(rally("deployment config"))) + self.assertIn("http://foo/", rally("deployment show")) def test_use(self): - self.rally.env.update(utils.TEST_ENV) - output = self.rally( + rally = utils.Rally() + rally.env.update(utils.TEST_ENV) + output = 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) + rally("deployment create --name t_create_env2 --fromenv") + rally("deployment use --deployment %s" % uuid) current_deployment = utils.get_global("RALLY_DEPLOYMENT", - self.rally.env) + rally.env) self.assertEqual(uuid, current_deployment) diff --git a/tests/functional/test_cli_plugin.py b/tests/functional/test_cli_plugin.py index 2980e5978d..e71f825462 100644 --- a/tests/functional/test_cli_plugin.py +++ b/tests/functional/test_cli_plugin.py @@ -22,31 +22,34 @@ class PluginTestCase(unittest.TestCase): def setUp(self): super(PluginTestCase, self).setUp() - self.rally = utils.Rally() def test_show_one(self): - result = self.rally("plugin show Dummy.dummy") + rally = utils.Rally() + result = rally("plugin show Dummy.dummy") self.assertIn("NAME", result) self.assertIn("NAMESPACE", result) self.assertIn("Dummy.dummy", result) self.assertIn("MODULE", result) def test_show_multiple(self): - result = self.rally("plugin show Dummy") + rally = utils.Rally() + result = rally("plugin show Dummy") self.assertIn("Multiple plugins found:", result) self.assertIn("Dummy.dummy", result) self.assertIn("Dummy.dummy_exception", result) self.assertIn("Dummy.dummy_random_fail_in_atomic", result) def test_show_not_found(self): + rally = utils.Rally() name = "Dummy666666" - result = self.rally("plugin show %s" % name) + result = rally("plugin show %s" % name) self.assertIn("There is no plugin: %s" % name, result) def test_show_not_found_in_specific_namespace(self): + rally = utils.Rally() name = "Dummy" namespace = "non_existing" - result = self.rally( + result = rally( "plugin show --name %(name)s --namespace %(namespace)s" % {"name": name, "namespace": namespace}) self.assertIn( @@ -55,15 +58,18 @@ class PluginTestCase(unittest.TestCase): result) def test_list(self): - result = self.rally("plugin list Dummy") + rally = utils.Rally() + result = rally("plugin list Dummy") self.assertIn("Dummy.dummy", result) self.assertIn("Dummy.dummy_exception", result) self.assertIn("Dummy.dummy_random_fail_in_atomic", result) def test_list_not_found_namespace(self): - result = self.rally("plugin list --namespace some") + rally = utils.Rally() + result = rally("plugin list --namespace some") self.assertIn("There is no plugin namespace: some", result) def test_list_not_found_name(self): - result = self.rally("plugin list Dummy2222") + rally = utils.Rally() + result = rally("plugin list Dummy2222") self.assertIn("There is no plugin: Dummy2222", result) diff --git a/tests/functional/utils.py b/tests/functional/utils.py index 4566cc1a3e..46ebe72409 100644 --- a/tests/functional/utils.py +++ b/tests/functional/utils.py @@ -93,6 +93,20 @@ class Rally(object): self.env = copy.deepcopy(os.environ) self.env["HOME"] = self.tmp_dir self.config_filename = None + self.method_name = None + self.class_name = None + + caller_frame = inspect.currentframe().f_back + if caller_frame.f_code.co_name == "__call__": + caller_frame = caller_frame.f_back + + self.method_name = caller_frame.f_code.co_name + if self.method_name == "setUp": + raise Exception("No rally instance should be generated in " + "setUp method") + + test_object = caller_frame.f_locals["self"] + self.class_name = test_object.__class__.__name__ if force_new_db or ("RCI_KEEP_DB" not in os.environ): config_filename = os.path.join(self.tmp_dir, "conf") @@ -139,20 +153,13 @@ class Rally(object): :return: complete report name to write report """ - caller_frame = inspect.currentframe().f_back - if caller_frame.f_code.co_name == "__call__": - caller_frame = caller_frame.f_back - method_name = caller_frame.f_code.co_name - test_object = caller_frame.f_locals["self"] - class_name = test_object.__class__.__name__ - - self._safe_make_dirs("%s/%s" % (self.reports_root, class_name)) + self._safe_make_dirs("%s/%s" % (self.reports_root, self.class_name)) suff = suffix or "" ext = extension or "txt" - path = "%s/%s/%s%s.%s" % (self.reports_root, class_name, - method_name, suff, ext) + path = "%s/%s/%s%s.%s" % (self.reports_root, self.class_name, + self.method_name, suff, ext) if path not in self._created_files: if os.path.exists(path):