From afbc46076e6f704e556f3515e3b0ec9de287a3a3 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 28 Jan 2024 17:23:27 -0800 Subject: [PATCH] add ping tests --- CHANGELOG | 3 +- jc/parsers/ping.py | 2 +- jc/parsers/ping_s.py | 2 +- .../ping-hostname-source-ip-streaming.json | 1 + .../ping-ip-source-ip-streaming.json | 1 + .../ping6-hostname-source-ip-streaming.json | 1 + .../ping6-ip-source-ip-streaming.json | 1 + tests/test_ping.py | 8 +++ tests/test_ping_s.py | 52 ++++++++++++++++++- 9 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/ubuntu-22.04/ping-hostname-source-ip-streaming.json create mode 100644 tests/fixtures/ubuntu-22.04/ping-ip-source-ip-streaming.json create mode 100644 tests/fixtures/ubuntu-22.04/ping6-hostname-source-ip-streaming.json create mode 100644 tests/fixtures/ubuntu-22.04/ping6-ip-source-ip-streaming.json diff --git a/CHANGELOG b/CHANGELOG index 36a14476..98326109 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ jc changelog -20240125 v1.24.1 +20240126 v1.24.1 - Add `kv-dup` parser for Key/Value files with duplicate keys - TODO: Add `path-list` string parser to parse path list strings found in env variables - Add `--slurp` functionality to wrap output from multiple lines into a single array. @@ -13,6 +13,7 @@ jc changelog For `/proc` file magic syntax, this is a list of `/proc` filenames in order. This can help with identifying which input matches to which output when using `--slurp` or when converting multiple files via `/proc` file magic syntax. +- Enhance `ping` and `ping-s` parsers to support the `-I` command option - Enhance `proc-net-tcp` parser to add opposite endian support for architectures like the s390x - Enhance `url` parser to add `parent`, `filename`, `stem`, and `extension` fields diff --git a/jc/parsers/ping.py b/jc/parsers/ping.py index cc5e7875..624e6739 100644 --- a/jc/parsers/ping.py +++ b/jc/parsers/ping.py @@ -167,7 +167,7 @@ import jc.utils class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.10' + version = '1.11' description = '`ping` and `ping6` command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' diff --git a/jc/parsers/ping_s.py b/jc/parsers/ping_s.py index 9658d3c7..6cb7220f 100644 --- a/jc/parsers/ping_s.py +++ b/jc/parsers/ping_s.py @@ -88,7 +88,7 @@ from jc.exceptions import ParseError class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.4' + version = '1.5' description = '`ping` and `ping6` command streaming parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' diff --git a/tests/fixtures/ubuntu-22.04/ping-hostname-source-ip-streaming.json b/tests/fixtures/ubuntu-22.04/ping-hostname-source-ip-streaming.json new file mode 100644 index 00000000..c16fdbea --- /dev/null +++ b/tests/fixtures/ubuntu-22.04/ping-hostname-source-ip-streaming.json @@ -0,0 +1 @@ +[{"type":"reply","destination_ip":"10.0.0.1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"10.0.0.1","icmp_seq":1,"ttl":119,"time_ms":1.0,"duplicate":false},{"type":"reply","destination_ip":"10.0.0.1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"10.0.0.1","icmp_seq":2,"ttl":119,"time_ms":1.0,"duplicate":false},{"type":"reply","destination_ip":"10.0.0.1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"10.0.0.1","icmp_seq":3,"ttl":119,"time_ms":1.0,"duplicate":false},{"type":"summary","destination_ip":"10.0.0.1","sent_bytes":56,"pattern":null,"packets_transmitted":3,"packets_received":3,"packet_loss_percent":0.0,"duplicates":0,"errors":null,"corrupted":null,"time_ms":2000.0,"round_trip_ms_min":1.0,"round_trip_ms_avg":1.0,"round_trip_ms_max":1.0,"round_trip_ms_stddev":1.0}] diff --git a/tests/fixtures/ubuntu-22.04/ping-ip-source-ip-streaming.json b/tests/fixtures/ubuntu-22.04/ping-ip-source-ip-streaming.json new file mode 100644 index 00000000..c16fdbea --- /dev/null +++ b/tests/fixtures/ubuntu-22.04/ping-ip-source-ip-streaming.json @@ -0,0 +1 @@ +[{"type":"reply","destination_ip":"10.0.0.1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"10.0.0.1","icmp_seq":1,"ttl":119,"time_ms":1.0,"duplicate":false},{"type":"reply","destination_ip":"10.0.0.1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"10.0.0.1","icmp_seq":2,"ttl":119,"time_ms":1.0,"duplicate":false},{"type":"reply","destination_ip":"10.0.0.1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"10.0.0.1","icmp_seq":3,"ttl":119,"time_ms":1.0,"duplicate":false},{"type":"summary","destination_ip":"10.0.0.1","sent_bytes":56,"pattern":null,"packets_transmitted":3,"packets_received":3,"packet_loss_percent":0.0,"duplicates":0,"errors":null,"corrupted":null,"time_ms":2000.0,"round_trip_ms_min":1.0,"round_trip_ms_avg":1.0,"round_trip_ms_max":1.0,"round_trip_ms_stddev":1.0}] diff --git a/tests/fixtures/ubuntu-22.04/ping6-hostname-source-ip-streaming.json b/tests/fixtures/ubuntu-22.04/ping6-hostname-source-ip-streaming.json new file mode 100644 index 00000000..20397769 --- /dev/null +++ b/tests/fixtures/ubuntu-22.04/ping6-hostname-source-ip-streaming.json @@ -0,0 +1 @@ +[{"type":"reply","destination_ip":"2001:0db8::1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"2001:0db8::1","icmp_seq":1,"ttl":114,"time_ms":15.7,"duplicate":false},{"type":"reply","destination_ip":"2001:0db8::1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"2001:0db8::1","icmp_seq":2,"ttl":114,"time_ms":11.3,"duplicate":false},{"type":"summary","destination_ip":"2001:0db8::1","sent_bytes":56,"pattern":null,"packets_transmitted":2,"packets_received":2,"packet_loss_percent":0.0,"duplicates":0,"errors":null,"corrupted":null,"time_ms":1001.0,"round_trip_ms_min":11.314,"round_trip_ms_avg":13.495,"round_trip_ms_max":15.676,"round_trip_ms_stddev":2.181}] diff --git a/tests/fixtures/ubuntu-22.04/ping6-ip-source-ip-streaming.json b/tests/fixtures/ubuntu-22.04/ping6-ip-source-ip-streaming.json new file mode 100644 index 00000000..12a28854 --- /dev/null +++ b/tests/fixtures/ubuntu-22.04/ping6-ip-source-ip-streaming.json @@ -0,0 +1 @@ +[{"type":"reply","destination_ip":"2001:0db8::1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"2001:0db8::1","icmp_seq":1,"ttl":114,"time_ms":13.4,"duplicate":false},{"type":"reply","destination_ip":"2001:0db8::1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"2001:0db8::1","icmp_seq":2,"ttl":114,"time_ms":11.0,"duplicate":false},{"type":"reply","destination_ip":"2001:0db8::1","sent_bytes":56,"pattern":null,"timestamp":null,"response_bytes":64,"response_ip":"2001:0db8::1","icmp_seq":2,"ttl":114,"time_ms":11.0,"duplicate":false},{"type":"summary","destination_ip":"2001:0db8::1","sent_bytes":56,"pattern":null,"packets_transmitted":2,"packets_received":2,"packet_loss_percent":0.0,"duplicates":0,"errors":null,"corrupted":null,"time_ms":1002.0,"round_trip_ms_min":11.021,"round_trip_ms_avg":12.204,"round_trip_ms_max":13.388,"round_trip_ms_stddev":1.183}] diff --git a/tests/test_ping.py b/tests/test_ping.py index b8cab42f..fd51a275 100644 --- a/tests/test_ping.py +++ b/tests/test_ping.py @@ -84,12 +84,16 @@ class MyTests(unittest.TestCase): # ubuntu 22.4 with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-dest-unreachable.out'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping_dest_unreachable = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-ip-source-ip.out'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping_ip_source_ip = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-hostname-source-ip.out'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping_hostname_source_ip = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping6-ip-source-ip.out'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping6_ip_source_ip = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping6-hostname-source-ip.out'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping6_hostname_source_ip = f.read() @@ -300,12 +304,16 @@ class MyTests(unittest.TestCase): # ubuntu 22.4 with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-dest-unreachable.json'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping_dest_unreachable_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-ip-source-ip.json'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping_ip_source_ip_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-hostname-source-ip.json'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping_hostname_source_ip_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping6-ip-source-ip.json'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping6_ip_source_ip_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping6-hostname-source-ip.json'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping6_hostname_source_ip_json = json.loads(f.read()) diff --git a/tests/test_ping_s.py b/tests/test_ping_s.py index 37d6702a..2c475755 100644 --- a/tests/test_ping_s.py +++ b/tests/test_ping_s.py @@ -90,6 +90,18 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-dest-unreachable.out'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping_dest_unreachable = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-ip-source-ip.out'), 'r', encoding='utf-8') as f: + ubuntu_22_4_ping_ip_source_ip = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-hostname-source-ip.out'), 'r', encoding='utf-8') as f: + ubuntu_22_4_ping_hostname_source_ip = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping6-ip-source-ip.out'), 'r', encoding='utf-8') as f: + ubuntu_22_4_ping6_ip_source_ip = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping6-hostname-source-ip.out'), 'r', encoding='utf-8') as f: + ubuntu_22_4_ping6_hostname_source_ip = f.read() + # fedora with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/fedora32/ping-ip-O.out'), 'r', encoding='utf-8') as f: fedora32_ping_ip_O = f.read() @@ -288,6 +300,18 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-dest-unreachable-streaming.json'), 'r', encoding='utf-8') as f: ubuntu_22_4_ping_dest_unreachable_streaming_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-hostname-source-ip-streaming.json'), 'r', encoding='utf-8') as f: + ubuntu_22_4_ping_hostname_I_streaming_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping-ip-source-ip-streaming.json'), 'r', encoding='utf-8') as f: + ubuntu_22_4_ping_ip_I_streaming_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping6-ip-source-ip-streaming.json'), 'r', encoding='utf-8') as f: + ubuntu_22_4_ping6_ip_I_streaming_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-22.04/ping6-hostname-source-ip-streaming.json'), 'r', encoding='utf-8') as f: + ubuntu_22_4_ping6_hostname_I_streaming_json = json.loads(f.read()) + # fedora with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/fedora32/ping-ip-O-streaming.json'), 'r', encoding='utf-8') as f: fedora32_ping_ip_O_streaming_json = json.loads(f.read()) @@ -569,12 +593,38 @@ class MyTests(unittest.TestCase): """ self.assertEqual(list(jc.parsers.ping_s.parse(self.ubuntu_18_4_ping6_hostname_O_D_p_s.splitlines(), quiet=True)), self.ubuntu_18_4_ping6_hostname_O_D_p_s_streaming_json) - def test_ping_dest_unreachable_ubuntu_22_4(self): + def test_ping_s_dest_unreachable_ubuntu_22_4(self): """ Test 'ping' on Ubuntu 22.4 with destination unreachable message """ self.assertEqual(list(jc.parsers.ping_s.parse(self.ubuntu_22_4_ping_dest_unreachable.splitlines(), quiet=True)), [self.ubuntu_22_4_ping_dest_unreachable_streaming_json]) + + def test_ping_s_hostname_I_ubuntu_22_4(self): + """ + Test 'ping -I' on ubuntu 22.04 + """ + self.assertEqual(list(jc.parsers.ping_s.parse(self.ubuntu_22_4_ping_hostname_source_ip.splitlines(), quiet=True)), self.ubuntu_22_4_ping_hostname_I_streaming_json) + + def test_ping_s_ip_I_ubuntu_22_4(self): + """ + Test 'ping -I' on ubuntu 22.04 + """ + self.assertEqual(list(jc.parsers.ping_s.parse(self.ubuntu_22_4_ping_ip_source_ip.splitlines(), quiet=True)), self.ubuntu_22_4_ping_ip_I_streaming_json) + + def test_ping6_s_ip_I_ubuntu_22_4(self): + """ + Test 'ping6 -I' on ubuntu 22.04 + """ + self.assertEqual(list(jc.parsers.ping_s.parse(self.ubuntu_22_4_ping6_ip_source_ip.splitlines(), quiet=True)), self.ubuntu_22_4_ping6_ip_I_streaming_json) + + def test_ping6_s_hostname_I_ubuntu_22_4(self): + """ + Test 'ping6 -I' on ubuntu 22.04 + """ + self.assertEqual(list(jc.parsers.ping_s.parse(self.ubuntu_22_4_ping6_hostname_source_ip.splitlines(), quiet=True)), self.ubuntu_22_4_ping6_hostname_I_streaming_json) + + def test_ping_s_ip_O_fedora32(self): """ Test 'ping -O' on fedora32