Ejemplo n.º 1
0
    def list_keys(self, prefix=None, after=None, limit=None) -> Response:
        """
        Used to get a list of keys matching a certain prefix.

        @param prefix: specifies what you want to get a list of.
        @param after: the item specified as a return value from this function last time you called it.
        @param limit: defaults to 1000 keys returned.
        @return: Response within key_count, next_after, and keys.
        """
        kwargs = {'domain': self._domain}
        if prefix is not None:
            kwargs['prefix'] = prefix
        if after is not None:
            kwargs['after'] = after
        if limit is not None:
            kwargs['limit'] = limit
        try:
            return self._do_request(backend.ListKeysConfig, **kwargs)
        except Exception as exception:
            if exception.code == 'none_match':
                # Empty result set from this list call should not result
                # in an exception. Return a mocked Mogile response instead.
                response = Response('OK \r\n', backend.ListKeysConfig)
                response.data = {
                    'key_count': 0,
                    'next_after': None,
                    'keys': {},
                }
                return response
            raise exception
Ejemplo n.º 2
0
 def test_set_state(self):
     return_value = Response('OK \r\n', SetStateConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         response = Backend([]).set_state(host='localhost',
                                          device=7,
                                          state='down').data
         self.assertEqual(response, {})
Ejemplo n.º 3
0
 def test_set_weight(self):
     return_value = Response('OK \r\n', SetWeightConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         response = Backend([]).set_weight(host='testhost10',
                                           device=6,
                                           weight=8).data
         self.assertEqual(response, {})
Ejemplo n.º 4
0
 def test_delete_class(self):
     return_value = Response('OK domain=testdomain&class=testclass\r\n',
                             DeleteClassConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         classes = Backend([]).delete_class('testdomain', 'testclass').data
         expected = {'domain': 'testdomain', 'class': 'testclass'}
         self.assertEqual(classes, expected)
Ejemplo n.º 5
0
 def test_create_device(self):
     return_value = Response('OK \r\n', CreateDeviceConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         response = Backend([]).create_device(hostname='testhost10',
                                              devid=6,
                                              hostip='0.0.0.0',
                                              state='alive').data
         self.assertEqual(response, {})
Ejemplo n.º 6
0
 def test_update_host(self):
     return_value = Response('OK hostid=7&hostname=hostname\r\n',
                             UpdateHostConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         response = Backend([]).update_host(host='localhost',
                                            ip='0.0.0.0',
                                            port=7001).data
         expected = {'id': '7', 'name': 'hostname'}
         self.assertEqual(response, expected)
Ejemplo n.º 7
0
 def test_get_domains(self):
     return_value = Response(
         'OK domain15class1name=default&domain25class1'
         'name=default&domain41class1mindevcount=2\r\n', GetDomainsConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         domains = Backend([]).get_domains().data['domains']
         self.assertEqual(domains[15]['classes'][1]['name'], 'default')
         self.assertEqual(domains[25]['classes'][1]['name'], 'default')
         self.assertEqual(domains[41]['classes'][1]['mindevcount'], '2')
Ejemplo n.º 8
0
 def test_parse_response_text(self):
     response = Response(
         'OK host6_hostip=10.0.0.25&'
         'host6_http_port=7500&host8_hostname=\r\n', GetHostsConfig)
     expected = [{
         'hostip': '10.0.0.25',
         'http_port': '7500'
     }, {
         'hostname': ''
     }]
     self.assertIn(expected[0], response.data['hosts'].values())
     self.assertIn(expected[1], response.data['hosts'].values())
Ejemplo n.º 9
0
 def list_keys(self, prefix=None, after=None, limit=None):
     kwargs = {'domain': self._domain}
     if prefix is not None:
         kwargs['prefix'] = prefix
     if after is not None:
         kwargs['after'] = after
     if limit is not None:
         kwargs['limit'] = limit
     try:
         return self._do_request(backend.ListKeysConfig, **kwargs)
     except Exception as exception:
         if exception.code == 'none_match':
             # Empty result set from this list call should not result
             # in an exception. Return a mocked Mogile response instead.
             response = Response('OK \r\n', backend.ListKeysConfig)
             response.data = {
                 'key_count': 0,
                 'next_after': None,
                 'keys': {},
             }
             return response
         raise exception
Ejemplo n.º 10
0
    def test_get_file(self):
        class FakeResponse:
            raw = io.BytesIO(b'foo\r\n')

        return_value = Response(
            'OK path1=http://10.0.0.2:7500/dev38/0/056/25'
            '4/0056254995.fid&paths=2&path2=http://10.0.0'
            '.1:7500/dev54/0/056/254/0056254995.fid\r\n', GetPathsConfig)
        with patch.object(requests, 'get', return_value=FakeResponse):
            with patch.object(Client, 'get_paths', return_value=return_value):
                client = Client([], 'domain')
                key = 'test_file_0.634434876753_1480606271.32_4'
                buf = client.get_file(key=key)
                self.assertEqual(buf.read(), b'foo\r\n')
Ejemplo n.º 11
0
 def test_get_devices_with_devices(self):
     return_value = Response(
         'OK dev27_mb_asof=&dev27_mb_total=1870562&dev'
         '26_mb_used=76672&devices=6\r\n', GetDevicesConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         devices = Backend([]).get_devices().data
         expected = [{
             'mb_asof': '',
             'mb_total': '1870562'
         }, {
             'mb_used': '76672'
         }]
         self.assertIn(expected[0], devices['devices'].values())
         self.assertIn(expected[1], devices['devices'].values())
Ejemplo n.º 12
0
 def test_update_class(self):
     return_value = Response(
         'OK mindevcount=3&domain=testdomain&class=tes'
         'tclass\r\n', CreateClassConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         classes = Backend([]).update_class(domain='testdomain',
                                            _class='testclass',
                                            mindevcount=3).data
         expected = {
             'mindevcount': '3',
             'domain': 'testdomain',
             'class': 'testclass'
         }
         self.assertEqual(expected, classes)
Ejemplo n.º 13
0
 def test_get_paths(self):
     return_value = Response(
         'OK path1=http://10.0.0.2:7500/dev38/0/056/25'
         '4/0056254995.fid&paths=2&path2=http://10.0.0'
         '.1:7500/dev54/0/056/254/0056254995.fid\r\n', GetPathsConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         client = Client([], 'domain')
         key = 'test_file_0.634434876753_1480606271.32_4'
         response = client.get_paths(key=key).data
         self.assertEqual(response['path_count'], 2)
         path_1 = 'http://10.0.0.2:7500/dev38/0/056/254/0056254995.fid'
         self.assertIn((1, path_1), response['paths'].items())
         path_2 = 'http://10.0.0.1:7500/dev54/0/056/254/0056254995.fid'
         self.assertIn((2, path_2), response['paths'].items())
Ejemplo n.º 14
0
 def test_list_keys_no_arguments(self):
     return_value = Response(
         'OK key_3=test_file_0.0129341319339_148060608'
         '0.74&key_21=test_file_0.634434876753_1480606'
         '271.32_4&key_count=666&next_after=after\r\n', ListKeysConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         client = Client([], 'domain')
         response = client.list_keys().data
         self.assertEqual(response['key_count'], 666)
         self.assertEqual(response['next_after'], 'after')
         self.assertIn((3, 'test_file_0.0129341319339_1480606080.74'),
                       response['keys'].items())
         self.assertIn((21, 'test_file_0.634434876753_1480606271.32_4'),
                       response['keys'].items())
Ejemplo n.º 15
0
    def test_store_file(self):
        # fake_put is used to read the full contents of `data`, in order for
        # Client.store_file to return the correct position in the file_handle
        # through seek().
        def fake_put(path, data, timeout=None):
            data.read()

        create_open = Response(
            'OK paths=1&path_1=http://10.0.0.1:7500/dev1/0'
            '/1/2/0000000001.fid&fid=56320928&dev_count=1&'
            'devid_1=57\r\n', CreateOpenConfig)
        create_close = Response('OK \r\n', CreateCloseConfig)
        with patch.object(Client, '_create_open', return_value=create_open), \
            patch.object(Client, '_create_close', return_value=create_close), \
                patch('requests.put', new=fake_put):
            client = Client([], 'domain')
            file_handle = io.BytesIO(b'asdf')
            response = client.store_file(file_handle=file_handle,
                                         key='testkey',
                                         _class='testclass')
            self.assertEqual(response['length'], 4)
            self.assertEqual(response['path'],
                             'http://10.0.0.1:7500/dev1/0/1/2/0000000001.fid')
Ejemplo n.º 16
0
 def test_get_hosts_with_hosts(self):
     return_value = Response(
         'OK host6_hostip=10.0.0.25&host6_http_port=75'
         '00&host8_hostname=&hosts=10\r\n', GetHostsConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         hosts = Backend([]).get_hosts().data
         expected = [{
             'hostip': '10.0.0.25',
             'http_port': '7500'
         }, {
             'hostname': ''
         }]
         self.assertIn(expected[0], hosts['hosts'].values())
         self.assertIn(expected[1], hosts['hosts'].values())
Ejemplo n.º 17
0
 def test_get_file_no_paths(self):
     return_value = Response('OK paths=0\r\n', GetPathsConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         with self.assertRaises(FileNotFoundError):
             Client([], 'domain').get_file(key='doesnotexist')
Ejemplo n.º 18
0
 def do_request(self, request):
     assert isinstance(request, Request)
     self._sock.send(bytes(request))
     response_text = self._recv_all()
     self._sock.close()
     return Response(response_text, request.config)
Ejemplo n.º 19
0
 def test_delete_domain(self):
     return_value = Response('OK domain=testdomain\r\n', DeleteDomainConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         domains = Backend([]).delete_domain('testdomain').data
         expected = {'domain': 'testdomain'}
         self.assertEqual(domains, expected)
Ejemplo n.º 20
0
 def test_delete_host(self):
     return_value = Response('OK \r\n', DeleteHostConfig)
     with patch.object(Backend, 'do_request', return_value=return_value):
         response = Backend([]).delete_host(host='localhost').data
         self.assertEqual(response, {})
Ejemplo n.º 21
0
 def test_instantiate_with_error(self):
     response_text = 'ERR unknown_command Unknown+server+command\r\n'
     with self.assertRaises(MogilefsError):
         Response(response_text, GetHostsConfig)
Ejemplo n.º 22
0
 def test_instantiate_with_str(self):
     response_text = 'OK host1_hostip=10.0.0.25'
     Response(response_text, GetHostsConfig)