|
| 1 | +"""Verify backend upsert behaviour for a NumberPool-sourced HFID attribute. |
| 2 | +
|
| 3 | +This mirrors the demo-service-catalog VLAN allocation: ``IpamVLAN`` has an HFID of |
| 4 | +``[l2domain, vlan_id]`` and ``vlan_id`` is allocated from a ``CoreNumberPool``. The client |
| 5 | +adds a guard (``InfrahubNode._validate_upsert``) that raises a ``ValidationError`` before any |
| 6 | +network call for exactly this shape. These tests deliberately bypass that guard so we can |
| 7 | +observe what the *server* does, and decide whether the client-side guard is blocking an |
| 8 | +operation the backend actually supports. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from typing import TYPE_CHECKING, Any |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from infrahub_sdk.node import InfrahubNode |
| 18 | +from infrahub_sdk.testing.docker import TestInfrahubDockerClient |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + from infrahub_sdk import InfrahubClient |
| 22 | + |
| 23 | +POOL_START = 100 |
| 24 | +POOL_END = 200 |
| 25 | + |
| 26 | +ALLOCATION_SCHEMA: dict[str, Any] = { |
| 27 | + "version": "1.0", |
| 28 | + "nodes": [ |
| 29 | + { |
| 30 | + "name": "Allocation", |
| 31 | + "namespace": "Testing", |
| 32 | + "label": "Allocation", |
| 33 | + "default_filter": "group__value", |
| 34 | + "order_by": ["group__value"], |
| 35 | + "display_labels": ["group__value"], |
| 36 | + # HFID combines a plain discriminator with a NumberPool-sourced attribute, |
| 37 | + # exactly like IpamVLAN's [l2domain, vlan_id]. |
| 38 | + "human_friendly_id": ["group__value", "code__value"], |
| 39 | + "attributes": [ |
| 40 | + {"name": "group", "kind": "Text"}, |
| 41 | + {"name": "code", "kind": "Number", "optional": True}, |
| 42 | + ], |
| 43 | + } |
| 44 | + ], |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +class TestUpsertNumberPoolHfid(TestInfrahubDockerClient): |
| 49 | + @pytest.fixture(scope="class") |
| 50 | + async def load_allocation_schema(self, default_branch: str, client: InfrahubClient) -> None: |
| 51 | + await client.schema.wait_until_converged(branch=default_branch) |
| 52 | + resp = await client.schema.load(schemas=[ALLOCATION_SCHEMA], branch=default_branch, wait_until_converged=True) |
| 53 | + assert resp.errors == {} |
| 54 | + |
| 55 | + @pytest.fixture(scope="class") |
| 56 | + async def code_pool(self, client: InfrahubClient, load_allocation_schema: None) -> InfrahubNode: |
| 57 | + pool = await client.create( |
| 58 | + kind="CoreNumberPool", |
| 59 | + name="Allocation Code Pool", |
| 60 | + node="TestingAllocation", |
| 61 | + node_attribute="code", |
| 62 | + start_range=POOL_START, |
| 63 | + end_range=POOL_END, |
| 64 | + ) |
| 65 | + await pool.save() |
| 66 | + return pool |
| 67 | + |
| 68 | + async def test_backend_upsert_creates_node_with_pool_sourced_hfid_attr( |
| 69 | + self, |
| 70 | + client: InfrahubClient, |
| 71 | + code_pool: InfrahubNode, |
| 72 | + monkeypatch: pytest.MonkeyPatch, |
| 73 | + ) -> None: |
| 74 | + """A no-id upsert of a pool-HFID node should create the node and allocate the pool value. |
| 75 | +
|
| 76 | + With the client guard bypassed this exercises the real path. If it succeeds, the |
| 77 | + client-side ``_validate_upsert`` guard is blocking an operation the backend supports |
| 78 | + (the regression introduced after #312 removed the HFID from the upsert payload). |
| 79 | + """ |
| 80 | + # Disable only the guard under investigation; everything else exercises the real path. |
| 81 | + monkeypatch.setattr(InfrahubNode, "_validate_upsert", lambda *_args, **_kwargs: None) |
| 82 | + |
| 83 | + node = await client.create(kind="TestingAllocation", group="g1", code=code_pool) |
| 84 | + await node.save(allow_upsert=True) |
| 85 | + |
| 86 | + assert node.id is not None, "Backend did not create the node on a no-id upsert" |
| 87 | + code_value = node.code.value |
| 88 | + assert code_value is not None, "Pool value was not allocated server-side" |
| 89 | + assert POOL_START <= code_value <= POOL_END |
| 90 | + |
| 91 | + async def test_backend_upsert_idempotency_with_pool_sourced_hfid_attr( |
| 92 | + self, |
| 93 | + client: InfrahubClient, |
| 94 | + code_pool: InfrahubNode, |
| 95 | + monkeypatch: pytest.MonkeyPatch, |
| 96 | + ) -> None: |
| 97 | + """Characterise re-run behaviour of a no-id upsert with the same discriminator. |
| 98 | +
|
| 99 | + Does a second upsert reuse the node, create a duplicate, or error? This is the open |
| 100 | + question behind #396 (idempotent number-pool allocation). The assertion documents the |
| 101 | + observed behaviour rather than asserting a fix. |
| 102 | + """ |
| 103 | + monkeypatch.setattr(InfrahubNode, "_validate_upsert", lambda *_args, **_kwargs: None) |
| 104 | + |
| 105 | + first = await client.create(kind="TestingAllocation", group="g2", code=code_pool) |
| 106 | + await first.save(allow_upsert=True) |
| 107 | + |
| 108 | + second = await client.create(kind="TestingAllocation", group="g2", code=code_pool) |
| 109 | + await second.save(allow_upsert=True) |
| 110 | + |
| 111 | + nodes = await client.filters(kind="TestingAllocation", group__value="g2") |
| 112 | + # One node => backend resolved the HFID despite the unresolved pool value (idempotent). |
| 113 | + # Two nodes => upsert fell back to create, so re-runs duplicate (the #396 limitation). |
| 114 | + assert len(nodes) in {1, 2} |
0 commit comments