Skip to content

Commit

Permalink
improve fake output
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed Mar 25, 2024
1 parent e7e8b9b commit 1ef376f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
49 changes: 39 additions & 10 deletions hyperglass/api/fake_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
# Project
from hyperglass.models.data import BGPRouteTable

PLAIN = r"""
BGP routing table entry for 4.0.0.0/9, version 1017877672
BGP_PLAIN = r"""BGP routing table entry for 4.0.0.0/9, version 1017877672
BGP Bestpath: deterministic-med
Paths: (10 available, best #9, table default)
Advertised to update-groups:
Expand Down Expand Up @@ -54,7 +53,7 @@
Community: 6939:7016 6939:8840 6939:9001
""" # noqa: W291,E501

ROUTES = [
BGP_ROUTES = [
{
"prefix": "1.1.1.0/24",
"active": True,
Expand Down Expand Up @@ -159,14 +158,44 @@
},
]

STRUCTURED = BGPRouteTable(vrf="default", count=len(ROUTES), routes=ROUTES, winning_weight="high")
STRUCTURED = BGPRouteTable(
vrf="default",
count=len(BGP_ROUTES),
routes=BGP_ROUTES,
winning_weight="high",
)

PING = r"""PING 1.1.1.1 (1.1.1.1): 56 data bytes
64 bytes from 1.1.1.1: icmp_seq=0 ttl=59 time=4.696 ms
64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=4.699 ms
64 bytes from 1.1.1.1: icmp_seq=2 ttl=59 time=4.640 ms
64 bytes from 1.1.1.1: icmp_seq=3 ttl=59 time=4.583 ms
64 bytes from 1.1.1.1: icmp_seq=4 ttl=59 time=4.640 ms
async def fake_output(structured: bool) -> t.Union[str, BGPRouteTable]:
"""Bypass the standard execution process and return static, fake output."""
output = PLAIN
--- 1.1.1.1 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max/stddev = 4.583/4.652/4.699/0.043 ms
"""

TRACEROUTE = r"""traceroute to 1.1.1.1 (1.1.1.1), 30 hops max, 52 byte packets
1 157.231.183.50 4.412 ms
2 129.219.10.4 4.612 ms
3 128.249.9.12 4.503 ms
4 139.15.19.3 7.458 ms
5 172.69.68.3 4.814 ms
6 1.1.1.1 4.564 ms
"""

if structured:
output = STRUCTURED

return output
async def fake_output(query_type: str, structured: bool) -> t.Union[str, BGPRouteTable]:
"""Bypass the standard execution process and return static, fake output."""

if "ping" in query_type:
return PING
if "traceroute" in query_type:
return TRACEROUTE
if "bgp" in query_type:
if structured:
return STRUCTURED
return BGP_PLAIN
return BGP_PLAIN
5 changes: 4 additions & 1 deletion hyperglass/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ async def query(

if state.params.fake_output:
# Return fake, static data for development purposes, if enabled.
output = await fake_output(query_data.device.structured_output or False)
output = await fake_output(
query_type=query_data.query_type,
structured=query_data.device.structured_output or False,
)
else:
# Pass request to execution module
output = await execute(query_data)
Expand Down

0 comments on commit 1ef376f

Please sign in to comment.