Browse Source

Ensure netlink.nla_slot tuple key is a string

Ensure Pyroute2 netlink.nla_slot first element ("name"), is properly
converted to a string value. E.g.:
  In:  (b'IFA_ADDRESS', '192.168.30.20')
  Out: ('IFA_ADDRESS', '192.168.30.20')

Python2 compatibility checks are removed from ip_lib.make_serializable.

Change-Id: I87efe6cf8734bae6523106944e99fbd7db9ef4d5
Related-Bug: #1846360
tags/16.0.0.0rc1
Rodolfo Alonso Hernandez 5 months ago
parent
commit
43e150e690
2 changed files with 9 additions and 10 deletions
  1. +3
    -7
      neutron/privileged/agent/linux/ip_lib.py
  2. +6
    -3
      neutron/tests/unit/privileged/agent/linux/test_ip_lib.py

+ 3
- 7
neutron/privileged/agent/linux/ip_lib.py View File

@@ -26,7 +26,6 @@ from pyroute2.netlink.rtnl import ifinfmsg
from pyroute2.netlink.rtnl import ndmsg
from pyroute2 import NetlinkError
from pyroute2 import netns
import six

from neutron._i18n import _
from neutron import privileged
@@ -520,16 +519,13 @@ def make_serializable(value):
of two elements.
"""
def _ensure_string(value):
# NOTE(ralonsoh): once PY2 is deprecated, the str() conversion will be
# no needed and six.binary_type --> bytes.
return (str(value.decode('utf-8'))
if isinstance(value, six.binary_type) else value)
return value.decode() if isinstance(value, bytes) else value

if isinstance(value, list):
return [make_serializable(item) for item in value]
elif isinstance(value, netlink.nla_slot):
return [value[0], make_serializable(value[1])]
elif isinstance(value, netlink.nla_base) and six.PY3:
return [_ensure_string(value[0]), make_serializable(value[1])]
elif isinstance(value, netlink.nla_base):
return make_serializable(value.dump())
elif isinstance(value, dict):
return {_ensure_string(key): make_serializable(data)


+ 6
- 3
neutron/tests/unit/privileged/agent/linux/test_ip_lib.py View File

@@ -229,13 +229,16 @@ class IpLibTestCase(base.BaseTestCase):

class MakeSerializableTestCase(base.BaseTestCase):

NLA_DATA = ifinfmsg.ifinfbase.state(data=b'54321')
NLA_DATA1 = ifinfmsg.ifinfbase.state(data=b'54321')
NLA_DATA2 = ifinfmsg.ifinfbase.state(data=b'abcdef')
INPUT_1 = {'key1': 'value1', b'key2': b'value2', 'key3': ('a', 2),
'key4': [1, 2, 'c'],
'key5': netlink.nla_slot('nla_name', NLA_DATA)}
b'key5': netlink.nla_slot('nla_name1', NLA_DATA1),
'key6': netlink.nla_slot(b'nla_name2', NLA_DATA2)}
OUTPUT_1 = {'key1': 'value1', 'key2': 'value2', 'key3': ('a', 2),
'key4': [1, 2, 'c'],
'key5': ['nla_name', '54321']}
'key5': ['nla_name1', '54321'],
'key6': ['nla_name2', 'abcdef']}

def test_make_serializable(self):
self.assertEqual(self.OUTPUT_1,


Loading…
Cancel
Save