summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2016-12-06 19:24:05 +0000
committerGerrit Code Review <review@openstack.org>2016-12-06 19:24:05 +0000
commit7df9306dd4ac9059f10a6b2cc0f53162de23eb20 (patch)
treefba360b6023aa9edd70889d4dc016a881655c475
parent0fc3b5e2cf4f9c44f6f3c2562bd80d71a4241014 (diff)
parent291d8bd17bfc04be9274899a455e404538f65d51 (diff)
downloadosprofiler-7df9306dd4ac9059f10a6b2cc0f53162de23eb20.tar.gz
Merge "Use oslo_utils.uuidutils.is_uuid_like"
-rw-r--r--osprofiler/cmd/commands.py4
-rw-r--r--osprofiler/tests/cmd/test_shell.py35
2 files changed, 24 insertions, 15 deletions
diff --git a/osprofiler/cmd/commands.py b/osprofiler/cmd/commands.py
index 8b29c29..91d463a 100644
--- a/osprofiler/cmd/commands.py
+++ b/osprofiler/cmd/commands.py
@@ -16,6 +16,8 @@
import json
import os
+from oslo_utils import uuidutils
+
from osprofiler.cmd import cliutils
from osprofiler.drivers import base
from osprofiler import exc
@@ -49,7 +51,7 @@ class TraceCommands(BaseCommand):
trace = None
- if os.path.exists(args.trace):
+ if not uuidutils.is_uuid_like(args.trace):
trace = json.load(open(args.trace))
else:
try:
diff --git a/osprofiler/tests/cmd/test_shell.py b/osprofiler/tests/cmd/test_shell.py
index c97c406..161a113 100644
--- a/osprofiler/tests/cmd/test_shell.py
+++ b/osprofiler/tests/cmd/test_shell.py
@@ -28,6 +28,9 @@ from osprofiler.tests import test
@ddt.ddt
class ShellTestCase(test.TestCase):
+
+ TRACE_ID = "c598094d-bbee-40b6-b317-d76003b679d3"
+
def setUp(self):
super(ShellTestCase, self).setUp()
self.old_environment = os.environ.copy()
@@ -65,6 +68,10 @@ class ShellTestCase(test.TestCase):
super(ShellTestCase, self).tearDown()
os.environ = self.old_environment
+ def _trace_show_cmd(self, format_=None):
+ cmd = "trace show %s" % self.TRACE_ID
+ return cmd if format_ is None else "%s --%s" % (cmd, format_)
+
@mock.patch("sys.stdout", six.StringIO())
@mock.patch("osprofiler.cmd.shell.OSProfilerShell")
def test_shell_main(self, mock_shell):
@@ -89,19 +96,19 @@ class ShellTestCase(test.TestCase):
os.environ.pop("OS_USERNAME")
msg = ("You must provide a username via either --os-username or "
"via env[OS_USERNAME]")
- self._test_with_command_error("trace show fake-uuid", msg)
+ self._test_with_command_error(self._trace_show_cmd(), msg)
def test_password_is_not_presented(self):
os.environ.pop("OS_PASSWORD")
msg = ("You must provide a password via either --os-password or "
"via env[OS_PASSWORD]")
- self._test_with_command_error("trace show fake-uuid", msg)
+ self._test_with_command_error(self._trace_show_cmd(), msg)
def test_auth_url(self):
os.environ.pop("OS_AUTH_URL")
msg = ("You must provide an auth url via either --os-auth-url or "
"via env[OS_AUTH_URL]")
- self._test_with_command_error("trace show fake-uuid", msg)
+ self._test_with_command_error(self._trace_show_cmd(), msg)
def test_no_project_and_domain_set(self):
os.environ.pop("OS_PROJECT_ID")
@@ -116,7 +123,7 @@ class ShellTestCase(test.TestCase):
"--os-user-domain-name or via env[OS_USER_DOMAIN_NAME] or a "
"domain_id via either --os-user-domain-id or via "
"env[OS_USER_DOMAIN_ID]")
- self._test_with_command_error("trace show fake-uuid", msg)
+ self._test_with_command_error(self._trace_show_cmd(), msg)
def test_trace_show_ceilometerclient_is_missed(self):
sys.modules["ceilometerclient"] = None
@@ -126,7 +133,7 @@ class ShellTestCase(test.TestCase):
msg = ("To use this command, you should install "
"'ceilometerclient' manually. Use command:\n "
"'pip install python-ceilometerclient'.")
- self._test_with_command_error("trace show fake-uuid", msg)
+ self._test_with_command_error(self._trace_show_cmd(), msg)
def test_trace_show_unauthorized(self):
class FakeHTTPUnauthorized(Exception):
@@ -135,7 +142,7 @@ class ShellTestCase(test.TestCase):
self.ceiloclient.client.get_client.side_effect = FakeHTTPUnauthorized
msg = "Invalid OpenStack Identity credentials."
- self._test_with_command_error("trace show fake_id", msg)
+ self._test_with_command_error(self._trace_show_cmd(), msg)
def test_trace_show_unknown_error(self):
class FakeException(Exception):
@@ -143,14 +150,14 @@ class ShellTestCase(test.TestCase):
self.ceiloclient.client.get_client.side_effect = FakeException
msg = "Something has gone wrong. See ceilometer logs for more details"
- self._test_with_command_error("trace show fake_id", msg)
+ self._test_with_command_error(self._trace_show_cmd(), msg)
@mock.patch("osprofiler.drivers.ceilometer.Ceilometer.get_report")
def test_trace_show_no_selected_format(self, mock_get):
mock_get.return_value = self._create_mock_notifications()
msg = ("You should choose one of the following output formats: "
"json, html or dot.")
- self._test_with_command_error("trace show fake_id", msg)
+ self._test_with_command_error(self._trace_show_cmd(), msg)
@mock.patch("osprofiler.drivers.ceilometer.Ceilometer.get_report")
@ddt.data(None, {"info": {"started": 0, "finished": 1, "name": "total"},
@@ -158,15 +165,14 @@ class ShellTestCase(test.TestCase):
def test_trace_show_trace_id_not_found(self, notifications, mock_get):
mock_get.return_value = notifications
- fake_trace_id = "fake_id"
msg = ("Trace with UUID %s not found. There are 3 possible reasons: \n"
" 1) You are using not admin credentials\n"
" 2) You specified wrong trace id\n"
" 3) You specified wrong HMAC Key in original calling\n"
" 4) Ceilometer didn't enable profiler notification topic"
- % fake_trace_id)
+ % self.TRACE_ID)
- self._test_with_command_error("trace show %s" % fake_trace_id, msg)
+ self._test_with_command_error(self._trace_show_cmd(), msg)
def _create_mock_notifications(self):
notifications = {
@@ -192,7 +198,7 @@ class ShellTestCase(test.TestCase):
notifications = self._create_mock_notifications()
mock_get.return_value = notifications
- self.run_command("trace show fake_id --json")
+ self.run_command(self._trace_show_cmd(format_="json"))
self.assertEqual("%s\n" % json.dumps(notifications, indent=2),
sys.stdout.getvalue())
@@ -214,7 +220,7 @@ class ShellTestCase(test.TestCase):
with mock.patch("osprofiler.cmd.commands.open",
mock.mock_open(read_data=html_template), create=True):
- self.run_command("trace show fake_id --html")
+ self.run_command(self._trace_show_cmd(format_="html"))
self.assertEqual("A long time ago in a galaxy far, far away..."
" some_data = %s"
"It is a period of civil war. Rebel"
@@ -232,7 +238,8 @@ class ShellTestCase(test.TestCase):
with mock.patch("osprofiler.cmd.commands.open",
mock.mock_open(), create=True) as mock_open:
- self.run_command("trace show fake_id --json --out='/file'")
+ self.run_command("%s --out='/file'" %
+ self._trace_show_cmd(format_="json"))
output = mock_open.return_value.__enter__.return_value
output.write.assert_called_once_with(json.dumps(notifications,