summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorOle Troan <ot@cisco.com>2018-06-12 21:06:44 +0200
committerFlorin Coras <florin.coras@gmail.com>2018-06-22 03:00:45 +0000
commita7564e8004fd6d9a63eb0605f752f27a71403645 (patch)
tree15a97b1b7852dfbdee28c56e0d58218689126c1b /test
parentd600ffe13a0f8f5d11fdea6ab7b24a364c968347 (diff)
Python API: Add enum and union support.
As well as a rewrite of the encoders/decoders to make it more readable and extensible. (Re-commit after fix to verify build.) Change-Id: Ic244d3cebe070bb2570491f8a24f4a1e203f889a Signed-off-by: Ole Troan <ot@cisco.com>
Diffstat (limited to 'test')
-rw-r--r--test/test_papi.py486
-rw-r--r--test/vpp_papi_provider.py2
2 files changed, 1 insertions, 487 deletions
diff --git a/test/test_papi.py b/test/test_papi.py
deleted file mode 100644
index e28352f9b9b..00000000000
--- a/test/test_papi.py
+++ /dev/null
@@ -1,486 +0,0 @@
-import binascii
-from framework import VppTestCase
-from vpp_papi import VPP
-import json
-
-""" TestPAPI is a subclass of VPPTestCase classes.
-
-Basic test for sanity check of the Python API binding.
-
-"""
-
-
-class TestPAPI(VppTestCase):
- """ PAPI Test Case """
-
- @classmethod
- def setUpClass(cls):
- super(TestPAPI, cls).setUpClass()
- cls.v = cls.vapi.papi
-
- def test_show_version(self):
- """ show version """
- rv = self.v.show_version()
- self.assertEqual(rv.retval, 0)
-
- def test_show_version_invalid_param(self):
- """ show version - invalid parameters"""
- self.assertRaises(ValueError, self.v.show_version, foobar='foo')
-
- def test_u8_array(self):
- """ u8 array """
- rv = self.v.get_node_index(node_name='ip4-lookup')
- self.assertEqual(rv.retval, 0)
- node_name = 'X' * 100
- self.assertRaises(ValueError, self.v.get_node_index,
- node_name=node_name)
-
-
-class TestPAPIMessageParsing(VppTestCase):
- """ PAPI Message parsing Test Case """
-
- show_version_msg = '''["show_version",
- ["u16", "_vl_msg_id"],
- ["u32", "client_index"],
- ["u32", "context"],
- {"crc" : "0xf18f9480"}
- ]'''
-
- ip_address_details_msg = '''["ip_address_details",
- ["u16", "_vl_msg_id"],
- ["u32", "client_index"],
- ["u32", "context"],
- ["u8", "ip", 16],
- ["u8", "prefix_length"],
- {"crc" : "0x87d522a1"}
- ]'''
-
- cli_inband_msg = '''["cli_inband",
- ["u16", "_vl_msg_id"],
- ["u32", "client_index"],
- ["u32", "context"],
- ["u32", "length"],
- ["u8", "cmd", 0, "length"],
- {"crc" : "0x22345937"}
- ]'''
-
- def test_adding_new_message_object(self):
- """ Add new message object """
- p = json.loads(TestPAPIMessageParsing.show_version_msg)
- msglist = VPP(testmode=json)
- msgdef = msglist.add_message(p[0], p[1:])
-
- # Verify that message can be retrieved
- self.assertTrue(msglist['show_version'])
- self.assertFalse(msglist['foobar'])
-
- # Test duplicate
- self.assertRaises(ValueError, msglist.add_message, p[0], p[1:])
-
- # Look at return tuple
- self.assertTrue(msglist.ret_tup('show_version'))
-
- def test_adding_new_message_object_with_array(self):
- """ New message with array """
- p = json.loads(TestPAPIMessageParsing.ip_address_details_msg)
- msglist = VPP(testmode=True)
- msglist.add_message(p[0], p[1:])
-
- self.assertTrue(msglist['ip_address_details'])
-
- def test_message_to_bytes(self):
- """ Message to byte encoding """
- msglist = VPP(testmode=True)
- p = json.loads(TestPAPIMessageParsing.show_version_msg)
- msgdef = msglist.add_message(p[0], p[1:])
-
- # Give me a byte string for given message and given arguments
-
- b = msglist.encode(msgdef, {'_vl_msg_id': 50, 'context': 123})
- self.assertEqual(10, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(rv._0, 50)
- self.assertEqual(rv.context, 123)
-
- p = json.loads(TestPAPIMessageParsing.ip_address_details_msg)
- msgdef = msglist.add_message(p[0], p[1:])
-
- # Give me a byte string for given message and given arguments
- b = msglist.encode(msgdef, {'_vl_msg_id': 50, 'context': 123,
- 'ip': b'\xf0\xf1\xf2',
- 'prefix_length': 12})
- self.assertEqual(27, len(b))
- rv = msglist.decode(msgdef, b)
-
- self.assertEqual(rv.context, 123)
- self.assertEqual(rv.ip, b'\xf0\xf1\xf2\x00\x00\x00' +
- '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
- self.assertEqual(rv.prefix_length, 12)
-
- p = json.loads(TestPAPIMessageParsing.cli_inband_msg)
- msgdef = msglist.add_message(p[0], p[1:])
-
- # Give me a byte string for given message and given arguments
- b = msglist.encode(msgdef, {'_vl_msg_id': 50, 'context': 123,
- 'length': 20,
- 'cmd': 'show version verbose'})
- self.assertEqual(34, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(rv._0, 50)
- self.assertEqual(rv.context, 123)
- self.assertEqual(rv.cmd.decode('ascii'), 'show version verbose')
-
- variable_array_16_msg = '''["variable_array_16",
- ["u32", "length"],
- ["u16", "list", 0, "length"]
- ]'''
-
- p = json.loads(variable_array_16_msg)
- msgdef = msglist.add_message(p[0], p[1:])
-
- # Give me a byte string for given message and given arguments
- b = msglist.encode(msgdef, {'list': [1, 2], 'length': 2})
- self.assertEqual(8, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(2, rv.length)
- self.assertEqual([1, 2], rv.list)
-
- def test_add_new_types(self):
- """ Add new types """
- counter_type = '''["ip4_fib_counter",
- ["u32", "address"],
- ["u8", "address_length"],
- ["u64", "packets"],
- ["u64", "bytes"],
- {"crc" : "0xb2739495"}
- ]'''
-
- with_type_msg = '''["with_type_msg",
- ["u32", "length"],
- ["u16", "list", 0, "length"],
- ["vl_api_ip4_fib_counter_t", "counter"]
- ]'''
-
- # Add new type
- msglist = VPP(testmode=True)
- p = json.loads(counter_type)
- msglist.add_type(p[0], p[1:])
- p = json.loads(with_type_msg)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2],
- 'counter': {'address': 4,
- 'address_length': 12,
- 'packets': 1235,
- 'bytes': 5678}})
- self.assertEqual(29, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(2, rv.length)
- self.assertEqual(5678, rv.counter.bytes)
-
- def test_add_two_new_types(self):
- """ Add new types 2 """
- mock_r1 = '''["mock_r1",
- ["u32", "a1"],
- {"crc" : "0xb2739495"}
- ]'''
- mock_r2 = '''["mock_r2",
- ["u32", "a1"],
- {"crc" : "0xb2739495"}
- ]'''
-
- mock_msg = '''["mock_msg",
- ["u32", "context"],
- ["i32", "retval"],
- ["vl_api_mock_r1_t", "r1"],
- ["vl_api_mock_r2_t", "r2"],
- {"crc" : "0xb2739495"}
- ]'''
-
- # Add new type
- msglist = VPP(testmode=True)
- p = json.loads(mock_r1)
- msglist.add_type(p[0], p[1:])
- p = json.loads(mock_r2)
- msglist.add_type(p[0], p[1:])
- p = json.loads(mock_msg)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'context': 2, 'retval': 0,
- 'r1': {'a1': 4}, 'r2': {'a1': 12}})
-
- self.assertEqual(16, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(4, rv.r1.a1)
-
- def test_nested_array_type(self):
- """ Nested array type """
- bier_type = '''["bier_table_id",
- ["u8", "bt_set"],
- ["u8", "bt_sub_domain"],
- ["u8", "bt_hdr_len_id"],
- {"crc" : "0xb2739495"}
- ]'''
- fib_path3 = '''["fib_path3",
- ["u32", "sw_if_index"],
- ["u8", "n_labels"],
- ["u32", "label_stack", 0, "n_labels"],
- {"crc" : "0xb2739495"}
- ]'''
-
- bier_route_details = '''["bier_route_details",
- ["u32", "client_index"],
- ["vl_api_bier_table_id_t", "br_tbl_id"],
- ["u32", "br_n_paths"],
- ["vl_api_fib_path3_t", "br_paths", 0, "br_n_paths"],
- {"crc" : "0xb2739495"}
- ]'''
-
- # Add new type
- msglist = VPP(testmode=True)
-
- p = json.loads(bier_type)
- msglist.add_type(p[0], p[1:])
- p = json.loads(fib_path3)
- msglist.add_type(p[0], p[1:])
-
- p = json.loads(bier_route_details)
- msgdef = msglist.add_message(p[0], p[1:])
-
- bt_tbl_id = {'bt_set': 1, 'bt_sub_domain': 2, 'bt_hdr_len_id': 3}
- fib_path = {'sw_if_index': 1, 'n_labels': 2,
- 'label_stack': [123, 456]}
-
- b = msglist.encode(msgdef, {'client_index': 2,
- 'br_tbl_id': bt_tbl_id,
- 'br_n_paths': 2,
- 'br_paths': [fib_path, fib_path]})
- self.assertEqual(37, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual([123, 456], rv.br_paths[1].label_stack)
- self.assertEqual(bt_tbl_id['bt_set'], rv.br_tbl_id.bt_set)
-
- def test_add_new_compound_type_with_array(self):
- """ New compound type with array """
- counter_type = '''["ip4_fib_counter",
- ["u32", "address"],
- ["u8", "address_length"],
- ["u64", "packets"],
- ["u64", "bytes"],
- {"crc" : "0xb2739495"}
- ]'''
-
- with_type_msg = '''["with_type_msg",
- ["u32", "length"],
- ["u16", "list", 0, "length"],
- ["vl_api_ip4_fib_counter_t", "counter", 2]
-
- ]'''
-
- # Add new type
- msglist = VPP(testmode=True)
- p = json.loads(counter_type)
- msglist.add_type(p[0], p[1:])
- p = json.loads(with_type_msg)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2],
- 'counter': [{'address': 4,
- 'address_length': 12,
- 'packets': 1235,
- 'bytes': 5678},
- {'address': 111,
- 'address_length': 222,
- 'packets': 333,
- 'bytes': 444}]})
- self.assertEqual(50, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual([1, 2], rv.list)
- self.assertEqual(1235, rv.counter[0].packets)
-
- with_type_variable_msg = '''["with_type_variable_msg",
- ["u32", "length"],
- ["vl_api_ip4_fib_counter_t", "counter", 0, "length"]
-
- ]'''
-
- p = json.loads(with_type_variable_msg)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'length': 2,
- 'counter': [{'address': 4,
- 'address_length': 12,
- 'packets': 1235,
- 'bytes': 5678},
- {'address': 111,
- 'address_length': 222,
- 'packets': 333,
- 'bytes': 444}]})
- self.assertEqual(46, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(2, rv.length)
- self.assertEqual(1235, rv.counter[0].packets)
- self.assertEqual(333, rv.counter[1].packets)
-
- def test_simple_array(self):
- """ Simple array """
- msglist = VPP(testmode=True)
-
- simple_byte_array = '''["simple_byte_array",
- ["u32", "length"],
- ["u8", "namecommand", 64]
-
- ]'''
- p = json.loads(simple_byte_array)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'length': 2, 'namecommand': 'foobar'})
- self.assertEqual(68, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(2, rv.length)
-
- simple_array = '''["simple_array",
- ["u32", "length"],
- ["u32", "list", 2]
-
- ]'''
- p = json.loads(simple_array)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2]})
- self.assertEqual(12, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(2, rv.length)
- self.assertEqual([1, 2], rv.list)
-
- simple_variable_array = '''["simple_variable_array",
- ["u32", "length"],
- ["u32", "list", 0, "length"]
-
- ]'''
- p = json.loads(simple_variable_array)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2]})
- self.assertEqual(12, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(2, rv.length)
- self.assertEqual([1, 2], rv.list)
-
- simple_variable_byte_array = '''["simple_variable_byte_array",
- ["u32", "length"],
- ["u8", "list", 0, "length"]
- ]'''
- p = json.loads(simple_variable_byte_array)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'length': 6, 'list': 'foobar'})
- self.assertEqual(10, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(6, rv.length)
- self.assertEqual('foobar', rv.list)
-
- def test_old_vla_array(self):
- """ Old style VLA array """
- msglist = VPP(testmode=True)
-
- # VLA
- vla_byte_array = '''["vla_byte_array",
- ["u32", "foobar"],
- ["u32", "list", 2],
- ["u32", "propercount"],
- ["u8", "propermask", 0, "propercount"],
- ["u8", "oldmask", 0],
- {"crc" : "0xb2739495"}
- ]'''
- p = json.loads(vla_byte_array)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'list': [123, 456], 'oldmask': b'foobar',
- 'propercount': 2,
- 'propermask': [8, 9]})
- self.assertEqual(24, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(b'foobar', rv.oldmask)
-
- def test_old_vla_array_not_last_member(self):
- """ Old VLA array arbitrary placement """
- msglist = VPP(testmode=True)
-
- # VLA
- vla_byte_array = '''["vla_byte_array",
- ["u8", "oldmask", 0],
- ["u32", "foobar"],
- {"crc" : "0xb2739495"}
- ]'''
- p = json.loads(vla_byte_array)
- self.assertRaises(ValueError, msglist.add_message, p[0], p[1:])
-
- def test_old_vla_array_u32(self):
- """ Old VLA u32 """
- msglist = VPP(testmode=True)
-
- # VLA
- vla_byte_array = '''["vla_byte_array",
- ["u32", "foobar"],
- ["u32", "oldmask", 0],
- {"crc" : "0xb2739495"}
- ]'''
- p = json.loads(vla_byte_array)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'foobar': 123,
- 'oldmask': [123, 456, 789]})
- self.assertEqual(16, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual([123, 456, 789], rv.oldmask)
-
- def test_old_vla_array_compound(self):
- """ Old VLA compound type """
- msglist = VPP(testmode=True)
-
- # VLA
- counter_type = '''["ip4_fib_counter",
- ["u32", "address"],
- ["u8", "address_length"],
- ["u64", "packets"],
- ["u64", "bytes"],
- {"crc" : "0xb2739495"}
- ]'''
-
- vla_byte_array = '''["vla_byte_array",
- ["vl_api_ip4_fib_counter_t", "counter", 0],
- {"crc" : "0xb2739495"}
- ]'''
-
- p = json.loads(counter_type)
- msglist.add_type(p[0], p[1:])
-
- p = json.loads(vla_byte_array)
- with self.assertRaises(NotImplementedError):
- msgdef = msglist.add_message(p[0], p[1:])
-
- def test_array_count_not_previous(self):
- """ VLA with aribtrary length field placement """
- msglist = VPP(testmode=True)
-
- # VLA
- vla_byte_array = '''["vla_byte_array",
- ["u32", "count"],
- ["u32", "filler"],
- ["u32", "lst", 0, "count"],
- {"crc" : "0xb2739495"}
- ]'''
-
- p = json.loads(vla_byte_array)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'count': 3, 'lst': [1, 2, 3],
- 'filler': 1})
- rv = msglist.decode(msgdef, b)
- self.assertEqual(rv.lst, [1, 2, 3])
-
- def test_argument_name(self):
- """ Argument name """
- msglist = VPP(testmode=True)
-
- simple_name = '''["simple_name",
- ["u32", "length"],
- ["u8", "name"]
- ]'''
- p = json.loads(simple_name)
- msgdef = msglist.add_message(p[0], p[1:])
- b = msglist.encode(msgdef, {'length': 6, 'name': 1})
- self.assertEqual(5, len(b))
- rv = msglist.decode(msgdef, b)
- self.assertEqual(6, rv.length)
- self.assertEqual(1, rv.name)
diff --git a/test/vpp_papi_provider.py b/test/vpp_papi_provider.py
index 29f07de459e..32c8eebef01 100644
--- a/test/vpp_papi_provider.py
+++ b/test/vpp_papi_provider.py
@@ -70,7 +70,7 @@ class VppPapiProvider(object):
for filename in fnmatch.filter(filenames, '*.api.json'):
jsonfiles.append(os.path.join(root, filename))
- self.vpp = VPP(jsonfiles, logger=test_class.logger)
+ self.vpp = VPP(jsonfiles, logger=test_class.logger, read_timeout=5)
self._events = deque()
def __enter__(self):