Skip to content

Commit

Permalink
fix: ethtool.Ring parsing be blocked by TypeError (#3954)
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxue Wang <xiaoxwan@redhat.com>
(cherry picked from commit c9d4379)
  • Loading branch information
JoySnow authored and xiangce committed Nov 16, 2023
1 parent a1e0de9 commit 77a874d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
11 changes: 8 additions & 3 deletions insights/parsers/ethtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ class Ring(CommandParser):
Within each the interface settings are available as four parameters -
``rx``, ``rx_mini``, ``rx_jumbo`` and ``tx``.
All the Ring parameter values are parsed into ``int`` type. For non-integer
parameter values, "n/a" will be convered to `-1` and other cases will be
coverted to `-2` for compatibility.
Attributes:
data (dict): Dictionary of keys with values in a list.
iface (str): Interface name.
Expand Down Expand Up @@ -589,7 +593,7 @@ def parse_content(self, content):

self.iface = extract_iface_name_from_content(content[0])

def set_section(section, data):
def set_section(section, section_data):
if section:
ringdata = Ring.Parameters(**section_data)
setattr(self, section, ringdata)
Expand All @@ -607,8 +611,9 @@ 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))
if unicode(value).isnumeric():
section_data[key.replace(" ", "_").lower()] = int(value)
parsed_value = int(value) if unicode(value).isnumeric() else (
-1 if value == 'n/a' else -2)
section_data[key.replace(" ", "_").lower()] = parsed_value

# Handle last found section, if any
set_section(section, section_data)
Expand Down
58 changes: 58 additions & 0 deletions insights/tests/parsers/test_ethtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,42 @@ def test_get_ethtool_c_short():
sos_commands/networkking/ethtool_-g_eth2
"""

TEST_ETHTOOL_G_3 = """
Ring parameters for ens3:
Pre-set maximums:
RX: 256
RX Mini: n/a
RX Jumbo: n/a
TX: 256
Current hardware settings:
RX: 256
RX Mini: n/a
RX Jumbo: n/a
TX: 256
""".strip()

TEST_ETHTOOL_G_PATH_3 = """
sos_commands/networkking/ethtool_-g_ens3
""".strip()

TEST_ETHTOOL_G_4 = """
Ring parameters for ens3:
Pre-set maximums:
RX: 256
RX Mini: some-unexpected-value
RX Jumbo: some-unexpected-value
TX: 256
Current hardware settings:
RX: 256
RX Mini: some-unexpected-value
RX Jumbo: some-unexpected-value
TX: 256
""".strip()

TEST_ETHTOOL_G_PATH_4 = """
sos_commands/networkking/ethtool_-g_ens3
""".strip()


def test_ethtool_g():
context = context_wrap(TEST_ETHTOOL_G)
Expand Down Expand Up @@ -298,6 +334,28 @@ def test_ethtool_g_2():
assert result.ifname == "eth2"


def test_ethtool_g_3():
context = context_wrap(TEST_ETHTOOL_G_3, path=TEST_ETHTOOL_G_PATH_3)
result = ethtool.Ring(context)
assert keys_in(["max", "current"], result.data)
assert result.ifname == "ens3"
assert result.data['max'].rx == 256
assert result.data['max'].rx_mini == -1
assert result.current.rx_jumbo == -1
assert result.current.tx == 256


def test_ethtool_g_4():
context = context_wrap(TEST_ETHTOOL_G_4, path=TEST_ETHTOOL_G_PATH_4)
result = ethtool.Ring(context)
assert keys_in(["max", "current"], result.data)
assert result.ifname == "ens3"
assert result.data['max'].rx == 256
assert result.data['max'].rx_mini == -2
assert result.current.rx_jumbo == -2
assert result.current.tx == 256


TEST_ETHTOOL_I_DOCS = '''
driver: bonding
version: 3.6.0
Expand Down

0 comments on commit 77a874d

Please sign in to comment.