From 1f9d8c039c9c7317b81aed186afb337f452c3f91 Mon Sep 17 00:00:00 2001 From: Oleksii Kliukin Date: Mon, 26 Oct 2015 17:19:35 +0100 Subject: [PATCH] Add tests for nofailover. --- tests/test_ha.py | 24 +++++++++++++++++++++++- tests/test_patroni.py | 6 ++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/test_ha.py b/tests/test_ha.py index aa176559..43d4cded 100644 --- a/tests/test_ha.py +++ b/tests/test_ha.py @@ -274,6 +274,11 @@ class TestHa(unittest.TestCase): f = Failover(0, MockPostgresql.name, '') self.ha.cluster = get_cluster_initialized_with_leader(f) self.assertEquals(self.ha.run_cycle(), 'manual failover: demoting myself') + self.ha.fetch_node_status = lambda e: (e, True, True, 0, {'nofailover': 'True'}) + self.assertEquals(self.ha.run_cycle(), 'no action. i am the leader with the lock') + # manual failover from the previous leader to us won't happen if we hold the nofailover flag + self.ha.cluster = get_cluster_initialized_with_leader(Failover(0, 'blabla', MockPostgresql.name)) + self.assertEquals(self.ha.run_cycle(), 'no action. i am the leader with the lock') @patch('requests.get', requests_get) def test_manual_failover_process_no_leader(self): @@ -286,8 +291,22 @@ class TestHa(unittest.TestCase): self.assertEquals(self.ha.run_cycle(), 'following a different leader because i am not the healthiest node') self.ha.cluster = get_cluster_initialized_without_leader(failover=Failover(0, MockPostgresql.name, '')) self.assertEquals(self.ha.run_cycle(), 'following a different leader because i am not the healthiest node') - self.ha.fetch_node_status = lambda e: (e, False, True, 0, {}) # accessible, in_recovery + self.ha.fetch_node_status = lambda e: (e, False, True, 0, {}) # inaccessible, in_recovery self.assertEquals(self.ha.run_cycle(), 'promoted self to leader by acquiring session lock') + # set failover flag to True for all members of the cluster + # this should elect the current member, as we are not going to call the API for it. + self.ha.cluster = get_cluster_initialized_without_leader(failover=Failover(0, '', 'other')) + self.ha.fetch_node_status = lambda e: (e, True, True, 0, {'nofailover': 'True'}) # accessible, in_recovery + self.assertEquals(self.ha.run_cycle(), 'promoted self to leader by acquiring session lock') + # same as previous, but set the current member to nofailover. In no case it should be elected as a leader + self.ha.patroni.nofailover = True + self.assertEquals(self.ha.run_cycle(), 'following a different leader because I am not allowed to promote') + + def test_is_healthiest_node(self): + self.ha.state_handler.is_leader = false + self.ha.patroni.nofailover = False + self.ha.fetch_node_status = lambda e: (e, True, True, 0, {}) + self.assertTrue(self.ha.is_healthiest_node()) def test__is_healthiest_node(self): self.assertTrue(self.ha._is_healthiest_node(self.ha.old_cluster.members)) @@ -300,6 +319,9 @@ class TestHa(unittest.TestCase): self.assertFalse(self.ha._is_healthiest_node(self.ha.old_cluster.members)) self.p.check_replication_lag = false self.assertFalse(self.ha._is_healthiest_node(self.ha.old_cluster.members)) + self.ha.patroni.nofailover = True + self.assertFalse(self.ha._is_healthiest_node(self.ha.old_cluster.members)) + self.ha.patroni.nofailover = False @patch('requests.get', requests_get) def test_fetch_node_status(self): diff --git a/tests/test_patroni.py b/tests/test_patroni.py index 52b10d7a..18f5c14b 100644 --- a/tests/test_patroni.py +++ b/tests/test_patroni.py @@ -74,3 +74,9 @@ class TestPatroni(unittest.TestCase): self.p.schedule_next_run() self.p.next_run = time.time() - self.p.nap_time - 1 self.p.schedule_next_run() + + def test_nofailover(self): + self.p.tags['nofailover'] = True + self.assertTrue(self.p.nofailover) + self.p.tags['nofailover'] = None + self.assertFalse(self.p.nofailover)