Exemplo n.º 1
0
 def test_test_and_set(self):
     """ Can test and set a value """
     client = etcd_gevent.Client()
     client.api_execute = mock.Mock(return_value=FakeHTTPResponse(
         200, '{"action":"SET",'
         '"node": {'
         '"key":"/testkey",'
         '"prevValue":"test",'
         '"value":"newvalue",'
         '"expiration":"2013-09-14T02:09:44.24390976+02:00",'
         '"ttl":49,"modifiedIndex":203}}'))
     result = client.test_and_set('/testkey', 'newvalue', 'test', ttl=19)
     self.assertEquals(
         etcd_gevent.EtcdResult(
             **{
                 u'action': u'SET',
                 u'node': {
                     u'expiration': u'2013-09-14T02:09:44.24390976+02:00',
                     u'modifiedIndex': 203,
                     u'key': u'/testkey',
                     u'prevValue': u'test',
                     u'ttl': 49,
                     u'value': u'newvalue'
                 }
             }), result)
Exemplo n.º 2
0
    def test_set(self):
        """ Can set a value """
        client = etcd_gevent.Client()
        client.api_execute = mock.Mock(return_value=FakeHTTPResponse(
            201, '{"action":"SET",'
            '"node": {'
            '"key":"/testkey",'
            '"value":"test",'
            '"newKey":true,'
            '"expiration":"2013-09-14T00:56:59.316195568+02:00",'
            '"ttl":19,"modifiedIndex":183}}'))

        result = client.set('/testkey', 'test', ttl=19)

        self.assertEquals(
            etcd_gevent.EtcdResult(
                **{
                    u'action': u'SET',
                    'node': {
                        u'expiration': u'2013-09-14T00:56:59.316195568+02:00',
                        u'modifiedIndex': 183,
                        u'key': u'/testkey',
                        u'newKey': True,
                        u'ttl': 19,
                        u'value': u'test'
                    }
                }), result)
Exemplo n.º 3
0
 def test_get_dir(self):
     """Can get values in dirs"""
     d = {
         u'action': u'get',
         u'node': {
             u'modifiedIndex': 190,
             u'key': u'/testkey',
             u'dir': True,
             u'nodes': [
                 {
                     u'key': u'/testDir/testKey',
                     u'modifiedIndex': 150,
                     u'value': 'test'
                 },
                 {
                     u'key': u'/testDir/testKey2',
                     u'modifiedIndex': 190,
                     u'value': 'test2'
                 }
             ]
         }
     }
     self._mock_api(200, d)
     res = self.client.read('/testDir', recursive=True)
     self.assertEquals(res, etcd_gevent.EtcdResult(**d))
Exemplo n.º 4
0
 def test_index_watch(self):
     """ Can watch values from index """
     client = etcd_gevent.Client()
     client.api_execute = mock.Mock(return_value=FakeHTTPResponse(
         200, '{"action":"SET",'
         '"node": {'
         '"key":"/testkey",'
         '"value":"test",'
         '"newKey":true,'
         '"expiration":"2013-09-14T01:35:07.623681365+02:00",'
         '"ttl":19,'
         '"modifiedIndex":180}}'))
     result = client.watch('/testkey', index=180)
     self.assertEquals(
         etcd_gevent.EtcdResult(
             **{
                 u'action': u'SET',
                 u'node': {
                     u'expiration': u'2013-09-14T01:35:07.623681365+02:00',
                     u'modifiedIndex': 180,
                     u'key': u'/testkey',
                     u'newKey': True,
                     u'ttl': 19,
                     u'value': u'test'
                 }
             }), result)
Exemplo n.º 5
0
    def test_acquired_no_timeout(self):
        self.locker._sequence = 4
        returns = [('/_locks/test_lock/4', None),
                   ('/_locks/test_lock/1',
                    etcd_gevent.EtcdResult(node={
                        "key": '/_locks/test_lock/4',
                        "modifiedIndex": 1
                    }))]

        def side_effect():
            return returns.pop()

        d = {
            u'action': u'get',
            u'node': {
                u'modifiedIndex': 190,
                u'key': u'/_locks/test_lock/4',
                u'value': self.locker.uuid
            }
        }
        self._mock_api(200, d)

        self.locker._get_locker = mock.create_autospec(self.locker._get_locker,
                                                       side_effect=side_effect)
        self.assertTrue(self.locker._acquired())
Exemplo n.º 6
0
 def check_watch(self, result):
     assert etcd_gevent.EtcdResult(
         **{
             u'action': u'SET',
             u'node': {
                 u'expiration': u'2013-09-14T01:35:07.623681365+02:00',
                 u'modifiedIndex': 180,
                 u'key': u'/testkey',
                 u'newKey': True,
                 u'ttl': 19,
                 u'value': u'test'
             }
         }) == result
Exemplo n.º 7
0
 def test_watch_index(self):
     """ Can watch a key starting from the given Index """
     d = {
         u'action': u'get',
         u'node': {
             u'modifiedIndex': 170,
             u'key': u'/testkey',
             u'value': u'testold'
         }
     }
     self._mock_api(200, d)
     res = self.client.read('/testkey', wait=True, waitIndex=True)
     self.assertEquals(res, etcd_gevent.EtcdResult(**d))
Exemplo n.º 8
0
 def test_watch(self):
     """ Can watch a key """
     d = {
         u'action': u'get',
         u'node': {
             u'modifiedIndex': 190,
             u'key': u'/testkey',
             u'value': u'test'
         }
     }
     self._mock_api(200, d)
     res = self.client.read('/testkey', wait=True)
     self.assertEquals(res, etcd_gevent.EtcdResult(**d))
Exemplo n.º 9
0
 def test_delete(self):
     """ Can delete a value """
     d = {
         u'action': u'delete',
         u'node': {
             u'key': u'/testkey',
             "modifiedIndex": 3,
             "createdIndex": 2
         }
     }
     self._mock_api(200, d)
     res = self.client.delete('/testKey')
     self.assertEquals(res, etcd_gevent.EtcdResult(**d))
Exemplo n.º 10
0
    def test_compare_and_swap(self):
        """ Can set compare-and-swap a value """
        d = {u'action': u'compareAndSwap',
             u'node': {
                 u'expiration': u'2013-09-14T00:56:59.316195568+02:00',
                 u'modifiedIndex': 183,
                 u'key': u'/testkey',
                 u'ttl': 19,
                 u'value': u'test'
             }
             }

        self._mock_api(200, d)
        res = self.client.write('/testkey', 'test', prevValue='test_old')
        self.assertEquals(res, etcd_gevent.EtcdResult(**d))
Exemplo n.º 11
0
    def test_set_plain(self):
        """ Can set a value """
        d = {u'action': u'set',
             u'node': {
                 u'expiration': u'2013-09-14T00:56:59.316195568+02:00',
                 u'modifiedIndex': 183,
                 u'key': u'/testkey',
                 u'ttl': 19,
                 u'value': u'test'
             }
         }

        self._mock_api(200, d)
        res = self.client.write('/testkey', 'test')
        self.assertEquals(res, etcd_gevent.EtcdResult(**d))
Exemplo n.º 12
0
    def test_refresh(self):
        """ Can refresh a new value """
        d = {
            u'action': u'update',
            u'node': {
                u'expiration': u'2016-05-31T08:27:54.660337Z',
                u'modifiedIndex': 183,
                u'key': u'/testkey',
                u'ttl': 600,
                u'value': u'test'
            }
        }

        self._mock_api(200, d)
        res = self.client.refresh('/testkey', ttl=600)
        self.assertEquals(res, etcd_gevent.EtcdResult(**d))
Exemplo n.º 13
0
 def _result_from_response(self, response):
     """ Creates an EtcdResult from json dictionary """
     raw_response = response.read()
     try:
         res = json.loads(raw_response.decode('utf-8'))
     except (TypeError, ValueError, UnicodeError) as e:
         raise etcd_gevent.EtcdException(
             'Server response was not valid JSON: %r' % e)
     try:
         r = etcd_gevent.EtcdResult(**res)
         if response.status_code == 201:
             r.newKey = True
         r.parse_headers(response)
         return r
     except Exception as e:
         raise etcd_gevent.EtcdException(
             'Unable to decode server response: %r' % e)
Exemplo n.º 14
0
 def test_get_locker(self):
     self.recursive_read()
     self.assertEquals((u'/_locks/test_lock/1',
                        etcd_gevent.EtcdResult(
                            node={
                                'newKey': False,
                                '_children': [],
                                'createdIndex': 33,
                                'modifiedIndex': 33,
                                'value': u'2qwwwq',
                                'expiration': None,
                                'key': u'/_locks/test_lock/1',
                                'ttl': None,
                                'action': None,
                                'dir': False
                            })), self.locker._get_locker())
     with self.assertRaises(etcd_gevent.EtcdLockExpired):
         self.locker._sequence = '35'
         self.locker._get_locker()
Exemplo n.º 15
0
    def test_get(self):
        """ Can get a value """
        client = etcd_gevent.Client()
        client.api_execute = mock.Mock(return_value=FakeHTTPResponse(
            200, '{"action":"GET",'
            '"node": {'
            '"key":"/testkey",'
            '"value":"test",'
            '"modifiedIndex":190}}'))

        result = client.get('/testkey')
        self.assertEquals(
            etcd_gevent.EtcdResult(
                **{
                    u'action': u'GET',
                    u'node': {
                        u'modifiedIndex': 190,
                        u'key': u'/testkey',
                        u'value': u'test'
                    }
                }), result)
Exemplo n.º 16
0
    def test_update_fails(self):
        """ Non-atomic updates fail """
        d = {
            u'action': u'set',
            u'node': {
                u'expiration': u'2013-09-14T00:56:59.316195568+02:00',
                u'modifiedIndex': 6,
                u'key': u'/testkey',
                u'ttl': 19,
                u'value': u'test'
            }
        }
        res = etcd_gevent.EtcdResult(**d)

        error = {
            "errorCode": 101,
            "message": "Compare failed",
            "cause": "[ != bar] [7 != 6]",
            "index": 6}
        self._mock_api(412, error)
        res.value = 'bar'
        self.assertRaises(ValueError, self.client.update, res)
Exemplo n.º 17
0
 def test_delete(self):
     """ Can delete a value """
     client = etcd_gevent.Client()
     client.api_execute = mock.Mock(return_value=FakeHTTPResponse(
         200, '{"action":"DELETE",'
         '"node": {'
         '"key":"/testkey",'
         '"prevValue":"test",'
         '"expiration":"2013-09-14T01:06:35.5242587+02:00",'
         '"modifiedIndex":189}}'))
     result = client.delete('/testkey')
     self.assertEquals(
         etcd_gevent.EtcdResult(
             **{
                 u'action': u'DELETE',
                 u'node': {
                     u'expiration': u'2013-09-14T01:06:35.5242587+02:00',
                     u'modifiedIndex': 189,
                     u'key': u'/testkey',
                     u'prevValue': u'test'
                 }
             }), result)