Пример #1
0
class TestZooKeeper(unittest.TestCase):
    @patch('patroni.dcs.zookeeper.KazooClient', MockKazooClient)
    def setUp(self):
        self.zk = ZooKeeper({
            'hosts': ['localhost:2181'],
            'scope': 'test',
            'name': 'foo',
            'ttl': 30,
            'retry_timeout': 10,
            'loop_wait': 10
        })

    def test_session_listener(self):
        self.zk.session_listener(KazooState.SUSPENDED)

    def test_reload_config(self):
        self.zk.reload_config({
            'ttl': 20,
            'retry_timeout': 10,
            'loop_wait': 10
        })
        self.zk.reload_config({'ttl': 20, 'retry_timeout': 10, 'loop_wait': 5})

    def test_get_node(self):
        self.assertIsNone(self.zk.get_node('/no_node'))

    def test_get_children(self):
        self.assertListEqual(self.zk.get_children('/no_node'), [])

    def test__inner_load_cluster(self):
        self.zk._base_path = self.zk._base_path.replace('test', 'bla')
        self.zk._inner_load_cluster()
        self.zk._base_path = self.zk._base_path = '/no_node'
        self.zk._inner_load_cluster()

    def test_get_cluster(self):
        self.assertRaises(ZooKeeperError, self.zk.get_cluster)
        cluster = self.zk.get_cluster()
        self.assertIsInstance(cluster.leader, Leader)
        self.zk.touch_member({'foo': 'foo'})

    def test_delete_leader(self):
        self.assertTrue(self.zk.delete_leader())

    def test_set_failover_value(self):
        self.zk.set_failover_value('')
        self.zk.set_failover_value('ok')
        self.zk.set_failover_value('Exception')

    def test_set_config_value(self):
        self.zk.set_config_value('', 1)
        self.zk.set_config_value('ok')
        self.zk.set_config_value('Exception')

    def test_initialize(self):
        self.assertFalse(self.zk.initialize())

    def test_cancel_initialization(self):
        self.zk.cancel_initialization()

    def test_touch_member(self):
        self.zk._name = 'buzz'
        self.zk.get_cluster()
        self.zk.touch_member({'new': 'new'})
        self.zk._name = 'bar'
        self.zk.touch_member({'new': 'new'})
        self.zk._name = 'na'
        self.zk._client.exists = 1
        self.zk.touch_member({'Exception': 'Exception'})
        self.zk._name = 'bar'
        self.zk.touch_member({'retry': 'retry'})
        self.zk._fetch_cluster = True
        self.zk.get_cluster()
        self.zk.touch_member({
            'conn_url': 'postgres://*****:*****@localhost:5434/postgres',
            'api_url': 'http://127.0.0.1:8009/patroni'
        })

    def test_take_leader(self):
        self.zk.take_leader()
        with patch.object(MockKazooClient, 'create',
                          Mock(side_effect=Exception)):
            self.zk.take_leader()

    def test_update_leader(self):
        self.assertTrue(self.zk.update_leader(None))

    def test_write_leader_optime(self):
        self.zk.last_leader_operation = '0'
        self.zk.write_leader_optime('1')
        with patch.object(MockKazooClient, 'create_async', Mock()):
            self.zk.write_leader_optime('1')
        with patch.object(MockKazooClient, 'set_async', Mock()):
            self.zk.write_leader_optime('2')
        self.zk._base_path = self.zk._base_path.replace('test', 'bla')
        self.zk.write_leader_optime('3')

    def test_delete_cluster(self):
        self.assertTrue(self.zk.delete_cluster())

    def test_watch(self):
        self.zk.watch(None, 0)
        self.zk.event.isSet = Mock(return_value=True)
        self.zk.watch(None, 0)

    def test__kazoo_connect(self):
        self.zk._client._retry.deadline = 1
        self.zk._orig_kazoo_connect = Mock(return_value=(0, 0))
        self.zk._kazoo_connect(None, None)

    def test_sync_state(self):
        self.zk.set_sync_state_value('')
        self.zk.set_sync_state_value('ok')
        self.zk.set_sync_state_value('Exception')
        self.zk.delete_sync_state()

    def test_set_history_value(self):
        self.zk.set_history_value('{}')
Пример #2
0
class TestZooKeeper(unittest.TestCase):

    @patch('patroni.dcs.zookeeper.KazooClient', MockKazooClient)
    def setUp(self):
        self.zk = ZooKeeper({'hosts': ['localhost:2181'], 'scope': 'test',
                             'name': 'foo', 'ttl': 30, 'retry_timeout': 10, 'loop_wait': 10})

    def test_session_listener(self):
        self.zk.session_listener(KazooState.SUSPENDED)

    def test_reload_config(self):
        self.zk.reload_config({'ttl': 20, 'retry_timeout': 10, 'loop_wait': 10})
        self.zk.reload_config({'ttl': 20, 'retry_timeout': 10, 'loop_wait': 5})

    def test_get_node(self):
        self.assertIsNone(self.zk.get_node('/no_node'))

    def test_get_children(self):
        self.assertListEqual(self.zk.get_children('/no_node'), [])

    def test__inner_load_cluster(self):
        self.zk._base_path = self.zk._base_path.replace('test', 'bla')
        self.zk._inner_load_cluster()
        self.zk._base_path = self.zk._base_path = '/no_node'
        self.zk._inner_load_cluster()

    def test_get_cluster(self):
        self.assertRaises(ZooKeeperError, self.zk.get_cluster)
        cluster = self.zk.get_cluster()
        self.assertIsInstance(cluster.leader, Leader)
        self.zk.touch_member({'foo': 'foo'})

    def test_delete_leader(self):
        self.assertTrue(self.zk.delete_leader())

    def test_set_failover_value(self):
        self.zk.set_failover_value('')
        self.zk.set_failover_value('ok')
        self.zk.set_failover_value('Exception')

    def test_set_config_value(self):
        self.zk.set_config_value('')
        self.zk.set_config_value('ok')
        self.zk.set_config_value('Exception')

    def test_initialize(self):
        self.assertFalse(self.zk.initialize())

    def test_cancel_initialization(self):
        self.zk.cancel_initialization()

    def test_touch_member(self):
        self.zk._name = 'buzz'
        self.zk.get_cluster()
        self.zk.touch_member({'new': 'new'})
        self.zk._name = 'bar'
        self.zk.touch_member({'new': 'new'})
        self.zk._name = 'na'
        self.zk._client.exists = 1
        self.zk.touch_member({'Exception': 'Exception'})
        self.zk._name = 'bar'
        self.zk.touch_member({'retry': 'retry'})
        self.zk._fetch_cluster = True
        self.zk.get_cluster()
        self.zk.touch_member({'retry': 'retry'})

    def test_take_leader(self):
        self.zk.take_leader()
        with patch.object(MockKazooClient, 'create', Mock(side_effect=Exception)):
            self.zk.take_leader()

    def test_update_leader(self):
        self.assertTrue(self.zk.update_leader(None))

    def test_write_leader_optime(self):
        self.zk.last_leader_operation = '0'
        self.zk.write_leader_optime('1')
        with patch.object(MockKazooClient, 'create_async', Mock()):
            self.zk.write_leader_optime('1')
        with patch.object(MockKazooClient, 'set_async', Mock()):
            self.zk.write_leader_optime('2')
        self.zk._base_path = self.zk._base_path.replace('test', 'bla')
        self.zk.write_leader_optime('3')

    def test_delete_cluster(self):
        self.assertTrue(self.zk.delete_cluster())

    def test_watch(self):
        self.zk.watch(None, 0)
        self.zk.event.isSet = Mock(return_value=True)
        self.zk.watch(None, 0)

    def test__kazoo_connect(self):
        self.zk._client._retry.deadline = 1
        self.zk._orig_kazoo_connect = Mock(return_value=(0, 0))
        self.zk._kazoo_connect(None, None)

    def test_sync_state(self):
        self.zk.set_sync_state_value('')
        self.zk.set_sync_state_value('ok')
        self.zk.set_sync_state_value('Exception')
        self.zk.delete_sync_state()

    def test_set_history_value(self):
        self.zk.set_history_value('{}')