From 42cd80361945355d852421fdec938d35f9d33b7a Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Mon, 13 Nov 2023 15:01:57 +0100 Subject: [PATCH] Fix bug with custom bootstrap (#2948) Patroni was falsely applying `--command` argument. Close https://github.com/zalando/patroni/issues/2947 --- patroni/postgresql/bootstrap.py | 7 +++---- tests/test_bootstrap.py | 9 ++++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/patroni/postgresql/bootstrap.py b/patroni/postgresql/bootstrap.py index 751c0797..a544bd73 100644 --- a/patroni/postgresql/bootstrap.py +++ b/patroni/postgresql/bootstrap.py @@ -188,10 +188,9 @@ class Bootstrap(object): params = [] if config.get('no_params') else ['--scope=' + self._postgresql.scope, '--datadir=' + self._postgresql.data_dir] # Add custom parameters specified by the user - reserved_args = {'no_params', 'keep_existing_recovery_conf', 'recovery_conf', 'scope', 'datadir'} - for arg, val in config.items(): - if arg not in reserved_args: - params.append(f"--{arg}={val}") + reserved_args = {'command', 'no_params', 'keep_existing_recovery_conf', 'recovery_conf', 'scope', 'datadir'} + params += [f"--{arg}={val}" for arg, val in config.items() if arg not in reserved_args] + try: logger.info('Running custom bootstrap script: %s', config['command']) if self._postgresql.cancellable.call(shlex.split(config['command']) + params) != 0: diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index 4c2d1c98..8724b03c 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -179,10 +179,17 @@ class TestBootstrap(BaseTestPostgresql): @patch.object(Postgresql, 'controldata', Mock(return_value={'Database cluster state': 'in production'})) def test_custom_bootstrap(self, mock_cancellable_subprocess_call): self.p.config._config.pop('pg_hba') - config = {'method': 'foo', 'foo': {'command': 'bar'}} + config = {'method': 'foo', 'foo': {'command': 'bar --arg1=val1'}} mock_cancellable_subprocess_call.return_value = 1 self.assertFalse(self.b.bootstrap(config)) + self.assertEqual(mock_cancellable_subprocess_call.call_args_list[0][0][0], + ['bar', '--arg1=val1', '--scope=batman', '--datadir=' + os.path.join('data', 'test0')]) + + mock_cancellable_subprocess_call.reset_mock() + config['foo']['no_params'] = 1 + self.assertFalse(self.b.bootstrap(config)) + self.assertEqual(mock_cancellable_subprocess_call.call_args_list[0][0][0], ['bar', '--arg1=val1']) mock_cancellable_subprocess_call.return_value = 0 with patch('multiprocessing.Process', Mock(side_effect=Exception("42"))), \