Repository: zalando/patroni
Pull request #2948 (2023-11-13 08:38:59)

Fix bug with custom bootstrap
Patroni was falsely applying `--command` argument.

Close https://github.com/zalando/patroni/issues/2947

coveralls: ## Pull Request Test Coverage Report for [Build 6847585508](https://coveralls.io/builds/63913543)


* **2** of **2**   **(100.0%)**  changed or added relevant lines in **1** file are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage remained the same at **99.858%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/63913543/badge)](https://coveralls.io/builds/63913543) |
| :-- | --: |
| Change from base [Build 6822435949](https://coveralls.io/builds/63878956): |  0.0% |
| Covered Lines: | 13342 |
| Relevant Lines: | 13361 |

---
##### 💛  - [Coveralls](https://coveralls.io)

hughcapet: 👍 
barthisrael: 👍 

Patch:
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:


Test patch:
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"))), \