From 5d02fc7808c1e46d17e52e73ba69d223e76d7bd8 Mon Sep 17 00:00:00 2001 From: zengyingzhe Date: Mon, 28 Sep 2015 18:40:25 +0800 Subject: [PATCH] Fix the bug when showing event in log format The event time displayed in log format is truncated, because the event log formater of heatclient resolves the event_time string incorrectly. Exchange the date and time fields splitted from event_time attribute, and don't truncate the last character of time_date[1]. Closes-Bug: #1498283 Change-Id: If94093e2b0531a78c26ba9baeceafc19b61df5f8 (cherry picked from commit 35dd0f21df68bab229b04e6c4cceff3ee477b482) --- heatclient/common/utils.py | 4 ++-- heatclient/tests/unit/fakes.py | 8 ++++---- heatclient/tests/unit/test_shell.py | 12 ++++++------ heatclient/tests/unit/test_utils.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/heatclient/common/utils.py b/heatclient/common/utils.py index 5a8b09cf..27fc02ed 100644 --- a/heatclient/common/utils.py +++ b/heatclient/common/utils.py @@ -102,8 +102,8 @@ def event_log_formatter(events): event_time = getattr(event, 'event_time', '') time_date = event_time.split('T') try: - event_time = time_date[0] - event_date = time_date[1][:-1] + event_date = time_date[0] + event_time = time_date[1] except IndexError: event_time = event_date = '' diff --git a/heatclient/tests/unit/fakes.py b/heatclient/tests/unit/fakes.py index 2469fad8..74ecf304 100644 --- a/heatclient/tests/unit/fakes.py +++ b/heatclient/tests/unit/fakes.py @@ -100,7 +100,7 @@ def mock_script_event_list( action="CREATE", final_state="COMPLETE", fakehttp=True): resp_dict = {"events": [ - {"event_time": "2013-12-05T14:14:31Z", + {"event_time": "2013-12-05T14:14:31", "id": rsrc_eventid1, "links": [{"href": "http://heat.example.com:8004/foo", "rel": "self"}, @@ -113,7 +113,7 @@ def mock_script_event_list( "resource_name": resource_name if resource_name else "testresource", "resource_status": "%s_IN_PROGRESS" % action, "resource_status_reason": "state changed"}, - {"event_time": "2013-12-05T14:14:32Z", + {"event_time": "2013-12-05T14:14:32", "id": rsrc_eventid2, "links": [{"href": "http://heat.example.com:8004/foo", "rel": "self"}, @@ -133,7 +133,7 @@ def mock_script_event_list( stack_event1 = "0159dccd-65e1-46e8-a094-697d20b009e5" stack_event2 = "8f591a36-7190-4adb-80da-00191fe22388" resp_dict["events"].insert( - 0, {"event_time": "2013-12-05T14:14:30Z", + 0, {"event_time": "2013-12-05T14:14:30", "id": stack_event1, "links": [{"href": "http://heat.example.com:8004/foo", "rel": "self"}, @@ -147,7 +147,7 @@ def mock_script_event_list( "resource_status": "%s_IN_PROGRESS" % action, "resource_status_reason": "state changed"}) resp_dict["events"].append( - {"event_time": "2013-12-05T14:14:33Z", + {"event_time": "2013-12-05T14:14:33", "id": stack_event2, "links": [{"href": "http://heat.example.com:8004/foo", "rel": "self"}, diff --git a/heatclient/tests/unit/test_shell.py b/heatclient/tests/unit/test_shell.py index 0e4bf033..6a1f01f2 100644 --- a/heatclient/tests/unit/test_shell.py +++ b/heatclient/tests/unit/test_shell.py @@ -425,8 +425,8 @@ class ShellTestNoMox(TestCase): eventid2, 'state changed', 'CREATE_IN_PROGRESS', - '2013-12-05T14:14:31Z', - '2013-12-05T14:14:32Z', + '2013-12-05T14:14:31', + '2013-12-05T14:14:32', ] for r in required: @@ -2923,8 +2923,8 @@ class ShellTestEvents(ShellBase): 'state changed', 'CREATE_IN_PROGRESS', 'CREATE_COMPLETE', - '2013-12-05T14:14:31Z', - '2013-12-05T14:14:32Z', + '2013-12-05T14:14:31', + '2013-12-05T14:14:32', ] for r in required: self.assertRegexpMatches(event_list_text, r) @@ -2953,9 +2953,9 @@ class ShellTestEvents(ShellBase): event_list_text = self.shell('event-list {0} --format log'.format( stack_id)) - expected = '14:14:31 2013-12-05 %s [aResource]: ' \ + expected = '2013-12-05 14:14:31 %s [aResource]: ' \ 'CREATE_IN_PROGRESS state changed\n' \ - '14:14:32 2013-12-05 %s [aResource]: CREATE_COMPLETE ' \ + '2013-12-05 14:14:32 %s [aResource]: CREATE_COMPLETE ' \ 'state changed\n' % (self.event_id_one, self.event_id_two) self.assertEqual(expected, event_list_text) diff --git a/heatclient/tests/unit/test_utils.py b/heatclient/tests/unit/test_utils.py index 10946eb6..f1950d1d 100644 --- a/heatclient/tests/unit/test_utils.py +++ b/heatclient/tests/unit/test_utils.py @@ -161,6 +161,35 @@ class ShellTest(testtools.TestCase): self.assertEqual('one\ntwo', utils.newline_list_formatter(['one', 'two'])) + def test_event_log_formatter(self): + event1 = {'event_time': '2015-09-28T12:12:12', + 'id': '123456789', + 'resource_name': 'res_name', + 'resource_status': 'CREATE_IN_PROGRESS', + 'resource_status_reason': 'CREATE started'} + event2 = {'event_time': '2015-09-28T12:12:22', + 'id': '123456789', + 'resource_name': 'res_name', + 'resource_status': 'CREATE_COMPLETE', + 'resource_status_reason': 'CREATE completed'} + events_list = [hc_res.Resource(manager=None, info=event1), + hc_res.Resource(manager=None, info=event2)] + + expected = ('2015-09-28 12:12:12 123456789 [res_name]: ' + 'CREATE_IN_PROGRESS CREATE started\n' + '2015-09-28 12:12:22 123456789 [res_name]: ' + 'CREATE_COMPLETE CREATE completed') + self.assertEqual(expected, utils.event_log_formatter(events_list)) + self.assertEqual('', utils.event_log_formatter([])) + + notime_event = {'id': '123', + 'resource_name': 'resname', + 'resource_status': 'CREATE_COMPLETE', + 'resource_status_reason': 'state changed'} + notime_event_list = [hc_res.Resource(manager=None, info=notime_event)] + self.assertEqual(' 123 [resname]: CREATE_COMPLETE state changed', + utils.event_log_formatter(notime_event_list)) + class ShellTestParameterFiles(testtools.TestCase):