blob: ab90eeaa45c324f365534fe0cad2808a292ce49a [file] [log] [blame]
Ole Troan5f9dcff2016-08-01 04:59:13 +02001#!/usr/bin/env python
Ole Troan6855f6c2016-04-09 03:16:30 +02002
Ole Troan5f9dcff2016-08-01 04:59:13 +02003from __future__ import print_function
Ole Troan6855f6c2016-04-09 03:16:30 +02004import unittest, sys, time, threading, struct, logging
Ole Troan5f9dcff2016-08-01 04:59:13 +02005import test_base
6import vpp_papi
Ole Troan6855f6c2016-04-09 03:16:30 +02007from ipaddress import *
8
9papi_event = threading.Event()
Ole Troan5f9dcff2016-08-01 04:59:13 +020010print(vpp_papi.VL_API_SW_INTERFACE_SET_FLAGS)
Ole Troan6855f6c2016-04-09 03:16:30 +020011def papi_event_handler(result):
Ole Troan5f9dcff2016-08-01 04:59:13 +020012 if result.vl_msg_id == vpp_papi.vpe.VL_API_SW_INTERFACE_SET_FLAGS:
13 return
14 if result.vl_msg_id == vpp_papi.vpe.VL_API_VNET_INTERFACE_COUNTERS:
15 print('Interface counters', result)
16 return
17 if result.vl_msg_id == vpp_papi.vpe.VL_API_VNET_IP6_FIB_COUNTERS:
18 print('IPv6 FIB counters', result)
Ole Troan6855f6c2016-04-09 03:16:30 +020019 papi_event.set()
20 return
Ole Troan6855f6c2016-04-09 03:16:30 +020021
Ole Troane6749e42016-04-28 12:50:20 +020022 print('Unknown message id:', result.vl_msg_id)
Ole Troan6855f6c2016-04-09 03:16:30 +020023
Ole Troan5f9dcff2016-08-01 04:59:13 +020024import glob, subprocess
Ole Troan6855f6c2016-04-09 03:16:30 +020025class TestPAPI(unittest.TestCase):
Ole Troan5f9dcff2016-08-01 04:59:13 +020026 @classmethod
27 def setUpClass(cls):
28 #
29 # Start main VPP process
30 cls.vpp_bin = glob.glob(test_base.scriptdir+'/../../../build-root/install-vpp*-native/vpp/bin/vpp')[0]
31 print("VPP BIN:", cls.vpp_bin)
32 cls.vpp = subprocess.Popen([cls.vpp_bin, "unix", "nodaemon"], stderr=subprocess.PIPE)
33 print('Started VPP')
34 # For some reason unless we let VPP start up the API cannot connect.
35 time.sleep(0.3)
36 @classmethod
37 def tearDownClass(cls):
38 cls.vpp.terminate()
Ole Troan6855f6c2016-04-09 03:16:30 +020039
40 def setUp(self):
Ole Troan5f9dcff2016-08-01 04:59:13 +020041 print("Connecting API")
Ole Troan6855f6c2016-04-09 03:16:30 +020042 r = vpp_papi.connect("test_papi")
43 self.assertEqual(r, 0)
44
45 def tearDown(self):
46 r = vpp_papi.disconnect()
47 self.assertEqual(r, 0)
Ole Troan5f9dcff2016-08-01 04:59:13 +020048
49 #
50 # The tests themselves
51 #
52
53 #
54 # Basic request / reply
55 #
Ole Troan6855f6c2016-04-09 03:16:30 +020056 def test_show_version(self):
57 t = vpp_papi.show_version()
Ole Troan5f9dcff2016-08-01 04:59:13 +020058 print('T', t);
Ole Troan6855f6c2016-04-09 03:16:30 +020059 program = t.program.decode().rstrip('\x00')
60 self.assertEqual('vpe', program)
61
62 #
Ole Troan5f9dcff2016-08-01 04:59:13 +020063 # Details / Dump
Ole Troan6855f6c2016-04-09 03:16:30 +020064 #
Ole Troan5f9dcff2016-08-01 04:59:13 +020065 def test_details_dump(self):
66 t = vpp_papi.sw_interface_dump(0, b'')
67 print('Dump/details T', t)
Ole Troan6855f6c2016-04-09 03:16:30 +020068
Ole Troan5f9dcff2016-08-01 04:59:13 +020069 #
70 # Arrays
71 #
72 def test_arrays(self):
73 t = vpp_papi.vnet_get_summary_stats()
74 print('Summary stats', t)
75 print('Packets:', t.total_pkts[0])
76 print('Packets:', t.total_pkts[1])
77 #
78 # Variable sized arrays and counters
79 #
80 #@unittest.skip("stats")
Ole Troan6855f6c2016-04-09 03:16:30 +020081 def test_want_stats(self):
82 pid = 123
83 vpp_papi.register_event_callback(papi_event_handler)
84 papi_event.clear()
Ole Troan5f9dcff2016-08-01 04:59:13 +020085
86 # Need to configure IPv6 to get som IPv6 FIB stats
87 t = vpp_papi.create_loopback('')
88 print(t)
89 self.assertEqual(t.retval, 0)
90
91 ifindex = t.sw_if_index
92 addr = str(IPv6Address('1::1').packed)
93 t = vpp_papi.sw_interface_add_del_address(ifindex, 1, 1, 0, 16, addr)
94 print(t)
95 self.assertEqual(t.retval, 0)
96
97 # Check if interface is up
98 # XXX: Add new API to query interface state based on ifindex, instead of dump all.
99 t = vpp_papi.sw_interface_set_flags(ifindex, 1, 1, 0)
100 self.assertEqual(t.retval, 0)
101
Ole Troan6855f6c2016-04-09 03:16:30 +0200102 t = vpp_papi.want_stats(True, pid)
103
104 print (t)
105
106 #
107 # Wait for some stats
108 #
Ole Troan5f9dcff2016-08-01 04:59:13 +0200109 self.assertEqual(papi_event.wait(15), True)
Ole Troan6855f6c2016-04-09 03:16:30 +0200110 t = vpp_papi.want_stats(False, pid)
111 print (t)
112
Ole Troan6855f6c2016-04-09 03:16:30 +0200113
Ole Troan5f9dcff2016-08-01 04:59:13 +0200114 #
115 # Plugins?
116 #
Ole Troan6855f6c2016-04-09 03:16:30 +0200117
118if __name__ == '__main__':
119 #logging.basicConfig(level=logging.DEBUG)
120 unittest.main()
Ole Troan5f9dcff2016-08-01 04:59:13 +0200121def test_papi():
122 print('test')