aboutsummaryrefslogtreecommitdiffstats
path: root/src/vpp-api/python/vpp_papi/tests
diff options
context:
space:
mode:
authorPaul Vinciguerra <pvinci@vinciconsulting.com>2019-06-07 13:35:37 -0400
committerOle Trøan <otroan@employees.org>2019-06-18 12:19:55 +0000
commitae8819f0a426953aa7ebf97c2e26940525b55fb1 (patch)
treeeba8fcf6fabc0b1ec1de5febc8c214030e1b74b1 /src/vpp-api/python/vpp_papi/tests
parent283cd2e9afcab1407d5614d79da4354790fa059a (diff)
misc: vpp_papi- add tests, clean up pep8
Type: test Change-Id: Ic81bd4a9eba3c89a746e7a9b9e471b59cd87fa40 Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
Diffstat (limited to 'src/vpp-api/python/vpp_papi/tests')
-rw-r--r--src/vpp-api/python/vpp_papi/tests/test_vpp_format.py130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/vpp-api/python/vpp_papi/tests/test_vpp_format.py b/src/vpp-api/python/vpp_papi/tests/test_vpp_format.py
new file mode 100644
index 00000000000..54b4846b15b
--- /dev/null
+++ b/src/vpp-api/python/vpp_papi/tests/test_vpp_format.py
@@ -0,0 +1,130 @@
+# Copyright (c) 2019. Vinci Consulting Corp. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import ipaddress
+import socket
+import unittest
+try:
+ text_type = unicode
+except NameError:
+ text_type = str
+
+from vpp_papi import vpp_format
+
+from parameterized import parameterized
+
+ip4_addr = '1.2.3.4'
+ip4_addrn = b'\x01\x02\x03\x04'
+ip4_prefix_len = 32
+ip4_prefix = '%s/%s' % (ip4_addr, ip4_prefix_len)
+ipv4_network = ipaddress.IPv4Network(text_type(ip4_prefix))
+ip4_addr_format_vl_api_address_t = {'un': {'ip4': b'\x01\x02\x03\x04'},
+ 'af': 0}
+ip4_addr_format_vl_api_prefix_t = {'prefix':
+ {'un': {'ip4': b'\x01\x02\x03\x04'},
+ 'af': 0},
+ 'len': ip4_prefix_len}
+ip4_addr_format_vl_api_prefix_packed_t = {'prefix': b'\x01\x02\x03\x04',
+ 'len': ip4_prefix_len}
+
+ip6_addr = 'dead::'
+ip6_addrn = b'\xde\xad\x00\x00\x00\x00\x00\x00' \
+ b'\x00\x00\x00\x00\x00\x00\x00\x00'
+ip6_prefix_len = 127
+ip6_prefix = '%s/%s' % (ip6_addr, ip6_prefix_len)
+ipv6_network = ipaddress.IPv6Network(text_type(ip6_prefix))
+ip6_addr_format_vl_api_address_t = {'un': {'ip6': b'\xde\xad\x00\x00'
+ b'\x00\x00\x00\x00'
+ b'\x00\x00\x00\x00'
+ b'\x00\x00\x00\x00'},
+ 'af': 1}
+ip6_addr_format_vl_api_prefix_t = {'prefix':
+ {'af': 1,
+ 'un': {
+ 'ip6': b'\xde\xad\x00\x00'
+ b'\x00\x00\x00\x00'
+ b'\x00\x00\x00\x00'
+ b'\x00\x00\x00\x00'}},
+ 'len': ip6_prefix_len}
+ip6_addr_format_vl_api_prefix_packed_t = {'prefix': b'\xde\xad\x00\x00'
+ b'\x00\x00\x00\x00'
+ b'\x00\x00\x00\x00'
+ b'\x00\x00\x00\x00',
+ 'len': ip6_prefix_len}
+
+
+class TestVppFormat(unittest.TestCase):
+
+ def test_format_vl_api_address_t(self):
+ res = vpp_format.format_vl_api_address_t(ip4_addr)
+ self.assertEqual(res, ip4_addr_format_vl_api_address_t)
+
+ # PY2: raises socket.error
+ # PY3: raises OSError
+ with self.assertRaises((TypeError,
+ socket.error,
+ OSError)):
+ res = vpp_format.format_vl_api_address_t(ip4_addrn)
+
+ res = vpp_format.format_vl_api_address_t(ip6_addr)
+ self.assertEqual(res, ip6_addr_format_vl_api_address_t)
+
+ with self.assertRaises(TypeError):
+ es = vpp_format.format_vl_api_address_t(ip6_addrn)
+
+ @parameterized.expand([('ip4 prefix',
+ ip4_prefix,
+ ip4_addr_format_vl_api_prefix_t),
+ ('ip6 prefix',
+ ip6_prefix,
+ ip6_addr_format_vl_api_prefix_t),
+ ('IPv4Network',
+ ipv4_network,
+ ip4_addr_format_vl_api_prefix_t),
+ ('IPv6Network',
+ ipv6_network,
+ ip6_addr_format_vl_api_prefix_t),
+ ])
+ def test_format_vl_api_prefix_t(self, _, arg, expected):
+
+ res = vpp_format.format_vl_api_prefix_t(arg)
+ self.assertEqual(res, expected)
+
+ def test_format_vl_api_ip6_prefix_t(self):
+ res = vpp_format.format_vl_api_ip6_prefix_t(ip6_prefix)
+ self.assertEqual(res, ip6_addr_format_vl_api_prefix_packed_t)
+
+ res = vpp_format.format_vl_api_ip6_prefix_t(ipv6_network)
+ self.assertEqual(res, ip6_addr_format_vl_api_prefix_packed_t)
+
+ def test_format_vl_api_ip4_prefix_t(self):
+ res = vpp_format.format_vl_api_ip4_prefix_t(ip4_prefix)
+ self.assertEqual(res, ip4_addr_format_vl_api_prefix_packed_t)
+
+ res = vpp_format.format_vl_api_ip4_prefix_t(ipv4_network)
+ self.assertEqual(res, ip4_addr_format_vl_api_prefix_packed_t)
+
+
+ def test_format_vl_api_ip6_prefix_t_raises(self):
+ # PY2: raises socket.error
+ # PY3: raises OSError
+ with self.assertRaises((socket.error, OSError)):
+ res = vpp_format.format_vl_api_ip6_prefix_t(ip4_prefix)
+
+ def test_format_vl_api_ip4_prefix_t_raises(self):
+ # PY2: raises socket.error
+ # PY3: raises OSError
+ with self.assertRaises((socket.error, OSError)):
+ res = vpp_format.format_vl_api_ip4_prefix_t(ip6_prefix)
+