From 59e2a9bd2f4288d68c6c266fbdd138583802431c Mon Sep 17 00:00:00 2001 From: Doug Szumski Date: Mon, 11 Feb 2019 15:00:47 +0000 Subject: [PATCH] Fix alarm-definition-list in Python 3 Python 3 uses unicode strings by default. This bug was caused by attempting to join a byte string to a unicode string when running with the Python 3 interpreter. A unit test was added to reproduce the problem. Whilst this fix allows alarm definitions to be listed, it doesn't fix the formatting problem in Python 3. A better fix may be to return unicode strings in Python 3 as part of a wider code audit. Story: 2004972 Task: 29427 Change-Id: Ib008ed2aacd93f499b15094cf5a7e1bb0bb50a1f --- monascaclient/common/utils.py | 2 +- .../v2_0/shell/test_alarm_definitions.py | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/monascaclient/common/utils.py b/monascaclient/common/utils.py index 6fea320..5122dd2 100644 --- a/monascaclient/common/utils.py +++ b/monascaclient/common/utils.py @@ -198,4 +198,4 @@ def format_list(in_list): else: key = k string_list.append(key) - return '\n'.join(string_list) + return b'\n'.join(string_list) diff --git a/monascaclient/tests/v2_0/shell/test_alarm_definitions.py b/monascaclient/tests/v2_0/shell/test_alarm_definitions.py index a01fe10..a844b11 100644 --- a/monascaclient/tests/v2_0/shell/test_alarm_definitions.py +++ b/monascaclient/tests/v2_0/shell/test_alarm_definitions.py @@ -71,6 +71,37 @@ class TestAlarmDefinitionShellV2(base.BaseTestCase): undetermined_actions=[ad_action_id] ) + @mock.patch('monascaclient.osc.migration.make_client') + def test_alarm_definitions_list(self, mc): + mc.return_value = c = FakeV2Client() + + c.alarm_definitions.list.return_value = [{ + "name": "ntp_sync_check", + "id": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", + "expression": "(max(ntp.offset{}, deterministic)>=1)", + "match_by": ['hostname'], + "description": "NTP time sync check", + "actions_enabled": True, + "deterministic": True, + "alarm_actions": ['aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'], + "ok_actions": [], + "undetermined_actions": [], + "severity": "HIGH", + }] + + name, cmd_class = migr.create_command_class( + 'do_alarm_definition_list', + shell + ) + cmd = cmd_class(mock.Mock(), mock.Mock()) + + parser = cmd.get_parser(name) + raw_args = [] + parsed_args = parser.parse_args(raw_args) + cmd.run(parsed_args) + + c.alarm_definitions.list.assert_called_once() + @mock.patch('monascaclient.osc.migration.make_client') def test_should_patch_name(self, mc): ad_id = '0495340b-58fd-4e1c-932b-5e6f9cc96490'