Skip to content

Commit

Permalink
ADCM-6246: Add tests for .imports node public API (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Starovoitov authored Dec 28, 2024
1 parent 059f118 commit 2c97cf5
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 2 deletions.
121 changes: 121 additions & 0 deletions tests/integration/bundles/cluster_export/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
- type: cluster
name: cluster_export
version: &version '1.0'
config:
- name: string
type: string
required: false
default: string
export:
- string

- name: service_export_0
type: service
version: *version
config:
- name: text
type: text
required: false
default: text
export:
- text

- name: service_export_1
type: service
version: *version
config:
- name: text
type: text
required: false
default: text
export:
- text

- name: service_export_2
type: service
version: *version
config:
- name: text
type: text
required: false
default: text
export:
- text

- name: service_export_3
type: service
version: *version
config:
- name: text
type: text
required: false
default: text
export:
- text

- name: service_export_4
type: service
version: *version
config:
- name: text
type: text
required: false
default: text
export:
- text

- name: service_export_5
type: service
version: *version
config:
- name: text
type: text
required: false
default: text
export:
- text

- name: service_export_6
type: service
version: *version
config:
- name: text
type: text
required: false
default: text
export:
- text

- name: service_export_7
type: service
version: *version
config:
- name: text
type: text
required: false
default: text
export:
- text

- name: service_export_8
type: service
version: *version
config:
- name: text
type: text
required: false
default: text
export:
- text

- name: service_export_9
type: service
version: *version
config:
- name: text
type: text
required: false
default: text
export:
- text
101 changes: 101 additions & 0 deletions tests/integration/bundles/cluster_import/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
- type: cluster
name: cluster_import
version: 1.0
import:
cluster_export_0:
versions:
min: 1.0
max: 2.0
cluster_export_1:
versions:
min: 1.0
max: 2.0
cluster_export_2:
versions:
min: 1.0
max: 2.0
cluster_export_3:
versions:
min: 1.0
max: 2.0
cluster_export_4:
versions:
min: 1.0
max: 2.0
cluster_export_5:
versions:
min: 1.0
max: 2.0
cluster_export_6:
versions:
min: 1.0
max: 2.0
cluster_export_7:
versions:
min: 1.0
max: 2.0
cluster_export_8:
versions:
min: 1.0
max: 2.0
cluster_export_9:
versions:
min: 1.0
max: 2.0
cluster_export_10:
versions:
min: 1.0
max: 2.0

- type: service
name: service_import_0
version: 1.0
import:
cluster_export_0:
versions:
min: 1.0
max: 2.0
service_export_0:
versions:
min: 1.0
max: 2.0
service_export_1:
versions:
min: 1.0
max: 2.0
service_export_2:
versions:
min: 1.0
max: 2.0
service_export_3:
versions:
min: 1.0
max: 2.0
service_export_4:
versions:
min: 1.0
max: 2.0
service_export_5:
versions:
min: 1.0
max: 2.0
service_export_6:
versions:
min: 1.0
max: 2.0
service_export_7:
versions:
min: 1.0
max: 2.0
service_export_8:
versions:
min: 1.0
max: 2.0
service_export_9:
versions:
min: 1.0
max: 2.0
config:
- name: string
type: string
default: string
2 changes: 1 addition & 1 deletion tests/integration/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from adcm_aio_client.core.filters import Filter
from adcm_aio_client.core.objects.cm import Cluster, Component, Service
from tests.integration.bundle import pack_bundle
from tests.integration.yaml import create_yaml
from tests.integration.yaml_ext import create_yaml

pytestmark = [pytest.mark.asyncio]

Expand Down
128 changes: 128 additions & 0 deletions tests/integration/test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
from pathlib import Path
from typing import NamedTuple
import asyncio

import pytest
import pytest_asyncio

from adcm_aio_client.core.client import ADCMClient
from adcm_aio_client.core.filters import Filter
from adcm_aio_client.core.objects._imports import Imports
from adcm_aio_client.core.objects.cm import Cluster, Service
from tests.integration.bundle import create_bundles_by_template, pack_bundle
from tests.integration.conftest import BUNDLES

pytestmark = [pytest.mark.asyncio]


class Context(NamedTuple):
client: ADCMClient
cluster_exports: list[Cluster]
service_exports: list[Service]
cluster_import: Cluster
tempdir: Path


@pytest_asyncio.fixture()
async def load_cluster_exports(adcm_client: ADCMClient, tmp_path: Path) -> list[Cluster]:
clusters = []
bundles = await create_bundles_by_template(
adcm_client,
tmp_path,
BUNDLES / "cluster_export",
target_name="name",
field_to_modify="cluster_export",
new_value="cluster_export",
number_of_bundles=10,
)
for i, bundle in enumerate(bundles):
cluster = await adcm_client.clusters.create(
bundle=bundle, name=f"cluster_export_{i}", description=f"Cluster export description {i}"
)
await cluster.services.add(filter_=Filter(attr="name", op="contains", value="export"))
clusters.append(cluster)

return clusters


@pytest_asyncio.fixture()
async def cluster_import(adcm_client: ADCMClient, tmp_path: Path) -> Cluster:
import_bundle_path = pack_bundle(from_dir=BUNDLES / "cluster_import", to=tmp_path)
import_bundle = await adcm_client.bundles.create(source=import_bundle_path)
import_cluster = await adcm_client.clusters.create(
bundle=import_bundle, name="Cluster import", description="Cluster import description"
)
await import_cluster.services.add(filter_=Filter(attr="name", op="contains", value="import"))

return import_cluster


@pytest_asyncio.fixture()
async def context(
adcm_client: ADCMClient, load_cluster_exports: list[Cluster], cluster_import: Cluster, tmp_path: Path
) -> Context:
services_by_cluster = await asyncio.gather(*[cluster.services.all() for cluster in load_cluster_exports])
services_exports = [service for sublist in services_by_cluster for service in sublist]

return Context(
client=adcm_client,
cluster_exports=load_cluster_exports,
cluster_import=cluster_import,
tempdir=tmp_path,
service_exports=services_exports,
)


async def test_cluster_import(context: Context) -> None:
cluster_import = context.cluster_import
cluster_exports = context.cluster_exports
imports = await cluster_import.imports

await _test_imports(imports, cluster_exports)


async def test_service_import(context: Context) -> None:
cluster_import = context.cluster_import
cluster_exports = context.cluster_exports
imports = await (await cluster_import.services.get(name__eq="service_import_0")).imports

export_services = await cluster_exports[0].services.all()

await _test_imports(imports, export_services)


async def _test_imports(imports: Imports, exports: list[Cluster] | list[Service]) -> None:
assert len(await imports._get_source_binds()) == 0

await imports.add([])
assert len(await imports._get_source_binds()) == 0

await imports.add(exports[:5])
assert len(binds := await imports._get_source_binds()) == 5
assert sorted([i[0] for i in binds]) == list(range(1, 6))

await imports.add(exports)
assert len(binds := await imports._get_source_binds()) == 10
assert sorted([i[0] for i in binds]) == list(range(1, 11))

await imports.remove([])
assert len(binds := await imports._get_source_binds()) == 10
assert sorted([i[0] for i in binds]) == list(range(1, 11))

await imports.remove([exports[-1]])
assert len(binds := await imports._get_source_binds()) == 9
assert sorted([i[0] for i in binds]) == list(range(1, 10))

await imports.remove(exports)
assert len(await imports._get_source_binds()) == 0

await imports.set(exports[5::])
assert len(binds := await imports._get_source_binds()) == 5
assert sorted([i[0] for i in binds]) == list(range(6, 11))

await imports.set(exports[:5])
assert len(binds := await imports._get_source_binds()) == 5
assert sorted([i[0] for i in binds]) == list(range(1, 6))

await imports.set([])
assert len(await imports._get_source_binds()) == 0
2 changes: 1 addition & 1 deletion tests/integration/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from adcm_aio_client.core.objects._imports import Imports
from adcm_aio_client.core.objects.cm import Cluster, License, Service
from tests.integration.bundle import pack_bundle
from tests.integration.yaml import create_yaml
from tests.integration.yaml_ext import create_yaml

pytestmark = [pytest.mark.asyncio]

Expand Down
File renamed without changes.

0 comments on commit 2c97cf5

Please sign in to comment.