Skip to content

Commit

Permalink
feat: allow host to be configured in pytest plugin(#167)
Browse files Browse the repository at this point in the history
* Allow test host to be customized

* make host configurable using the solara settings and make the host

and the ports configurable via the command line for pytest.

---------

Co-authored-by: Maarten A. Breddels <[email protected]>
  • Loading branch information
astrofrog and maartenbreddels committed Jul 1, 2023
1 parent 5c26b61 commit 30b3e33
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
11 changes: 6 additions & 5 deletions solara/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ def ssg_crawl(*args, **kwargs): # type: ignore


HERE = Path(__file__).parent
HOST_DEFAULT = os.environ.get("HOST", "localhost")
if "arm64-apple-darwin" in HOST_DEFAULT: # conda activate script
HOST_DEFAULT = "localhost"

LOGGING_CONFIG: dict = {
"version": 1,
Expand Down Expand Up @@ -121,7 +118,11 @@ def cli():

@cli.command()
@click.option("--port", default=int(os.environ.get("PORT", 8765)))
@click.option("--host", default=HOST_DEFAULT)
@click.option(
"--host",
default=settings.main.host,
help="Host to listen on. Defaults to the $HOST environment or $SOLARA_HOST when available or localhost when not given.",
)
@click.option(
"--dev/--no-dev",
default=False,
Expand Down Expand Up @@ -419,7 +420,7 @@ def ssg_run():
@cli.command()
@click.argument("app")
@click.option("--port", default=int(os.environ.get("PORT", 8765)))
@click.option("--host", default=HOST_DEFAULT)
@click.option("--host", default=settings.main.host)
@click.option(
"--headed/--no-headed",
is_flag=True,
Expand Down
7 changes: 7 additions & 0 deletions solara/server/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import site
import sys
import uuid
Expand Down Expand Up @@ -118,6 +119,11 @@ class Config:
env_file = ".env"


HOST_DEFAULT = os.environ.get("HOST", "localhost")
if "arm64-apple-darwin" in HOST_DEFAULT: # conda activate script
HOST_DEFAULT = "localhost"


class MainSettings(pydantic.BaseSettings):
use_pdb: bool = False
mode: str = "production"
Expand All @@ -126,6 +132,7 @@ class MainSettings(pydantic.BaseSettings):
root_path: Optional[str] = None # e.g. /myapp (without trailing slash)
base_url: str = "" # e.g. https://myapp.solara.run/myapp/
platform: str = sys.platform
host = HOST_DEFAULT

class Config:
env_prefix = "solara_"
Expand Down
52 changes: 37 additions & 15 deletions solara/test/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@

logger = logging.getLogger("solara.pytest_plugin")

TEST_PORT = int(os.environ.get("PORT", "18765")) + 100 # do not interfere with the solara integration tests
TEST_PORT_START = int(os.environ.get("PORT", "18765")) + 100 # do not interfere with the solara integration tests
TEST_HOST = solara.server.settings.main.host
TIMEOUT = float(os.environ.get("SOLARA_PW_TIMEOUT", "18"))


@pytest.fixture(scope="session")
def solara_server(request):
global TEST_PORT
webserver = ServerStarlette(TEST_PORT)
TEST_PORT += 1
def solara_server(pytestconfig: Any, request):
port = pytestconfig.getoption("--solara-port")
host = pytestconfig.getoption("--solara-host")
webserver = ServerStarlette(port, host)

try:
webserver.serve_threaded()
Expand Down Expand Up @@ -260,12 +261,11 @@ def serve(self):


@pytest.fixture(scope="session")
def voila_server(notebook_path):
global TEST_PORT
port = TEST_PORT
TEST_PORT += 1
def voila_server(pytestconfig: Any, notebook_path):
port = pytestconfig.getoption("--voila-port")
host = pytestconfig.getoption("--solara-host")
write_notebook(["print('hello')"], notebook_path)
server = ServerVoila(notebook_path, port)
server = ServerVoila(notebook_path, port, host)
try:
server.serve_threaded()
server.wait_until_serving()
Expand All @@ -275,12 +275,11 @@ def voila_server(notebook_path):


@pytest.fixture(scope="session")
def jupyter_server(notebook_path):
global TEST_PORT
port = TEST_PORT
TEST_PORT += 1
def jupyter_server(pytestconfig: Any, notebook_path):
port = pytestconfig.getoption("--jupyter-port")
host = pytestconfig.getoption("--solara-host")
write_notebook(["print('hello')"], notebook_path)
server = ServerJupyter(notebook_path, port)
server = ServerJupyter(notebook_path, port, host)
try:
server.serve_threaded()
server.wait_until_serving()
Expand Down Expand Up @@ -606,3 +605,26 @@ def pytest_addoption(parser: Any) -> None:
default=False,
help="On compare failure, store to the reference image. Useful for running in CI and downloading the snapshots.",
)
group.addoption(
"--solara-host",
default=TEST_HOST,
help="Host or IP all servers will bind to (solara, jupyter, voila)",
)
group.addoption(
"--solara-port",
type=int,
default=TEST_PORT_START + 0,
help="Port the solara server is running on for the test",
)
group.addoption(
"--jupyter-port",
type=int,
default=TEST_PORT_START + 1,
help="Port the jupyter server is running on for the test (for classic notebook and juptyer lab)",
)
group.addoption(
"--voila-port",
type=int,
default=TEST_PORT_START + 2,
help="Port the voila server is running on for the test (for classic notebook and juptyer lab)",
)
10 changes: 10 additions & 0 deletions solara/website/pages/docs/content/10-howto/50-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ After inspecting and approving the screenshots, you can copy them to the `solara
Visual testing with solara is based on [Playwright for Python](https://playwright.dev/python/), which provides a `page` fixture. However, this fixture will make a new page for each test, which is not what we want. Therefore, we provide a `page_session` fixture that will reuse the same page for all tests. This is important because it will make the tests faster.

By following these recommendations and guidelines, you can efficiently test your Solara applications and ensure a smooth developer experience.

# Configuration

## Changing the Hostname

To configure the hostname the socket is bound to when starting the test server, use the `HOST` or `SOLARA_HOST` environment variable (e.g. `SOLARA_HOST=0.0.0.0`). This hostname is also used for the jupyter server and voila. Alternatively the `--solara-host` argument can be passed on the command line for pytest.

## Changing the Port

To configure the ports the socket is bound to when starting the test servers, use the `PORT` environment variable (e.g. `PORT=18865`). This port and subsequent port will be used for solara-server, jupyter-server and voila. Alternatively the `--solara-port` argument can be passed on the command line for pytest for the solara server, and `--jupyter-port` and `--voila-port` for the ports of jupyter server and voila respectively.

0 comments on commit 30b3e33

Please sign in to comment.