|
| 1 | +"""Tests graph operations using v2 objects""" |
| 2 | + |
| 3 | +from collections.abc import AsyncGenerator |
| 4 | +from uuid import uuid4 |
| 5 | + |
| 6 | +import networkx as nx |
| 7 | +import pytest |
| 8 | +import pytest_asyncio |
| 9 | +from httpx import AsyncClient |
| 10 | + |
| 11 | +from lsst.cmservice.common.enums import StatusEnum |
| 12 | +from lsst.cmservice.common.graph import graph_from_edge_list_v2, processable_graph_nodes, validate_graph |
| 13 | +from lsst.cmservice.common.types import AnyAsyncSession |
| 14 | +from lsst.cmservice.db.campaigns_v2 import Edge, Node |
| 15 | + |
| 16 | +pytestmark = pytest.mark.asyncio(loop_scope="module") |
| 17 | +"""All tests in this module will run in the same event loop.""" |
| 18 | + |
| 19 | + |
| 20 | +@pytest_asyncio.fixture(scope="module", loop_scope="module") |
| 21 | +async def test_campaign(aclient: AsyncClient) -> AsyncGenerator[str]: |
| 22 | + """Fixture managing a test campaign with two (additional) nodes.""" |
| 23 | + campaign_name = uuid4().hex[-8:] |
| 24 | + node_ids = [] |
| 25 | + |
| 26 | + x = await aclient.post( |
| 27 | + "/cm-service/v2/campaigns", |
| 28 | + json={ |
| 29 | + "apiVersion": "io.lsst.cmservice/v1", |
| 30 | + "kind": "campaign", |
| 31 | + "metadata": {"name": campaign_name}, |
| 32 | + "spec": {}, |
| 33 | + }, |
| 34 | + ) |
| 35 | + campaign_edge_url = x.headers["Edges"] |
| 36 | + campaign = x.json() |
| 37 | + |
| 38 | + # create a trio of nodes for the campaign |
| 39 | + for _ in range(3): |
| 40 | + x = await aclient.post( |
| 41 | + "/cm-service/v2/nodes", |
| 42 | + json={ |
| 43 | + "apiVersion": "io.lsst.cmservice/v1", |
| 44 | + "kind": "node", |
| 45 | + "metadata": {"name": uuid4().hex[-8:], "namespace": campaign["id"]}, |
| 46 | + "spec": {}, |
| 47 | + }, |
| 48 | + ) |
| 49 | + node = x.json() |
| 50 | + node_ids.append(node["name"]) |
| 51 | + |
| 52 | + # Create edges between each campaign node with parallelization |
| 53 | + _ = await aclient.post( |
| 54 | + "/cm-service/v2/edges", |
| 55 | + json={ |
| 56 | + "apiVersion": "io.lsst.cmservice/v1", |
| 57 | + "kind": "edge", |
| 58 | + "metadata": {"name": uuid4().hex[-8:], "namespace": campaign["id"]}, |
| 59 | + "spec": { |
| 60 | + "source": "START", |
| 61 | + "target": node_ids[0], |
| 62 | + }, |
| 63 | + }, |
| 64 | + ) |
| 65 | + _ = await aclient.post( |
| 66 | + "/cm-service/v2/edges", |
| 67 | + json={ |
| 68 | + "apiVersion": "io.lsst.cmservice/v1", |
| 69 | + "kind": "edge", |
| 70 | + "metadata": {"name": uuid4().hex[-8:], "namespace": campaign["id"]}, |
| 71 | + "spec": { |
| 72 | + "source": node_ids[0], |
| 73 | + "target": node_ids[1], |
| 74 | + }, |
| 75 | + }, |
| 76 | + ) |
| 77 | + _ = await aclient.post( |
| 78 | + "/cm-service/v2/edges", |
| 79 | + json={ |
| 80 | + "apiVersion": "io.lsst.cmservice/v1", |
| 81 | + "kind": "edge", |
| 82 | + "metadata": {"name": uuid4().hex[-8:], "namespace": campaign["id"]}, |
| 83 | + "spec": { |
| 84 | + "source": node_ids[0], |
| 85 | + "target": node_ids[2], |
| 86 | + }, |
| 87 | + }, |
| 88 | + ) |
| 89 | + _ = await aclient.post( |
| 90 | + "/cm-service/v2/edges", |
| 91 | + json={ |
| 92 | + "apiVersion": "io.lsst.cmservice/v1", |
| 93 | + "kind": "edge", |
| 94 | + "metadata": {"name": uuid4().hex[-8:], "namespace": campaign["id"]}, |
| 95 | + "spec": { |
| 96 | + "source": node_ids[1], |
| 97 | + "target": "END", |
| 98 | + }, |
| 99 | + }, |
| 100 | + ) |
| 101 | + _ = await aclient.post( |
| 102 | + "/cm-service/v2/edges", |
| 103 | + json={ |
| 104 | + "apiVersion": "io.lsst.cmservice/v1", |
| 105 | + "kind": "edge", |
| 106 | + "metadata": {"name": uuid4().hex[-8:], "namespace": campaign["id"]}, |
| 107 | + "spec": { |
| 108 | + "source": node_ids[2], |
| 109 | + "target": "END", |
| 110 | + }, |
| 111 | + }, |
| 112 | + ) |
| 113 | + yield campaign_edge_url |
| 114 | + |
| 115 | + |
| 116 | +async def test_build_and_walk_graph( |
| 117 | + aclient: AsyncClient, session: AnyAsyncSession, test_campaign: str |
| 118 | +) -> None: |
| 119 | + """Test the generation and traversal of a campaign graph as created in the |
| 120 | + ``test_campaign`` fixture. |
| 121 | +
|
| 122 | + Test that the graph is traversed from START to END in order, and that as |
| 123 | + graph nodes are "processable" they can be handled. In this test, the status |
| 124 | + of each node is set to "accepted" and updated in the databse. The campaign |
| 125 | + graph is recreated between each stage of the mock graph processing. |
| 126 | +
|
| 127 | + The test campaign is a set of 3 nodes arranged in a graph: |
| 128 | +
|
| 129 | + ``` |
| 130 | + START --> A --> B --> END |
| 131 | + --> C --> |
| 132 | + ``` |
| 133 | + """ |
| 134 | + edge_list = [Edge.model_validate(edge) for edge in (await aclient.get(test_campaign)).json()] |
| 135 | + graph = await graph_from_edge_list_v2(edge_list, Node, session) |
| 136 | + |
| 137 | + # the START node should be the only processable Node |
| 138 | + for node in processable_graph_nodes(graph): |
| 139 | + assert node.name == "START" |
| 140 | + assert node.status is StatusEnum.waiting |
| 141 | + # Add the Node back to the session and update its status |
| 142 | + session.add(node) |
| 143 | + await session.refresh(node) |
| 144 | + node.status = StatusEnum.accepted |
| 145 | + await session.commit() |
| 146 | + |
| 147 | + # Repeat the graph building and traversal, this time expecting a single |
| 148 | + # node that is not "START" |
| 149 | + graph = await graph_from_edge_list_v2(edge_list, Node, session) |
| 150 | + for node in processable_graph_nodes(graph): |
| 151 | + assert node.name != "START" |
| 152 | + assert node.status is StatusEnum.waiting |
| 153 | + # Add the Node back to the session and update its status |
| 154 | + session.add(node) |
| 155 | + await session.refresh(node) |
| 156 | + node.status = StatusEnum.accepted |
| 157 | + await session.commit() |
| 158 | + |
| 159 | + # Repeat the graph building and traversal, this time expecting a pair of |
| 160 | + # nodes processable in parallel |
| 161 | + graph = await graph_from_edge_list_v2(edge_list, Node, session) |
| 162 | + count = 0 |
| 163 | + for node in processable_graph_nodes(graph): |
| 164 | + count += 1 |
| 165 | + assert node.name != "START" |
| 166 | + assert node.status is StatusEnum.waiting |
| 167 | + # Add the Node back to the session and update its status |
| 168 | + session.add(node) |
| 169 | + await session.refresh(node) |
| 170 | + node.status = StatusEnum.accepted |
| 171 | + await session.commit() |
| 172 | + assert count == 2 |
| 173 | + |
| 174 | + # Finally, expect the END node |
| 175 | + graph = await graph_from_edge_list_v2(edge_list, Node, session) |
| 176 | + for node in processable_graph_nodes(graph): |
| 177 | + assert node.name == "END" |
| 178 | + assert node.status is StatusEnum.waiting |
| 179 | + # Add the Node back to the session and update its status |
| 180 | + session.add(node) |
| 181 | + await session.refresh(node) |
| 182 | + node.status = StatusEnum.accepted |
| 183 | + await session.commit() |
| 184 | + |
| 185 | + |
| 186 | +def test_validate_graph() -> None: |
| 187 | + """Test basic graph validation operations using a simple DAG.""" |
| 188 | + edge_list = [("A", "B"), ("B", "C"), ("C", "D"), ("C", "E"), ("D", "F"), ("E", "F")] |
| 189 | + |
| 190 | + g = nx.DiGraph() |
| 191 | + g.add_edges_from(edge_list) |
| 192 | + |
| 193 | + # this is a valid graph |
| 194 | + assert validate_graph(g, "A", "F") |
| 195 | + |
| 196 | + # add a new parallel node with no path to sink |
| 197 | + g.add_edge("C", "CC") |
| 198 | + assert not validate_graph(g, "A", "F") |
| 199 | + |
| 200 | + # create a cycle with the new node |
| 201 | + g.add_edge("CC", "A") |
| 202 | + assert not validate_graph(g, "A", "F") |
| 203 | + |
| 204 | + # correct the path |
| 205 | + g.remove_edge("CC", "A") |
| 206 | + g.add_edge("CC", "F") |
| 207 | + assert validate_graph(g, "A", "F") |
| 208 | + |
| 209 | + # remove the edges from a node |
| 210 | + g.remove_edge("CC", "F") |
| 211 | + g.remove_edge("C", "CC") |
| 212 | + # the graph is invalid because "CC" is now an isolate |
| 213 | + assert not validate_graph(g, "A", "F") |
| 214 | + |
| 215 | + # remove the unneeded node |
| 216 | + g.remove_node("CC") |
| 217 | + assert validate_graph(g, "A", "F") |
0 commit comments