1515from patroni .__main__ import check_psycopg , main as _main , Patroni
1616from patroni .api import RestApiServer
1717from patroni .async_executor import AsyncExecutor
18- from patroni .dcs import Cluster , Member
18+ from patroni .dcs import Cluster , ClusterConfig , Member
1919from patroni .dcs .etcd import AbstractEtcdClientWithFailover
2020from patroni .exceptions import DCSError
2121from patroni .postgresql import Postgresql
@@ -90,11 +90,22 @@ def setUp(self):
9090 def tearDown (self ):
9191 logging .getLogger ().handlers [:] = self ._handlers
9292
93- @patch ('patroni.dcs.AbstractDCS.get_cluster' , Mock (side_effect = [None , DCSError ('foo' ), None ]))
94- def test_load_dynamic_configuration (self ):
93+ def test_apply_dynamic_configuration (self ):
94+ empty_cluster = Cluster .empty ()
95+ self .p .config ._dynamic_configuration = {}
96+ self .p .apply_dynamic_configuration (empty_cluster )
97+ self .assertEqual (self .p .config ._dynamic_configuration ['ttl' ], 30 )
98+
99+ without_config = empty_cluster ._asdict ()
100+ del without_config ['config' ]
101+ cluster = Cluster (
102+ config = ClusterConfig (version = 1 , modify_version = 1 , data = {"ttl" : 40 }),
103+ ** without_config
104+ )
95105 self .p .config ._dynamic_configuration = {}
96- self .p .load_dynamic_configuration ()
97- self .p .load_dynamic_configuration ()
106+ self .p .apply_dynamic_configuration (cluster )
107+ self .assertEqual (self .p .config ._dynamic_configuration ['ttl' ], 40 )
108+
98109
99110 @patch ('sys.argv' , ['patroni.py' , 'postgres0.yml' ])
100111 @patch ('time.sleep' , Mock (side_effect = SleepException ))
@@ -276,11 +287,9 @@ def test_check_psycopg(self):
276287
277288 def test_ensure_unique_name (self ):
278289 # None/empty cluster implies unique name
279- with patch ('patroni.dcs.AbstractDCS.get_cluster' , Mock (return_value = None )):
280- self .assertIsNone (self .p .ensure_unique_name ())
290+ self .assertIsNone (self .p .ensure_unique_name (None ))
281291 empty_cluster = Cluster .empty ()
282- with patch ('patroni.dcs.AbstractDCS.get_cluster' , Mock (return_value = empty_cluster )):
283- self .assertIsNone (self .p .ensure_unique_name ())
292+ self .assertIsNone (self .p .ensure_unique_name (empty_cluster ))
284293 without_members = empty_cluster ._asdict ()
285294 del without_members ['members' ]
286295
@@ -289,8 +298,7 @@ def test_ensure_unique_name(self):
289298 members = [Member (version = 1 , name = "distinct" , session = 1 , data = {})],
290299 ** without_members
291300 )
292- with patch ('patroni.dcs.AbstractDCS.get_cluster' , Mock (return_value = okay_cluster )):
293- self .assertIsNone (self .p .ensure_unique_name ())
301+ self .assertIsNone (self .p .ensure_unique_name (okay_cluster ))
294302
295303 # Cluster with a member with the same name that is running
296304 bad_cluster = Cluster (
@@ -299,10 +307,17 @@ def test_ensure_unique_name(self):
299307 })],
300308 ** without_members
301309 )
302- with patch ('patroni.dcs.AbstractDCS.get_cluster' , Mock (return_value = bad_cluster )):
303- # If the api of the running node cannot be reached, this implies unique name
304- with patch ('urllib3.PoolManager.request' , Mock (side_effect = ConnectionError )):
305- self .assertIsNone (self .p .ensure_unique_name ())
306- # Only if the api of the running node is reachable do we throw an error
307- with patch ('urllib3.PoolManager.request' , Mock ()):
308- self .assertRaises (SystemExit , self .p .ensure_unique_name )
310+ # If the api of the running node cannot be reached, this implies unique name
311+ with patch ('urllib3.PoolManager.request' , Mock (side_effect = ConnectionError )):
312+ self .assertIsNone (self .p .ensure_unique_name (bad_cluster ))
313+ # Only if the api of the running node is reachable do we throw an error
314+ with patch ('urllib3.PoolManager.request' , Mock ()):
315+ self .assertRaises (SystemExit , self .p .ensure_unique_name , bad_cluster )
316+
317+ @patch ('patroni.dcs.AbstractDCS.get_cluster' , Mock (side_effect = [DCSError ('foo' ), DCSError ('foo' ), None ]))
318+ def test_ensure_dcs_access (self ):
319+ with patch ('patroni.__main__.logger.warning' ) as mock_logger :
320+ result = self .p .ensure_dcs_access ()
321+ self .assertEqual (result , None )
322+ self .assertEqual (mock_logger .call_count , 2 )
323+
0 commit comments