예제 #1
0
def serve(entrypoint: str, location: str, deps: str, wsgi: bool, host: str,
          port: int) -> typing.Any:
    """CLI serve."""
    if deps:
        click.echo(f"Installing deps: {deps}")
        _deps(deps, location)

    signal.signal(signal.SIGTERM, handle_sigterm)

    app = ps.generate_app()
    app = ps.append_entrypoint(app, entrypoint, os.path.abspath(location))

    if not wsgi:
        click.echo("Starting Flask Development server")
        app.run(
            host=os.getenv("DPLOY_KICKSTART_HOST", "0.0.0.0"),
            port=int(os.getenv("DPLOY_KICKSTART_PORT", 8080)),
        )
    else:
        click.echo("Starting Waitress server")
        waitress_serve(
            TransLogger(app, setup_console_handler=False),
            host=os.getenv("dploy_kickstart_HOST", "0.0.0.0"),
            port=int(os.getenv("dploy_kickstart_PORT", 8080)),
        )
예제 #2
0
def test_server_generation(
    entrypoint,
    method,
    path,
    payload,
    response,
    accept,
    content_type,
    error,
    status_code,
):
    p = os.path.join(THIS_DIR, "assets")

    try:
        app = ps.generate_app()
        app = ps.append_entrypoint(app, entrypoint, p)

    except:
        assert error
        return

    t_client = app.test_client()
    try:
        if "json" in content_type:
            r = getattr(t_client, method)(
                path,
                json=payload,
                headers={
                    "Accept": accept,
                    "Content-Type": content_type
                },
            )

        else:
            r = getattr(t_client, method)(
                path,
                data=payload,
                headers={
                    "Accept": accept,
                    "Content-Type": content_type
                },
            )
    except:
        assert error
        return

    assert r.status_code == status_code

    if error:  # we expect an error here, no need to keep processing contents
        assert r.status_code != 200
        return

    if "json" in content_type:
        assert r.json == response
    else:
        try:
            assert r.data == response
        except:
            a = 5
예제 #3
0
def test_healthz(path, status_code, method, error):
    p = os.path.join(THIS_DIR, "assets")
    stream = StringIO()
    handler = logging.StreamHandler(stream)
    app = ps.generate_app()
    app = ps.append_entrypoint(app, "server_t1.py", p)
    app.logger.addHandler(handler)
    t_client = app.test_client()
    resp = getattr(t_client, method)("/healthz/")
    try:
        assert resp.status_code == status_code
    except AssertionError:
        assert error
예제 #4
0
def test_server_logs(entrypoint, method, path, payload, accept, content_type,
                     str_pattern, error):
    p = os.path.join(THIS_DIR, "assets")
    stream = StringIO()
    handler = logging.StreamHandler(stream)
    app = ps.generate_app()
    app = ps.append_entrypoint(app, entrypoint, p)
    app.logger.addHandler(handler)
    t_client = app.test_client()
    _ = getattr(t_client, method)(path,
                                  json=payload,
                                  headers={
                                      "Accept": accept,
                                      "Content-Type": content_type
                                  })
    str_log_stream = stream.getvalue()

    try:
        assert re.match(str_pattern, str_log_stream)
    except AssertionError:
        assert error
예제 #5
0
def test_client():
    app = ds.generate_app()
    app.config["TESTING"] = True
    return app.test_client()