Fix bug with custom bootstrap (#2948)

Patroni was falsely applying `--command` argument.

Close https://github.com/zalando/patroni/issues/2947
This commit is contained in:
Alexander Kukushkin
2023-11-13 15:01:57 +01:00
committed by GitHub
parent 7370f70f13
commit 1870dcd8f9
2 changed files with 11 additions and 5 deletions
+3 -4
View File
@@ -188,10 +188,9 @@ class Bootstrap(object):
params = [] if config.get('no_params') else ['--scope=' + self._postgresql.scope, params = [] if config.get('no_params') else ['--scope=' + self._postgresql.scope,
'--datadir=' + self._postgresql.data_dir] '--datadir=' + self._postgresql.data_dir]
# Add custom parameters specified by the user # Add custom parameters specified by the user
reserved_args = {'no_params', 'keep_existing_recovery_conf', 'recovery_conf', 'scope', 'datadir'} reserved_args = {'command', 'no_params', 'keep_existing_recovery_conf', 'recovery_conf', 'scope', 'datadir'}
for arg, val in config.items(): params += [f"--{arg}={val}" for arg, val in config.items() if arg not in reserved_args]
if arg not in reserved_args:
params.append(f"--{arg}={val}")
try: try:
logger.info('Running custom bootstrap script: %s', config['command']) logger.info('Running custom bootstrap script: %s', config['command'])
if self._postgresql.cancellable.call(shlex.split(config['command']) + params) != 0: if self._postgresql.cancellable.call(shlex.split(config['command']) + params) != 0:
+8 -1
View File
@@ -179,10 +179,17 @@ class TestBootstrap(BaseTestPostgresql):
@patch.object(Postgresql, 'controldata', Mock(return_value={'Database cluster state': 'in production'})) @patch.object(Postgresql, 'controldata', Mock(return_value={'Database cluster state': 'in production'}))
def test_custom_bootstrap(self, mock_cancellable_subprocess_call): def test_custom_bootstrap(self, mock_cancellable_subprocess_call):
self.p.config._config.pop('pg_hba') 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 mock_cancellable_subprocess_call.return_value = 1
self.assertFalse(self.b.bootstrap(config)) 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 mock_cancellable_subprocess_call.return_value = 0
with patch('multiprocessing.Process', Mock(side_effect=Exception("42"))), \ with patch('multiprocessing.Process', Mock(side_effect=Exception("42"))), \