diff --git a/backend/infrahub/core/branch/tasks.py b/backend/infrahub/core/branch/tasks.py index 6c21481de2..354390acf2 100644 --- a/backend/infrahub/core/branch/tasks.py +++ b/backend/infrahub/core/branch/tasks.py @@ -22,7 +22,7 @@ from infrahub.core.validators.models.validate_migration import SchemaValidateMigrationData from infrahub.core.validators.tasks import schema_validate_migrations from infrahub.dependencies.registry import get_component_registry -from infrahub.events.branch_action import BranchCreateEvent, BranchDeleteEvent +from infrahub.events.branch_action import BranchCreateEvent, BranchDeleteEvent, BranchRebaseEvent from infrahub.exceptions import BranchNotFoundError, MergeFailedError, ValidationError from infrahub.graphql.mutations.models import BranchCreateModel # noqa: TCH001 from infrahub.log import get_log_data @@ -31,6 +31,7 @@ from infrahub.worker import WORKER_IDENTITY from infrahub.workflows.catalogue import ( BRANCH_CANCEL_PROPOSED_CHANGES, + DIFF_REFRESH_ALL, GIT_REPOSITORIES_CREATE_BRANCH, IPAM_RECONCILIATION, ) @@ -109,19 +110,19 @@ async def rebase_branch(branch: str) -> None: obj.update_schema_hash() await obj.save(db=service.database) - # Execute the migrations - migrations = await merger.calculate_migrations(target_schema=updated_schema) + # Execute the migrations + migrations = await merger.calculate_migrations(target_schema=updated_schema) - errors = await schema_apply_migrations( - message=SchemaApplyMigrationData( - branch=merger.source_branch, - new_schema=candidate_schema, - previous_schema=schema_in_main_before, - migrations=migrations, + errors = await schema_apply_migrations( + message=SchemaApplyMigrationData( + branch=merger.source_branch, + new_schema=candidate_schema, + previous_schema=schema_in_main_before, + migrations=migrations, + ) ) - ) - for error in errors: - log.error(error) + for error in errors: + log.error(error) # ------------------------------------------------------------- # Trigger the reconciliation of IPAM data after the rebase @@ -136,18 +137,12 @@ async def rebase_branch(branch: str) -> None: workflow=IPAM_RECONCILIATION, parameters={"branch": obj.name, "ipam_node_details": ipam_node_details} ) + await service.workflow.submit_workflow(workflow=DIFF_REFRESH_ALL, parameters={"branch_name": obj.name}) + # ------------------------------------------------------------- # Generate an event to indicate that a branch has been rebased - # NOTE: we still need to convert this event and potentially pull - # some tasks currently executed based on the event into this workflow # ------------------------------------------------------------- - log_data = get_log_data() - request_id = log_data.get("request_id", "") - message = messages.EventBranchRebased( - branch=obj.name, - meta=Meta(initiator_id=WORKER_IDENTITY, request_id=request_id), - ) - await service.send(message=message) + await service.event.send(event=BranchRebaseEvent(branch=obj.name, branch_id=obj.get_id())) @flow(name="branch-merge", flow_run_name="Merge branch {branch} into main") diff --git a/backend/infrahub/core/diff/models.py b/backend/infrahub/core/diff/models.py index fa898455b1..1db1bcbb5c 100644 --- a/backend/infrahub/core/diff/models.py +++ b/backend/infrahub/core/diff/models.py @@ -13,10 +13,3 @@ class RequestDiffUpdate(BaseModel): name: str | None = None from_time: str | None = None to_time: str | None = None - - -class RequestDiffRefresh(BaseModel): - """Request diff be recalculated from scratch.""" - - branch_name: str = Field(..., description="The branch associated with the diff") - diff_id: str = Field(..., description="The id for this diff") diff --git a/backend/infrahub/core/diff/payload_builder.py b/backend/infrahub/core/diff/payload_builder.py index 346f4804d8..4b918a264a 100644 --- a/backend/infrahub/core/diff/payload_builder.py +++ b/backend/infrahub/core/diff/payload_builder.py @@ -4,6 +4,7 @@ from infrahub.core.manager import NodeManager from infrahub.core.registry import registry +from infrahub.exceptions import SchemaNotFoundError from infrahub.log import get_logger if TYPE_CHECKING: @@ -14,12 +15,17 @@ async def get_display_labels_per_kind( - kind: str, ids: list[str], branch_name: str, db: InfrahubDatabase + kind: str, ids: list[str], branch_name: str, db: InfrahubDatabase, skip_missing_schema: bool = False ) -> dict[str, str]: """Return the display_labels of a list of nodes of a specific kind.""" branch = await registry.get_branch(branch=branch_name, db=db) schema_branch = db.schema.get_schema_branch(name=branch.name) - fields = schema_branch.generate_fields_for_display_label(name=kind) + try: + fields = schema_branch.generate_fields_for_display_label(name=kind) + except SchemaNotFoundError: + if skip_missing_schema: + return {} + raise nodes = await NodeManager.get_many(ids=ids, fields=fields, db=db, branch=branch) return {node_id: await node.render_display_label(db=db) for node_id, node in nodes.items()} @@ -31,7 +37,9 @@ async def get_display_labels(nodes: dict[str, dict[str, list[str]]], db: Infrahu if branch_name not in response: response[branch_name] = {} for kind, ids in items.items(): - labels = await get_display_labels_per_kind(kind=kind, ids=ids, db=db, branch_name=branch_name) + labels = await get_display_labels_per_kind( + kind=kind, ids=ids, db=db, branch_name=branch_name, skip_missing_schema=True + ) response[branch_name].update(labels) return response diff --git a/backend/infrahub/core/diff/tasks.py b/backend/infrahub/core/diff/tasks.py index cba7d2fea4..000bb33317 100644 --- a/backend/infrahub/core/diff/tasks.py +++ b/backend/infrahub/core/diff/tasks.py @@ -2,10 +2,12 @@ from infrahub.core import registry from infrahub.core.diff.coordinator import DiffCoordinator -from infrahub.core.diff.models import RequestDiffRefresh, RequestDiffUpdate +from infrahub.core.diff.models import RequestDiffUpdate +from infrahub.core.diff.repository.repository import DiffRepository from infrahub.dependencies.registry import get_component_registry from infrahub.log import get_logger from infrahub.services import services +from infrahub.workflows.catalogue import DIFF_REFRESH from infrahub.workflows.utils import add_branch_tag log = get_logger() @@ -31,14 +33,31 @@ async def update_diff(model: RequestDiffUpdate) -> None: ) -@flow(name="diff-refresh", flow_run_name="Recreate diff for branch {model.branch_name}") -async def refresh_diff(model: RequestDiffRefresh) -> None: +@flow(name="diff-refresh", flow_run_name="Recreate diff for branch {branch_name}") +async def refresh_diff(branch_name: str, diff_id: str) -> None: service = services.service - await add_branch_tag(branch_name=model.branch_name) + await add_branch_tag(branch_name=branch_name) component_registry = get_component_registry() base_branch = await registry.get_branch(db=service.database, branch=registry.default_branch) - diff_branch = await registry.get_branch(db=service.database, branch=model.branch_name) + diff_branch = await registry.get_branch(db=service.database, branch=branch_name) diff_coordinator = await component_registry.get_component(DiffCoordinator, db=service.database, branch=diff_branch) - await diff_coordinator.recalculate(base_branch=base_branch, diff_branch=diff_branch, diff_id=model.diff_id) + await diff_coordinator.recalculate(base_branch=base_branch, diff_branch=diff_branch, diff_id=diff_id) + + +@flow(name="diff-refresh-all", flow_run_name="Recreate all diffs for branch {branch_name}") +async def refresh_diff_all(branch_name: str) -> None: + service = services.service + await add_branch_tag(branch_name=branch_name) + + component_registry = get_component_registry() + default_branch = registry.get_branch_from_registry() + diff_repository = await component_registry.get_component(DiffRepository, db=service.database, branch=default_branch) + diff_roots_to_refresh = await diff_repository.get_empty_roots(diff_branch_names=[branch_name]) + + for diff_root in diff_roots_to_refresh: + if diff_root.base_branch_name != diff_root.diff_branch_name: + await service.workflow.submit_workflow( + workflow=DIFF_REFRESH, parameters={"branch_name": diff_root.diff_branch_name, "diff_id": diff_root.uuid} + ) diff --git a/backend/infrahub/events/branch_action.py b/backend/infrahub/events/branch_action.py index 3bd7b6a4fd..0e612ca402 100644 --- a/backend/infrahub/events/branch_action.py +++ b/backend/infrahub/events/branch_action.py @@ -3,6 +3,7 @@ from infrahub.message_bus import InfrahubMessage from infrahub.message_bus.messages.event_branch_delete import EventBranchDelete from infrahub.message_bus.messages.refresh_registry_branches import RefreshRegistryBranches +from infrahub.message_bus.messages.refresh_registry_rebasedbranch import RefreshRegistryRebasedBranch from .models import InfrahubBranchEvent @@ -63,3 +64,28 @@ def get_messages(self) -> list[InfrahubMessage]: RefreshRegistryBranches(), ] return events # type: ignore + + +class BranchRebaseEvent(InfrahubBranchEvent): + """Event generated when a branch has been rebased""" + + branch_id: str = Field(..., description="The ID of the mutated node") + + def get_name(self) -> str: + return f"{self.get_event_namespace()}.branch.rebased" + + def get_resource(self) -> dict[str, str]: + return { + "prefect.resource.id": f"infrahub.branch.{self.branch}", + "infrahub.branch.id": self.branch_id, + } + + def get_messages(self) -> list[InfrahubMessage]: + events = [ + # EventBranchRebased( + # branch=self.branch, + # meta=self.get_message_meta(), + # ), + RefreshRegistryRebasedBranch(branch=self.branch), + ] + return events # type: ignore diff --git a/backend/infrahub/graphql/mutations/diff.py b/backend/infrahub/graphql/mutations/diff.py index 631bc3d3ca..1e862b81a8 100644 --- a/backend/infrahub/graphql/mutations/diff.py +++ b/backend/infrahub/graphql/mutations/diff.py @@ -7,7 +7,7 @@ from infrahub.core.diff.coordinator import DiffCoordinator from infrahub.core.diff.models import RequestDiffUpdate from infrahub.dependencies.registry import get_component_registry -from infrahub.workflows.catalogue import REQUEST_DIFF_UPDATE +from infrahub.workflows.catalogue import DIFF_UPDATE if TYPE_CHECKING: from ..initialization import GraphqlContext @@ -63,6 +63,6 @@ async def mutate( to_time=to_timestamp_str, ) if context.service: - await context.service.workflow.submit_workflow(workflow=REQUEST_DIFF_UPDATE, parameters={"model": model}) + await context.service.workflow.submit_workflow(workflow=DIFF_UPDATE, parameters={"model": model}) return {"ok": True} diff --git a/backend/infrahub/message_bus/operations/__init__.py b/backend/infrahub/message_bus/operations/__init__.py index 3869de60c8..489cc48654 100644 --- a/backend/infrahub/message_bus/operations/__init__.py +++ b/backend/infrahub/message_bus/operations/__init__.py @@ -24,7 +24,6 @@ "check.repository.merge_conflicts": check.repository.merge_conflicts, "check.repository.user_check": check.repository.user_check, "event.branch.merge": event.branch.merge, - "event.branch.rebased": event.branch.rebased, "event.node.mutated": event.node.mutated, "event.schema.update": event.schema.update, "event.worker.new_primary_api": event.worker.new_primary_api, diff --git a/backend/infrahub/message_bus/operations/event/branch.py b/backend/infrahub/message_bus/operations/event/branch.py index d31122c792..65d1972638 100644 --- a/backend/infrahub/message_bus/operations/event/branch.py +++ b/backend/infrahub/message_bus/operations/event/branch.py @@ -4,15 +4,14 @@ from infrahub.core import registry from infrahub.core.diff.model.path import BranchTrackingId -from infrahub.core.diff.models import RequestDiffRefresh, RequestDiffUpdate +from infrahub.core.diff.models import RequestDiffUpdate from infrahub.core.diff.repository.repository import DiffRepository from infrahub.dependencies.registry import get_component_registry from infrahub.log import get_logger from infrahub.message_bus import InfrahubMessage, messages from infrahub.services import InfrahubServices from infrahub.workflows.catalogue import ( - REQUEST_DIFF_REFRESH, - REQUEST_DIFF_UPDATE, + DIFF_UPDATE, TRIGGER_ARTIFACT_DEFINITION_GENERATE, TRIGGER_GENERATOR_DEFINITION_RUN, ) @@ -51,35 +50,7 @@ async def merge(message: messages.EventBranchMerge, service: InfrahubServices) - ): request_diff_update_model = RequestDiffUpdate(branch_name=diff_root.diff_branch_name) await service.workflow.submit_workflow( - workflow=REQUEST_DIFF_UPDATE, parameters={"model": request_diff_update_model} - ) - - for event in events: - event.assign_meta(parent=message) - await service.send(message=event) - - -@flow(name="event-branch-rebased") -async def rebased(message: messages.EventBranchRebased, service: InfrahubServices) -> None: - log.info("Branch rebased", branch=message.branch) - - events: List[InfrahubMessage] = [ - messages.RefreshRegistryRebasedBranch(branch=message.branch), - ] - - # for every diff that touches the rebased branch, recalculate it - component_registry = get_component_registry() - default_branch = registry.get_branch_from_registry() - diff_repository = await component_registry.get_component(DiffRepository, db=service.database, branch=default_branch) - diff_roots_to_refresh = await diff_repository.get_empty_roots(diff_branch_names=[message.branch]) - - for diff_root in diff_roots_to_refresh: - if diff_root.base_branch_name != diff_root.diff_branch_name: - request_diff_refresh_model = RequestDiffRefresh( - branch_name=diff_root.diff_branch_name, diff_id=diff_root.uuid - ) - await service.workflow.submit_workflow( - workflow=REQUEST_DIFF_REFRESH, parameters={"model": request_diff_refresh_model} + workflow=DIFF_UPDATE, parameters={"model": request_diff_update_model} ) for event in events: diff --git a/backend/infrahub/workflows/catalogue.py b/backend/infrahub/workflows/catalogue.py index bddb519833..79e9d8c334 100644 --- a/backend/infrahub/workflows/catalogue.py +++ b/backend/infrahub/workflows/catalogue.py @@ -102,20 +102,27 @@ tags=[WorkflowTag.DATABASE_CHANGE], ) -REQUEST_DIFF_UPDATE = WorkflowDefinition( +DIFF_UPDATE = WorkflowDefinition( name="diff-update", type=WorkflowType.CORE, module="infrahub.core.diff.tasks", function="update_diff", ) -REQUEST_DIFF_REFRESH = WorkflowDefinition( +DIFF_REFRESH = WorkflowDefinition( name="diff-refresh", type=WorkflowType.CORE, module="infrahub.core.diff.tasks", function="refresh_diff", ) +DIFF_REFRESH_ALL = WorkflowDefinition( + name="diff-refresh-all", + type=WorkflowType.INTERNAL, + module="infrahub.core.diff.tasks", + function="refresh_diff_all", +) + GIT_REPOSITORIES_SYNC = WorkflowDefinition( name="git_repositories_sync", type=WorkflowType.INTERNAL, @@ -385,6 +392,9 @@ BRANCH_VALIDATE, COMPUTED_ATTRIBUTE_SETUP, COMPUTED_ATTRIBUTE_SETUP_PYTHON, + DIFF_REFRESH, + DIFF_REFRESH_ALL, + DIFF_UPDATE, GIT_REPOSITORIES_CREATE_BRANCH, GIT_REPOSITORIES_DIFF_NAMES_ONLY, GIT_REPOSITORIES_IMPORT_OBJECTS, @@ -399,8 +409,6 @@ QUERY_COMPUTED_ATTRIBUTE_TRANSFORM_TARGETS, REQUEST_ARTIFACT_DEFINITION_GENERATE, REQUEST_ARTIFACT_GENERATE, - REQUEST_DIFF_REFRESH, - REQUEST_DIFF_UPDATE, REQUEST_GENERATOR_DEFINITION_RUN, REQUEST_GENERATOR_RUN, REQUEST_PROPOSED_CHANGE_DATA_INTEGRITY, diff --git a/backend/tests/helpers/schema/__init__.py b/backend/tests/helpers/schema/__init__.py index 4b5610843b..8dbbc3a085 100644 --- a/backend/tests/helpers/schema/__init__.py +++ b/backend/tests/helpers/schema/__init__.py @@ -27,15 +27,13 @@ async def load_schema( db: InfrahubDatabase, schema: SchemaRoot, branch_name: str | None = None, update_db: bool = False ) -> None: - default_branch_name = registry.default_branch - branch_schema = registry.schema.get_schema_branch(name=branch_name or default_branch_name) - tmp_schema = branch_schema.duplicate() - tmp_schema.load_schema(schema=schema) - tmp_schema.process() - + branch_name = branch_name or registry.default_branch + branch_schema = registry.schema.get_schema_branch(name=branch_name) + registry.schema.register_schema(schema=schema, branch=branch_name) await registry.schema.update_schema_branch( - schema=tmp_schema, db=db, branch=branch_name or default_branch_name, update_db=update_db + schema=branch_schema.duplicate(), db=db, branch=branch_name, update_db=update_db ) + registry.get_branch_from_registry(branch_name).update_schema_hash() __all__ = [ diff --git a/backend/tests/integration/ipam/base.py b/backend/tests/integration/ipam/base.py index 5e82ff7d8f..c655faa763 100644 --- a/backend/tests/integration/ipam/base.py +++ b/backend/tests/integration/ipam/base.py @@ -7,13 +7,34 @@ from infrahub.core import registry from infrahub.core.constants import InfrahubKind from infrahub.core.node import Node +from infrahub.core.schema import SchemaRoot +from infrahub.core.schema.schema_branch import SchemaBranch from tests.helpers.test_app import TestInfrahubApp if TYPE_CHECKING: + from infrahub_sdk import InfrahubClient + + from infrahub.core.schema import SchemaRoot + from infrahub.core.schema.schema_branch import SchemaBranch from infrahub.database import InfrahubDatabase -class TestIpamReconcileBase(TestInfrahubApp): +class TestIpam(TestInfrahubApp): + @pytest.fixture(scope="class") + async def register_ipam_schema( + self, initialize_registry, ipam_schema: SchemaRoot, client: InfrahubClient + ) -> SchemaBranch: + # During registry initialization, default_branch might have already been loaded in db, and thus a new python + # object corresponding to default branch would be created. Then, we can't rely on default_branch fixture here, + # as updating default_branch schema hash would not update the object within registry. + # This is why this fixture depends on initialize_registry and client (which calls initialize_registry). + default_branch_name = registry.default_branch + schema_branch = registry.schema.register_schema(schema=ipam_schema, branch=default_branch_name) + registry.get_branch_from_registry(default_branch_name).update_schema_hash() + return schema_branch + + +class TestIpamReconcileBase(TestIpam): @pytest.fixture(scope="class") async def initial_dataset( self, diff --git a/backend/tests/integration/ipam/conftest.py b/backend/tests/integration/ipam/conftest.py index d1d834d166..d8ec3ee4fd 100644 --- a/backend/tests/integration/ipam/conftest.py +++ b/backend/tests/integration/ipam/conftest.py @@ -2,11 +2,8 @@ import pytest -from infrahub.core import registry -from infrahub.core.branch import Branch from infrahub.core.constants import BranchSupportType, InfrahubKind from infrahub.core.schema import SchemaRoot -from infrahub.core.schema.schema_branch import SchemaBranch @pytest.fixture(scope="class") @@ -35,10 +32,3 @@ async def ipam_schema() -> SchemaRoot: } return SchemaRoot(**SCHEMA) - - -@pytest.fixture(scope="class") -async def register_ipam_schema(default_branch: Branch, ipam_schema: SchemaRoot) -> SchemaBranch: - schema_branch = registry.schema.register_schema(schema=ipam_schema, branch=default_branch.name) - default_branch.update_schema_hash() - return schema_branch diff --git a/backend/tests/integration/ipam/test_ipam_rebase_reconcile.py b/backend/tests/integration/ipam/test_ipam_rebase_reconcile.py index a8f37f256a..2c0f08269a 100644 --- a/backend/tests/integration/ipam/test_ipam_rebase_reconcile.py +++ b/backend/tests/integration/ipam/test_ipam_rebase_reconcile.py @@ -72,7 +72,6 @@ async def test_step01_add_address( new_address = await Node.init(schema=address_schema, db=db, branch=branch) await new_address.new(db=db, address="10.10.0.2", ip_namespace=initial_dataset["ns1"].id) await new_address.save(db=db) - success = await client.branch.rebase(branch_name=branch.name) assert success is True diff --git a/backend/tests/integration/ipam/test_ipam_utilization.py b/backend/tests/integration/ipam/test_ipam_utilization.py index 8baf35bd69..7f404e9016 100644 --- a/backend/tests/integration/ipam/test_ipam_utilization.py +++ b/backend/tests/integration/ipam/test_ipam_utilization.py @@ -11,7 +11,7 @@ from infrahub.core.node import Node from infrahub.graphql.initialization import prepare_graphql_params from tests.helpers.graphql import graphql -from tests.helpers.test_app import TestInfrahubApp +from tests.integration.ipam.base import TestIpam if TYPE_CHECKING: from infrahub.core.branch import Branch @@ -56,7 +56,7 @@ }""" -class TestIpamUtilization(TestInfrahubApp): +class TestIpamUtilization(TestIpam): @pytest.fixture(scope="class") async def initial_dataset( self, diff --git a/backend/tests/integration/schema_lifecycle/test_schema_migration_branch.py b/backend/tests/integration/schema_lifecycle/test_schema_migration_branch.py index c9cf816c41..961279ca49 100644 --- a/backend/tests/integration/schema_lifecycle/test_schema_migration_branch.py +++ b/backend/tests/integration/schema_lifecycle/test_schema_migration_branch.py @@ -50,7 +50,7 @@ def branch1(self) -> Branch: return state.branch @pytest.fixture(scope="class") - async def initial_dataset(self, db: InfrahubDatabase, initialize_registry, schema_step01): + async def initial_dataset(self, db: InfrahubDatabase, initialize_registry, schema_step01, client: InfrahubClient): await load_schema(db=db, schema=schema_step01) # Load data in the MAIN branch first diff --git a/backend/tests/integration/shared.py b/backend/tests/integration/shared.py index 9d64d10c76..0ccecadd1b 100644 --- a/backend/tests/integration/shared.py +++ b/backend/tests/integration/shared.py @@ -1,15 +1,9 @@ from typing import Any, Dict -from infrahub.core import registry from infrahub.core.schema import SchemaRoot from infrahub.database import InfrahubDatabase +from tests.helpers.schema import load_schema as load_schema_root async def load_schema(db: InfrahubDatabase, schema: Dict[str, Any]): - default_branch_name = registry.default_branch - branch_schema = registry.schema.get_schema_branch(name=default_branch_name) - tmp_schema = branch_schema.duplicate() - tmp_schema.load_schema(schema=SchemaRoot(**schema)) - tmp_schema.process() - - await registry.schema.update_schema_branch(schema=tmp_schema, db=db, branch=default_branch_name, update_db=True) + await load_schema_root(db=db, schema=SchemaRoot(**schema), update_db=True) diff --git a/backend/tests/unit/message_bus/operations/event/test_branch.py b/backend/tests/unit/message_bus/operations/event/test_branch.py index 451ea7ded2..5662e09a51 100644 --- a/backend/tests/unit/message_bus/operations/event/test_branch.py +++ b/backend/tests/unit/message_bus/operations/event/test_branch.py @@ -5,17 +5,16 @@ from infrahub.core.branch import Branch from infrahub.core.diff.model.path import BranchTrackingId, EnrichedDiffRoot -from infrahub.core.diff.models import RequestDiffRefresh, RequestDiffUpdate +from infrahub.core.diff.models import RequestDiffUpdate from infrahub.core.diff.repository.repository import DiffRepository from infrahub.core.timestamp import Timestamp from infrahub.dependencies.component.registry import ComponentDependencyRegistry from infrahub.message_bus import messages -from infrahub.message_bus.operations.event.branch import merge, rebased +from infrahub.message_bus.operations.event.branch import merge from infrahub.services import InfrahubServices, services from infrahub.services.adapters.workflow.local import WorkflowLocalExecution from infrahub.workflows.catalogue import ( - REQUEST_DIFF_REFRESH, - REQUEST_DIFF_UPDATE, + DIFF_UPDATE, TRIGGER_ARTIFACT_DEFINITION_GENERATE, TRIGGER_GENERATOR_DEFINITION_RUN, ) @@ -92,11 +91,11 @@ async def test_merged(default_branch: Branch, init_service: InfrahubServices, pr parameters={"branch": target_branch_name}, ), call( - workflow=REQUEST_DIFF_UPDATE, + workflow=DIFF_UPDATE, parameters={"model": RequestDiffUpdate(branch_name=tracked_diff_roots[0].diff_branch_name)}, ), call( - workflow=REQUEST_DIFF_UPDATE, + workflow=DIFF_UPDATE, parameters={"model": RequestDiffUpdate(branch_name=tracked_diff_roots[1].diff_branch_name)}, ), ] @@ -110,58 +109,3 @@ async def test_merged(default_branch: Branch, init_service: InfrahubServices, pr assert len(service.message_bus.messages) == 1 assert service.message_bus.messages[0] == messages.RefreshRegistryBranches() - - -async def test_rebased(default_branch: Branch, prefect_test_fixture): - """Validate that a rebased branch triggers a registry refresh and cancels open proposed changes""" - branch_name = "cr1234" - right_now = Timestamp() - message = messages.EventBranchRebased(branch=branch_name) - - recorder = BusRecorder() - database = MagicMock() - service = InfrahubServices(message_bus=recorder, database=database, workflow=WorkflowLocalExecution()) - diff_roots = [ - EnrichedDiffRoot( - base_branch_name="main", - diff_branch_name=branch_name, - from_time=right_now, - to_time=right_now, - uuid=str(uuid4()), - partner_uuid=str(uuid4()), - ) - for _ in range(2) - ] - diff_repo = AsyncMock(spec=DiffRepository) - diff_repo.get_empty_roots.return_value = diff_roots - mock_component_registry = Mock(spec=ComponentDependencyRegistry) - mock_get_component_registry = MagicMock(return_value=mock_component_registry) - mock_component_registry.get_component.return_value = diff_repo - - with ( - patch("infrahub.message_bus.operations.event.branch.get_component_registry", new=mock_get_component_registry), - patch( - "infrahub.services.adapters.workflow.local.WorkflowLocalExecution.submit_workflow" - ) as mock_submit_workflow, - ): - await rebased(message=message, service=service) - - expected_calls = [ - call( - workflow=REQUEST_DIFF_REFRESH, - parameters={"model": RequestDiffRefresh(branch_name=branch_name, diff_id=diff_roots[0].uuid)}, - ), - call( - workflow=REQUEST_DIFF_REFRESH, - parameters={"model": RequestDiffRefresh(branch_name=branch_name, diff_id=diff_roots[1].uuid)}, - ), - ] - mock_submit_workflow.assert_has_calls(expected_calls) - assert mock_submit_workflow.call_count == len(expected_calls) - - mock_component_registry.get_component.assert_awaited_once_with(DiffRepository, db=database, branch=default_branch) - diff_repo.get_empty_roots.assert_awaited_once_with(diff_branch_names=[branch_name]) - assert len(recorder.messages) == 1 - assert isinstance(recorder.messages[0], messages.RefreshRegistryRebasedBranch) - refresh_message: messages.RefreshRegistryRebasedBranch = recorder.messages[0] - assert refresh_message.branch == "cr1234" diff --git a/backend/tests/unit/message_bus/operations/refresh/__init__.py b/backend/tests/unit/message_bus/operations/refresh/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/backend/tests/unit/message_bus/operations/refresh/test_registry.py b/backend/tests/unit/message_bus/operations/refresh/test_registry.py deleted file mode 100644 index e9e7168c83..0000000000 --- a/backend/tests/unit/message_bus/operations/refresh/test_registry.py +++ /dev/null @@ -1,24 +0,0 @@ -from uuid import uuid4 - -from infrahub.core.branch import Branch -from infrahub.core.registry import registry -from infrahub.database import InfrahubDatabase -from infrahub.message_bus import Meta, messages -from infrahub.message_bus.operations.refresh.registry import rebased_branch -from infrahub.services import InfrahubServices -from tests.adapters.message_bus import BusSimulator - - -async def test_rebased_branch(db: InfrahubDatabase): - """Validate that a deleted branch triggers a registry refresh and cancels open proposed changes""" - - branch_name = "test_rebased_branch" - branch = Branch(name=branch_name) - await branch.save(db=db) - message = messages.RefreshRegistryRebasedBranch(branch=branch_name, meta=Meta(initiator_id=str(uuid4()))) - - recorder = BusSimulator() - service = InfrahubServices(database=db, message_bus=recorder) - assert branch_name not in registry.branch - await rebased_branch(message=message, service=service) - assert branch_name in registry.branch