diff --git a/tests/integration/bundles/cluster_export/config.yaml b/tests/integration/bundles/cluster_export/config.yaml new file mode 100644 index 0000000..c3bd9d8 --- /dev/null +++ b/tests/integration/bundles/cluster_export/config.yaml @@ -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 diff --git a/tests/integration/bundles/cluster_import/config.yaml b/tests/integration/bundles/cluster_import/config.yaml new file mode 100644 index 0000000..6227f90 --- /dev/null +++ b/tests/integration/bundles/cluster_import/config.yaml @@ -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 diff --git a/tests/integration/test_component.py b/tests/integration/test_component.py index 94ff701..e605520 100644 --- a/tests/integration/test_component.py +++ b/tests/integration/test_component.py @@ -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] diff --git a/tests/integration/test_import.py b/tests/integration/test_import.py new file mode 100644 index 0000000..af5489b --- /dev/null +++ b/tests/integration/test_import.py @@ -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 diff --git a/tests/integration/test_service.py b/tests/integration/test_service.py index bb971e0..2df7cb5 100644 --- a/tests/integration/test_service.py +++ b/tests/integration/test_service.py @@ -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] diff --git a/tests/integration/yaml.py b/tests/integration/yaml_ext.py similarity index 100% rename from tests/integration/yaml.py rename to tests/integration/yaml_ext.py