jupyter-server-proxy
jupyter-server-proxy copied to clipboard
Looking up dynamically allocated port numbers to support testing of proxied services
Is there a simple pattern for creating basic tests of proxied services, eg using a tool such as playwright?
If we know the port number a service is running on, then a simple test of the following form can be used to check that the page loads with the desired title, and then grabs a screenshot of it:
#test_app.py
PORT= 8765
def test_initial_load(page: Page):
page.goto(f"http://127.0.0.1:{PORT}")
expect(page).to_have_title("Page Title")
page.screenshot(path="proxied-service-screenshot.png")
#Usage:
# pip install pytest-playwright
# playwright install
# playwright install-deps
# pytest
Using the proxy name alias doesn't seem to get resolved, whereas specifying the port number explicitly does seem to work.
But in a general case, how can we find the port number for a proxied service, particularly if it it allocated dynamically?
I note also that we can get some general metadata about proxied services from http://localhost/server-proxy/servers-info, although not the port numbers.
Forgive what's probably a stupid question, but can you test it via the proxy URL (e.g. http://127.0.0.1:8888/foo), rather than looking up the port for foo to access it directly?
If JSP launches the server, you need to make at least 1 request via the proxy to ensure it's launched before you try to talk to it. And the server may also know (via some sort of config) what URL it expects to be visible at, so it might not work properly without the proxy URL.
If you do need to access it directly, one option would be to load the same config and entry points that JSP does, and launch services in your test code (at least those using dynamic ports), copying the code to get a random port number:
https://github.com/jupyterhub/jupyter-server-proxy/blob/5ff4c77245a4e92d4c7bb1e7651b65f7a898312b/jupyter_server_proxy/handlers.py#L593-L596
IIRC, my issue was I have dozens of servers running on different ports, and if I launch a new Jupyter server and to test it, I don't necessarily know what port it's running on if the port is dynamically allocated.
Using the proxy name alias doesn't seem to get resolved, whereas specifying the port number explicitly does seem to work.
The proxy name should server:8888/<service-name> work with dynamically allocated ports, we have some tests:
https://github.com/jupyterhub/jupyter-server-proxy/blob/31e8b303072d99ddbea9fcdef8169ec19f1f25a4/tests/resources/jupyter_server_config.py#L31-L88
https://github.com/jupyterhub/jupyter-server-proxy/blob/main/tests/test_proxies.py
So I think it's worth investigating why it's not working for you.
IIRC, my issue was I have dozens of servers running on different ports, and if I launch a new Jupyter server and to test it, I don't necessarily know what port it's running on if the port is dynamically allocated.
I didn't quite follow this. Is the issue that you don't know which port either Jupyter or your proxied service is running on?
If your tests know how to talk to the Jupyter server, then as @manics said, the configured proxy name should work, so it's worth figuring out why it doesn't.
In https://github.com/jupyterhub/jupyter-server-proxy/blob/main/tests/test_proxies.py, the PORT is set as:
PORT = os.getenv('TEST_PORT', 8888)
If I start a jupyter server, and the default 8888 is already allocated, the server is launched on a new port number. What port number? How do I assign that number to eg the env var TEST_PORT as used in the test script?
OK, I think what you're really asking is "how do I find out what port jupyter-server is running on" rather than anything specific to jupyter-server-proxy, since it also applies to any extensions you want to test.
For CI testing I usually aim for as much control over the environment as possible, so in that situation I'd tell jupyter-server which port (jupyter-lab --port=12345 ..., export TEST_PORT=12345 pytest ...) to use rather than letting it pick one itself, same as how the CI tests override the token for convenience:
https://github.com/jupyterhub/jupyter-server-proxy/blob/31e8b303072d99ddbea9fcdef8169ec19f1f25a4/.github/workflows/test.yaml#L81-L84
If you want to find out the dynamically chosen jupyter-server port though it might be worth asking over on the jupyter-server repo or on Discourse?
For what it's worth, I think the port info could be added to the api at server-proxy/servers-info. The api is only used for UI right now. The endpoint has the comment, "Don't send anything that might be a callable, or leak sensitive info," but it is authenticated, and I'm not sure what is considered sensitive and how it could be leaked.