blob: 8cbbfc59e03694e067b3b2d83d4a62eb42f3505d [file] [log] [blame]
Ole Troan5f9dcff2016-08-01 04:59:13 +02001from __future__ import print_function
Ole Troan57c3d662016-09-12 22:00:32 +02002import unittest, sys, time, threading, struct, logging, os
Ole Troan5f9dcff2016-08-01 04:59:13 +02003import vpp_papi
Ole Troan6855f6c2016-04-09 03:16:30 +02004from ipaddress import *
Ole Troan57c3d662016-09-12 22:00:32 +02005scriptdir = os.path.dirname(os.path.realpath(__file__))
Ole Troan6855f6c2016-04-09 03:16:30 +02006papi_event = threading.Event()
Ole Troan57c3d662016-09-12 22:00:32 +02007print(vpp_papi.vpe.VL_API_SW_INTERFACE_SET_FLAGS)
Ole Troan6855f6c2016-04-09 03:16:30 +02008def papi_event_handler(result):
Ole Troan5f9dcff2016-08-01 04:59:13 +02009 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 Troan6855f6c2016-04-09 03:16:30 +020016 papi_event.set()
17 return
Ole Troan6855f6c2016-04-09 03:16:30 +020018
Ole Troane6749e42016-04-28 12:50:20 +020019 print('Unknown message id:', result.vl_msg_id)
Ole Troan6855f6c2016-04-09 03:16:30 +020020
Ole Troan5f9dcff2016-08-01 04:59:13 +020021import glob, subprocess
Ole Troan6855f6c2016-04-09 03:16:30 +020022class TestPAPI(unittest.TestCase):
Ole Troan5f9dcff2016-08-01 04:59:13 +020023 @classmethod
24 def setUpClass(cls):
25 #
26 # Start main VPP process
Ole Troan57c3d662016-09-12 22:00:32 +020027 cls.vpp_bin = glob.glob(scriptdir+'/../../../build-root/install-vpp*-native/vpp/bin/vpp')[0]
Ole Troan5f9dcff2016-08-01 04:59:13 +020028 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 Troan6855f6c2016-04-09 03:16:30 +020036
37 def setUp(self):
Ole Troan5f9dcff2016-08-01 04:59:13 +020038 print("Connecting API")
Ole Troan6855f6c2016-04-09 03:16:30 +020039 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 Troan5f9dcff2016-08-01 04:59:13 +020045
46 #
47 # The tests themselves
48 #
49
50 #
51 # Basic request / reply
52 #
Ole Troan6855f6c2016-04-09 03:16:30 +020053 def test_show_version(self):
54 t = vpp_papi.show_version()
Ole Troan5f9dcff2016-08-01 04:59:13 +020055 print('T', t);
Ole Troan6855f6c2016-04-09 03:16:30 +020056 program = t.program.decode().rstrip('\x00')
57 self.assertEqual('vpe', program)
58
59 #
Ole Troan5f9dcff2016-08-01 04:59:13 +020060 # Details / Dump
Ole Troan6855f6c2016-04-09 03:16:30 +020061 #
Ole Troan5f9dcff2016-08-01 04:59:13 +020062 def test_details_dump(self):
63 t = vpp_papi.sw_interface_dump(0, b'')
64 print('Dump/details T', t)
Ole Troan6855f6c2016-04-09 03:16:30 +020065
Ole Troan5f9dcff2016-08-01 04:59:13 +020066 #
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 Troan6855f6c2016-04-09 03:16:30 +020078 def test_want_stats(self):
79 pid = 123
80 vpp_papi.register_event_callback(papi_event_handler)
81 papi_event.clear()
Ole Troan5f9dcff2016-08-01 04:59:13 +020082
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 Troan57c3d662016-09-12 22:00:32 +020089 addr = str(IPv6Address(u'1::1').packed)
Ole Troan5f9dcff2016-08-01 04:59:13 +020090 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 Troan6855f6c2016-04-09 03:16:30 +020099 t = vpp_papi.want_stats(True, pid)
100
101 print (t)
102
103 #
104 # Wait for some stats
105 #
Ole Troan5f9dcff2016-08-01 04:59:13 +0200106 self.assertEqual(papi_event.wait(15), True)
Ole Troan6855f6c2016-04-09 03:16:30 +0200107 t = vpp_papi.want_stats(False, pid)
108 print (t)
109
Ole Troan6855f6c2016-04-09 03:16:30 +0200110
Ole Troan5f9dcff2016-08-01 04:59:13 +0200111 #
112 # Plugins?
113 #
Ole Troan6855f6c2016-04-09 03:16:30 +0200114
115if __name__ == '__main__':
116 #logging.basicConfig(level=logging.DEBUG)
117 unittest.main()
Ole Troan5f9dcff2016-08-01 04:59:13 +0200118def test_papi():
119 print('test')