Skip to content

Commit

Permalink
ADCM-6321: Add advanced tests for mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Starovoitov committed Feb 11, 2025
1 parent 307d0ac commit 1f21812
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions tests/integration/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from collections.abc import Iterable
from collections.abc import AsyncGenerator, Iterable
from contextlib import suppress
from itertools import chain
from pathlib import Path
import asyncio

from asyncstdlib.contextlib import contextmanager
from httpx import AsyncClient
import pytest
import pytest_asyncio
Expand Down Expand Up @@ -90,8 +91,23 @@ async def create_additional_user(httpx_client: AsyncClient, username: str) -> Cr
return Credentials(username, "Password1234")


def assert_mapping(mapping: Iterable[tuple[Component, Host]], expected: Iterable[tuple[Component, Host]]) -> bool:
return sorted([(p.id, v.id) for p, v in mapping]) == sorted([(p.id, v.id) for p, v in expected])
def assert_mapping(mapping: Iterable[tuple[Component, Host]], expected: Iterable[tuple[Component, Host]]) -> None:
assert sorted([(p.id, v.id) for p, v in mapping]) == sorted([(p.id, v.id) for p, v in expected])


@contextmanager
async def new_admin_session(adcm: ADCMContainer) -> AsyncGenerator[ADCMClient, None]:
session = None
credentials = Credentials(username="admin", password="admin") # noqa: S106
kwargs = {"verify": False, "timeout": 10, "retry_interval": 1, "retry_attempts": 1}
try:
session = ADCMSession(url=adcm.url, credentials=credentials, **kwargs)
yield await session.__aenter__()
except Exception as e: # noqa: BLE001
print(f"An error occurred while creating an admin session: {e}")
finally:
if session:
await session.__aexit__()


async def test_cluster_mapping(adcm_client: ADCMClient, cluster: Cluster, hosts: FiveHosts) -> None:
Expand Down Expand Up @@ -245,9 +261,7 @@ async def test_apply_remote_changes_if_local_mapping_did_not_change(
await mapping.add(c1, (h1, h2))
await mapping.save()

credentials = Credentials(username="admin", password="admin") # noqa: S106
kwargs = {"verify": False, "timeout": 10, "retry_interval": 1, "retry_attempts": 1}
async with ADCMSession(url=adcm.url, credentials=credentials, **kwargs) as session_2:
async with new_admin_session(adcm) as session_2:
mapping_session2 = await (await session_2.clusters.get(name__contains="Awesome")).mapping
refreshed_entries = (await mapping_session2.refresh(strategy=apply_remote_changes)).all()

Expand All @@ -268,9 +282,7 @@ async def test_full_mapping_update_with_apply_local_changes(
await mapping_session_1.add((c2, c3), h1)
await mapping_session_1.save()

credentials = Credentials(username="admin", password="admin") # noqa: S106
kwargs = {"verify": False, "timeout": 10, "retry_interval": 1, "retry_attempts": 1}
async with ADCMSession(url=adcm.url, credentials=credentials, **kwargs) as session_2:
async with new_admin_session(adcm) as session_2:
mapping_session2 = await (await session_2.clusters.get(name__contains="Awesome")).mapping

await mapping_session2.add(c3, (h2, h3))
Expand All @@ -296,9 +308,7 @@ async def test_full_mapping_update_with_apply_remote_changes(
await mapping_session_1.add((c2, c3), h1)
await mapping_session_1.save()

credentials = Credentials(username="admin", password="admin") # noqa: S106
kwargs = {"verify": False, "timeout": 10, "retry_interval": 1, "retry_attempts": 1}
async with ADCMSession(url=adcm.url, credentials=credentials, **kwargs) as session_2:
async with new_admin_session(adcm) as session_2:
mapping_session2 = await (await session_2.clusters.get(name__contains="Awesome")).mapping

await mapping_session2.add((c3, c1), h2)
Expand Down Expand Up @@ -327,11 +337,9 @@ async def test_mapping_with_hosts_in_mm(adcm: ADCMContainer, cluster: Cluster, h
await mapping_session_1.save()

refreshed_entries = (await mapping_session_1.refresh()).all()
assert_mapping(refreshed_entries, [])
assert_mapping(refreshed_entries, [(c1, h1)])

credentials = Credentials(username="admin", password="admin") # noqa: S106
kwargs = {"verify": False, "timeout": 10, "retry_interval": 1, "retry_attempts": 1}
async with ADCMSession(url=adcm.url, credentials=credentials, **kwargs) as session_2:
async with new_admin_session(adcm) as session_2:
mapping_session_2 = await (await session_2.clusters.get(name__contains="Awesome")).mapping
await mapping_session_2.add(c1, h2)
await mapping_session_2.save()
Expand All @@ -354,9 +362,7 @@ async def test_partial_mapping_update_with_apply_local_changes(
await mapping_session_1.add(c2, h1)
await mapping_session_1.save()

credentials = Credentials(username="admin", password="admin") # noqa: S106
kwargs = {"verify": False, "timeout": 10, "retry_interval": 1, "retry_attempts": 1}
async with ADCMSession(url=adcm.url, credentials=credentials, **kwargs) as session_2:
async with new_admin_session(adcm) as session_2:
mapping_session2 = await (await session_2.clusters.get(name__contains="Awesome")).mapping

await mapping_session2.add(c1, (h1, h2))
Expand All @@ -380,9 +386,7 @@ async def test_partial_mapping_update_with_apply_remote_changes(
await mapping_session_1.add(c2, h1)
await mapping_session_1.save()

credentials = Credentials(username="admin", password="admin") # noqa: S106
kwargs = {"verify": False, "timeout": 10, "retry_interval": 1, "retry_attempts": 1}
async with ADCMSession(url=adcm.url, credentials=credentials, **kwargs) as session_2:
async with new_admin_session(adcm) as session_2:
mapping_session2 = await (await session_2.clusters.get(name__contains="Awesome")).mapping

await mapping_session2.add(c1, (h1, h2))
Expand Down Expand Up @@ -410,9 +414,7 @@ async def test_remapping_host_with_mm(adcm: ADCMContainer, cluster: Cluster, hos
refreshed_entries = (await mapping_session_1.refresh(strategy=apply_remote_changes)).all()
assert_mapping(refreshed_entries, [(c1, h1)])

credentials = Credentials(username="admin", password="admin") # noqa: S106
kwargs = {"verify": False, "timeout": 10, "retry_interval": 1, "retry_attempts": 1}
async with ADCMSession(url=adcm.url, credentials=credentials, **kwargs) as session_2:
async with new_admin_session(adcm) as session_2:
mapping_session2 = await (await session_2.clusters.get(name__contains="Awesome")).mapping
await mapping_session2.remove(c1, h1)

Expand All @@ -423,13 +425,14 @@ async def test_remapping_host_with_mm(adcm: ADCMContainer, cluster: Cluster, hos
async def test_mapping_service_with_dependency_from_another_service(
cluster_with_service_dependencies: Cluster, hosts: FiveHosts
) -> None:
service_1 = await cluster_with_service_dependencies.services.get(name__eq="second_service")
cluster = cluster_with_service_dependencies
service_1 = await cluster.services.get(name__eq="second_service")

c1 = await service_1.components.get(name__eq="first_component")
h1, h2, *_ = hosts
await cluster_with_service_dependencies.hosts.add((h1, h2))
await cluster.hosts.add((h1, h2))

mapping = await cluster_with_service_dependencies.mapping
mapping = await cluster.mapping
await mapping.add(c1, h1)

with suppress(ConflictError):
Expand All @@ -438,11 +441,7 @@ async def test_mapping_service_with_dependency_from_another_service(
refreshed_entries = (await mapping.refresh(strategy=apply_remote_changes)).all()
assert_mapping(refreshed_entries, [(c1, h1)])

service_2 = (
await cluster_with_service_dependencies.services.add(
filter_=Filter(attr="name", op="eq", value="first_service")
)
)[0]
service_2 = (await cluster.services.add(filter_=Filter(attr="name", op="eq", value="first_service")))[0]

c2 = await service_2.components.get(name__eq="first_component")

Expand All @@ -457,13 +456,14 @@ async def test_mapping_service_with_dependency_from_another_service(
async def test_mapping_component_with_dependency_from_another_service(
cluster_with_bound_component: Cluster, hosts: FiveHosts
) -> None:
cluster = cluster_with_bound_component
service_1 = await cluster_with_bound_component.services.get(name__eq="second_service")

c1 = await service_1.components.get(name__eq="first_component")
h1, h2, *_ = hosts
await cluster_with_bound_component.hosts.add((h1, h2))
await cluster.hosts.add((h1, h2))

mapping = await cluster_with_bound_component.mapping
mapping = await cluster.mapping
await mapping.add(c1, h1)

with suppress(ConflictError):
Expand All @@ -472,9 +472,7 @@ async def test_mapping_component_with_dependency_from_another_service(
refreshed_entries = (await mapping.refresh(strategy=apply_remote_changes)).all()
assert_mapping(refreshed_entries, [(c1, h1)])

service_2 = (
await cluster_with_bound_component.services.add(filter_=Filter(attr="name", op="eq", value="first_service"))
)[0]
service_2 = (await cluster.services.add(filter_=Filter(attr="name", op="eq", value="first_service")))[0]

c2 = await service_2.components.get(name__eq="first_component")

Expand Down

0 comments on commit 1f21812

Please sign in to comment.