|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import copy |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +import pytest |
| 7 | +from infrahub_sdk.exceptions import GraphQLError |
| 8 | + |
| 9 | +from infrahub.core.node import Node |
| 10 | +from tests.constants import TestKind |
| 11 | +from tests.helpers.schema import DEVICE_SCHEMA, load_schema |
| 12 | +from tests.helpers.schema.device import LAG_INTERFACE |
| 13 | +from tests.helpers.test_app import TestInfrahubApp |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from infrahub_sdk import InfrahubClient |
| 17 | + |
| 18 | + from infrahub.core.branch import Branch |
| 19 | + from infrahub.core.schema import SchemaRoot |
| 20 | + from infrahub.core.schema.schema_branch import SchemaBranch |
| 21 | + from infrahub.database import InfrahubDatabase |
| 22 | + |
| 23 | + |
| 24 | +class TestPeerRelativesConstraint(TestInfrahubApp): |
| 25 | + @pytest.fixture(scope="class", autouse=True) |
| 26 | + def schema(self, default_branch: Branch, register_internal_schema: SchemaBranch) -> SchemaRoot: |
| 27 | + schema_with_lag = copy.deepcopy(DEVICE_SCHEMA) |
| 28 | + schema_with_lag.nodes[0].generate_template = False |
| 29 | + schema_with_lag.nodes.append(LAG_INTERFACE) |
| 30 | + return schema_with_lag |
| 31 | + |
| 32 | + @pytest.fixture(scope="class") |
| 33 | + async def data( |
| 34 | + self, |
| 35 | + db: InfrahubDatabase, |
| 36 | + initialize_registry: None, |
| 37 | + client: InfrahubClient, |
| 38 | + default_branch: Branch, |
| 39 | + schema: SchemaRoot, |
| 40 | + ) -> dict[str, Node]: |
| 41 | + await load_schema(db, schema=schema, update_db=True) |
| 42 | + |
| 43 | + device_1 = await Node.init(db=db, schema=TestKind.DEVICE) |
| 44 | + await device_1.new(db=db, name="Foo", manufacturer="Foo Inc.", weight=10, airflow="Front to rear") |
| 45 | + |
| 46 | + interfaces_1: list[Node] = [] |
| 47 | + interfaces_1_ids: list[str] = [] |
| 48 | + for if_name in ["et-0/0/0", "et-0/0/1", "et-0/0/2", "et-0/0/3"]: |
| 49 | + interface = await Node.init(db=db, schema=TestKind.PHYSICAL_INTERFACE) |
| 50 | + await interface.new(db=db, name=if_name, phys_type="QSFP28 (100GE)", device=device_1) |
| 51 | + await interface.save(db=db) |
| 52 | + interfaces_1.append(interface) |
| 53 | + interfaces_1_ids.append(interface.id) |
| 54 | + |
| 55 | + await device_1.interfaces.update(db=db, data=interfaces_1) # type: ignore[attr-defined] |
| 56 | + await device_1.save(db=db) |
| 57 | + |
| 58 | + device_2 = await Node.init(db=db, schema=TestKind.DEVICE) |
| 59 | + await device_2.new(db=db, name="Bar", manufacturer="Bar Inc.", weight=10, airflow="Front to rear") |
| 60 | + |
| 61 | + interfaces_2: list[Node] = [] |
| 62 | + interfaces_2_ids: list[str] = [] |
| 63 | + for if_name in ["et-0/0/0", "et-0/0/1", "et-0/0/2", "et-0/0/3"]: |
| 64 | + interface = await Node.init(db=db, schema=TestKind.PHYSICAL_INTERFACE) |
| 65 | + await interface.new(db=db, name=if_name, phys_type="QSFP28 (100GE)", device=device_2) |
| 66 | + await interface.save(db=db) |
| 67 | + interfaces_2.append(interface) |
| 68 | + interfaces_2_ids.append(interface.id) |
| 69 | + |
| 70 | + await device_2.interfaces.update(db=db, data=interfaces_2) # type: ignore[attr-defined] |
| 71 | + await device_2.save(db=db) |
| 72 | + |
| 73 | + return {"device_1": device_1, "device_2": device_2} |
| 74 | + |
| 75 | + async def test_create_lag_main( |
| 76 | + self, db: InfrahubDatabase, data: dict[str, Node], client: InfrahubClient, default_branch: Branch |
| 77 | + ) -> None: |
| 78 | + device = await client.get(kind=TestKind.DEVICE, id=data["device_1"].id, branch=default_branch.name) |
| 79 | + await device.interfaces.fetch() |
| 80 | + |
| 81 | + lag = await client.create( |
| 82 | + kind=TestKind.LAG_INTERFACE, |
| 83 | + name="ae0", |
| 84 | + device=device, |
| 85 | + members=[i.peer for i in device.interfaces], # type: ignore[attr-defined] |
| 86 | + branch=default_branch.name, |
| 87 | + ) |
| 88 | + await lag.save() |
| 89 | + |
| 90 | + assert len(lag.members.peers) == 4 |
| 91 | + assert sorted(lag.members.peer_ids) == sorted([i.id for i in device.interfaces]) |
| 92 | + |
| 93 | + async def test_create_incorrect_lag_main( |
| 94 | + self, db: InfrahubDatabase, data: dict[str, Node], client: InfrahubClient, default_branch: Branch |
| 95 | + ) -> None: |
| 96 | + device_1 = await client.get(kind=TestKind.DEVICE, id=data["device_1"].id, branch=default_branch.name) |
| 97 | + await device_1.interfaces.fetch() |
| 98 | + device_2 = await client.get(kind=TestKind.DEVICE, id=data["device_2"].id, branch=default_branch.name) |
| 99 | + await device_2.interfaces.fetch() |
| 100 | + |
| 101 | + lag = await client.create( |
| 102 | + kind=TestKind.LAG_INTERFACE, |
| 103 | + name="ae1", |
| 104 | + device=device_1, |
| 105 | + members=[i.peer for i in list(device_1.interfaces)[:-1] + list(device_2.interfaces)], # type: ignore[attr-defined] |
| 106 | + branch=default_branch.name, |
| 107 | + ) |
| 108 | + |
| 109 | + with pytest.raises(GraphQLError) as exc: |
| 110 | + await lag.save() |
| 111 | + assert ( |
| 112 | + "must have the same set of peers for their 'TestingPhysicalInterface.device' relationship" |
| 113 | + in exc.value.errors[0]["message"] |
| 114 | + ) |
| 115 | + |
| 116 | + async def test_create_lag_branch( |
| 117 | + self, db: InfrahubDatabase, data: dict[str, Node], client: InfrahubClient, default_branch: Branch |
| 118 | + ) -> None: |
| 119 | + branch = await client.branch.create(branch_name="test-lag") |
| 120 | + |
| 121 | + device = await client.get(kind=TestKind.DEVICE, id=data["device_2"].id, branch=branch.name) |
| 122 | + await device.interfaces.fetch() |
| 123 | + |
| 124 | + lag = await client.create( |
| 125 | + kind=TestKind.LAG_INTERFACE, |
| 126 | + name="ae0", |
| 127 | + device=device, |
| 128 | + members=[i.peer for i in device.interfaces], # type: ignore[attr-defined] |
| 129 | + branch=branch.name, |
| 130 | + ) |
| 131 | + await lag.save() |
| 132 | + |
| 133 | + assert len(lag.members.peers) == 4 |
| 134 | + assert sorted(lag.members.peer_ids) == sorted([i.id for i in device.interfaces]) |
| 135 | + |
| 136 | + async def test_create_incorrect_lag_branch( |
| 137 | + self, db: InfrahubDatabase, data: dict[str, Node], client: InfrahubClient, default_branch: Branch |
| 138 | + ) -> None: |
| 139 | + branch = await client.branch.create(branch_name="test-lag-incorrect") |
| 140 | + |
| 141 | + device_1 = await client.get(kind=TestKind.DEVICE, id=data["device_1"].id, branch=branch.name) |
| 142 | + await device_1.interfaces.fetch() |
| 143 | + device_2 = await client.get(kind=TestKind.DEVICE, id=data["device_2"].id, branch=branch.name) |
| 144 | + await device_2.interfaces.fetch() |
| 145 | + |
| 146 | + lag = await client.create( |
| 147 | + kind=TestKind.LAG_INTERFACE, |
| 148 | + name="ae1", |
| 149 | + device=device_2, |
| 150 | + members=[i.peer for i in list(device_1.interfaces)[:-1] + list(device_2.interfaces)[:-1]], # type: ignore[attr-defined] |
| 151 | + branch=branch.name, |
| 152 | + ) |
| 153 | + |
| 154 | + with pytest.raises(GraphQLError) as exc: |
| 155 | + await lag.save() |
| 156 | + assert ( |
| 157 | + "must have the same set of peers for their 'TestingPhysicalInterface.device' relationship" |
| 158 | + in exc.value.errors[0]["message"] |
| 159 | + ) |
0 commit comments