From a3f65978a7b6719a901406a31eaf338d9c2f61c3 Mon Sep 17 00:00:00 2001 From: Guilherme Maluf Balzana Date: Tue, 19 Jul 2022 16:55:04 +0200 Subject: [PATCH] Fix log message when catching `get_wsrep_value` exception `get_wresp_value` can fail in `cursor.execute()` and no log message would be written. This commit fix and ensure the right message is logged. Closes-Bug: #1942936 Change-Id: Idbb1170bbf43fdecec233137d6581bf8f799baa9 --- hooks/percona_utils.py | 4 ++-- unit_tests/test_percona_utils.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/hooks/percona_utils.py b/hooks/percona_utils.py index 3adccba..1ff6a55 100644 --- a/hooks/percona_utils.py +++ b/hooks/percona_utils.py @@ -465,8 +465,8 @@ def get_wsrep_value(key): try: cursor.execute("show status like '{}'".format(key)) ret = cursor.fetchall() - except Exception: - log("Failed to get '%s'", ERROR) + except Exception as e: + log("Failed to get key={} '{}'".format(key, e), ERROR) return None finally: cursor.close() diff --git a/unit_tests/test_percona_utils.py b/unit_tests/test_percona_utils.py index 46a3306..e029910 100644 --- a/unit_tests/test_percona_utils.py +++ b/unit_tests/test_percona_utils.py @@ -321,6 +321,24 @@ class UtilsTests(CharmTestCase): ['percona-xtradb-cluster-server-5.5', 'percona-xtradb-cluster-client-5.5']) + @mock.patch.object(percona_utils, 'log') + @mock.patch.object(percona_utils, 'get_db_helper') + def test_get_wsrep_value(self, get_db_helper, log): + _err_msg = '(000, "Query with \'wsrep_not_found_key\' failed")' + _log_msg = f"Failed to get key=wsrep_not_found_key '{_err_msg}'" + + __db_helper = mock.MagicMock() + __db_helper.get_mysql_root_password.return_value = "password" + __db_helper.connect.return_value = True + + __cursor = mock.MagicMock() + __cursor.execute.side_effect = Exception(_err_msg) + __db_helper.connection.cursor.return_value = __cursor + + get_db_helper.return_value = __db_helper + percona_utils.get_wsrep_value('wsrep_not_found_key') + log.assert_called_with(_log_msg, 'ERROR') + @mock.patch.object(percona_utils, 'get_wsrep_value') def test_cluster_in_sync_not_ready(self, _wsrep_value): _wsrep_value.side_effect = [None, None]