Skip to content

Commit

Permalink
ADCM-6286 Add example tests for host groups (#82)
Browse files Browse the repository at this point in the history
Co-authored-by: Araslanov Egor <[email protected]>
  • Loading branch information
2 people authored and a-alferov committed Jan 23, 2025
1 parent ae102fa commit e574c86
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 20 deletions.
1 change: 1 addition & 0 deletions tests/integration/bundles/complex_cluster/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
- type: cluster
name: Some Cluster
config_group_customization: yes
version: 1
allow_maintenance_mode: true

Expand Down
13 changes: 3 additions & 10 deletions tests/integration/examples/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@

import pytest_asyncio

from adcm_aio_client._types import Credentials
from adcm_aio_client import Credentials
from adcm_aio_client.objects import Bundle
from tests.integration.setup_environment import ADCMContainer

TIMEOUT = 10
RETRY_INTERVAL = 1
RETRY_ATTEMPTS = 1
DEFAULT_SESSION_KWARGS = {
"timeout": TIMEOUT,
"retry_interval": RETRY_INTERVAL,
"retry_attempts": RETRY_ATTEMPTS,
"credentials": Credentials(username="admin", password="admin"), # noqa: S106
}
REQUEST_KWARGS: dict = {"timeout": 10, "retry_interval": 1, "retry_attempts": 1}
CREDENTIALS = Credentials(username="admin", password="admin") # noqa: S106


@pytest_asyncio.fixture()
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/examples/test_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from adcm_aio_client import ADCMSession
from tests.integration.bundle import pack_bundle
from tests.integration.conftest import BUNDLES
from tests.integration.examples.conftest import DEFAULT_SESSION_KWARGS
from tests.integration.examples.conftest import CREDENTIALS, REQUEST_KWARGS
from tests.integration.setup_environment import ADCMContainer

pytestmark = [pytest.mark.asyncio]
Expand All @@ -26,7 +26,7 @@ async def test_bundle(adcm: ADCMContainer, packed_bundle_with_license: Path) ->
- bundle removal
- retrieval with filtering / all bundles
"""
async with ADCMSession(url=adcm.url, **DEFAULT_SESSION_KWARGS) as client:
async with ADCMSession(url=adcm.url, credentials=CREDENTIALS, **REQUEST_KWARGS) as client:
all_bundles = await client.bundles.all()
assert len(all_bundles) == 4

Expand Down
103 changes: 103 additions & 0 deletions tests/integration/examples/test_host_groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import asyncio

import pytest

from adcm_aio_client import ADCMSession, Filter
from adcm_aio_client.config import ParameterHG
from adcm_aio_client.objects import ActionHostGroup, ConfigHostGroup
from tests.integration.examples.conftest import CREDENTIALS, REQUEST_KWARGS
from tests.integration.setup_environment import ADCMContainer

pytestmark = [pytest.mark.asyncio]


async def test_config_host_group(adcm: ADCMContainer) -> None:
"""
Config Host Group API Examples:
- creation
- deletion
- hosts management
- various retrieval approaches
- config basic interaction
"""
async with ADCMSession(url=adcm.url, credentials=CREDENTIALS, **REQUEST_KWARGS) as client:
bundle = await client.bundles.get(name__eq="simple_provider")
hostprovider = await client.hostproviders.create(bundle=bundle, name="For Hosts")
bundle = await client.bundles.get(name__icontains="Some cluste", version__eq="1")
cluster = await client.clusters.create(bundle=bundle, name="For CHG")
hosts = await asyncio.gather(
*(
client.hosts.create(hostprovider=hostprovider, name=f"host-for-group-{i}", cluster=cluster)
for i in range(3)
)
)

# Config host groups exist for Cluster, Service, Component and HostProvider.
# Not all hosts can be added to groups, only "related":
# refer to documentation for more info.
group: ConfigHostGroup = await cluster.config_host_groups.create(name="Cluster Group", hosts=hosts)

config = await group.config
config["string_field", ParameterHG].set("chanded value")
await config.save()
assert (await group.config_history[-1]).id == config.id

assert len(await group.hosts.all()) == 3
one_of_hosts, *_ = await group.hosts.filter(name__in=["host-for-group-1", "host-for-group-0"])
await group.hosts.remove(host=one_of_hosts)
assert len(await group.hosts.all()) == 2
assert len(await cluster.hosts.all()) == 3
await group.hosts.add(host=Filter(attr="name", op="eq", value=one_of_hosts.name))

# Since it's there's only one group, get without parameters will work,
# yet avoid using it like that.
group_ = await cluster.config_host_groups.get()
assert group_.id == group.id

await group.delete()
assert len(await cluster.config_host_groups.all()) == 0


async def test_action_host_group(adcm: ADCMContainer) -> None:
"""
Action Host Group API Examples:
- creation
- deletion
- hosts management
- various retrieval approaches
- action basic interaction
"""
async with ADCMSession(url=adcm.url, credentials=CREDENTIALS, **REQUEST_KWARGS) as client:
bundle = await client.bundles.get(name__eq="simple_provider")
hostprovider = await client.hostproviders.create(bundle=bundle, name="For Hosts")
bundle = await client.bundles.get(name__icontains="Some cluste", version__eq="1")
cluster = await client.clusters.create(bundle=bundle, name="For AHG")
hosts = await asyncio.gather(
*(
client.hosts.create(hostprovider=hostprovider, name=f"host-for-group-{i}", cluster=cluster)
for i in range(3)
)
)

group: ActionHostGroup = await cluster.action_host_groups.create(name="Cluster Group")
await group.hosts.add(host=hosts)

# Complex actions may have own `config` and `mapping` node as all actions do
action = await group.actions.get(display_name__contains="I will survive")
task = await action.run()
await task.wait(timeout=50)

assert len(await group.hosts.all()) == 3
one_of_hosts, *_ = await group.hosts.filter(name__in=["host-for-group-1", "host-for-group-0"])
await group.hosts.remove(host=one_of_hosts)
assert len(await group.hosts.all()) == 2
assert len(await cluster.hosts.all()) == 3
await group.hosts.add(host=Filter(attr="name", op="eq", value=one_of_hosts.name))

# Since it's there's only one group, get without parameters will work,
# yet avoid using it like that.
group_ = await cluster.action_host_groups.get()
assert group_.id == group.id

await group.delete()
assert len(await cluster.action_host_groups.all()) == 0
12 changes: 4 additions & 8 deletions tests/integration/examples/test_iteration_with_cluster.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest

from adcm_aio_client import ADCMSession, Credentials, Filter
from adcm_aio_client import ADCMSession, Filter
from adcm_aio_client.config import Parameter
from tests.integration.examples.conftest import RETRY_ATTEMPTS, RETRY_INTERVAL, TIMEOUT
from tests.integration.examples.conftest import CREDENTIALS, REQUEST_KWARGS
from tests.integration.setup_environment import ADCMContainer

pytestmark = [pytest.mark.asyncio]
Expand All @@ -14,11 +14,8 @@ async def test_iteration_with_cluster(adcm: ADCMContainer) -> None:
configuring cluster configuration, launching actions on the cluster and updating the cluster.
"""
url = adcm.url
credentials = Credentials(username="admin", password="admin") # noqa: S106

async with ADCMSession(
url=url, credentials=credentials, timeout=TIMEOUT, retry_attempts=RETRY_ATTEMPTS, retry_interval=RETRY_INTERVAL
) as client:
async with ADCMSession(url=url, credentials=CREDENTIALS, **REQUEST_KWARGS) as client:
clusters = await client.clusters.all()
assert len(clusters) == 0

Expand All @@ -28,8 +25,7 @@ async def test_interaction_with_cluster(adcm: ADCMContainer) -> None:
Interaction with clusters: creating, deleting, getting a list of clusters using filtering,
configuring cluster configuration, launching actions on the cluster and updating the cluster.
"""
credentials = Credentials(username="admin", password="admin") # noqa: S106
async with ADCMSession(url=adcm.url, credentials=credentials) as client:
async with ADCMSession(url=adcm.url, credentials=CREDENTIALS, **REQUEST_KWARGS) as client:
simple_cluster = await client.clusters.create(
bundle=await client.bundles.get(name__eq="Simple Cluster"), name="simple_cluster"
)
Expand Down

0 comments on commit e574c86

Please sign in to comment.