Skip to content

Commit

Permalink
fix: fix errors in ethtool (#3605)
Browse files Browse the repository at this point in the history
* fix: fix 'invalid literal for int() with base 10: n/a' error and 'bad line' parse exception in ethtool

Signed-off-by: Chen lizhong <[email protected]>

* test: fix flake8 error in test file

Signed-off-by: Chen lizhong <[email protected]>

* test: Remove print in test file

Signed-off-by: Chen lizhong <[email protected]>

* test: update test for n/a checking

Signed-off-by: Chen lizhong <[email protected]>

* fix: use isnumeric() method instead of 'n/a in value'

Signed-off-by: Chen lizhong <[email protected]>

* fix: support python3 and python2 both for isnumeric()

Signed-off-by: Chen lizhong <[email protected]>

* fix: fix flake8

Signed-off-by: Chen lizhong <[email protected]>

* fix: fix error in python2.6

Signed-off-by: Chen lizhong <[email protected]>
  • Loading branch information
chenlizhong authored Dec 1, 2022
1 parent b6e75ab commit 9ed8f4a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
20 changes: 15 additions & 5 deletions insights/parsers/ethtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@

import os
import re
import sys
from collections import namedtuple
from ..parsers import ParseException
from .. import parser, LegacyItemAccess, CommandParser
from insights.specs import Specs

if sys.version_info[0] == 3:
# Python 3
unicode = str


def extract_iface_name_from_path(path, name):
"""
Expand Down Expand Up @@ -501,9 +506,10 @@ def parse_content(self, content):
for line in content[2:]:
if line.strip():
(key, value) = [s.strip() for s in line.split(":", 1)]
value = int(value)
self.data[key] = value
setattr(self, key.replace("-", "_"), value)
if unicode(value).isnumeric():
value = int(value)
self.data[key] = value
setattr(self, key.replace("-", "_"), value)


@parser(Specs.ethtool_g)
Expand Down Expand Up @@ -600,7 +606,8 @@ def set_section(section, data):
elif ':' in line:
# key: value, store in section data for now
key, value = (s.strip() for s in line.split(":", 1))
section_data[key.replace(" ", "_").lower()] = int(value)
if unicode(value).isnumeric():
section_data[key.replace(" ", "_").lower()] = int(value)

# Handle last found section, if any
set_section(section, section_data)
Expand Down Expand Up @@ -756,6 +763,7 @@ class TimeStamp(CommandParser):
>>> eno1.data['Hardware Receive Filter Modes']['all']
'HWTSTAMP_FILTER_ALL'
"""

@property
def ifname(self):
"""(str): the interface name"""
Expand All @@ -767,6 +775,7 @@ def parse_content(self, content):

group = None
for line in content[1:]:
line = line.strip()
if ":" in line:
key, val = [i.strip() for i in line.split(':', 1)]
group = {}
Expand All @@ -775,7 +784,7 @@ def parse_content(self, content):
key, val = [i.strip() for i in line.split(None, 1)]
group[key] = val.strip('()')
elif line:
raise ParseException('bad line: {0}'.format(line))
group[line] = None


@parser(Specs.ethtool)
Expand Down Expand Up @@ -853,6 +862,7 @@ class Ethtool(CommandParser):
>>> ethinfo.supported_ports # This is converted to a list of strings
['TP', 'MII']
"""

@property
def ifname(self):
"""str: Return the name of network interface in content."""
Expand Down
37 changes: 30 additions & 7 deletions insights/tests/parsers/test_ethtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import doctest


TEST_ETHTOOL_A_DOCS = '''
Pause parameters for eth0:
Autonegotiate: on
Expand Down Expand Up @@ -67,6 +66,16 @@
TX negotiated: off
""".strip()

ETHTOOL_T_NO_CAP_VALUE = """
Time stamping parameters for ens192:
Capabilities:
software-receive
software-system-clock
PTP Hardware Clock: none
Hardware Transmit Timestamp Modes: none
Hardware Receive Filter Modes: none
"""


def test_ethtool_a():
context = context_wrap(SUCCESS_ETHTOOL_A, path=SUCCESS_ETHTOOL_A_PATH)
Expand Down Expand Up @@ -142,7 +151,7 @@ def test_ethtool_a_blank_line():
tx-frame-low: 25
tx-usecs-high: 0
tx-frame-high: 0
tx-frame-high: n/a
""".strip()

Expand All @@ -168,15 +177,15 @@ def test_get_ethtool_c():
context.path = TEST_ETHTOOL_C_PATH
result = ethtool.CoalescingInfo(context)
assert keys_in(["adaptive-rx", "adaptive-tx", "pkt-rate-high",
"tx-usecs-irq", "tx-frame-low", "tx-usecs-high", "tx-frame-high"], result.data)
"tx-usecs-irq", "tx-frame-low", "tx-usecs-high"], result.data)
assert result.ifname == "eth2"
assert not result.adaptive_rx
assert not result.adaptive_tx
assert result.pkt_rate_high == 10
assert result.tx_usecs_irq == 0
assert result.tx_frame_low == 25
assert result.tx_usecs_high == 0
assert result.tx_frame_high == 0
assert 'tx_frame_high' not in result.data


def test_get_ethtool_c_cannot_get():
Expand Down Expand Up @@ -657,6 +666,16 @@ def test_ethtool_S_f():
all (HWTSTAMP_FILTER_ALL)
'''

ETHTOOL_T_NO_CAP_VALUE = """
Time stamping parameters for ens192:
Capabilities:
software-receive
software-system-clock
PTP Hardware Clock: none
Hardware Transmit Timestamp Modes: none
Hardware Receive Filter Modes: none
"""


def test_ethtool_timestamp():
timestamp = ethtool.TimeStamp(context_wrap(TEST_ETHTOOL_TIMESTAMP, path="sbin/ethtool_-T_eno1"))
Expand All @@ -666,9 +685,13 @@ def test_ethtool_timestamp():
assert timestamp.data['PTP Hardware Clock'] == '0'
assert timestamp.data['Hardware Transmit Timestamp Modes']['off'] == 'HWTSTAMP_TX_OFF'
assert timestamp.data['Hardware Receive Filter Modes']['all'] == 'HWTSTAMP_FILTER_ALL'
with pytest.raises(ParseException) as pe:
ethtool.TimeStamp(context_wrap(TEST_ETHTOOL_TIMESTAMP_AB, path="sbin/ethtool_-T_eno1"))
assert 'bad line:' in str(pe)


def test_ethtool_timestamp_no_cap_value():
timestamp = ethtool.TimeStamp(context_wrap(ETHTOOL_T_NO_CAP_VALUE, path="sbin/ethtool_-T_ens192"))
assert timestamp.ifname == 'ens192'
assert timestamp.data['Capabilities']['software-receive'] is None
assert timestamp.data['Hardware Transmit Timestamp Modes'] == 'none'


TEST_EXTRACT_FROM_PATH_1 = """
Expand Down

0 comments on commit 9ed8f4a

Please sign in to comment.