|
| 1 | +"""Unit tests for remote packet capture utilities.""" |
| 2 | + |
| 3 | +from unittest.mock import Mock, patch |
| 4 | + |
| 5 | +from mistapi.device_utils.__tools.remote_capture import ( |
| 6 | + _build_pcap_body, |
| 7 | + org_mxedge_remote_pcap, |
| 8 | + site_mxedge_remote_pcap, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +def _run_trigger_and_build_websocket(*, trigger_fn, ws_factory_fn): |
| 13 | + trigger_response = trigger_fn() |
| 14 | + return ws_factory_fn(trigger_response) |
| 15 | + |
| 16 | + |
| 17 | +@patch("mistapi.device_utils.__tools.remote_capture.SitePcapEvents") |
| 18 | +@patch("mistapi.device_utils.__tools.remote_capture.site_pcaps.startSitePacketCapture") |
| 19 | +@patch("mistapi.device_utils.__tools.remote_capture.WebSocketWrapper") |
| 20 | +def test_site_mxedge_capture_uses_site_endpoint_and_stream( |
| 21 | + wrapper_class, start_capture, pcap_events |
| 22 | +) -> None: |
| 23 | + session = Mock() |
| 24 | + wrapper_class.return_value.start_with_trigger.side_effect = ( |
| 25 | + _run_trigger_and_build_websocket |
| 26 | + ) |
| 27 | + |
| 28 | + site_mxedge_remote_pcap( |
| 29 | + session, |
| 30 | + site_id="site-id", |
| 31 | + device_interfaces={ |
| 32 | + "mxedge-id": { |
| 33 | + "port1": "udp port 67 or udp port 68", |
| 34 | + "oobm": None, |
| 35 | + } |
| 36 | + }, |
| 37 | + tcpdump_expression="host 192.0.2.1", |
| 38 | + ) |
| 39 | + |
| 40 | + start_capture.assert_called_once_with( |
| 41 | + session, |
| 42 | + site_id="site-id", |
| 43 | + body={ |
| 44 | + "duration": 600, |
| 45 | + "format": "stream", |
| 46 | + "max_pkt_len": 512, |
| 47 | + "mxedges": { |
| 48 | + "mxedge-id": { |
| 49 | + "interfaces": { |
| 50 | + "port1": {"tcpdump_expression": "udp port 67 or udp port 68"}, |
| 51 | + "oobm": {}, |
| 52 | + } |
| 53 | + } |
| 54 | + }, |
| 55 | + "num_packets": 1024, |
| 56 | + "tcpdump_expression": "host 192.0.2.1", |
| 57 | + "type": "mxedge", |
| 58 | + }, |
| 59 | + ) |
| 60 | + pcap_events.assert_called_once_with(session, site_id="site-id") |
| 61 | + |
| 62 | + |
| 63 | +@patch("mistapi.device_utils.__tools.remote_capture.OrgPcapEvents") |
| 64 | +@patch("mistapi.device_utils.__tools.remote_capture.org_pcaps.startOrgPacketCapture") |
| 65 | +@patch("mistapi.device_utils.__tools.remote_capture.WebSocketWrapper") |
| 66 | +def test_org_mxedge_capture_uses_org_endpoint_and_stream( |
| 67 | + wrapper_class, start_capture, pcap_events |
| 68 | +) -> None: |
| 69 | + session = Mock() |
| 70 | + wrapper_class.return_value.start_with_trigger.side_effect = ( |
| 71 | + _run_trigger_and_build_websocket |
| 72 | + ) |
| 73 | + |
| 74 | + org_mxedge_remote_pcap( |
| 75 | + session, |
| 76 | + org_id="org-id", |
| 77 | + device_interfaces={"mxedge-id": {"kni0": "tcp port 443", "oobm": None}}, |
| 78 | + ) |
| 79 | + |
| 80 | + start_capture.assert_called_once_with( |
| 81 | + session, |
| 82 | + org_id="org-id", |
| 83 | + body={ |
| 84 | + "duration": 600, |
| 85 | + "format": "stream", |
| 86 | + "max_pkt_len": 512, |
| 87 | + "mxedges": { |
| 88 | + "mxedge-id": { |
| 89 | + "interfaces": { |
| 90 | + "kni0": {"tcpdump_expression": "tcp port 443"}, |
| 91 | + "oobm": {}, |
| 92 | + } |
| 93 | + } |
| 94 | + }, |
| 95 | + "num_packets": 1024, |
| 96 | + "type": "mxedge", |
| 97 | + }, |
| 98 | + ) |
| 99 | + pcap_events.assert_called_once_with(session, org_id="org-id") |
| 100 | + |
| 101 | + |
| 102 | +def test_gateway_capture_keeps_main_and_per_port_expressions_independent() -> None: |
| 103 | + body = _build_pcap_body( |
| 104 | + { |
| 105 | + "00000000-0000-0000-1000-001122334455": { |
| 106 | + "ge-0/0/0": "tcp", |
| 107 | + "ge-0/0/1": None, |
| 108 | + }, |
| 109 | + "001122334466": {"ge-1/0/0": "port 443"}, |
| 110 | + }, |
| 111 | + "gateways", |
| 112 | + "gateway", |
| 113 | + "udp", |
| 114 | + 600, |
| 115 | + 1500, |
| 116 | + 100, |
| 117 | + ) |
| 118 | + |
| 119 | + assert body["tcpdump_expression"] == "udp" |
| 120 | + assert body["gateways"] == { |
| 121 | + "001122334455": { |
| 122 | + "ports": { |
| 123 | + "ge-0/0/0": {"tcpdump_expression": "tcp"}, |
| 124 | + "ge-0/0/1": {}, |
| 125 | + } |
| 126 | + }, |
| 127 | + "001122334466": {"ports": {"ge-1/0/0": {"tcpdump_expression": "port 443"}}}, |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | +def test_switch_capture_omits_unspecified_main_expression() -> None: |
| 132 | + body = _build_pcap_body( |
| 133 | + {"001122334455": {"ge-0/0/0": "arp"}}, |
| 134 | + "switches", |
| 135 | + "switch", |
| 136 | + None, |
| 137 | + 60, |
| 138 | + 512, |
| 139 | + 10, |
| 140 | + ) |
| 141 | + |
| 142 | + assert "tcpdump_expression" not in body |
| 143 | + assert body["switches"]["001122334455"]["ports"]["ge-0/0/0"] == { |
| 144 | + "tcpdump_expression": "arp" |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | +def test_org_pcap_events_uses_org_channel() -> None: |
| 149 | + from mistapi.websockets.orgs import PcapEvents |
| 150 | + |
| 151 | + websocket = PcapEvents(Mock(), org_id="org-id") |
| 152 | + |
| 153 | + assert vars(websocket)["_channels"] == ["/orgs/org-id/pcaps"] |
0 commit comments