Exemplo n.º 1
0
 def test_get_cluster(self):
     self.assertIsInstance(self.etcd3.get_cluster(), Cluster)
     self.client._kv_cache = None
     with patch.object(urllib3.PoolManager, 'urlopen') as mock_urlopen:
         mock_urlopen.return_value = MockResponse()
         mock_urlopen.return_value.content = json.dumps({
             "header": {
                 "revision": "1"
             },
             "kvs": [{
                 "key":
                 base64_encode('/patroni/test/status'),
                 "value":
                 base64_encode('{"optime":1234567,"slots":{"ls":12345}}'),
                 "mod_revision":
                 '1'
             }]
         })
         self.assertIsInstance(self.etcd3.get_cluster(), Cluster)
         mock_urlopen.return_value.content = json.dumps({
             "header": {
                 "revision": "1"
             },
             "kvs": [{
                 "key": base64_encode('/patroni/test/status'),
                 "value": base64_encode('{'),
                 "mod_revision": '1'
             }]
         })
         self.assertIsInstance(self.etcd3.get_cluster(), Cluster)
         mock_urlopen.side_effect = UnsupportedEtcdVersion('')
         self.assertRaises(UnsupportedEtcdVersion, self.etcd3.get_cluster)
         mock_urlopen.side_effect = SleepException()
         self.assertRaises(Etcd3Error, self.etcd3.get_cluster)
Exemplo n.º 2
0
 def test_call_rpc(self, mock_urlopen):
     request = {'key': base64_encode('/patroni/test/leader')}
     mock_urlopen.return_value = MockResponse()
     mock_urlopen.return_value.content = '{"succeeded":true,"header":{"revision":"1"}}'
     self.client.call_rpc('/kv/txn', {'success': [{'request_delete_range': request}]})
     self.client.call_rpc('/kv/put', request)
     self.client.call_rpc('/kv/deleterange', request)
Exemplo n.º 3
0
def mock_urlopen(self, method, url, **kwargs):
    ret = MockResponse()
    if method == 'GET' and url.endswith('/version'):
        ret.content = '{"etcdserver": "3.3.13", "etcdcluster": "3.3.0"}'
    elif method != 'POST':
        raise Exception('Unexpected request method: {0} {1} {2}'.format(
            method, url, kwargs))
    elif url.endswith('/cluster/member/list'):
        ret.content = '{"members":[{"clientURLs":["http://localhost:2379", "http://localhost:4001"]}]}'
    elif url.endswith('/auth/authenticate'):
        ret.content = '{"token":"authtoken"}'
    elif url.endswith('/lease/grant'):
        ret.content = '{"ID": "123"}'
    elif url.endswith('/lease/keepalive'):
        ret.content = '{"result":{"TTL":30}}'
    elif url.endswith('/kv/range'):
        ret.content = json.dumps({
            "header": {
                "revision": "1"
            },
            "kvs": [{
                "key": base64_encode('/patroni/test/leader'),
                "value": base64_encode('foo'),
                "lease": "bla",
                "mod_revision": '1'
            }, {
                "key": base64_encode('/patroni/test/members/foo'),
                "value": base64_encode('{}'),
                "lease": "123",
                "mod_revision": '1'
            }, {
                "key": base64_encode('/patroni/test/failover'),
                "value": base64_encode('{}'),
                "mod_revision": '1'
            }]
        })
    elif url.endswith('/watch'):
        key = base64_encode('/patroni/test/config')
        ret.read_chunked = Mock(return_value=[
            json.dumps({
                'result': {
                    'events': [
                        {
                            'kv': {
                                'key': key,
                                'value': base64_encode('bar'),
                                'mod_revision': '2'
                            }
                        },
                        {
                            'kv': {
                                'key': key,
                                'value': base64_encode('buzz'),
                                'mod_revision': '3'
                            }
                        },
                        {
                            'type': 'DELETE',
                            'kv': {
                                'key': key,
                                'mod_revision': '4'
                            }
                        },
                        {
                            'kv': {
                                'key': base64_encode(
                                    '/patroni/test/optime/leader'),
                                'value': base64_encode('1234567'),
                                'mod_revision': '5'
                            }
                        },
                    ]
                }
            })[:-1].encode('utf-8'),
            b'}{"error":{"grpc_code":14,"message":"","http_code":503}}'
        ])
    elif url.endswith('/kv/put') or url.endswith('/kv/txn'):
        ret.status_code = 400
        ret.content = '{"code":5,"error":"etcdserver: requested lease not found"}'
    elif not url.endswith('/kv/deleterange'):
        raise Exception('Unexpected url: {0} {1} {2}'.format(
            method, url, kwargs))
    return ret