Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 1 | from __future__ import print_function |
Ole Troan | 57c3d66 | 2016-09-12 22:00:32 +0200 | [diff] [blame] | 2 | import unittest, sys, time, threading, struct, logging, os |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 3 | import vpp_papi |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 4 | from ipaddress import * |
Ole Troan | 57c3d66 | 2016-09-12 22:00:32 +0200 | [diff] [blame] | 5 | scriptdir = os.path.dirname(os.path.realpath(__file__)) |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 6 | papi_event = threading.Event() |
Ole Troan | 57c3d66 | 2016-09-12 22:00:32 +0200 | [diff] [blame] | 7 | print(vpp_papi.vpe.VL_API_SW_INTERFACE_SET_FLAGS) |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 8 | def papi_event_handler(result): |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 9 | if result.vl_msg_id == vpp_papi.vpe.VL_API_SW_INTERFACE_SET_FLAGS: |
| 10 | return |
| 11 | if result.vl_msg_id == vpp_papi.vpe.VL_API_VNET_INTERFACE_COUNTERS: |
| 12 | print('Interface counters', result) |
| 13 | return |
| 14 | if result.vl_msg_id == vpp_papi.vpe.VL_API_VNET_IP6_FIB_COUNTERS: |
| 15 | print('IPv6 FIB counters', result) |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 16 | papi_event.set() |
| 17 | return |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 18 | |
Ole Troan | e6749e4 | 2016-04-28 12:50:20 +0200 | [diff] [blame] | 19 | print('Unknown message id:', result.vl_msg_id) |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 20 | |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 21 | import glob, subprocess |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 22 | class TestPAPI(unittest.TestCase): |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 23 | @classmethod |
| 24 | def setUpClass(cls): |
| 25 | # |
| 26 | # Start main VPP process |
Ole Troan | 57c3d66 | 2016-09-12 22:00:32 +0200 | [diff] [blame] | 27 | cls.vpp_bin = glob.glob(scriptdir+'/../../../build-root/install-vpp*-native/vpp/bin/vpp')[0] |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 28 | print("VPP BIN:", cls.vpp_bin) |
| 29 | cls.vpp = subprocess.Popen([cls.vpp_bin, "unix", "nodaemon"], stderr=subprocess.PIPE) |
| 30 | print('Started VPP') |
| 31 | # For some reason unless we let VPP start up the API cannot connect. |
| 32 | time.sleep(0.3) |
| 33 | @classmethod |
| 34 | def tearDownClass(cls): |
| 35 | cls.vpp.terminate() |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 36 | |
| 37 | def setUp(self): |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 38 | print("Connecting API") |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 39 | r = vpp_papi.connect("test_papi") |
| 40 | self.assertEqual(r, 0) |
| 41 | |
| 42 | def tearDown(self): |
| 43 | r = vpp_papi.disconnect() |
| 44 | self.assertEqual(r, 0) |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 45 | |
| 46 | # |
| 47 | # The tests themselves |
| 48 | # |
| 49 | |
| 50 | # |
| 51 | # Basic request / reply |
| 52 | # |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 53 | def test_show_version(self): |
| 54 | t = vpp_papi.show_version() |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 55 | print('T', t); |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 56 | program = t.program.decode().rstrip('\x00') |
| 57 | self.assertEqual('vpe', program) |
| 58 | |
| 59 | # |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 60 | # Details / Dump |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 61 | # |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 62 | def test_details_dump(self): |
| 63 | t = vpp_papi.sw_interface_dump(0, b'') |
| 64 | print('Dump/details T', t) |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 65 | |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 66 | # |
| 67 | # Arrays |
| 68 | # |
| 69 | def test_arrays(self): |
| 70 | t = vpp_papi.vnet_get_summary_stats() |
| 71 | print('Summary stats', t) |
| 72 | print('Packets:', t.total_pkts[0]) |
| 73 | print('Packets:', t.total_pkts[1]) |
| 74 | # |
| 75 | # Variable sized arrays and counters |
| 76 | # |
| 77 | #@unittest.skip("stats") |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 78 | def test_want_stats(self): |
| 79 | pid = 123 |
| 80 | vpp_papi.register_event_callback(papi_event_handler) |
| 81 | papi_event.clear() |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 82 | |
| 83 | # Need to configure IPv6 to get som IPv6 FIB stats |
| 84 | t = vpp_papi.create_loopback('') |
| 85 | print(t) |
| 86 | self.assertEqual(t.retval, 0) |
| 87 | |
| 88 | ifindex = t.sw_if_index |
Ole Troan | 57c3d66 | 2016-09-12 22:00:32 +0200 | [diff] [blame] | 89 | addr = str(IPv6Address(u'1::1').packed) |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 90 | t = vpp_papi.sw_interface_add_del_address(ifindex, 1, 1, 0, 16, addr) |
| 91 | print(t) |
| 92 | self.assertEqual(t.retval, 0) |
| 93 | |
| 94 | # Check if interface is up |
| 95 | # XXX: Add new API to query interface state based on ifindex, instead of dump all. |
| 96 | t = vpp_papi.sw_interface_set_flags(ifindex, 1, 1, 0) |
| 97 | self.assertEqual(t.retval, 0) |
| 98 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 99 | t = vpp_papi.want_stats(True, pid) |
| 100 | |
| 101 | print (t) |
| 102 | |
| 103 | # |
| 104 | # Wait for some stats |
| 105 | # |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 106 | self.assertEqual(papi_event.wait(15), True) |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 107 | t = vpp_papi.want_stats(False, pid) |
| 108 | print (t) |
| 109 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 110 | |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 111 | # |
| 112 | # Plugins? |
| 113 | # |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 114 | |
| 115 | if __name__ == '__main__': |
| 116 | #logging.basicConfig(level=logging.DEBUG) |
| 117 | unittest.main() |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 118 | def test_papi(): |
| 119 | print('test') |