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
This commit is contained in:
Guilherme Maluf Balzana
2022-07-19 16:55:04 +02:00
parent 66f004c2ba
commit a3f65978a7
2 changed files with 20 additions and 2 deletions

View File

@@ -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()

View File

@@ -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]