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
This commit is contained in:
Rodolfo Alonso Hernandez 2020-02-26 16:46:15 +00:00
parent 4d5817003c
commit 43e150e690
2 changed files with 9 additions and 10 deletions

View File

@ -26,7 +26,6 @@ from pyroute2.netlink.rtnl import ifinfmsg
from pyroute2.netlink.rtnl import ndmsg from pyroute2.netlink.rtnl import ndmsg
from pyroute2 import NetlinkError from pyroute2 import NetlinkError
from pyroute2 import netns from pyroute2 import netns
import six
from neutron._i18n import _ from neutron._i18n import _
from neutron import privileged from neutron import privileged
@ -520,16 +519,13 @@ def make_serializable(value):
of two elements. of two elements.
""" """
def _ensure_string(value): def _ensure_string(value):
# NOTE(ralonsoh): once PY2 is deprecated, the str() conversion will be return value.decode() if isinstance(value, bytes) else value
# no needed and six.binary_type --> bytes.
return (str(value.decode('utf-8'))
if isinstance(value, six.binary_type) else value)
if isinstance(value, list): if isinstance(value, list):
return [make_serializable(item) for item in value] return [make_serializable(item) for item in value]
elif isinstance(value, netlink.nla_slot): elif isinstance(value, netlink.nla_slot):
return [value[0], make_serializable(value[1])] return [_ensure_string(value[0]), make_serializable(value[1])]
elif isinstance(value, netlink.nla_base) and six.PY3: elif isinstance(value, netlink.nla_base):
return make_serializable(value.dump()) return make_serializable(value.dump())
elif isinstance(value, dict): elif isinstance(value, dict):
return {_ensure_string(key): make_serializable(data) return {_ensure_string(key): make_serializable(data)

View File

@ -229,13 +229,16 @@ class IpLibTestCase(base.BaseTestCase):
class MakeSerializableTestCase(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), INPUT_1 = {'key1': 'value1', b'key2': b'value2', 'key3': ('a', 2),
'key4': [1, 2, 'c'], '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), OUTPUT_1 = {'key1': 'value1', 'key2': 'value2', 'key3': ('a', 2),
'key4': [1, 2, 'c'], 'key4': [1, 2, 'c'],
'key5': ['nla_name', '54321']} 'key5': ['nla_name1', '54321'],
'key6': ['nla_name2', 'abcdef']}
def test_make_serializable(self): def test_make_serializable(self):
self.assertEqual(self.OUTPUT_1, self.assertEqual(self.OUTPUT_1,