From 05f40ab88b9e4994701b5f065ae26f466ca69071 Mon Sep 17 00:00:00 2001 From: Skrynnik Daniil Date: Fri, 27 Dec 2024 19:22:35 +0300 Subject: [PATCH] ADCM-6247: fixes after merge --- tests/integration/test_cluster.py | 2 +- tests/integration/test_host.py | 98 ++++++++++++++++++++++++++----- 2 files changed, 83 insertions(+), 17 deletions(-) diff --git a/tests/integration/test_cluster.py b/tests/integration/test_cluster.py index b4c51ef..7db8e0a 100644 --- a/tests/integration/test_cluster.py +++ b/tests/integration/test_cluster.py @@ -1,8 +1,8 @@ from collections.abc import Collection from pathlib import Path -import asyncio import random import string +import asyncio from httpx import AsyncClient import pytest diff --git a/tests/integration/test_host.py b/tests/integration/test_host.py index bbd21a5..45fcf9e 100644 --- a/tests/integration/test_host.py +++ b/tests/integration/test_host.py @@ -1,4 +1,5 @@ from pathlib import Path +from typing import NamedTuple import pytest import pytest_asyncio @@ -23,6 +24,15 @@ pytestmark = [pytest.mark.asyncio] +class Expected(NamedTuple): + name: str + description: str + provider_id: int + cluster_id: int | None + status: str = "down" + maintenance_mode: str = "off" + + @pytest_asyncio.fixture() async def cluster_bundle(adcm_client: ADCMClient, tmp_path: Path) -> Bundle: bundle_path = pack_bundle(from_dir=BUNDLES / "complex_cluster", to=tmp_path) @@ -30,8 +40,10 @@ async def cluster_bundle(adcm_client: ADCMClient, tmp_path: Path) -> Bundle: @pytest_asyncio.fixture() -async def cluster(adcm_client: ADCMClient, cluster_bundle: Bundle) -> Cluster: - return await adcm_client.clusters.create(bundle=cluster_bundle, name="Cluster", description="Cluster description") +async def cluster(adcm_client: ADCMClient, complex_cluster_bundle: Bundle) -> Cluster: + return await adcm_client.clusters.create( + bundle=complex_cluster_bundle, name="Cluster", description="Cluster description" + ) @pytest_asyncio.fixture() @@ -54,8 +66,12 @@ async def test_host(adcm_client: ADCMClient, hostprovider: HostProvider, cluster name=f"test-host-{i}", ) - await cluster.hosts.add(host=await adcm_client.hosts.get(name__eq="test-host-0")) - await _test_host_properties(adcm_client.hosts, hostprovider, cluster) + expected = Expected(name="test-host-0", description="", cluster_id=cluster.id, provider_id=hostprovider.id) + host = await adcm_client.hosts.get(name__eq=expected.name) + await cluster.hosts.add(host=host) + await host.refresh() + + await _test_host_properties(host, expected) await _test_host_accessors(adcm_client.hosts, cluster) await _test_pagination(adcm_client.hosts) @@ -79,25 +95,75 @@ async def test_host_in_host_group(adcm_client: ADCMClient, hostprovider: HostPro await config_host_group.hosts.add(await cluster.hosts.filter(name__contains="test-host")) await action_host_group.hosts.add(await cluster.hosts.filter(name__contains="test-host")) - await _test_host_properties(config_host_group.hosts, hostprovider, cluster) - await _test_host_properties(action_host_group.hosts, hostprovider, cluster) + expected = Expected(name="test-host-0", description="", cluster_id=cluster.id, provider_id=hostprovider.id) + await _test_host_properties(await config_host_group.hosts.get(name__eq=expected.name), expected) + await _test_host_properties(await action_host_group.hosts.get(name__eq=expected.name), expected) assert len(await config_host_group.hosts.all()) == 80 assert len(await action_host_group.hosts.all()) == 80 -async def _test_host_properties( - hosts_node: HostsInActionHostGroupNode | HostsInConfigHostGroupNode | HostsNode, - hostprovider: HostProvider, - cluster: Cluster, +async def test_host_objects( + adcm_client: ADCMClient, hostprovider_bundle: Bundle, complex_cluster_bundle: Bundle ) -> None: - host = await hosts_node.get(name__eq="test-host-0") - assert host.name == "test-host-0" - assert (await host.hostprovider).name == hostprovider.name - assert (await host.cluster).name == cluster.name # pyright: ignore[reportOptionalMemberAccess] + """Testing similarity, accessibility of attributes of host objects got from different sources""" + + host_name = "Target-test-host" + provider = await adcm_client.hostproviders.create(bundle=hostprovider_bundle, name="New provider") + await adcm_client.hosts.create(hostprovider=provider, name=host_name) + target_host = await adcm_client.hosts.get(name__eq=host_name) + + cluster = await adcm_client.clusters.create( + bundle=complex_cluster_bundle, name="Cluster with hosts", description="descr" + ) + service = (await cluster.services.add(Filter(attr="name", op="eq", value="example_1")))[0] + component = await service.components.get(name__eq="first") + + await cluster.hosts.add(host=target_host) + await target_host.refresh() + + mapping = await cluster.mapping + await mapping.add(component=component, host=target_host) + await mapping.save() + + chg = await cluster.config_host_groups.create(name="chg", hosts=[target_host]) + ahg = await cluster.action_host_groups.create(name="ahg", hosts=[target_host]) + + expected = Expected(name=host_name, description="", cluster_id=cluster.id, provider_id=provider.id) + from_chg = await chg.hosts.get(name__eq=expected.name) + await _test_host_properties(from_chg, expected) + from_ahg = await ahg.hosts.get(name__eq=expected.name) + await _test_host_properties(from_ahg, expected) + from_mapping = await mapping.hosts.get(name__eq=expected.name) + await _test_host_properties(from_mapping, expected) + from_cluster = await cluster.hosts.get(name__eq=expected.name) + await _test_host_properties(from_cluster, expected) + from_component = await component.hosts.get(name__eq=expected.name) + await _test_host_properties(from_component, expected) + from_provider = await provider.hosts.get(name__eq=expected.name) + await _test_host_properties(from_provider, expected) + + assert ( + target_host.id + == from_chg.id + == from_ahg.id + == from_mapping.id + == from_cluster.id + == from_component.id + == from_provider.id + ) + + +async def _test_host_properties(host: Host, expected: Expected) -> None: + assert isinstance(host.id, int) + assert host.name == expected.name + assert isinstance(host_cluster := await host.cluster, Cluster) + assert host_cluster.id == expected.cluster_id assert isinstance(host.actions, ActionsAccessor) - assert await host.get_status() == "down" - assert (await host.maintenance_mode).value == "off" + assert isinstance(await host.actions.all(), list) + assert await host.get_status() == expected.status + assert (await host.hostprovider).id == expected.provider_id + assert (await host.maintenance_mode).value == expected.maintenance_mode async def _test_host_accessors(