Example #1
0
    def test_retry_request(self):
        with unittest.mock.patch('http.client.HTTPConnection',
                                 autospec=True) as mock_connection:

            class HTTPResponseMock:
                def __init__(self, status, content):
                    self.status = status
                    self.content = content

                def read(self):
                    return self.content.encode('utf-8')

            mock_connection.return_value.getresponse.side_effect = [
                ConnectionError(),
                HTTPResponseMock(400, '{"code":400, "'),
                HTTPResponseMock(200, '{"code":200, "response": "response"}'),
            ]

            store_object = store.StoreKVS('localhost', 8010)
            response = store_object.make_request('/test', {'test': 'test'})
            mock_connection.return_value.request.assert_has_calls([
                unittest.mock.call('POST', '/test', '{"test": "test"}'),
                unittest.mock.call('POST', '/test', '{"test": "test"}'),
                unittest.mock.call('POST', '/test', '{"test": "test"}')
            ])
            self.assertEqual(response, 'response')
Example #2
0
 def test_cache_get_success(self):
     with unittest.mock.patch('store.StoreKVS.make_request',
                              autospec=True) as mock_make_request:
         mock_make_request.return_value = 'response'
         store_object = store.StoreKVS('localhost', 8010)
         value = store_object.cache_get('123')
         mock_make_request.assert_called_once_with(store_object,
                                                   '/cache_get',
                                                   {'key': '123'})
         self.assertEqual(value, 'response')
Example #3
0
 def setUpClass(cls):
     cls.root = pathlib.Path('./test_api_integration')
     if cls.root.is_dir():
         shutil.rmtree(str(cls.root))
     cls.root.mkdir(parents=True)
     cls.kvs = ManageKVS(8012, cls.root)
     cls.api = ManageAPI(8082, cls.root, 'localhost,8012,10,3')
     cls.kvs.start()
     cls.api.start()
     cls.store = store.StoreKVS('localhost', 8012)
     super().setUpClass()
Example #4
0
    def test_cache_get_fail(self):
        with unittest.mock.patch('store.StoreKVS.make_request',
                                 autospec=True) as mock_make_request:
            mock_make_request.side_effect = ConnectionError
            store_object = store.StoreKVS('localhost', 8010)
            value = store_object.cache_get('123')
            mock_make_request.assert_called_once_with(store_object,
                                                      '/cache_get',
                                                      {'key': '123'})
            self.assertIsNone(value)

        with unittest.mock.patch('store.StoreKVS.make_request',
                                 autospec=True) as mock_make_request:
            mock_make_request.side_effect = KeyError
            store_object = store.StoreKVS('localhost', 8010)
            value = store_object.cache_get('123')
            mock_make_request.assert_called_once_with(store_object,
                                                      '/cache_get',
                                                      {'key': '123'})
            self.assertIsNone(value)
Example #5
0
 def test_data_set_success(self):
     with unittest.mock.patch('store.StoreKVS.make_request',
                              autospec=True) as mock_make_request:
         mock_make_request.return_value = None
         store_object = store.StoreKVS('localhost', 8010)
         store_object.set('123', 456)
         mock_make_request.assert_called_once_with(store_object,
                                                   '/data_set', {
                                                       'key': '123',
                                                       'value': 456
                                                   })
Example #6
0
    def test_data_get_fail(self):
        with unittest.mock.patch('store.StoreKVS.make_request',
                                 autospec=True) as mock_make_request:
            mock_make_request.side_effect = ConnectionError
            store_object = store.StoreKVS('localhost', 8010)
            with self.assertRaises(ConnectionError):
                store_object.get('123')
            mock_make_request.assert_called_once_with(store_object,
                                                      '/data_get',
                                                      {'key': '123'})

        with unittest.mock.patch('store.StoreKVS.make_request',
                                 autospec=True) as mock_make_request:
            mock_make_request.side_effect = KeyError
            store_object = store.StoreKVS('localhost', 8010)
            with self.assertRaises(KeyError):
                store_object.get('123')
            mock_make_request.assert_called_once_with(store_object,
                                                      '/data_get',
                                                      {'key': '123'})
Example #7
0
 def test_cache_set_fail(self):
     with unittest.mock.patch('store.StoreKVS.make_request',
                              autospec=True) as mock_make_request:
         mock_make_request.side_effect = ConnectionError
         store_object = store.StoreKVS('localhost', 8010)
         store_object.cache_set('123', 456, 789)
         mock_make_request.assert_called_once_with(store_object,
                                                   '/cache_set', {
                                                       'key': '123',
                                                       'value': 456,
                                                       'timeout': 789
                                                   })
Example #8
0
    def test_retry_request_fail(self):
        with unittest.mock.patch('http.client.HTTPConnection',
                                 autospec=True) as mock_connection:
            mock_connection.return_value.getresponse.side_effect = [
                ConnectionError(),
                ConnectionError(),
                ConnectionError(),
            ]

            store_object = store.StoreKVS('localhost', 8010)
            with self.assertRaises(ConnectionError):
                store_object.make_request('/test', {'test': 'test'})
            mock_connection.return_value.request.assert_has_calls([
                unittest.mock.call('POST', '/test', '{"test": "test"}'),
                unittest.mock.call('POST', '/test', '{"test": "test"}'),
                unittest.mock.call('POST', '/test', '{"test": "test"}')
            ])
 def make_store(self):
     return store.StoreKVS('localhost', 8011)
Example #10
0
        context.update(r)
        logging.info(context)
        self.wfile.write(json.dumps(r).encode("utf-8"))
        return

    def log_message(self, format, *args):
        logging.info('HTTP: ' + format, *args)


if __name__ == "__main__":
    op = OptionParser()
    op.add_option("-p", "--port", action="store", type=int, default=8080)
    op.add_option("-l", "--log", action="store", default=None)
    op.add_option("-s",
                  "--storage",
                  action="store",
                  default="localhost,8010,10,3")
    (opts, args) = op.parse_args()
    logging.basicConfig(filename=opts.log,
                        level=logging.INFO,
                        format='[%(asctime)s] %(levelname).1s %(message)s',
                        datefmt='%Y.%m.%d %H:%M:%S')
    MainHTTPHandler.store = store.StoreKVS(*opts.storage.split(','))
    server = HTTPServer(("localhost", opts.port), MainHTTPHandler)
    logging.info("Starting server at %s" % opts.port)
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
    server.server_close()
Example #11
0
 def test_connect(self):
     with unittest.mock.patch('http.client.HTTPConnection',
                              autospec=True) as mock_connection:
         store_object = store.StoreKVS('localhost', 8010)
         mock_connection.assert_called_once_with('localhost', 8010, 10)