diff --git a/tests/test_callback_executor.py b/tests/test_callback_executor.py index 9fa1019e..fc4a95c2 100644 --- a/tests/test_callback_executor.py +++ b/tests/test_callback_executor.py @@ -17,6 +17,9 @@ class TestCallbackExecutor(unittest.TestCase): self.assertIsNone(ce.call([])) + mock_popen.return_value.kill.side_effect = OSError + self.assertRaises(Exception, ce.call, []) + mock_popen.side_effect = Exception ce = CallbackExecutor() ce._callback_event.wait = Mock(side_effect=[None, Exception]) diff --git a/tests/test_ctl.py b/tests/test_ctl.py index c24b4ecc..297a1f7f 100644 --- a/tests/test_ctl.py +++ b/tests/test_ctl.py @@ -548,13 +548,14 @@ class TestCtl(unittest.TestCase): def test_edit_config(self, mock_get_dcs): mock_get_dcs.return_value = self.e mock_get_dcs.return_value.get_cluster = get_cluster_initialized_with_leader + mock_get_dcs.return_value.set_config_value = Mock(return_value=False) os.environ['EDITOR'] = 'true' self.runner.invoke(ctl, ['edit-config', 'dummy']) self.runner.invoke(ctl, ['edit-config', 'dummy', '-s', 'foo=bar']) self.runner.invoke(ctl, ['edit-config', 'dummy', '--replace', 'postgres0.yml']) self.runner.invoke(ctl, ['edit-config', 'dummy', '--apply', '-'], input='foo: bar') self.runner.invoke(ctl, ['edit-config', 'dummy', '--force', '--apply', '-'], input='foo: bar') - mock_get_dcs.return_value.set_config_value = Mock(return_value=True) + mock_get_dcs.return_value.set_config_value.return_value = True self.runner.invoke(ctl, ['edit-config', 'dummy', '--force', '--apply', '-'], input='foo: bar') @patch('patroni.ctl.get_dcs') diff --git a/tests/test_etcd.py b/tests/test_etcd.py index 9f90d783..fb66d139 100644 --- a/tests/test_etcd.py +++ b/tests/test_etcd.py @@ -210,8 +210,8 @@ class TestClient(unittest.TestCase): self.client.api_execute('/', 'POST', timeout=0) self.client._machines_cache = [self.client._base_uri] self.assertRaises(etcd.EtcdWatchTimedOut, self.client.api_execute, '/timeout', 'POST', params={'wait': 'true'}) + self.assertRaises(etcd.EtcdWatchTimedOut, self.client.api_execute, '/timeout', 'POST', params={'wait': 'true'}) self.assertRaises(etcd.EtcdException, self.client.api_execute, '/', '') - self.client._update_machines_cache = True with patch.object(Client, '_load_machines_cache', Mock(side_effect=etcd.EtcdException)): self.assertRaises(etcd.EtcdException, self.client.api_execute, '/', 'GET') diff --git a/tests/test_ha.py b/tests/test_ha.py index e86eb261..d1ead91f 100644 --- a/tests/test_ha.py +++ b/tests/test_ha.py @@ -152,6 +152,8 @@ def run_async(self, func, args=()): @patch.object(Postgresql, 'checkpoint', Mock()) @patch.object(Postgresql, 'cancellable_subprocess_call', Mock(return_value=0)) @patch.object(Postgresql, '_get_local_timeline_lsn_from_replication_connection', Mock(return_value=[2, 10])) +@patch.object(Postgresql, 'get_master_timeline', Mock(return_value=2)) +@patch.object(Postgresql, 'restore_configuration_files', Mock()) @patch.object(etcd.Client, 'write', etcd_write) @patch.object(etcd.Client, 'read', etcd_read) @patch.object(etcd.Client, 'delete', Mock(side_effect=etcd.EtcdException)) @@ -322,7 +324,7 @@ class TestHa(unittest.TestCase): self.assertEqual(self.ha.run_cycle(), 'Not promoting self because watchdog could not be activated') def test_leader_with_lock(self): - self.ha.cluster = get_cluster_not_initialized_without_leader() + self.ha.cluster = get_cluster_initialized_with_leader() self.ha.cluster.is_unlocked = false self.ha.has_lock = true self.assertEqual(self.ha.run_cycle(), 'no action. i am the leader with the lock') @@ -577,6 +579,7 @@ class TestHa(unittest.TestCase): self.ha.is_paused = true self.assertFalse(self.ha.is_healthiest_node()) + @patch('requests.get', requests_get) def test__is_healthiest_node(self): self.assertTrue(self.ha._is_healthiest_node(self.ha.old_cluster.members)) self.p.is_leader = false @@ -681,6 +684,7 @@ class TestHa(unittest.TestCase): msg = 'no action. i am a secondary and i am following a leader' self.assertEqual(self.ha.run_cycle(), msg) + @patch('requests.get', requests_get) def test_process_unhealthy_standby_cluster_as_standby_leader(self): self.p.is_leader = false self.p.name = 'leader' @@ -999,13 +1003,16 @@ class TestHa(unittest.TestCase): # will not say bootstrap from leader as replica can't self elect self.assertEqual(self.ha.run_cycle(), "trying to bootstrap from replica 'other'") + @patch('psycopg2.connect', psycopg2_connect) def test_update_cluster_history(self): - self.p.get_master_timeline = Mock(return_value=1) self.ha.has_lock = true self.ha.cluster.is_unlocked = false - self.assertEqual(self.ha.run_cycle(), 'no action. i am the leader with the lock') + for tl in (1, 3): + self.p.get_master_timeline = Mock(return_value=tl) + self.assertEqual(self.ha.run_cycle(), 'no action. i am the leader with the lock') @patch('sys.exit', return_value=1) + @patch('requests.get', requests_get) def test_abort_join(self, exit_mock): self.ha.cluster = get_cluster_not_initialized_without_leader() self.p.is_leader = false diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 339f962e..17cc6013 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -624,6 +624,7 @@ class TestPostgresql(unittest.TestCase): self.assertTrue('host replication replicator 127.0.0.1/32 md5\n' in lines) @patch.object(Postgresql, 'cancellable_subprocess_call') + @patch.object(Postgresql, 'get_major_version', Mock(return_value=90600)) def test_custom_bootstrap(self, mock_cancellable_subprocess_call): self.p.config.pop('pg_hba') config = {'method': 'foo', 'foo': {'command': 'bar'}} @@ -653,10 +654,12 @@ class TestPostgresql(unittest.TestCase): @patch('time.sleep', Mock()) @patch('os.unlink', Mock()) + @patch('shutil.copy', Mock()) @patch('os.path.isfile', Mock(return_value=True)) @patch.object(Postgresql, 'run_bootstrap_post_init', Mock(return_value=True)) @patch.object(Postgresql, '_custom_bootstrap', Mock(return_value=True)) @patch.object(Postgresql, 'start', Mock(return_value=True)) + @patch.object(Postgresql, 'get_major_version', Mock(return_value=90600)) def test_post_bootstrap(self): config = {'method': 'foo', 'foo': {'command': 'bar'}} self.p.bootstrap(config) @@ -680,7 +683,7 @@ class TestPostgresql(unittest.TestCase): self.p.set_state('stopped') self.p.reload_config({'authentication': {'superuser': {'username': 'p', 'password': 'p'}, 'replication': {'username': 'r', 'password': 'r'}}, - 'listen': '*', 'retry_timeout': 10, 'parameters': {'hba_file': 'foo'}}) + 'listen': '*', 'retry_timeout': 10, 'parameters': {'wal_level': '', 'hba_file': 'foo'}}) with patch.object(Postgresql, 'restart', Mock()) as mock_restart: self.p.post_bootstrap({}, task) mock_restart.assert_called_once() @@ -717,6 +720,8 @@ class TestPostgresql(unittest.TestCase): self.assertEqual(self.p.get_postgres_role_from_data_directory(), 'replica') def test_remove_data_directory(self): + os.makedirs(os.path.join(self.data_dir, 'foo')) + os.symlink('foo', os.path.join(self.data_dir, 'pg_wal')) self.p.remove_data_directory() open(self.data_dir, 'w').close() self.p.remove_data_directory() @@ -990,7 +995,9 @@ class TestPostgresql(unittest.TestCase): self.p.cleanup_archive_status() @patch('os.unlink', Mock()) + @patch('os.listdir', Mock(return_value=[])) @patch('os.path.isfile', Mock(return_value=True)) + @patch.object(Postgresql, 'read_postmaster_opts', Mock(return_value={})) @patch.object(Postgresql, 'single_user_mode', Mock(return_value=0)) def test_fix_cluster_state(self): self.assertTrue(self.p.fix_cluster_state())