|
| 1 | +# pylint: disable=unused-argument |
| 2 | +# pylint: disable=redefined-outer-name |
| 3 | + |
| 4 | +from collections.abc import Callable |
| 5 | +from typing import AsyncIterator, NamedTuple |
| 6 | + |
| 7 | +import pytest |
| 8 | +import traitlets.config |
| 9 | +from dask_gateway import Gateway, GatewayCluster, auth |
| 10 | +from dask_gateway_server.app import DaskGateway |
| 11 | +from dask_gateway_server.backends.local import UnsafeLocalBackend |
| 12 | +from distributed import Client |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def local_dask_gateway_server_config( |
| 17 | + unused_tcp_port_factory: Callable, |
| 18 | +) -> traitlets.config.Config: |
| 19 | + c = traitlets.config.Config() |
| 20 | + assert isinstance(c.DaskGateway, traitlets.config.Config) |
| 21 | + assert isinstance(c.ClusterConfig, traitlets.config.Config) |
| 22 | + assert isinstance(c.Proxy, traitlets.config.Config) |
| 23 | + assert isinstance(c.SimpleAuthenticator, traitlets.config.Config) |
| 24 | + c.DaskGateway.backend_class = UnsafeLocalBackend |
| 25 | + c.DaskGateway.address = f"127.0.0.1:{unused_tcp_port_factory()}" |
| 26 | + c.Proxy.address = f"127.0.0.1:{unused_tcp_port_factory()}" |
| 27 | + c.DaskGateway.authenticator_class = "dask_gateway_server.auth.SimpleAuthenticator" |
| 28 | + c.SimpleAuthenticator.password = "qweqwe" # noqa: S105 |
| 29 | + c.ClusterConfig.worker_cmd = [ |
| 30 | + "dask-worker", |
| 31 | + "--resources", |
| 32 | + f"CPU=12,GPU=1,RAM={16e9}", |
| 33 | + ] |
| 34 | + # NOTE: This must be set such that the local unsafe backend creates a worker with enough cores/memory |
| 35 | + c.ClusterConfig.worker_cores = 12 |
| 36 | + c.ClusterConfig.worker_memory = "16G" |
| 37 | + c.ClusterConfig.cluster_max_workers = 3 |
| 38 | + |
| 39 | + c.DaskGateway.log_level = "DEBUG" |
| 40 | + return c |
| 41 | + |
| 42 | + |
| 43 | +class DaskGatewayServer(NamedTuple): |
| 44 | + address: str |
| 45 | + proxy_address: str |
| 46 | + password: str |
| 47 | + server: DaskGateway |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +async def local_dask_gateway_server( |
| 52 | + local_dask_gateway_server_config: traitlets.config.Config, |
| 53 | +) -> AsyncIterator[DaskGatewayServer]: |
| 54 | + print("--> creating local dask gateway server") |
| 55 | + dask_gateway_server = DaskGateway(config=local_dask_gateway_server_config) |
| 56 | + dask_gateway_server.initialize([]) # that is a shitty one! |
| 57 | + print("--> local dask gateway server initialized") |
| 58 | + await dask_gateway_server.setup() |
| 59 | + await dask_gateway_server.backend.proxy._proxy_contacted # pylint: disable=protected-access |
| 60 | + |
| 61 | + print("--> local dask gateway server setup completed") |
| 62 | + yield DaskGatewayServer( |
| 63 | + f"http://{dask_gateway_server.backend.proxy.address}", |
| 64 | + f"gateway://{dask_gateway_server.backend.proxy.tcp_address}", |
| 65 | + local_dask_gateway_server_config.SimpleAuthenticator.password, # type: ignore |
| 66 | + dask_gateway_server, |
| 67 | + ) |
| 68 | + print("--> local dask gateway server switching off...") |
| 69 | + await dask_gateway_server.cleanup() |
| 70 | + print("...done") |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture |
| 74 | +async def dask_gateway( |
| 75 | + local_dask_gateway_server: DaskGatewayServer, |
| 76 | +) -> Gateway: |
| 77 | + async with Gateway( |
| 78 | + local_dask_gateway_server.address, |
| 79 | + local_dask_gateway_server.proxy_address, |
| 80 | + asynchronous=True, |
| 81 | + auth=auth.BasicAuth("pytest_user", local_dask_gateway_server.password), |
| 82 | + ) as gateway: |
| 83 | + print(f"--> {gateway=} created") |
| 84 | + cluster_options = await gateway.cluster_options() |
| 85 | + gateway_versions = await gateway.get_versions() |
| 86 | + clusters_list = await gateway.list_clusters() |
| 87 | + print(f"--> {gateway_versions=}, {cluster_options=}, {clusters_list=}") |
| 88 | + for option in cluster_options.items(): |
| 89 | + print(f"--> {option=}") |
| 90 | + return gateway |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture |
| 94 | +async def dask_gateway_cluster(dask_gateway: Gateway) -> AsyncIterator[GatewayCluster]: |
| 95 | + async with dask_gateway.new_cluster() as cluster: |
| 96 | + yield cluster |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture |
| 100 | +async def dask_gateway_cluster_client( |
| 101 | + dask_gateway_cluster: GatewayCluster, |
| 102 | +) -> AsyncIterator[Client]: |
| 103 | + async with dask_gateway_cluster.get_client() as client: |
| 104 | + yield client |
0 commit comments