Skip to content

Commit

Permalink
rpc: add graph valiate subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
willcl-ark committed Feb 22, 2024
1 parent a187a78 commit 1c10aad
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cli/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ def create(
},
)
)

@graph.command()
@click.argument("graph", type=Path)
def validate(graph: Path):
"""
Validate a graph file against the schema.
"""
print(rpc_call("graph_validate", {"graph_path": graph.as_posix()}))
15 changes: 15 additions & 0 deletions src/warnet/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from logging.handlers import RotatingFileHandler
from pathlib import Path

import jsonschema
import networkx as nx
import scenarios
from flask import Flask, jsonify, request
Expand All @@ -24,6 +25,8 @@
from warnet.utils import (
create_cycle_graph,
gen_config_dir,
load_schema,
validate_graph_schema,
)
from warnet.warnet import Warnet

Expand Down Expand Up @@ -169,6 +172,7 @@ def setup_rpc(self):
self.jsonrpc.register(self.network_export)
# Graph
self.jsonrpc.register(self.graph_generate)
self.jsonrpc.register(self.graph_validate)
# Debug
self.jsonrpc.register(self.generate_deployment)
# Server
Expand Down Expand Up @@ -457,6 +461,17 @@ def graph_generate(
self.logger.error(msg)
raise ServerError(message=msg) from e

def graph_validate(self, graph_path: str) -> str:

schema = load_schema()
with open(graph_path) as f:
graph = nx.parse_graphml(f.read(), node_type=int)
try:
validate_graph_schema(schema, graph)
except (jsonschema.ValidationError, jsonschema.SchemaError) as e:
raise ServerError(message=f"Schema of {graph_path} is invalid: {e}") from e
return f"Schema of {graph_path} is valid"

def network_down(self, network: str = "warnet") -> str:
"""
Stop all containers in <network>.
Expand Down
4 changes: 4 additions & 0 deletions test/graph_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
with open(tf, "w") as file:
file.write(xml)

# Validate the graph schema
assert "invalid" not in base.warcli(f"graph validate {Path(tf)}")
print(f"Graph at {tf} validated successfully")

# Test that the graph actually works
print(base.warcli(f"network start {Path(tf)}"))
base.wait_for_all_tanks_status(target="running")
Expand Down

0 comments on commit 1c10aad

Please sign in to comment.