aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOle Troan <ot@cisco.com>2018-12-06 17:35:12 +0100
committerNeale Ranns <nranns@cisco.com>2018-12-10 08:01:56 +0000
commit7f99183a20d60cd5c648cc23d7a2f30a594a215b (patch)
treebb3934aab296ce5fa531bfc5ab47921a113ffd26
parent521a8d7df423a0b5aaf259d49ca9230705bc25ee (diff)
Test framework: StringIO fixes for Python3
Add 2/3 support to binarytomac and mactobinary and move to vpp_mac.py Change-Id: I3dc7e4a24486aee22140c781aae7e44e58935877 Signed-off-by: Ole Troan <ot@cisco.com>
-rw-r--r--src/vpp-api/python/vpp_papi/vpp_stats.py9
-rw-r--r--test/framework.py6
-rw-r--r--test/run_tests.py6
-rw-r--r--test/test_bond.py2
-rw-r--r--test/test_dhcp.py3
-rw-r--r--test/test_gbp.py1
-rw-r--r--test/test_ip4_irb.py2
-rw-r--r--test/test_ipip.py26
-rw-r--r--test/test_l2bd_arp_term.py4
-rw-r--r--test/test_mtu.py37
-rw-r--r--test/test_nat.py12
-rw-r--r--test/test_p2p_ethernet.py2
-rw-r--r--test/test_pppoe.py4
-rwxr-xr-xtest/test_util.py19
-rw-r--r--test/test_vxlan.py20
-rw-r--r--test/test_vxlan_gbp.py20
-rw-r--r--test/util.py36
-rw-r--r--test/vpp_interface.py14
-rw-r--r--test/vpp_l2.py3
-rw-r--r--test/vpp_mac.py13
-rw-r--r--test/vpp_neighbor.py2
-rw-r--r--test/vpp_papi_provider.py7
-rw-r--r--test/vpp_pppoe_interface.py2
23 files changed, 124 insertions, 126 deletions
diff --git a/src/vpp-api/python/vpp_papi/vpp_stats.py b/src/vpp-api/python/vpp_papi/vpp_stats.py
index bb6cdf5ffdc..06daaf86f50 100644
--- a/src/vpp-api/python/vpp_papi/vpp_stats.py
+++ b/src/vpp-api/python/vpp_papi/vpp_stats.py
@@ -196,8 +196,13 @@ class VPPStats(object):
retries = 0
while True:
try:
- dir = self.ls(name)
- return self.dump(dir).values()[0]
+ d = self.ls(name)
+ s = self.dump(d)
+ if len(s) > 1:
+ raise AttributeError('Matches multiple counters {}'
+ .format(name))
+ k, v = s.popitem()
+ return v
except VPPStatsIOError as e:
if retries > 10:
return None
diff --git a/test/framework.py b/test/framework.py
index 8a1bfcb660b..c84c8cafb59 100644
--- a/test/framework.py
+++ b/test/framework.py
@@ -120,7 +120,7 @@ def pump_output(testclass):
split = read.splitlines(True)
if len(stderr_fragment) > 0:
split[0] = "%s%s" % (stderr_fragment, split[0])
- if len(split) > 0 and split[-1].endswith("\n"):
+ if len(split) > 0 and split[-1].endswith(b"\n"):
limit = None
else:
limit = -1
@@ -487,7 +487,7 @@ class VppTestCase(unittest.TestCase):
if hasattr(cls, 'pump_thread_stop_flag'):
cls.pump_thread_stop_flag.set()
if hasattr(cls, 'pump_thread_wakeup_pipe'):
- os.write(cls.pump_thread_wakeup_pipe[1], 'ding dong wake up')
+ os.write(cls.pump_thread_wakeup_pipe[1], b'ding dong wake up')
if hasattr(cls, 'pump_thread'):
cls.logger.debug("Waiting for pump thread to stop")
cls.pump_thread.join()
@@ -528,7 +528,7 @@ class VppTestCase(unittest.TestCase):
stderr_log(single_line_delim)
stderr_log('VPP output to stderr while running %s:', cls.__name__)
stderr_log(single_line_delim)
- vpp_output = "".join(cls.vpp_stderr_deque)
+ vpp_output = "".join(str(cls.vpp_stderr_deque))
with open(cls.tempdir + '/vpp_stderr.txt', 'w') as f:
f.write(vpp_output)
stderr_log('\n%s', vpp_output)
diff --git a/test/run_tests.py b/test/run_tests.py
index cbca7f96e39..f5d5211078e 100644
--- a/test/run_tests.py
+++ b/test/run_tests.py
@@ -139,7 +139,11 @@ class TestCaseWrapper(object):
self.finished_parent_end, self.finished_child_end = Pipe(duplex=False)
self.result_parent_end, self.result_child_end = Pipe(duplex=False)
self.testcase_suite = testcase_suite
- self.stdouterr_queue = manager.StreamQueue()
+ if sys.version[0] == '2':
+ self.stdouterr_queue = manager.StreamQueue()
+ else:
+ from multiprocessing import get_context
+ self.stdouterr_queue = manager.StreamQueue(ctx=get_context())
self.logger = get_parallel_logger(self.stdouterr_queue)
self.child = Process(target=test_runner_wrapper,
args=(testcase_suite,
diff --git a/test/test_bond.py b/test/test_bond.py
index e354601ca91..b955f899190 100644
--- a/test/test_bond.py
+++ b/test/test_bond.py
@@ -7,7 +7,7 @@ from framework import VppTestCase, VppTestRunner
from scapy.packet import Raw
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, UDP
-from util import mactobinary
+from vpp_mac import mactobinary
from vpp_bond_interface import VppBondInterface
diff --git a/test/test_dhcp.py b/test/test_dhcp.py
index 179221d6584..db3e3f3bab0 100644
--- a/test/test_dhcp.py
+++ b/test/test_dhcp.py
@@ -8,7 +8,7 @@ from framework import VppTestCase, VppTestRunner, running_extended_tests
from vpp_neighbor import VppNeighbor
from vpp_ip_route import find_route, VppIpTable
from util import mk_ll_addr
-
+from vpp_mac import mactobinary, binarytomac
from scapy.layers.l2 import Ether, getmacbyip, ARP
from scapy.layers.inet import IP, UDP, ICMP
from scapy.layers.inet6 import IPv6, in6_getnsmac
@@ -20,7 +20,6 @@ from scapy.layers.dhcp6 import DHCP6, DHCP6_Solicit, DHCP6_RelayForward, \
from socket import AF_INET, AF_INET6
from scapy.utils import inet_pton, inet_ntop
from scapy.utils6 import in6_ptop
-from util import mactobinary
DHCP4_CLIENT_PORT = 68
DHCP4_SERVER_PORT = 67
diff --git a/test/test_gbp.py b/test/test_gbp.py
index 53dcf3bfd34..efac2de6756 100644
--- a/test/test_gbp.py
+++ b/test/test_gbp.py
@@ -28,7 +28,6 @@ from scapy.layers.vxlan import VXLAN
from socket import AF_INET, AF_INET6
from scapy.utils import inet_pton, inet_ntop
-from util import mactobinary
from vpp_papi_provider import L2_VTR_OP
diff --git a/test/test_ip4_irb.py b/test/test_ip4_irb.py
index cf7d89e4f6e..a7fe7875053 100644
--- a/test/test_ip4_irb.py
+++ b/test/test_ip4_irb.py
@@ -31,7 +31,7 @@ from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, UDP
from framework import VppTestCase, VppTestRunner
-from util import mactobinary
+from vpp_mac import mactobinary
from vpp_papi_provider import L2_PORT_TYPE
diff --git a/test/test_ipip.py b/test/test_ipip.py
index 989330faa6f..e4a893bca51 100644
--- a/test/test_ipip.py
+++ b/test/test_ipip.py
@@ -8,7 +8,8 @@ from framework import VppTestCase, VppTestRunner
from vpp_ip import DpoProto
from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable
from socket import AF_INET, AF_INET6, inet_pton
-import StringIO
+from util import reassemble4
+
""" Testipip is a subclass of VPPTestCase classes.
@@ -17,21 +18,6 @@ IPIP tests.
"""
-# Replace by deframent from scapy.
-def reassemble(listoffragments):
- buffer = StringIO.StringIO()
- first = listoffragments[0]
- buffer.seek(20)
- for pkt in listoffragments:
- buffer.seek(pkt[IP].frag*8)
- buffer.write(pkt[IP].payload)
- first.len = len(buffer.getvalue()) + 20
- first.flags = 0
- del(first.chksum)
- header = str(first[IP])[:20]
- return first[IP].__class__(header + buffer.getvalue())
-
-
class TestIPIP(VppTestCase):
""" IPIP Test Case """
@@ -60,7 +46,7 @@ class TestIPIP(VppTestCase):
i.admin_down()
def validate(self, rx, expected):
- self.assertEqual(rx, expected.__class__(str(expected)))
+ self.assertEqual(rx, expected.__class__(expected))
def generate_ip4_frags(self, payload_length, fragment_size):
p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
@@ -214,7 +200,7 @@ class TestIPIP(VppTestCase):
self.pg1.add_stream(frags)
self.pg_start()
rx = self.pg0.get_capture(6)
- reass_pkt = reassemble(rx)
+ reass_pkt = reassemble4(rx)
p4_reply.ttl -= 1
p4_reply.id = 256
self.validate(reass_pkt, p4_reply)
@@ -225,7 +211,7 @@ class TestIPIP(VppTestCase):
self.pg1.add_stream(frags)
self.pg_start()
rx = self.pg0.get_capture(2)
- reass_pkt = reassemble(rx)
+ reass_pkt = reassemble4(rx)
p4_reply.ttl -= 1
p4_reply.id = 512
self.validate(reass_pkt, p4_reply)
@@ -320,7 +306,7 @@ class TestIPIP6(VppTestCase):
rv = self.vapi.ipip_del_tunnel(sw_if_index=self.tunnel_if_index)
def validate(self, rx, expected):
- self.assertEqual(rx, expected.__class__(str(expected)))
+ self.assertEqual(rx, expected.__class__(expected))
def generate_ip6_frags(self, payload_length, fragment_size):
p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
diff --git a/test/test_l2bd_arp_term.py b/test/test_l2bd_arp_term.py
index 92942d38789..ddba79b79f0 100644
--- a/test/test_l2bd_arp_term.py
+++ b/test/test_l2bd_arp_term.py
@@ -19,8 +19,8 @@ from scapy.layers.inet6 import IPv6, UDP, ICMPv6ND_NS, ICMPv6ND_RS, \
ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, ICMPv6DestUnreach, icmp6types
from framework import VppTestCase, VppTestRunner
-from util import Host, ppp, mactobinary
-from vpp_mac import VppMacAddress
+from util import Host, ppp
+from vpp_mac import VppMacAddress, mactobinary
from vpp_ip import VppIpAddress
diff --git a/test/test_mtu.py b/test/test_mtu.py
index 57d56001ee2..3203e40e114 100644
--- a/test/test_mtu.py
+++ b/test/test_mtu.py
@@ -15,30 +15,17 @@ from framework import VppTestCase, VppTestRunner
from vpp_ip import DpoProto
from vpp_ip_route import VppIpRoute, VppRoutePath
from socket import AF_INET, AF_INET6, inet_pton
-import StringIO
+from util import reassemble4
+
""" Test_mtu is a subclass of VPPTestCase classes.
MTU tests.
"""
-def reassemble(listoffragments):
- buffer = StringIO.StringIO()
- first = listoffragments[0]
- buffer.seek(20)
- for pkt in listoffragments:
- # pkt.show2()
- buffer.seek(pkt[IP].frag*8)
- buffer.write(pkt[IP].payload)
- first.len = len(buffer.getvalue()) + 20
- first.flags = 0
- del(first.chksum)
- header = str(first[IP])[:20]
- return first[IP].__class__(header + buffer.getvalue())
-
-
class TestMTU(VppTestCase):
""" MTU Test Case """
+ maxDiff = None
@classmethod
def setUpClass(cls):
@@ -65,7 +52,7 @@ class TestMTU(VppTestCase):
i.admin_down()
def validate(self, rx, expected):
- self.assertEqual(rx, expected.__class__(str(expected)))
+ self.assertEqual(rx, expected.__class__(expected))
def validate_bytes(self, rx, expected):
self.assertEqual(rx, expected)
@@ -111,14 +98,14 @@ class TestMTU(VppTestCase):
ttl=254, len=576, id=0) /
p_icmp4 / p_ip4 / p_payload)
icmp4_reply[1].ttl -= 1
- n = icmp4_reply.__class__(str(icmp4_reply))
- s = str(icmp4_reply)
+ n = icmp4_reply.__class__(icmp4_reply)
+ s = bytes(icmp4_reply)
icmp4_reply = s[0:576]
rx = self.send_and_expect(self.pg0, p4*11, self.pg0)
for p in rx:
# p.show2()
# n.show2()
- self.validate_bytes(str(p[1]), icmp4_reply)
+ self.validate_bytes(bytes(p[1]), icmp4_reply)
# Now with DF off. Expect fragments.
# First go with 1500 byte packets.
@@ -134,7 +121,7 @@ class TestMTU(VppTestCase):
self.pg0.add_stream(p4*1)
self.pg_start()
rx = self.pg1.get_capture(3)
- reass_pkt = reassemble(rx)
+ reass_pkt = reassemble4(rx)
self.validate(reass_pkt, p4_reply)
'''
@@ -152,7 +139,7 @@ class TestMTU(VppTestCase):
self.pg0.add_stream(p4*1)
self.pg_start()
rx = self.pg1.get_capture(16)
- reass_pkt = reassemble(rx)
+ reass_pkt = reassemble4(rx)
reass_pkt.show2()
p4_reply.show2()
self.validate(reass_pkt, p4_reply)
@@ -191,13 +178,13 @@ class TestMTU(VppTestCase):
hlim=255, plen=1240) /
p_icmp6 / p_ip6 / p_payload)
icmp6_reply[2].hlim -= 1
- n = icmp6_reply.__class__(str(icmp6_reply))
- s = str(icmp6_reply)
+ n = icmp6_reply.__class__(icmp6_reply)
+ s = bytes(icmp6_reply)
icmp6_reply_str = s[0:1280]
rx = self.send_and_expect(self.pg0, p6*9, self.pg0)
for p in rx:
- self.validate_bytes(str(p[1]), icmp6_reply_str)
+ self.validate_bytes(bytes(p[1]), icmp6_reply_str)
# Reset MTU
self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index,
diff --git a/test/test_nat.py b/test/test_nat.py
index c03cf08323b..b873074eccc 100644
--- a/test/test_nat.py
+++ b/test/test_nat.py
@@ -3,7 +3,6 @@
import socket
import unittest
import struct
-import StringIO
import random
from framework import VppTestCase, VppTestRunner, running_extended_tests
@@ -19,10 +18,11 @@ from util import ppp
from ipfix import IPFIX, Set, Template, Data, IPFIXDecoder
from time import sleep
from util import ip4_range
-from util import mactobinary
+from vpp_mac import mactobinary
from syslog_rfc5424_parser import SyslogMessage, ParseError
from syslog_rfc5424_parser.constants import SyslogFacility, SyslogSeverity
from vpp_papi_provider import SYSLOG_SEVERITY
+from io import BytesIO
class MethodHolder(VppTestCase):
@@ -725,13 +725,13 @@ class MethodHolder(VppTestCase):
:returns: Reassembled IPv4 packet
"""
- buffer = StringIO.StringIO()
+ buffer = BytesIO()
for p in frags:
self.assertEqual(p[IP].src, src)
self.assertEqual(p[IP].dst, dst)
self.assert_ip_checksum_valid(p)
buffer.seek(p[IP].frag * 8)
- buffer.write(p[IP].payload)
+ buffer.write(bytes(p[IP].payload))
ip = IP(src=frags[0][IP].src, dst=frags[0][IP].dst,
proto=frags[0][IP].proto)
if ip.proto == IP_PROTOS.tcp:
@@ -754,12 +754,12 @@ class MethodHolder(VppTestCase):
:returns: Reassembled IPv6 packet
"""
- buffer = StringIO.StringIO()
+ buffer = BytesIO()
for p in frags:
self.assertEqual(p[IPv6].src, src)
self.assertEqual(p[IPv6].dst, dst)
buffer.seek(p[IPv6ExtHdrFragment].offset * 8)
- buffer.write(p[IPv6ExtHdrFragment].payload)
+ buffer.write(bytes(p[IPv6ExtHdrFragment].payload))
ip = IPv6(src=frags[0][IPv6].src, dst=frags[0][IPv6].dst,
nh=frags[0][IPv6ExtHdrFragment].nh)
if ip.nh == IP_PROTOS.tcp:
diff --git a/test/test_p2p_ethernet.py b/test/test_p2p_ethernet.py
index f055ffeb521..f08d0cc2b1f 100644
--- a/test/test_p2p_ethernet.py
+++ b/test/test_p2p_ethernet.py
@@ -13,7 +13,7 @@ from framework import VppTestCase, VppTestRunner
from vpp_sub_interface import VppP2PSubint
from vpp_ip import DpoProto
from vpp_ip_route import VppIpRoute, VppRoutePath
-from util import mactobinary
+from vpp_mac import mactobinary
class P2PEthernetAPI(VppTestCase):
diff --git a/test/test_pppoe.py b/test/test_pppoe.py
index d69058f1aeb..615b7a0c880 100644
--- a/test/test_pppoe.py
+++ b/test/test_pppoe.py
@@ -14,8 +14,8 @@ from scapy.layers.ppp import PPPoE, PPPoED, PPP
from scapy.layers.inet import IP, UDP
from scapy.layers.inet6 import IPv6
from scapy.volatile import RandMAC, RandIP
-
-from util import ppp, ppc, mactobinary
+from vpp_mac import mactobinary
+from util import ppp, ppc
import socket
diff --git a/test/test_util.py b/test/test_util.py
new file mode 100755
index 00000000000..49095d85931
--- /dev/null
+++ b/test/test_util.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+"""Test framework utilitty functions tests"""
+
+import unittest
+from framework import VppTestCase, VppTestRunner
+from vpp_mac import mactobinary, binarytomac
+
+
+class TestUtil (VppTestCase):
+ """ MAC to binary and back """
+ def test_mac_to_binary(self):
+ mac = 'aa:bb:cc:dd:ee:ff'
+ b = mactobinary(mac)
+ mac2 = binarytomac(b)
+ self.assertEqual(type(mac), type(mac2))
+ self.assertEqual(mac2, mac)
+
+if __name__ == '__main__':
+ unittest.main(testRunner=VppTestRunner)
diff --git a/test/test_vxlan.py b/test/test_vxlan.py
index 3c824b5761a..34bf11a53e4 100644
--- a/test/test_vxlan.py
+++ b/test/test_vxlan.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python
import socket
-from util import ip4n_range, ip4_range
+from util import ip4n_range, ip4_range, reassemble4
import unittest
from framework import VppTestCase, VppTestRunner
from template_bd import BridgeDomain
@@ -11,22 +11,6 @@ from scapy.layers.inet import IP, UDP
from scapy.layers.vxlan import VXLAN
from scapy.utils import atol
-import StringIO
-
-
-def reassemble(listoffragments):
- buffer = StringIO.StringIO()
- first = listoffragments[0]
- buffer.seek(20)
- for pkt in listoffragments:
- buffer.seek(pkt[IP].frag*8)
- buffer.write(pkt[IP].payload)
- first.len = len(buffer.getvalue()) + 20
- first.flags = 0
- del(first.chksum)
- header = str(first[IP])[:20]
- return first[IP].__class__(header + buffer.getvalue())
-
class TestVxlan(BridgeDomain, VppTestCase):
""" VXLAN Test Case """
@@ -259,7 +243,7 @@ class TestVxlan(BridgeDomain, VppTestCase):
# Pick first received frame and check if it's correctly encapsulated.
out = self.pg0.get_capture(2)
ether = out[0]
- pkt = reassemble(out)
+ pkt = reassemble4(out)
pkt = ether / pkt
self.check_encapsulation(pkt, self.single_tunnel_bd)
diff --git a/test/test_vxlan_gbp.py b/test/test_vxlan_gbp.py
index 919ecbc6203..ee106a5769f 100644
--- a/test/test_vxlan_gbp.py
+++ b/test/test_vxlan_gbp.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python
import socket
-from util import ip4_range
+from util import ip4_range, reassemble4_ether
import unittest
from framework import VppTestCase, VppTestRunner
from template_bd import BridgeDomain
@@ -12,22 +12,6 @@ from scapy.layers.inet import IP, UDP
from scapy.layers.vxlan import VXLAN
from scapy.utils import atol
-import StringIO
-
-
-def reassemble(listoffragments):
- buffer = StringIO.StringIO()
- first = listoffragments[0]
- buffer.seek(20)
- for pkt in listoffragments:
- buffer.seek(pkt[IP].frag*8)
- buffer.write(pkt[IP].payload)
- first.len = len(buffer.getvalue()) + 20
- first.flags = 0
- del(first.chksum)
- header = str(first[Ether])[:34]
- return first[Ether].__class__(header + buffer.getvalue())
-
class TestVxlanGbp(VppTestCase):
""" VXLAN GBP Test Case """
@@ -258,7 +242,7 @@ class TestVxlanGbp(VppTestCase):
# Pick first received frame and check if it's correctly encapsulated.
out = self.pg0.get_capture(2)
- pkt = reassemble(out)
+ pkt = reassemble4_ether(out)
self.check_encapsulation(pkt, self.single_tunnel_bd)
payload = self.decapsulate(pkt)
diff --git a/test/util.py b/test/util.py
index 1ab5c1f2350..a3ec6e3326b 100644
--- a/test/util.py
+++ b/test/util.py
@@ -4,7 +4,6 @@ import socket
import sys
import os.path
from abc import abstractmethod, ABCMeta
-from cStringIO import StringIO
from scapy.utils6 import in6_mactoifaceid
from scapy.layers.l2 import Ether
@@ -14,11 +13,13 @@ from scapy.layers.inet6 import IPv6, IPv6ExtHdrFragment, IPv6ExtHdrRouting,\
IPv6ExtHdrHopByHop
from scapy.utils import hexdump
from socket import AF_INET6
+from io import BytesIO
+from vpp_mac import mactobinary
def ppp(headline, packet):
""" Return string containing the output of scapy packet.show() call. """
- o = StringIO()
+ o = BytesIO()
old_stdout = sys.stdout
sys.stdout = o
print(headline)
@@ -58,11 +59,6 @@ def ip4n_range(ip4n, s, e):
for ip in ip4_range(ip4, s, e))
-def mactobinary(mac):
- """ Convert the : separated format into binary packet data for the API """
- return mac.replace(':', '').decode('hex')
-
-
def mk_ll_addr(mac):
euid = in6_mactoifaceid(mac)
addr = "fe80::" + euid
@@ -441,3 +437,29 @@ def fragment_rfc8200(packet, identification, fragsize, _logger=None):
pkts[-1][IPv6ExtHdrFragment].m = 0 # reset more-flags in last fragment
return pkts
+
+
+def reassemble4_core(listoffragments, return_ip):
+ buffer = BytesIO()
+ first = listoffragments[0]
+ buffer.seek(20)
+ for pkt in listoffragments:
+ buffer.seek(pkt[IP].frag*8)
+ buffer.write(bytes(pkt[IP].payload))
+ first.len = len(buffer.getvalue()) + 20
+ first.flags = 0
+ del(first.chksum)
+ if return_ip:
+ header = bytes(first[IP])[:20]
+ return first[IP].__class__(header + buffer.getvalue())
+ else:
+ header = bytes(first[Ether])[:34]
+ return first[Ether].__class__(header + buffer.getvalue())
+
+
+def reassemble4_ether(listoffragments):
+ return reassemble4_core(listoffragments, False)
+
+
+def reassemble4(listoffragments):
+ return reassemble4_core(listoffragments, True)
diff --git a/test/vpp_interface.py b/test/vpp_interface.py
index a464bf38984..3235d3f68c6 100644
--- a/test/vpp_interface.py
+++ b/test/vpp_interface.py
@@ -4,7 +4,8 @@ from abc import abstractmethod, ABCMeta
from six import moves
-from util import Host, mk_ll_addr, mactobinary
+from util import Host, mk_ll_addr
+from vpp_mac import mactobinary, binarytomac
class VppInterface(object):
@@ -231,10 +232,9 @@ class VppInterface(object):
r = self.test.vapi.sw_interface_dump()
for intf in r:
if intf.sw_if_index == self.sw_if_index:
- self._name = intf.interface_name.split(b'\0', 1)[0]
- self._local_mac = \
- ':'.join(binascii.hexlify(intf.l2_address)[i:i + 2]
- for i in range(0, 12, 2))
+ self._name = intf.interface_name.split(b'\0',
+ 1)[0].decode('utf8')
+ self._local_mac = binarytomac(intf.l2_address)
self._dump = intf
break
else:
@@ -274,7 +274,7 @@ class VppInterface(object):
:param vrf_id: The FIB table / VRF ID. (Default value = 0)
"""
for host in self._remote_hosts:
- macn = host.mac.replace(":", "").decode('hex')
+ macn = mactobinary(host.mac)
ipn = host.ip4n
self.test.vapi.ip_neighbor_add_del(
self.sw_if_index, macn, ipn)
@@ -305,7 +305,7 @@ class VppInterface(object):
:param vrf_id: The FIB table / VRF ID. (Default value = 0)
"""
for host in self._remote_hosts:
- macn = host.mac.replace(":", "").decode('hex')
+ macn = mactobinary(host.mac)
ipn = host.ip6n
self.test.vapi.ip_neighbor_add_del(
self.sw_if_index, macn, ipn, is_ipv6=1)
diff --git a/test/vpp_l2.py b/test/vpp_l2.py
index a6b43efe14c..e2f3760f1d1 100644
--- a/test/vpp_l2.py
+++ b/test/vpp_l2.py
@@ -4,9 +4,8 @@
"""
from vpp_object import *
-from util import mactobinary
from vpp_ip import VppIpAddress
-from vpp_mac import VppMacAddress
+from vpp_mac import VppMacAddress, mactobinary
from vpp_lo_interface import VppLoInterface
diff --git a/test/vpp_mac.py b/test/vpp_mac.py
index 787d77c58bd..b20bf54634c 100644
--- a/test/vpp_mac.py
+++ b/test/vpp_mac.py
@@ -2,8 +2,19 @@
MAC Types
"""
+import binascii
-from util import mactobinary
+
+def mactobinary(mac):
+ """ Convert the : separated format into binary packet data for the API """
+ return binascii.unhexlify(mac.replace(':', ''))
+
+
+def binarytomac(binary):
+ """ Convert binary packed data in a : separated string """
+ x = b':'.join(binascii.hexlify(binary)[i:i + 2]
+ for i in range(0, 12, 2))
+ return str(x.decode('ascii'))
class VppMacAddress():
diff --git a/test/vpp_neighbor.py b/test/vpp_neighbor.py
index b4803c24cbd..46854c9ba8c 100644
--- a/test/vpp_neighbor.py
+++ b/test/vpp_neighbor.py
@@ -6,7 +6,7 @@
from socket import inet_pton, inet_ntop, AF_INET, AF_INET6
from vpp_object import *
-from util import mactobinary
+from vpp_mac import mactobinary
def find_nbr(test, sw_if_index, ip_addr, is_static=0, inet=AF_INET, mac=None):
diff --git a/test/vpp_papi_provider.py b/test/vpp_papi_provider.py
index 612a678ae9d..26d5fcf721c 100644
--- a/test/vpp_papi_provider.py
+++ b/test/vpp_papi_provider.py
@@ -4,7 +4,7 @@ import time
from collections import deque
from six import moves
-
+from vpp_mac import mactobinary
from hook import Hook
from vpp_l2 import L2_PORT_TYPE
@@ -210,7 +210,7 @@ class VppPapiProvider(object):
"""
self.hook.before_cli(cli)
cli += '\n'
- r = self.papi.cli_inband(length=len(cli), cmd=cli)
+ r = self.papi.cli_inband(length=len(cli), cmd=str(cli).encode('utf8'))
self.hook.after_cli(cli)
if hasattr(r, 'reply'):
return r.reply.decode().rstrip('\x00')
@@ -224,7 +224,7 @@ class VppPapiProvider(object):
return cli + "\n" + str(self.cli(cli))
def _convert_mac(self, mac):
- return mac.replace(':', '').decode('hex')
+ return mactobinary(mac)
def show_version(self):
""" """
@@ -1037,7 +1037,6 @@ class VppPapiProvider(object):
:param is_static: (Default value = 0)
:param is_no_adj_fib: (Default value = 0)
"""
-
return self.api(
self.papi.ip_neighbor_add_del,
{'sw_if_index': sw_if_index,
diff --git a/test/vpp_pppoe_interface.py b/test/vpp_pppoe_interface.py
index 507d8251442..28d8a714972 100644
--- a/test/vpp_pppoe_interface.py
+++ b/test/vpp_pppoe_interface.py
@@ -1,7 +1,7 @@
from vpp_interface import VppInterface
import socket
-from util import mactobinary
+from vpp_mac import mactobinary
class VppPppoeInterface(VppInterface):