From abbd4820fd0c681a161a2564fba8b7353bbcc1f3 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 1 Mar 2024 13:52:45 -0800 Subject: [PATCH] add ethtool parser tests --- jc/parsers/ethtool.py | 2 +- tests/fixtures/generic/ethtool--default1.json | 1 + tests/fixtures/generic/ethtool--default1.out | 26 +++++++++++++++ tests/fixtures/generic/ethtool--default2.json | 1 + tests/fixtures/generic/ethtool--default2.out | 33 +++++++++++++++++++ .../generic/ethtool--module-info.json | 1 + .../fixtures/generic/ethtool--module-info.out | 30 +++++++++++++++++ tests/test_ethtool.py | 28 ++++++++++++++++ 8 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/generic/ethtool--default1.json create mode 100644 tests/fixtures/generic/ethtool--default1.out create mode 100644 tests/fixtures/generic/ethtool--default2.json create mode 100644 tests/fixtures/generic/ethtool--default2.out create mode 100644 tests/fixtures/generic/ethtool--module-info.json create mode 100644 tests/fixtures/generic/ethtool--module-info.out create mode 100644 tests/test_ethtool.py diff --git a/jc/parsers/ethtool.py b/jc/parsers/ethtool.py index 8daa3a40..e3605113 100644 --- a/jc/parsers/ethtool.py +++ b/jc/parsers/ethtool.py @@ -149,7 +149,7 @@ def _parse_default(data: str) -> JSONDictType: raw_output['name'] = line.split()[2][:-1] continue - data_line = line.replace('\t', ' ') + data_line = line.replace('\t', ' ') if not data_line.startswith(' '): # 9 spaces mode = '' diff --git a/tests/fixtures/generic/ethtool--default1.json b/tests/fixtures/generic/ethtool--default1.json new file mode 100644 index 00000000..75d39613 --- /dev/null +++ b/tests/fixtures/generic/ethtool--default1.json @@ -0,0 +1 @@ +{"name":"enp0s3","supported_pause_frame_use":"No","supports_auto_negotiation":true,"supported_fec_modes":"Not reported","advertised_pause_frame_use":false,"advertised_auto_negotiation":true,"advertised_fec_modes":"Not reported","speed":"1000Mb/s","duplex":"Full","port":"Twisted Pair","phyad":"0","transceiver":"internal","auto_negotiation":false,"mdi_x":"off (auto)","supports_wake_on":"umbg","wake_on":"d","link_detected":true,"supported_ports":["TP"],"supported_link_modes":["10baseT/Half","10baseT/Full","100baseT/Half","100baseT/Full","1000baseT/Full"],"advertised_link_modes":["10baseT/Half","10baseT/Full","100baseT/Half","100baseT/Full","1000baseT/Full"],"current_message_level":["0x00000007 (7)","drv probe link"],"speed_bps":1000000000} diff --git a/tests/fixtures/generic/ethtool--default1.out b/tests/fixtures/generic/ethtool--default1.out new file mode 100644 index 00000000..fc1a0f2a --- /dev/null +++ b/tests/fixtures/generic/ethtool--default1.out @@ -0,0 +1,26 @@ +Settings for enp0s3: + Supported ports: [ TP ] + Supported link modes: 10baseT/Half 10baseT/Full + 100baseT/Half 100baseT/Full + 1000baseT/Full + Supported pause frame use: No + Supports auto-negotiation: Yes + Supported FEC modes: Not reported + Advertised link modes: 10baseT/Half 10baseT/Full + 100baseT/Half 100baseT/Full + 1000baseT/Full + Advertised pause frame use: No + Advertised auto-negotiation: Yes + Advertised FEC modes: Not reported + Speed: 1000Mb/s + Duplex: Full + Port: Twisted Pair + PHYAD: 0 + Transceiver: internal + Auto-negotiation: on + MDI-X: off (auto) + Supports Wake-on: umbg + Wake-on: d + Current message level: 0x00000007 (7) + drv probe link + Link detected: yes diff --git a/tests/fixtures/generic/ethtool--default2.json b/tests/fixtures/generic/ethtool--default2.json new file mode 100644 index 00000000..87e9b69f --- /dev/null +++ b/tests/fixtures/generic/ethtool--default2.json @@ -0,0 +1 @@ +{"name":"enp1s0","supported_pause_frame_use":"Symmetric Receive-only","supports_auto_negotiation":true,"supported_fec_modes":"Not reported","advertised_pause_frame_use":false,"advertised_auto_negotiation":true,"advertised_fec_modes":"Not reported","speed":"40000Mb/s","duplex":"Full","auto_negotiation":false,"port":"FIBRE","phyad":"0","transceiver":"internal","supports_wake_on":"d","wake_on":"d","link_detected":true,"supported_ports":["FIBRE"],"supported_link_modes":["10000baseKX4/Full","40000baseCR4/Full","40000baseSR4/Full","56000baseCR4/Full","56000baseSR4/Full","1000baseX/Full","10000baseCR/Full","10000baseSR/Full"],"advertised_link_modes":["10000baseKX4/Full","40000baseCR4/Full","40000baseSR4/Full","1000baseX/Full","10000baseCR/Full","10000baseSR/Full"],"current_message_level":["0x00000014 (20)","link ifdown"],"speed_bps":40000000000} diff --git a/tests/fixtures/generic/ethtool--default2.out b/tests/fixtures/generic/ethtool--default2.out new file mode 100644 index 00000000..29a85f2f --- /dev/null +++ b/tests/fixtures/generic/ethtool--default2.out @@ -0,0 +1,33 @@ +Settings for enp1s0: + Supported ports: [ FIBRE ] + Supported link modes: 10000baseKX4/Full + 40000baseCR4/Full + 40000baseSR4/Full + 56000baseCR4/Full + 56000baseSR4/Full + 1000baseX/Full + 10000baseCR/Full + 10000baseSR/Full + Supported pause frame use: Symmetric Receive-only + Supports auto-negotiation: Yes + Supported FEC modes: Not reported + Advertised link modes: 10000baseKX4/Full + 40000baseCR4/Full + 40000baseSR4/Full + 1000baseX/Full + 10000baseCR/Full + 10000baseSR/Full + Advertised pause frame use: Symmetric + Advertised auto-negotiation: Yes + Advertised FEC modes: Not reported + Speed: 40000Mb/s + Duplex: Full + Auto-negotiation: off + Port: FIBRE + PHYAD: 0 + Transceiver: internal + Supports Wake-on: d + Wake-on: d + Current message level: 0x00000014 (20) + link ifdown + Link detected: yes diff --git a/tests/fixtures/generic/ethtool--module-info.json b/tests/fixtures/generic/ethtool--module-info.json new file mode 100644 index 00000000..7ae4da50 --- /dev/null +++ b/tests/fixtures/generic/ethtool--module-info.json @@ -0,0 +1 @@ +{"identifier":"0x03 (SFP)","extended_identifier":"0x04 (GBIC/SFP defined by 2-wire interface ID)","connector":"0x21 (Copper pigtail)","transceiver_codes":"0x01 0x00 0x00 0x04 0x00 0x04 0x80 0xd5 0x00","transceiver_type":["Infiniband: 1X Copper Passive","Ethernet: 1000BASE-CX","Passive Cable","FC: Twin Axial Pair (TW)","FC: 1200 MBytes/sec","FC: 800 MBytes/sec","FC: 400 MBytes/sec","FC: 200 MBytes/sec","FC: 100 MBytes/sec"],"encoding":"0x00 (unspecified)","br_nominal":"10300MBd","rate_identifier":"0x00 (unspecified)","length_smf_km":"0km","length_smf":"0m","length_50um":"0m","length_62_5um":"0m","length_copper":"2m","length_om3":"0m","passive_cu_cmplnce":"0x01 (SFF-8431 appendix E) [SFF-8472 rev10.4 only]","vendor_name":"UbiquitiNetworks","vendor_oui":"00:40:20","vendor_pn":"UDC-2","vendor_rev":"","option_values":"0x00 0x00","br_margin_max":"0%","br_margin_min":"0%"} diff --git a/tests/fixtures/generic/ethtool--module-info.out b/tests/fixtures/generic/ethtool--module-info.out new file mode 100644 index 00000000..21302a12 --- /dev/null +++ b/tests/fixtures/generic/ethtool--module-info.out @@ -0,0 +1,30 @@ + Identifier : 0x03 (SFP) + Extended identifier : 0x04 (GBIC/SFP defined by 2-wire interface ID) + Connector : 0x21 (Copper pigtail) + Transceiver codes : 0x01 0x00 0x00 0x04 0x00 0x04 0x80 0xd5 0x00 + Transceiver type : Infiniband: 1X Copper Passive + Transceiver type : Ethernet: 1000BASE-CX + Transceiver type : Passive Cable + Transceiver type : FC: Twin Axial Pair (TW) + Transceiver type : FC: 1200 MBytes/sec + Transceiver type : FC: 800 MBytes/sec + Transceiver type : FC: 400 MBytes/sec + Transceiver type : FC: 200 MBytes/sec + Transceiver type : FC: 100 MBytes/sec + Encoding : 0x00 (unspecified) + BR, Nominal : 10300MBd + Rate identifier : 0x00 (unspecified) + Length (SMF,km) : 0km + Length (SMF) : 0m + Length (50um) : 0m + Length (62.5um) : 0m + Length (Copper) : 2m + Length (OM3) : 0m + Passive Cu cmplnce. : 0x01 (SFF-8431 appendix E) [SFF-8472 rev10.4 only] + Vendor name : UbiquitiNetworks + Vendor OUI : 00:40:20 + Vendor PN : UDC-2 + Vendor rev : + Option values : 0x00 0x00 + BR margin, max : 0% + BR margin, min : 0% diff --git a/tests/test_ethtool.py b/tests/test_ethtool.py new file mode 100644 index 00000000..ed5e2083 --- /dev/null +++ b/tests/test_ethtool.py @@ -0,0 +1,28 @@ +import unittest + +from tests import utils_for_test as test_utils + +# Execute these steps for standard tests: +# - Save this file as `test_{parser_name}.py` since the helper methods extract parser names from the filename. +# - Organize fixtures in `tests/fixtures` for optimal structure. +# - Format fixtures as follows (using double dashes): +# - `{parser_name}--{some_test_description}.out` for command output. +# - `{parser_name}--{some_test_description}.json` for expected JSON after parsing. + +class MyTests(unittest.TestCase): + + def test_ethtool_nodata(self): + """ + Test 'ethtool' with no data + """ + test_utils.run_no_data(self, __file__, {}) + + def test_ethtool_all_fixtures(self): + """ + Test 'ethtool' with various fixtures + """ + test_utils.run_all_fixtures(self, __file__) + + +if __name__ == '__main__': + unittest.main()