Beispiel #1
0
    def test_connections(self):
        process = run_server()
        process2 = run_server(listen=8006, status=8086, bootstrap='tcp://127.0.0.1:8005')
        process3 = run_server(listen=8007, status=8087, bootstrap='tcp://127.0.0.1:8005')

        process.terminate()
        process2.terminate()
        process3.terminate()
Beispiel #2
0
def setup_server(environment='development', server_root=''):
    global test_server, model

    patch_auth()
    test_server = run_server(test_mode=True,
                             environment=environment,
                             server_root=server_root)
Beispiel #3
0
async def test_access_log_format(access_log_format, expected_output, caplog):
    async def app(scope, receive, send):  # pragma: no cover
        assert scope["type"] == "http"
        await send(
            {
                "type": "http.response.start",
                "status": 204,
                "headers": [(b"test-response-header", b"response-header-val")],
            }
        )
        await send({"type": "http.response.body", "body": b"", "more_body": False})

    config = Config(app=app, access_log_format=access_log_format)
    with caplog_for_logger(caplog, "uvicorn.access"):
        async with run_server(config):
            async with httpx.AsyncClient() as client:
                response = await client.get(
                    "http://127.0.0.1:8000",
                    headers={"test-request-header": "request-header-val"},
                )
        assert response.status_code == 204

    access_log_messages = [
        record.message for record in caplog.records if "uvicorn.access" in record.name
    ]

    assert len(access_log_messages) == 1
    assert access_log_messages[0] == expected_output
Beispiel #4
0
async def test_unknown_status_code(caplog):
    async def app(scope, receive, send):
        assert scope["type"] == "http"
        await send({
            "type": "http.response.start",
            "status": 599,
            "headers": []
        })
        await send({
            "type": "http.response.body",
            "body": b"",
            "more_body": False
        })

    config = Config(app=app)
    with caplog_for_logger(caplog, "uvicorn.access"):
        async with run_server(config):
            async with httpx.AsyncClient() as client:
                response = await client.get("http://127.0.0.1:8000")

        assert response.status_code == 599
        messages = [
            record.message for record in caplog.records
            if record.name == "uvicorn.access"
        ]
        assert '"GET / HTTP/1.1" 599' in messages.pop()
def setUpModule(func):
    func.return_value = objectstore_loc
    global test_server, model

    patch_auth()
    test_server = run_server(test_mode=False)
    model = cherrypy.tree.apps['/plugins/kimchi'].root.model
Beispiel #6
0
def setUpModule():
    global test_server, model

    patch_auth()
    model = GingerModel()

    test_server = run_server(test_mode=True, model=model)
Beispiel #7
0
def setUpModule():
    global test_server, model, tmpfile

    patch_auth()
    tmpfile = tempfile.mktemp()
    model = MockModel(tmpfile)
    test_server = run_server(test_mode=True, model=model)
Beispiel #8
0
def setUpModule():
    global test_server, model

    patch_auth()
    test_server = run_server(test_mode=True)
    model = cherrypy.tree.apps['/plugins/kimchi'].root.model
    iso_gen.construct_fake_iso(MOCK_ISO, True, '14.04', 'ubuntu')
Beispiel #9
0
async def test_server_can_read_messages_in_buffer_after_close(
        ws_protocol_cls, http_protocol_cls):
    frames = []

    class App(WebSocketResponse):
        async def websocket_connect(self, message):
            await self.send({"type": "websocket.accept"})
            # Ensure server doesn't start reading frames from read buffer until
            # after client has sent close frame, but server is still able to
            # read these frames
            await asyncio.sleep(0.2)

        async def websocket_receive(self, message):
            frames.append(message.get("bytes"))

    async def send_text(url):
        async with websockets.connect(url) as websocket:
            await websocket.send(b"abc")
            await websocket.send(b"abc")
            await websocket.send(b"abc")

    config = Config(app=App,
                    ws=ws_protocol_cls,
                    http=http_protocol_cls,
                    lifespan="off")
    async with run_server(config):
        await send_text("ws://127.0.0.1:8000")

    assert frames == [b"abc", b"abc", b"abc"]
Beispiel #10
0
async def test_invalid_upgrade(protocol_cls):
    def app(scope):
        return None

    config = Config(app=app, ws=protocol_cls)
    async with run_server(config):
        async with httpx.AsyncClient() as client:
            response = await client.get(
                "http://127.0.0.1:8000",
                headers={
                    "upgrade": "websocket",
                    "connection": "upgrade"
                },
                timeout=5,
            )
        if response.status_code == 426:
            # response.text == ""
            pass  # ok, wsproto 0.13
        else:
            assert response.status_code == 400
            assert response.text.lower().strip().rstrip(".") in [
                "missing sec-websocket-key header",
                "missing sec-websocket-version header",  # websockets
                "missing or empty sec-websocket-key header",  # wsproto
                "failed to open a websocket connection: missing "
                "sec-websocket-key header",
                "failed to open a websocket connection: missing or empty "
                "sec-websocket-key header",
            ]
Beispiel #11
0
def setUpModule(func):
    func.return_value = objectstore_loc
    global test_server, model

    patch_auth()
    test_server = run_server(test_mode=False)
    model = cherrypy.tree.apps['/plugins/kimchi'].root.model
Beispiel #12
0
async def test_server_reject_connection(ws_protocol_cls, http_protocol_cls):
    async def app(scope, receive, send):
        assert scope["type"] == "websocket"

        # Pull up first recv message.
        message = await receive()
        assert message["type"] == "websocket.connect"

        # Reject the connection.
        await send({"type": "websocket.close"})
        # -- At this point websockets' recv() is unusable. --

        # This doesn't raise `TypeError`:
        # See https://github.com/encode/uvicorn/issues/244
        message = await receive()
        assert message["type"] == "websocket.disconnect"

    async def websocket_session(url):
        try:
            async with websockets.connect(url):
                pass  # pragma: no cover
        except Exception:
            pass

    config = Config(app=app,
                    ws=ws_protocol_cls,
                    http=http_protocol_cls,
                    lifespan="off")
    async with run_server(config):
        await websocket_session("ws://127.0.0.1:8000")
Beispiel #13
0
def setUpModule():
    global test_server, model, tmpfile

    patch_auth()
    tmpfile = tempfile.mktemp()
    model = MockModel(tmpfile)
    test_server = run_server(test_mode=True, model=model)
Beispiel #14
0
async def test_default_default_headers():
    config = Config(app=app, loop="asyncio", limit_max_requests=1)
    async with run_server(config):
        async with httpx.AsyncClient() as client:
            response = await client.get("http://127.0.0.1:8000")
            assert response.headers[
                "server"] == "uvicorn" and response.headers["date"]
Beispiel #15
0
async def test_send_binary_data_to_server_bigger_than_default(
        protocol_cls, client_size_sent, server_size_max, expected_result):
    class App(WebSocketResponse):
        async def websocket_connect(self, message):
            await self.send({"type": "websocket.accept"})

        async def websocket_receive(self, message):
            _bytes = message.get("bytes")
            await self.send({"type": "websocket.send", "bytes": _bytes})

    async def send_text(url):
        async with websockets.connect(url,
                                      max_size=client_size_sent) as websocket:
            await websocket.send(b"\x01" * client_size_sent)
            return await websocket.recv()

    config = Config(app=App,
                    ws=protocol_cls,
                    lifespan="off",
                    ws_max_size=server_size_max)
    async with run_server(config):
        if expected_result == 0:
            data = await send_text("ws://127.0.0.1:8000")
            assert data == b"\x01" * client_size_sent
        else:
            with pytest.raises(websockets.ConnectionClosedError) as e:
                data = await send_text("ws://127.0.0.1:8000")
            assert e.value.code == expected_result
Beispiel #16
0
async def test_trace_logging_on_ws_protocol(ws_protocol, caplog):
    async def websocket_app(scope, receive, send):
        assert scope["type"] == "websocket"
        while True:
            message = await receive()
            if message["type"] == "websocket.connect":
                await send({"type": "websocket.accept"})
            elif message["type"] == "websocket.disconnect":
                break

    async def open_connection(url):
        async with websockets.connect(url) as websocket:
            return websocket.open

    config = Config(app=websocket_app, log_level="trace", ws=ws_protocol)
    with caplog_for_logger(caplog, "uvicorn.error"):
        async with run_server(config):
            is_open = await open_connection("ws://127.0.0.1:8000")
        assert is_open
        messages = [
            record.message
            for record in caplog.records
            if record.name == "uvicorn.error"
        ]
        assert any(" - Upgrading to WebSocket" in message for message in messages)
        assert any(" - WebSocket connection made" in message for message in messages)
        assert any(" - WebSocket connection lost" in message for message in messages)
Beispiel #17
0
async def test_app_close(protocol_cls, code, reason):
    async def app(scope, receive, send):
        while True:
            message = await receive()
            if message["type"] == "websocket.connect":
                await send({"type": "websocket.accept"})
            elif message["type"] == "websocket.receive":
                reply = {"type": "websocket.close"}

                if code is not None:
                    reply["code"] = code

                if reason is not None:
                    reply["reason"] = reason

                await send(reply)
            elif message["type"] == "websocket.disconnect":
                break

    async def websocket_session(url):
        async with websockets.connect(url) as websocket:
            await websocket.ping()
            await websocket.send("abc")
            await websocket.recv()

    config = Config(app=app, ws=protocol_cls, lifespan="off")
    async with run_server(config):
        with pytest.raises(websockets.exceptions.ConnectionClosed) as exc_info:
            await websocket_session("ws://127.0.0.1:8000")
        assert exc_info.value.code == (code or 1000)
        assert exc_info.value.reason == (reason or "")
Beispiel #18
0
def setUpModule():
    global test_server, model

    patch_auth()
    test_server = run_server(test_mode=True)
    model = cherrypy.tree.apps['/plugins/kimchi'].root.model
    iso_gen.construct_fake_iso(MOCK_ISO, True, '14.04', 'ubuntu')
Beispiel #19
0
    def test_vm_livemigrate_persistent_API(self):
        patch_auth()

        inst = model.Model(libvirt_uri='qemu:///system',
                           objstore_loc=self.tmp_store)

        host = '127.0.0.1'
        port = get_free_port('http')
        ssl_port = get_free_port('https')
        cherrypy_port = get_free_port('cherrypy_port')

        with RollbackContext() as rollback:
            test_server = run_server(host,
                                     port,
                                     ssl_port,
                                     test_mode=True,
                                     cherrypy_port=cherrypy_port,
                                     model=inst)
            rollback.prependDefer(test_server.stop)

            self.request = partial(request, host, ssl_port)

            self.create_vm_test()
            rollback.prependDefer(rollback_wrapper, self.inst.vm_delete,
                                  u'test_vm_migrate')

            # removing cdrom because it is not shared storage and will make
            # the migration fail
            dev_list = self.inst.vmstorages_get_list('test_vm_migrate')
            self.inst.vmstorage_delete('test_vm_migrate', dev_list[0])

            try:
                self.inst.vm_start('test_vm_migrate')
            except Exception, e:
                self.fail('Failed to start the vm, reason: %s' % e.message)

            migrate_url = "/plugins/kimchi/vms/%s/migrate" % 'test_vm_migrate'

            req = json.dumps({
                'remote_host': KIMCHI_LIVE_MIGRATION_TEST,
                'user': '******'
            })
            resp = self.request(migrate_url, req, 'POST')
            self.assertEquals(202, resp.status)
            task = json.loads(resp.read())
            wait_task(self._task_lookup, task['id'])
            task = json.loads(
                self.request('/plugins/kimchi/tasks/%s' % task['id'],
                             '{}').read())
            self.assertEquals('finished', task['status'])

            try:
                remote_conn = self.get_remote_conn()
                rollback.prependDefer(remote_conn.close)
                remote_vm = remote_conn.lookupByName('test_vm_migrate')
                self.assertTrue(remote_vm.isPersistent())
                remote_vm.destroy()
                remote_vm.undefine()
            except Exception, e:
                self.fail('Migration test failed: %s' % e.message)
Beispiel #20
0
async def test_send_after_protocol_close(ws_protocol_cls, http_protocol_cls):
    class App(WebSocketResponse):
        async def websocket_connect(self, message):
            await self.send({"type": "websocket.accept"})
            await self.send({"type": "websocket.send", "text": "123"})
            await self.send({"type": "websocket.close"})
            with pytest.raises(Exception):
                await self.send({"type": "websocket.send", "text": "123"})

    async def get_data(url):
        async with websockets.connect(url) as websocket:
            data = await websocket.recv()
            is_open = True
            try:
                await websocket.recv()
            except Exception:
                is_open = False
            return (data, is_open)

    config = Config(app=App,
                    ws=ws_protocol_cls,
                    http=http_protocol_cls,
                    lifespan="off")
    async with run_server(config):
        (data, is_open) = await get_data("ws://127.0.0.1:8000")
        assert data == "123"
        assert not is_open
Beispiel #21
0
def setUpModule():
    global test_server, model

    patch_auth()
    model = MockModel('/tmp/obj-store-test')
    test_server = run_server(test_mode=True, model=model)
    iso_gen.construct_fake_iso(MOCK_ISO, True, '14.04', 'ubuntu')
def setUpModule():
    global model, test_server, fake_iso
    cherrypy.request.headers = {'Accept': 'application/json'}
    patch_auth()
    test_server = run_server(test_mode=True)
    model = cherrypy.tree.apps['/plugins/kimchi'].root.model
    fake_iso = '/tmp/fake.iso'
    iso_gen.construct_fake_iso(fake_iso, True, '12.04', 'ubuntu')
Beispiel #23
0
def setUpModule():
    global model, test_server, fake_iso
    cherrypy.request.headers = {'Accept': 'application/json'}
    model = mockmodel.MockModel('/tmp/obj-store-test')
    patch_auth()
    test_server = run_server(test_mode=True, model=model)
    fake_iso = '/tmp/fake.iso'
    iso_gen.construct_fake_iso(fake_iso, True, '12.04', 'ubuntu')
Beispiel #24
0
def setUpModule():
    global test_server, model, host, port, ssl_port

    model = GingerModel()
    host = '127.0.0.1'
    port = get_free_port('http')
    ssl_port = get_free_port('https')
    test_server = run_server(host, port, ssl_port, test_mode=True, model=model)
Beispiel #25
0
def setUpModule():
    global model, test_server, fake_iso
    cherrypy.request.headers = {'Accept': 'application/json'}
    patch_auth()
    test_server = run_server(test_mode=True)
    model = cherrypy.tree.apps['/plugins/kimchi'].root.model
    fake_iso = '/tmp/fake.iso'
    iso_gen.construct_fake_iso(fake_iso, True, '12.04', 'ubuntu')
Beispiel #26
0
def setUpModule():
    global model, test_server, fake_iso
    cherrypy.request.headers = {'Accept': 'application/json'}
    model = mockmodel.MockModel('/tmp/obj-store-test')
    patch_auth()
    test_server = run_server(test_mode=True, model=model)
    fake_iso = '/tmp/fake.iso'
    iso_gen.construct_fake_iso(fake_iso, True, '12.04', 'ubuntu')
Beispiel #27
0
def setUpModule():
    global test_server, model, host, port, ssl_port

    model = GingerModel()
    host = '127.0.0.1'
    port = get_free_port('http')
    ssl_port = get_free_port('https')
    test_server = run_server(host, port, ssl_port, test_mode=True, model=model)
Beispiel #28
0
def setUpModule():
    global test_server, model

    patch_auth()
    test_server = run_server(test_mode=True)
    model = cherrypy.tree.apps['/plugins/kimchi'].root.model

    # Create fake ISO to do the tests
    construct_fake_iso(fake_iso, True, '12.04', 'ubuntu')
Beispiel #29
0
def setUpModule():
    global test_server, model, host, port, ssl_port

    patch_auth(sudo=False)
    model = mockmodel.MockModel("/tmp/obj-store-test")
    host = "127.0.0.1"
    port = get_free_port("http")
    ssl_port = get_free_port("https")
    test_server = run_server(host, port, ssl_port, test_mode=True, model=model)
Beispiel #30
0
def setUpModule():
    global test_server, model

    patch_auth()
    test_server = run_server(test_mode=True)
    model = cherrypy.tree.apps['/plugins/kimchi'].root.model

    # Create fake ISO to do the tests
    construct_fake_iso(fake_iso, True, '12.04', 'ubuntu')
def setUpModule():
    global test_server, model

    patch_auth(sudo=False)
    model = mockmodel.MockModel("/tmp/obj-store-test")
    test_server = run_server(test_mode=True, model=model)

    # Create fake ISO to do the tests
    construct_fake_iso(fake_iso, True, "12.04", "ubuntu")
def setUpModule():
    global test_server, model, host, port, ssl_port

    patch_auth(sudo=False)
    model = mockmodel.MockModel('/tmp/obj-store-test')
    host = '127.0.0.1'
    port = get_free_port('http')
    ssl_port = get_free_port('https')
    test_server = run_server(host, port, ssl_port, test_mode=True, model=model)
Beispiel #33
0
def setUpModule():
    global test_server, model

    patch_auth(sudo=False)
    model = mockmodel.MockModel('/tmp/obj-store-test')
    test_server = run_server(test_mode=True, model=model)

    # Create fake ISO to do the tests
    construct_fake_iso(fake_iso, True, '12.04', 'ubuntu')
Beispiel #34
0
    def test_vm_livemigrate_persistent_API(self):
        patch_auth()

        inst = model.Model(libvirt_uri='qemu:///system',
                           objstore_loc=self.tmp_store)

        host = '127.0.0.1'
        port = get_free_port('http')
        ssl_port = get_free_port('https')
        cherrypy_port = get_free_port('cherrypy_port')

        with RollbackContext() as rollback:
            test_server = run_server(host, port, ssl_port, test_mode=True,
                                     cherrypy_port=cherrypy_port, model=inst)
            rollback.prependDefer(test_server.stop)

            self.request = partial(request, host, ssl_port)

            self.create_vm_test()
            rollback.prependDefer(rollback_wrapper, self.inst.vm_delete,
                                  u'test_vm_migrate')

            # removing cdrom because it is not shared storage and will make
            # the migration fail
            dev_list = self.inst.vmstorages_get_list('test_vm_migrate')
            self.inst.vmstorage_delete('test_vm_migrate',  dev_list[0])

            try:
                self.inst.vm_start('test_vm_migrate')
            except Exception, e:
                self.fail('Failed to start the vm, reason: %s' % e.message)

            migrate_url = "/plugins/kimchi/vms/%s/migrate" % 'test_vm_migrate'

            req = json.dumps({'remote_host': KIMCHI_LIVE_MIGRATION_TEST,
                             'user': '******'})
            resp = self.request(migrate_url, req, 'POST')
            self.assertEquals(202, resp.status)
            task = json.loads(resp.read())
            wait_task(self._task_lookup, task['id'])
            task = json.loads(
                self.request(
                    '/plugins/kimchi/tasks/%s' % task['id'],
                    '{}'
                ).read()
            )
            self.assertEquals('finished', task['status'])

            try:
                remote_conn = self.get_remote_conn()
                rollback.prependDefer(remote_conn.close)
                remote_vm = remote_conn.lookupByName('test_vm_migrate')
                self.assertTrue(remote_vm.isPersistent())
                remote_vm.destroy()
                remote_vm.undefine()
            except Exception, e:
                self.fail('Migration test failed: %s' % e.message)
def setUpModule():
    global test_server, model, host, port, ssl_port

    patch_auth(sudo=False)
    model = mockmodel.MockModel('/tmp/obj-store-test')
    host = '127.0.0.1'
    port = get_free_port('http')
    ssl_port = get_free_port('https')
    test_server = run_server(host, port, ssl_port, test_mode=True, model=model)
def setUpModule():
    global test_server, model, host, port, ssl_port, cherrypy_port

    patch_auth()
    model = Model(None, "/tmp/obj-store-test")
    host = "127.0.0.1"
    port = get_free_port("http")
    ssl_port = get_free_port("https")
    cherrypy_port = get_free_port("cherrypy_port")
    test_server = run_server(host, port, ssl_port, test_mode=True, cherrypy_port=cherrypy_port, model=model)
Beispiel #37
0
def setUpModule():
    global test_server, model, host, port, ssl_port, cherrypy_port

    patch_auth()
    model = MockModel('/tmp/obj-store-test')
    host = '127.0.0.1'
    port = get_free_port('http')
    ssl_port = get_free_port('https')
    cherrypy_port = get_free_port('cherrypy_port')
    test_server = run_server(host, port, ssl_port, test_mode=True,
                             cherrypy_port=cherrypy_port, model=model)
Beispiel #38
0
async def test_run_chain(tls_ca_ssl_context, tls_certificate_pem_path):
    config = Config(
        app=app,
        loop="asyncio",
        limit_max_requests=1,
        ssl_certfile=tls_certificate_pem_path,
    )
    async with run_server(config):
        async with httpx.AsyncClient(verify=tls_ca_ssl_context) as client:
            response = await client.get("https://127.0.0.1:8000")
    assert response.status_code == 204
Beispiel #39
0
def setUpModule():
    global host, port, ssl_port, model, test_server, fake_iso
    cherrypy.request.headers = {'Accept': 'application/json'}
    model = mockmodel.MockModel('/tmp/obj-store-test')
    patch_auth()
    port = get_free_port('http')
    ssl_port = get_free_port('https')
    host = '127.0.0.1'
    test_server = run_server(host, port, ssl_port, test_mode=True, model=model)
    fake_iso = '/tmp/fake.iso'
    iso_gen.construct_fake_iso(fake_iso, True, '12.04', 'ubuntu')
Beispiel #40
0
async def test_disable_default_date_header():
    config = Config(
        app=app,
        loop="asyncio",
        limit_max_requests=1,
        date_header=False,
    )
    async with run_server(config):
        async with httpx.AsyncClient() as client:
            response = await client.get("http://127.0.0.1:8000")
            assert "date" not in response.headers
Beispiel #41
0
async def test_override_server_header_multiple_times():
    config = Config(
        app=app,
        loop="asyncio",
        limit_max_requests=1,
        headers=[("Server", "over-ridden"), ("Server", "another-value")],
    )
    async with run_server(config):
        async with httpx.AsyncClient() as client:
            response = await client.get("http://127.0.0.1:8000")
            assert (response.headers["server"] == "over-ridden, another-value"
                    and response.headers["date"])
Beispiel #42
0
def setUpModule():
    global test_server, model, host, ssl_port, tmpfile

    patch_auth()
    tmpfile = tempfile.mktemp()
    model = MockModel(tmpfile)
    host = '127.0.0.1'
    port = get_free_port('http')
    ssl_port = get_free_port('https')
    cherrypy_port = get_free_port('cherrypy_port')
    test_server = run_server(host, port, ssl_port, test_mode=True,
                             cherrypy_port=cherrypy_port, model=model)
def setUpModule():
    global test_server, model, host, port, ssl_port

    patch_auth(sudo=False)
    model = mockmodel.MockModel('/tmp/obj-store-test')
    host = '127.0.0.1'
    port = get_free_port('http')
    ssl_port = get_free_port('https')
    test_server = run_server(host, port, ssl_port, test_mode=True, model=model)

    # Create fake ISO to do the tests
    construct_fake_iso(fake_iso, True, '12.04', 'ubuntu')
Beispiel #44
0
async def test_return_close_header():
    config = Config(app=app, host="localhost", loop="asyncio", limit_max_requests=1)
    async with run_server(config):
        async with httpx.AsyncClient() as client:
            response = await client.get(
                "http://127.0.0.1:8000", headers={"connection": "close"}
            )

    assert response.status_code == 204
    assert (
        "connection" in response.headers and response.headers["connection"] == "close"
    )
Beispiel #45
0
def setUpModule():
    global host, port, ssl_port, model, test_server, fake_iso
    cherrypy.request.headers = {'Accept': 'application/json'}
    model = mockmodel.MockModel('/tmp/obj-store-test')
    patch_auth()
    port = get_free_port('http')
    ssl_port = get_free_port('https')
    host = '127.0.0.1'
    test_server = run_server(host, port, ssl_port, test_mode=True,
                             model=model)
    fake_iso = '/tmp/fake.iso'
    open(fake_iso, 'w').close()