From bf3b5baca536e8c19992e1493ef532b49a7ae5c7 Mon Sep 17 00:00:00 2001 From: Yevhenii Semendiak Date: Wed, 12 Apr 2023 08:37:29 +0300 Subject: [PATCH] speedup tests by using session-scoped fixtures --- tests/integration/conftest.py | 26 +++++++++++++------------- tests/integration/conftest_auth.py | 4 ++-- tests/integration/conftest_config.py | 6 +++--- tests/integration/conftest_kube.py | 10 +++++----- tests/integration/test_api.py | 17 ++++++++++------- 5 files changed, 33 insertions(+), 30 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 259d7efa..1263e237 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -76,7 +76,7 @@ def minikube_ip() -> str: return subprocess.check_output(("minikube", "ip"), text=True).strip() -@pytest.fixture +@pytest.fixture(scope="session") async def client() -> AsyncIterator[aiohttp.ClientSession]: async with aiohttp.ClientSession() as session: yield session @@ -104,7 +104,7 @@ async def wait_for_service( await asyncio.sleep(interval_s) -@pytest.fixture +@pytest.fixture(scope="session") # TODO (A Yushkovskiy, 05-May-2019) This fixture should have scope="session" in order # to be faster, but it causes mysterious errors `RuntimeError: Event loop is closed` async def platform_api_config( @@ -123,7 +123,7 @@ async def platform_api_config( ) -@pytest.fixture +@pytest.fixture(scope="session") async def container_runtime_config( in_minikube: bool, kube_container_runtime: str ) -> ContainerRuntimeConfig: @@ -139,7 +139,7 @@ async def container_runtime_config( return ContainerRuntimeConfig(name=kube_container_runtime, port=url.port) -@pytest.fixture +@pytest.fixture(scope="session") async def container_runtime_client_registry( container_runtime_config: ContainerRuntimeConfig, ) -> AsyncIterator[ContainerRuntimeClientRegistry]: @@ -149,7 +149,7 @@ async def container_runtime_client_registry( yield registry -@pytest.fixture +@pytest.fixture(scope="session") # TODO (A Yushkovskiy, 05-May-2019) This fixture should have scope="session" in order # to be faster, but it causes mysterious errors `RuntimeError: Event loop is closed` async def es_config( @@ -170,7 +170,7 @@ async def es_config( yield ElasticsearchConfig(hosts=[es_host]) -@pytest.fixture +@pytest.fixture(scope="session") async def es_client(es_config: ElasticsearchConfig) -> AsyncIterator[Elasticsearch]: """Elasticsearch client that goes directly to elasticsearch-logging service without any authentication. @@ -179,7 +179,7 @@ async def es_client(es_config: ElasticsearchConfig) -> AsyncIterator[Elasticsear yield es_client -@pytest.fixture +@pytest.fixture(scope="session") def s3_config(s3_logs_bucket: str, s3_logs_key_prefix_format: str) -> S3Config: s3_url = get_service_url(service_name="minio") return S3Config( @@ -192,7 +192,7 @@ def s3_config(s3_logs_bucket: str, s3_logs_key_prefix_format: str) -> S3Config: ) -@pytest.fixture +@pytest.fixture(scope="session") async def s3_client(s3_config: S3Config) -> AsyncIterator[AioBaseClient]: session = aiobotocore.session.get_session() async with session.create_client( @@ -205,17 +205,17 @@ async def s3_client(s3_config: S3Config) -> AsyncIterator[AioBaseClient]: yield client -@pytest.fixture +@pytest.fixture(scope="session") def s3_logs_bucket() -> str: return "logs" -@pytest.fixture +@pytest.fixture(scope="session") def s3_logs_key_prefix_format() -> str: return "kube.var.log.containers.{pod_name}_{namespace_name}_{container_name}" -@pytest.fixture +@pytest.fixture(scope="session") async def registry_config(request: FixtureRequest, in_minikube: bool) -> RegistryConfig: if in_minikube: external_url = URL("http://registry.kube-system") @@ -227,7 +227,7 @@ async def registry_config(request: FixtureRequest, in_minikube: bool) -> Registr return RegistryConfig(URL("http://localhost:5000")) -@pytest.fixture +@pytest.fixture(scope="session") def config_factory( auth_config: PlatformAuthConfig, platform_api_config: PlatformApiConfig, @@ -260,7 +260,7 @@ def _f(**kwargs: Any) -> Config: return _f -@pytest.fixture +@pytest.fixture(scope="session") def config(config_factory: Callable[..., Config]) -> Config: return config_factory() diff --git a/tests/integration/conftest_auth.py b/tests/integration/conftest_auth.py index 19af3ed7..fb6e3abc 100644 --- a/tests/integration/conftest_auth.py +++ b/tests/integration/conftest_auth.py @@ -45,7 +45,7 @@ def auth_config( ) -@pytest.fixture +@pytest.fixture(scope="session") async def auth_client( auth_config: PlatformAuthConfig, ) -> AsyncGenerator[AuthClient, None]: @@ -63,7 +63,7 @@ def headers(self) -> dict[str, str]: return {AUTHORIZATION: f"Bearer {self.token}"} -@pytest.fixture +@pytest.fixture(scope="session") async def share_job( auth_client: AuthClient, cluster_name: str ) -> AsyncIterator[Callable[[_User, _User, str], Awaitable[None]]]: diff --git a/tests/integration/conftest_config.py b/tests/integration/conftest_config.py index 14e3e195..46c08297 100644 --- a/tests/integration/conftest_config.py +++ b/tests/integration/conftest_config.py @@ -21,7 +21,7 @@ def cluster_token(token_factory: Callable[[str], str]) -> str: return token_factory("cluster") -@pytest.fixture +@pytest.fixture(scope="session") async def platform_config_url( platform_api_config: PlatformApiConfig, in_minikube: bool ) -> URL: @@ -31,14 +31,14 @@ async def platform_config_url( return URL(get_service_url("platformconfig", namespace="default")) -@pytest.fixture +@pytest.fixture(scope="session") def platform_config( platform_config_url: URL, token_factory: Callable[[str], str] ) -> PlatformConfig: return PlatformConfig(url=platform_config_url, token=token_factory("cluster")) -@pytest.fixture +@pytest.fixture(scope="session") @pytest.mark.usefixtures("cluster_name") async def platform_config_client( platform_config_url: URL, cluster_token: str diff --git a/tests/integration/conftest_kube.py b/tests/integration/conftest_kube.py index eebeb33c..ec8a26e2 100644 --- a/tests/integration/conftest_kube.py +++ b/tests/integration/conftest_kube.py @@ -194,7 +194,7 @@ def cert_authority_data_pem( return None -@pytest.fixture +@pytest.fixture(scope="session") async def kube_config(request: FixtureRequest, in_minikube: bool) -> KubeConfig: if in_minikube: return KubeConfig( @@ -221,7 +221,7 @@ async def kube_config(request: FixtureRequest, in_minikube: bool) -> KubeConfig: return kube_config -@pytest.fixture +@pytest.fixture(scope="session") async def kube_client(kube_config: KubeConfig) -> AsyncIterator[MyKubeClient]: # TODO (A Danshyn 06/06/18): create a factory method client = MyKubeClient( @@ -242,19 +242,19 @@ async def kube_client(kube_config: KubeConfig) -> AsyncIterator[MyKubeClient]: yield client -@pytest.fixture +@pytest.fixture(scope="session") async def _kube_node(kube_client: KubeClient) -> Node: nodes = await kube_client.get_nodes() assert len(nodes) == 1, "Should be exactly one minikube node" return nodes[0] -@pytest.fixture +@pytest.fixture(scope="session") async def kube_node_name(_kube_node: Node) -> str: return _kube_node.name -@pytest.fixture +@pytest.fixture(scope="session") async def kube_container_runtime(_kube_node: Node) -> str: version = _kube_node.container_runtime_version end = version.find("://") diff --git a/tests/integration/test_api.py b/tests/integration/test_api.py index 4bb93592..3af36bcd 100644 --- a/tests/integration/test_api.py +++ b/tests/integration/test_api.py @@ -172,14 +172,14 @@ def generate_job_url(self, job_id: str) -> URL: return self.jobs_base_url / job_id -@pytest.fixture +@pytest.fixture(scope="session") async def monitoring_api(config: Config) -> AsyncIterator[MonitoringApiEndpoints]: app = await create_app(config) async with create_local_app_server(app, port=8080) as address: yield MonitoringApiEndpoints(address=address) -@pytest.fixture +@pytest.fixture(scope="session") async def monitoring_api_s3_storage( config_s3_storage: Config, ) -> AsyncIterator[MonitoringApiEndpoints]: @@ -188,7 +188,7 @@ async def monitoring_api_s3_storage( yield MonitoringApiEndpoints(address=address) -@pytest.fixture +@pytest.fixture(scope="session") async def platform_api( platform_api_config: PlatformApiConfig, ) -> AsyncIterator[PlatformApiEndpoints]: @@ -287,7 +287,7 @@ async def drop_job(self, job_id: str, assert_success: bool = True) -> None: assert response.status == HTTPNoContent.status_code -@pytest.fixture +@pytest.fixture(scope="session") def jobs_client_factory( platform_api: PlatformApiEndpoints, client: aiohttp.ClientSession ) -> Iterator[Callable[[_User], JobsClient]]: @@ -297,7 +297,7 @@ def impl(user: _User) -> JobsClient: yield impl -@pytest.fixture +@pytest.fixture(scope="session") async def jobs_client( regular_user1: _User, jobs_client_factory: Callable[[_User], JobsClient], @@ -305,7 +305,7 @@ async def jobs_client( return jobs_client_factory(regular_user1) -@pytest.fixture +@pytest.fixture(scope="session") def job_request_factory() -> Callable[[], dict[str, Any]]: def _factory() -> dict[str, Any]: return { @@ -326,7 +326,7 @@ async def job_submit( return job_request_factory() -@pytest.fixture +@pytest.fixture(scope="session") async def job_factory( jobs_client: JobsClient, job_request_factory: Callable[[], dict[str, Any]], @@ -970,8 +970,11 @@ async def test_save_ok( config: Config, ) -> None: url = monitoring_api.generate_save_url(job_id=infinite_job) + print(url) headers = jobs_client.headers + print(headers) repository = f"{config.registry.host}/alpine" + print(repository) image = f"{repository}:{infinite_job}" payload = {"container": {"image": image}}