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
This commit is contained in:
Amy Ge 2017-07-20 10:02:35 -07:00
parent 53a77e76c7
commit 863154ebfc
3 changed files with 89 additions and 69 deletions

View File

@ -24,28 +24,30 @@ class DeploymentTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
super(DeploymentTestCase, self).setUp() super(DeploymentTestCase, self).setUp()
self.rally = utils.Rally()
def test_create_fromenv_list_show(self): def test_create_fromenv_list_show(self):
self.rally.env.update(utils.TEST_ENV) rally = utils.Rally()
self.rally("deployment create --name t_create_env --fromenv") rally.env.update(utils.TEST_ENV)
self.assertIn("t_create_env", self.rally("deployment list")) 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.assertIn(utils.TEST_ENV["OS_AUTH_URL"],
self.rally("deployment show")) rally("deployment show"))
def test_create_fromfile(self): def test_create_fromfile(self):
self.rally.env.update(utils.TEST_ENV) rally = utils.Rally()
self.rally("deployment create --name t_create_env --fromenv") rally.env.update(utils.TEST_ENV)
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(rally("deployment config"))
self.rally("deployment create --name t_create_file " rally("deployment create --name t_create_file "
"--filename /tmp/.tmp.deployment") "--filename /tmp/.tmp.deployment")
self.assertIn("t_create_file", self.rally("deployment list")) self.assertIn("t_create_file", rally("deployment list"))
def test_config(self): def test_config(self):
self.rally.env.update(utils.TEST_ENV) rally = utils.Rally()
self.rally("deployment create --name t_create_env --fromenv") rally.env.update(utils.TEST_ENV)
config = json.loads(self.rally("deployment config")) rally("deployment create --name t_create_env --fromenv")
config = json.loads(rally("deployment config"))
self.assertIn("creds", config) self.assertIn("creds", config)
self.assertIn("openstack", config["creds"]) self.assertIn("openstack", config["creds"])
oscreds = config["creds"]["openstack"] oscreds = config["creds"]["openstack"]
@ -65,37 +67,39 @@ class DeploymentTestCase(unittest.TestCase):
oscreds["auth_url"]) oscreds["auth_url"])
def test_destroy(self): def test_destroy(self):
self.rally.env.update(utils.TEST_ENV) rally = utils.Rally()
self.rally("deployment create --name t_create_env --fromenv") rally.env.update(utils.TEST_ENV)
self.assertIn("t_create_env", self.rally("deployment list")) rally("deployment create --name t_create_env --fromenv")
self.rally("deployment destroy") self.assertIn("t_create_env", rally("deployment list"))
self.assertNotIn("t_create_env", self.rally("deployment list")) rally("deployment destroy")
self.assertNotIn("t_create_env", rally("deployment list"))
def test_check_success(self): def test_check_success(self):
self.assertTrue(self.rally("deployment check")) rally = utils.Rally()
self.assertTrue(rally("deployment check"))
def test_check_fail(self): def test_check_fail(self):
self.rally.env.update(utils.TEST_ENV) rally = utils.Rally()
self.rally("deployment create --name t_create_env --fromenv") rally.env.update(utils.TEST_ENV)
self.assertRaises(utils.RallyCliError, self.rally, rally("deployment create --name t_create_env --fromenv")
("deployment check")) self.assertRaises(utils.RallyCliError, rally, "deployment check")
def test_check_debug(self): def test_check_debug(self):
self.rally.env.update(utils.TEST_ENV) rally = utils.Rally()
self.rally("deployment create --name t_create_env --fromenv") rally.env.update(utils.TEST_ENV)
config = json.loads(self.rally("deployment config")) rally("deployment create --name t_create_env --fromenv")
config = json.loads(rally("deployment config"))
config["creds"]["openstack"]["admin"]["password"] = "fakepassword" config["creds"]["openstack"]["admin"]["password"] = "fakepassword"
file = utils.JsonTempFile(config) file = utils.JsonTempFile(config)
self.rally("deployment create --name t_create_file_debug " rally("deployment create --name t_create_file_debug "
"--filename %s" % file.filename) "--filename %s" % file.filename)
self.assertIn("t_create_file_debug", self.rally("deployment list")) self.assertIn("t_create_file_debug", rally("deployment list"))
self.assertEqual(config, self.assertEqual(config,
json.loads(self.rally("deployment config"))) json.loads(rally("deployment config")))
self.assertRaises(utils.RallyCliError, self.rally, self.assertRaises(utils.RallyCliError, rally, "deployment check")
("deployment check"))
try: try:
self.rally("--debug deployment check") rally("--debug deployment check")
except utils.RallyCliError as e: except utils.RallyCliError as e:
self.assertIn( self.assertIn(
"[-] Unable to authenticate for user %(username)s in" "[-] Unable to authenticate for user %(username)s in"
@ -115,32 +119,35 @@ class DeploymentTestCase(unittest.TestCase):
" authentication info") " authentication info")
def test_recreate(self): def test_recreate(self):
self.rally.env.update(utils.TEST_ENV) rally = utils.Rally()
self.rally("deployment create --name t_create_env --fromenv") rally.env.update(utils.TEST_ENV)
self.rally("deployment recreate --deployment t_create_env") rally("deployment create --name t_create_env --fromenv")
self.assertIn("t_create_env", self.rally("deployment list")) rally("deployment recreate --deployment t_create_env")
self.assertIn("t_create_env", rally("deployment list"))
def test_recreate_from_file(self): def test_recreate_from_file(self):
self.rally.env.update(utils.TEST_ENV) rally = utils.Rally()
self.rally("deployment create --name t_create_env --fromenv") rally.env.update(utils.TEST_ENV)
config = json.loads(self.rally("deployment config")) rally("deployment create --name t_create_env --fromenv")
config = json.loads(rally("deployment config"))
config["creds"]["openstack"]["auth_url"] = "http://foo/" config["creds"]["openstack"]["auth_url"] = "http://foo/"
file = utils.JsonTempFile(config) file = utils.JsonTempFile(config)
self.rally("deployment recreate --deployment t_create_env " rally("deployment recreate --deployment t_create_env "
"--filename %s" % file.filename) "--filename %s" % file.filename)
self.assertIn("t_create_env", self.rally("deployment list")) self.assertIn("t_create_env", rally("deployment list"))
self.assertEqual(config, self.assertEqual(config,
json.loads(self.rally("deployment config"))) json.loads(rally("deployment config")))
self.assertIn("http://foo/", self.rally("deployment show")) self.assertIn("http://foo/", rally("deployment show"))
def test_use(self): def test_use(self):
self.rally.env.update(utils.TEST_ENV) rally = utils.Rally()
output = self.rally( rally.env.update(utils.TEST_ENV)
output = 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") rally("deployment create --name t_create_env2 --fromenv")
self.rally("deployment use --deployment %s" % uuid) rally("deployment use --deployment %s" % uuid)
current_deployment = utils.get_global("RALLY_DEPLOYMENT", current_deployment = utils.get_global("RALLY_DEPLOYMENT",
self.rally.env) rally.env)
self.assertEqual(uuid, current_deployment) self.assertEqual(uuid, current_deployment)

View File

@ -22,31 +22,34 @@ class PluginTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
super(PluginTestCase, self).setUp() super(PluginTestCase, self).setUp()
self.rally = utils.Rally()
def test_show_one(self): 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("NAME", result)
self.assertIn("NAMESPACE", result) self.assertIn("NAMESPACE", result)
self.assertIn("Dummy.dummy", result) self.assertIn("Dummy.dummy", result)
self.assertIn("MODULE", result) self.assertIn("MODULE", result)
def test_show_multiple(self): 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("Multiple plugins found:", result)
self.assertIn("Dummy.dummy", result) self.assertIn("Dummy.dummy", result)
self.assertIn("Dummy.dummy_exception", result) self.assertIn("Dummy.dummy_exception", result)
self.assertIn("Dummy.dummy_random_fail_in_atomic", result) self.assertIn("Dummy.dummy_random_fail_in_atomic", result)
def test_show_not_found(self): def test_show_not_found(self):
rally = utils.Rally()
name = "Dummy666666" name = "Dummy666666"
result = self.rally("plugin show %s" % name) result = rally("plugin show %s" % name)
self.assertIn("There is no plugin: %s" % name, result) self.assertIn("There is no plugin: %s" % name, result)
def test_show_not_found_in_specific_namespace(self): def test_show_not_found_in_specific_namespace(self):
rally = utils.Rally()
name = "Dummy" name = "Dummy"
namespace = "non_existing" namespace = "non_existing"
result = self.rally( result = rally(
"plugin show --name %(name)s --namespace %(namespace)s" "plugin show --name %(name)s --namespace %(namespace)s"
% {"name": name, "namespace": namespace}) % {"name": name, "namespace": namespace})
self.assertIn( self.assertIn(
@ -55,15 +58,18 @@ class PluginTestCase(unittest.TestCase):
result) result)
def test_list(self): 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", result)
self.assertIn("Dummy.dummy_exception", result) self.assertIn("Dummy.dummy_exception", result)
self.assertIn("Dummy.dummy_random_fail_in_atomic", result) self.assertIn("Dummy.dummy_random_fail_in_atomic", result)
def test_list_not_found_namespace(self): 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) self.assertIn("There is no plugin namespace: some", result)
def test_list_not_found_name(self): 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) self.assertIn("There is no plugin: Dummy2222", result)

View File

@ -93,6 +93,20 @@ class Rally(object):
self.env = copy.deepcopy(os.environ) self.env = copy.deepcopy(os.environ)
self.env["HOME"] = self.tmp_dir self.env["HOME"] = self.tmp_dir
self.config_filename = None 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): if force_new_db or ("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")
@ -139,20 +153,13 @@ class Rally(object):
:return: complete report name to write report :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 self._safe_make_dirs("%s/%s" % (self.reports_root, self.class_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))
suff = suffix or "" suff = suffix or ""
ext = extension or "txt" ext = extension or "txt"
path = "%s/%s/%s%s.%s" % (self.reports_root, class_name, path = "%s/%s/%s%s.%s" % (self.reports_root, self.class_name,
method_name, suff, ext) self.method_name, suff, ext)
if path not in self._created_files: if path not in self._created_files:
if os.path.exists(path): if os.path.exists(path):