Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADCM-6280: Prepare an example of interaction with a service #81

Merged
merged 6 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]:
Starovoitov marked this conversation as resolved.
Show resolved Hide resolved
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)
78 changes: 78 additions & 0 deletions tests/integration/examples/test_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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:
a-alferov marked this conversation as resolved.
Show resolved Hide resolved
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

# 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()