Skip to content

Commit

Permalink
speedup tests by using session-scoped fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
YevheniiSemendiak committed Apr 12, 2023
1 parent 0ab6f81 commit bf3b5ba
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
26 changes: 13 additions & 13 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand All @@ -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]:
Expand All @@ -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(
Expand All @@ -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.
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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")
Expand All @@ -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,
Expand Down Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/conftest_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def auth_config(
)


@pytest.fixture
@pytest.fixture(scope="session")
async def auth_client(
auth_config: PlatformAuthConfig,
) -> AsyncGenerator[AuthClient, None]:
Expand All @@ -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]]]:
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/conftest_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/conftest_kube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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("://")
Expand Down
17 changes: 10 additions & 7 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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]:
Expand Down Expand Up @@ -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]]:
Expand All @@ -297,15 +297,15 @@ 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],
) -> JobsClient:
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 {
Expand All @@ -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]],
Expand Down Expand Up @@ -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}}

Expand Down

0 comments on commit bf3b5ba

Please sign in to comment.