From f8adca19e26bcf6622eea4ab0525b41a31b15924 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Wed, 24 Jan 2018 11:40:35 +0000 Subject: [PATCH] Retrieve root password from leaderdb if needed Within update_root_password if there is no root password in the config() object then cfg['root-password'] will return None. set_mysql_root_password is then called with None which triggers a TypeError. The desired root password is in the leaderdb so fallback to retrieving it from there if it is not in config(). unit_tests/test_utils.py was also updated to fix the behaviour of config.changed to mimic that of the real method in charm helpers. Change-Id: I8d22a66f335c0c8e893cda699a55056476e6d9d5 --- hooks/percona_utils.py | 2 +- unit_tests/test_percona_utils.py | 14 ++++++++++++++ unit_tests/test_utils.py | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/hooks/percona_utils.py b/hooks/percona_utils.py index 36e8207..a1a61de 100644 --- a/hooks/percona_utils.py +++ b/hooks/percona_utils.py @@ -841,7 +841,7 @@ def update_root_password(): m_helper = get_db_helper() # password that needs to be set - new_root_passwd = cfg['root-password'] + new_root_passwd = cfg['root-password'] or root_password() m_helper.set_mysql_root_password(new_root_passwd) # check the password was changed diff --git a/unit_tests/test_percona_utils.py b/unit_tests/test_percona_utils.py index b06322f..5bbf7d4 100644 --- a/unit_tests/test_percona_utils.py +++ b/unit_tests/test_percona_utils.py @@ -640,3 +640,17 @@ class TestUpdateBootstrapUUID(CharmTestCase): mock_leader_set.assert_called_with( settings={'mysql.passwd': new_password}) + + @mock.patch.object(percona_utils, 'leader_get') + @mock.patch.object(percona_utils, 'get_db_helper') + def test_update_root_password_None(self, mock_get_db_helper, + mock_leader_get): + # Test fix for 1744961 + my_mock = mock.Mock() + mock_get_db_helper.return_value = my_mock + self.config.side_effect = self.test_config.get + leader_config = {'root-password': 'leaderpass'} + mock_leader_get.side_effect = lambda k: leader_config[k] + + percona_utils.update_root_password() + my_mock.set_mysql_root_password.assert_called_once_with('leaderpass') diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py index c32389b..8b33002 100644 --- a/unit_tests/test_utils.py +++ b/unit_tests/test_utils.py @@ -83,6 +83,8 @@ class TestConfig(object): self.config_prev.pop(k) def changed(self, k): + if not self.config_prev: + return True return self.get(k) != self.previous(k) def get(self, attr=None):