Skip to content

Commit

Permalink
ADCM-6280: Prepare an example of interaction with a service (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanBalalan authored Jan 22, 2025
1 parent b535a37 commit bd71d1d
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 3 deletions.
5 changes: 5 additions & 0 deletions tests/integration/bundles/complex_cluster/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@
c2:
actions: *actions

- type: service
name: with_license
version: 1
license: ./license.txt

- type: service
name: complex_config
version: 0.3
Expand Down
33 changes: 30 additions & 3 deletions tests/integration/examples/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from collections.abc import Generator
from collections.abc import AsyncGenerator, Generator

import pytest_asyncio

from adcm_aio_client import Credentials
from adcm_aio_client.objects import Bundle
from adcm_aio_client import ADCMSession, Credentials
from adcm_aio_client.client import ADCMClient
from adcm_aio_client.objects import Bundle, Cluster, Host
from tests.integration.setup_environment import ADCMContainer

REQUEST_KWARGS: dict = {"timeout": 10, "retry_interval": 1, "retry_attempts": 1}
Expand All @@ -20,3 +21,29 @@ def adcm(
) -> Generator[ADCMContainer, None, None]:
_ = simple_cluster_bundle, complex_cluster_bundle, previous_complex_cluster_bundle, simple_hostprovider_bundle
yield adcm


@pytest_asyncio.fixture()
async def admin_client(adcm: ADCMContainer) -> AsyncGenerator[ADCMClient, None]:
async with ADCMSession(url=adcm.url, credentials=CREDENTIALS, **REQUEST_KWARGS) as client:
yield client


@pytest_asyncio.fixture()
async def example_cluster(admin_client: ADCMClient) -> AsyncGenerator[Cluster, None]:
cluster_bundle = await admin_client.bundles.get(name__eq="Some Cluster", version__eq="1")
cluster = await admin_client.clusters.create(bundle=cluster_bundle, name="Example cluster")

yield cluster

await cluster.delete()


@pytest_asyncio.fixture()
async def three_hosts(admin_client: ADCMClient, simple_hostprovider_bundle: Bundle) -> list[Host]:
provider = await admin_client.hostproviders.create(bundle=simple_hostprovider_bundle, name="Hostprovider")
names = {"host-1", "host-2", "host-3"}
for name in names:
await admin_client.hosts.create(hostprovider=provider, name=name)

return await admin_client.hosts.filter(name__in=names)
101 changes: 101 additions & 0 deletions tests/integration/examples/test_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import pytest

from adcm_aio_client import Filter
from adcm_aio_client.errors import ObjectDoesNotExistError
from adcm_aio_client.objects import Cluster, Component, Host, Service

pytestmark = [pytest.mark.asyncio]


async def test_service(example_cluster: Cluster, three_hosts: list[Host]) -> None:
"""
Service (`cluster.services`) API Examples:
- adding a service to cluster
- with accepting license
- with dependencies
- retrieval with filtering / all cluster's services
- iteration through all cluster's services
- service removal
- turning on maintenance mode
- refreshing service's data
Component (`service.components`) API Examples:
- retrieval with filtering / all service's components
- retrieval hosts, mapped to component with filtering / all mapped hosts
- turning on maintenance mode
- refreshing component's data
"""
cluster = example_cluster
host_1, host_2, host_3 = sorted(three_hosts, key=lambda host: host.name)

# Add services "example_1" and "example_2" using Filter
await cluster.services.add(filter_=Filter(attr="name", op="iin", value=["Example_1", "example_2"]))

# Add service with all it's dependencies
# Services "service_with_requires_my_service" and "my_service" will be added
await cluster.services.add(
filter_=Filter(attr="name", op="eq", value="service_with_requires_my_service"), with_dependencies=True
)

# Add service and accept license
await cluster.services.add(filter_=Filter(attr="name", op="eq", value="with_license"), accept_license=True)

# Get all cluster's services
all_added_services: list[Service] = await cluster.services.all() # noqa: F841

# iterate through all cluster's services
async for service in cluster.services.iter(): # noqa: B007
pass

# Remove services from cluster. Leave single "example_1" service
for service in await cluster.services.filter(
name__in=["example_2", "service_with_requires_my_service", "my_service", "with_license"]
):
await service.delete()

# Get non-existent (already removed) service "example_2"
none_service: None = await cluster.services.get_or_none(name__eq="example_2") # pyright: ignore[reportAssignmentType] # noqa: F841

# Get "example_1" service using case-insensitive filter by display_name
service = await cluster.services.get(display_name__ieq="first example") # actual display_name is "First Example"

# Turn on service's maintenance mode
service_mm = await service.maintenance_mode
await service_mm.on()

# Sync service's data with remote state
await service.refresh()

# Get all service's components
all_components: list[Component] = await service.components.all() # noqa: F841

# Get specific component
component = await service.components.get(name__eq="first")

# Prepare data: add 3 hosts to cluster, map two of them to component
await cluster.hosts.add(host=[host_1, host_2, host_3])
mapping = await cluster.mapping
await mapping.add(component=component, host=[host_1, host_2])
await mapping.save()

# Get all component's hosts
component_hosts: list[Host] = await component.hosts.all() # noqa: F841

# Iterate through all service's components
async for comp in service.components.iter(): # noqa: B007
pass

# Get all hosts, mapped to component
all_component_hosts: list[Host] = await component.hosts.all() # noqa: F841

# Get specific host, mapped to this component
host_2_from_component = await component.hosts.get(name__eq=host_2.name) # noqa: F841

# Trying to get host_3 from component, get error: host_3 is not mapped to component
with pytest.raises(ObjectDoesNotExistError, match="No objects found with the given filter."):
host_3_from_component = await component.hosts.get(name__eq=host_3.name) # noqa: F841

# Turn on maintenance mode on component
await (await component.maintenance_mode).on()

# Sync component's data with remote state
await component.refresh()

0 comments on commit bd71d1d

Please sign in to comment.