blob: f45f791e627f36f0ec6d66e8d6f2abae9bf9cbb9 [file] [log] [blame]
Ole Troana03f4ef2016-12-02 12:53:55 +01001#!/usr/bin/env python
2
3from __future__ import print_function
4import unittest, sys, threading, struct, logging, os
5from vpp_papi import VPP
6from ipaddress import *
7import glob, json
8
9papi_event = threading.Event()
10import glob
11
12import fnmatch
13import os
14
15jsonfiles = []
16for root, dirnames, filenames in os.walk('../../../build-root/'):
17 if root.find('install-') == -1: continue
18 for filename in fnmatch.filter(filenames, '*.api.json'):
19 jsonfiles.append(os.path.join(root, filename))
20
21class TestPAPI(unittest.TestCase):
22 show_version_msg = '''["show_version",
23 ["u16", "_vl_msg_id"],
24 ["u32", "client_index"],
25 ["u32", "context"],
26 {"crc" : "0xf18f9480"}
27 ]'''
28
29 ip_address_details_msg = '''["ip_address_details",
30 ["u16", "_vl_msg_id"],
31 ["u32", "client_index"],
32 ["u32", "context"],
33 ["u8", "ip", 16],
34 ["u8", "prefix_length"],
35 {"crc" : "0x87d522a1"}
36 ]'''
37
38 cli_inband_msg = '''["cli_inband",
39 ["u16", "_vl_msg_id"],
40 ["u32", "client_index"],
41 ["u32", "context"],
42 ["u32", "length"],
43 ["u8", "cmd", 0, "length"],
44 {"crc" : "0x22345937"}
45 ]'''
46
47 def test_adding_new_message_object(self):
48 p = json.loads(TestPAPI.show_version_msg)
Ole Troanf5984bd2016-12-18 13:15:08 +010049 msglist = VPP(testmode=json)
Ole Troana03f4ef2016-12-02 12:53:55 +010050 msgdef = msglist.add_message(p[0], p[1:])
51
52 # Verify that message can be retrieved
53 self.assertTrue(msglist['show_version'])
54 self.assertFalse(msglist['foobar'])
55
56 # Test duplicate
57 self.assertRaises(ValueError, msglist.add_message, p[0], p[1:])
58
59 # Look at return tuple
60 self.assertTrue(msglist.ret_tup('show_version'))
61
62 def test_adding_new_message_object_with_array(self):
63 p = json.loads(TestPAPI.ip_address_details_msg)
Ole Troanf5984bd2016-12-18 13:15:08 +010064 msglist = VPP(testmode=True)
Ole Troana03f4ef2016-12-02 12:53:55 +010065 msglist.add_message(p[0], p[1:])
66
67 self.assertTrue(msglist['ip_address_details'])
68
69 def test_message_to_bytes(self):
Ole Troanf5984bd2016-12-18 13:15:08 +010070 msglist = VPP(testmode=True)
Ole Troana03f4ef2016-12-02 12:53:55 +010071 p = json.loads(TestPAPI.show_version_msg)
72 msgdef = msglist.add_message(p[0], p[1:])
73
74 # Give me a byte string for given message and given arguments
75
76 b = msglist.encode(msgdef, {'_vl_msg_id' : 50, 'context' : 123 })
77 self.assertEqual(10, len(b))
78 rv = msglist.decode(msgdef, b)
79 self.assertEqual(rv._0, 50)
80 self.assertEqual(rv.context, 123)
81
82
83 p = json.loads(TestPAPI.ip_address_details_msg)
84 msgdef = msglist.add_message(p[0], p[1:])
85
86 # Give me a byte string for given message and given arguments
87 b = msglist.encode(msgdef, {'_vl_msg_id' : 50, 'context' : 123,
88 'ip' : b'\xf0\xf1\xf2',
89 'prefix_length' : 12})
90 self.assertEqual(27, len(b))
91 rv = msglist.decode(msgdef, b)
92
93 self.assertEqual(rv.context, 123)
94 self.assertEqual(rv.ip, b'\xf0\xf1\xf2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
95 self.assertEqual(rv.prefix_length, 12)
96
97 p = json.loads(TestPAPI.cli_inband_msg)
98 msgdef = msglist.add_message(p[0], p[1:])
99
100 # Give me a byte string for given message and given arguments
101 b = msglist.encode(msgdef, { '_vl_msg_id' : 50, 'context' : 123,
102 'length' : 20, 'cmd' : 'show version verbose'})
103 self.assertEqual(34, len(b))
104 rv = msglist.decode(msgdef, b)
105 self.assertEqual(rv._0, 50)
106 self.assertEqual(rv.context, 123)
107 self.assertEqual(rv.cmd.decode('ascii'), 'show version verbose')
108
109 variable_array_16_msg = '''["variable_array_16",
110 ["u32", "length"],
111 ["u16", "list", 0, "length"]
112 ]'''
113
114 p = json.loads(variable_array_16_msg)
115 msgdef = msglist.add_message(p[0], p[1:])
116
117 # Give me a byte string for given message and given arguments
118 b = msglist.encode(msgdef, { 'list' : [1, 2], 'length' :2})
119 self.assertEqual(8, len(b))
120 rv = msglist.decode(msgdef, b)
121 self.assertEqual(2, rv.length)
122 self.assertEqual([1,2], rv.list)
123
124 def test_add_new_types(self):
125 counter_type = '''["ip4_fib_counter",
126 ["u32", "address"],
127 ["u8", "address_length"],
128 ["u64", "packets"],
129 ["u64", "bytes"],
130 {"crc" : "0xb2739495"}
131 ]'''
132
133 with_type_msg = '''["with_type_msg",
134 ["u32", "length"],
135 ["u16", "list", 0, "length"],
Ole Troanf5984bd2016-12-18 13:15:08 +0100136 ["vl_api_ip4_fib_counter_t", "counter"]
Ole Troana03f4ef2016-12-02 12:53:55 +0100137 ]'''
138
139 # Add new type
Ole Troanf5984bd2016-12-18 13:15:08 +0100140 msglist = VPP(testmode=True)
Ole Troana03f4ef2016-12-02 12:53:55 +0100141 p = json.loads(counter_type)
142 msglist.add_type(p[0], p[1:])
143 p = json.loads(with_type_msg)
144 msgdef = msglist.add_message(p[0], p[1:])
145 b = msglist.encode(msgdef, {'length' : 2, 'list' : [1,2],
146 'counter' : { 'address' : 4, 'address_length' : 12,
147 'packets': 1235, 'bytes' : 5678}})
148 self.assertEqual(29, len(b)) # feil
149 rv = msglist.decode(msgdef, b)
150 self.assertEqual(2, rv.length)
151 self.assertEqual(5678, rv.counter.bytes)
152
153 def test_add_new_compound_type_with_array(self):
154 counter_type = '''["ip4_fib_counter",
155 ["u32", "address"],
156 ["u8", "address_length"],
157 ["u64", "packets"],
158 ["u64", "bytes"],
159 {"crc" : "0xb2739495"}
160 ]'''
161
162 with_type_msg = '''["with_type_msg",
163 ["u32", "length"],
164 ["u16", "list", 0, "length"],
Ole Troanf5984bd2016-12-18 13:15:08 +0100165 ["vl_api_ip4_fib_counter_t", "counter", 2]
Ole Troana03f4ef2016-12-02 12:53:55 +0100166
167 ]'''
168
169 # Add new type
Ole Troanf5984bd2016-12-18 13:15:08 +0100170 msglist = VPP(testmode=True)
Ole Troana03f4ef2016-12-02 12:53:55 +0100171 p = json.loads(counter_type)
172 msglist.add_type(p[0], p[1:])
173 p = json.loads(with_type_msg)
174 msgdef = msglist.add_message(p[0], p[1:])
175 b = msglist.encode(msgdef, {'length' : 2, 'list' : [1,2],
176 'counter' : [{ 'address' : 4, 'address_length' : 12,
177 'packets': 1235, 'bytes' : 5678},
178 { 'address' : 111, 'address_length' : 222,
179 'packets': 333, 'bytes' : 444}]})
180 self.assertEqual(50, len(b))
181 rv = msglist.decode(msgdef, b)
182 self.assertEqual([1,2], rv.list)
183 self.assertEqual(1235, rv.counter[0].packets)
184
185 with_type_variable_msg = '''["with_type_variable_msg",
186 ["u32", "length"],
Ole Troanf5984bd2016-12-18 13:15:08 +0100187 ["vl_api_ip4_fib_counter_t", "counter", 0, "length"]
Ole Troana03f4ef2016-12-02 12:53:55 +0100188
189 ]'''
190
191 p = json.loads(with_type_variable_msg)
192 msgdef = msglist.add_message(p[0], p[1:])
193 b = msglist.encode(msgdef, {'length' : 2,
194 'counter' : [{ 'address' : 4, 'address_length' : 12,
195 'packets': 1235, 'bytes' : 5678},
196 { 'address' : 111, 'address_length' : 222,
197 'packets': 333, 'bytes' : 444}]})
198 self.assertEqual(46, len(b))
199 rv = msglist.decode(msgdef, b)
200 self.assertEqual(2, rv.length)
201 self.assertEqual(1235, rv.counter[0].packets)
202 self.assertEqual(333, rv.counter[1].packets)
203
Ole Troana03f4ef2016-12-02 12:53:55 +0100204 def test_simple_array(self):
Ole Troanf5984bd2016-12-18 13:15:08 +0100205 msglist = VPP(testmode=True)
Ole Troana03f4ef2016-12-02 12:53:55 +0100206
207 simple_byte_array = '''["simple_byte_array",
208 ["u32", "length"],
209 ["u8", "namecommand", 64]
210
211 ]'''
212 p = json.loads(simple_byte_array)
213 msgdef = msglist.add_message(p[0], p[1:])
214 b = msglist.encode(msgdef, {'length': 2, 'namecommand': 'foobar'})
215 self.assertEqual(68, len(b))
216 rv = msglist.decode(msgdef, b)
217 self.assertEqual(2, rv.length)
218
219 simple_array = '''["simple_array",
220 ["u32", "length"],
221 ["u32", "list", 2]
222
223 ]'''
224 p = json.loads(simple_array)
225 msgdef = msglist.add_message(p[0], p[1:])
226 b = msglist.encode(msgdef, {'length': 2, 'list': [1,2]})
227 self.assertEqual(12, len(b))
228 rv = msglist.decode(msgdef, b)
229 self.assertEqual(2, rv.length)
230 self.assertEqual([1,2], rv.list)
231
232 simple_variable_array = '''["simple_variable_array",
233 ["u32", "length"],
234 ["u32", "list", 0, "length"]
235
236 ]'''
237 p = json.loads(simple_variable_array)
238 msgdef = msglist.add_message(p[0], p[1:])
239 b = msglist.encode(msgdef, {'length':2, 'list': [1,2]})
240 self.assertEqual(12, len(b))
241 rv = msglist.decode(msgdef, b)
242 self.assertEqual(2, rv.length)
243 self.assertEqual([1,2], rv.list)
244
245 simple_variable_byte_array = '''["simple_variable_byte_array",
246 ["u32", "length"],
247 ["u8", "list", 0, "length"]
248 ]'''
249 p = json.loads(simple_variable_byte_array)
250 msgdef =msglist.add_message(p[0], p[1:])
251 b = msglist.encode(msgdef, {'length': 6, 'list' : 'foobar'})
252 self.assertEqual(10, len(b))
253 rv = msglist.decode(msgdef, b)
254 self.assertEqual(6, rv.length)
255 self.assertEqual('foobar', rv.list)
256
Ole Troanf5984bd2016-12-18 13:15:08 +0100257 def test_old_vla_array(self):
258 msglist = VPP(testmode = True)
259
260 # VLA
261 vla_byte_array = '''["vla_byte_array",
262 ["u32", "foobar"],
263 ["u32", "list", 2],
264 ["u32", "propercount"],
265 ["u8", "propermask", 0, "propercount"],
266 ["u8", "oldmask", 0],
267 {"crc" : "0xb2739495"}
268 ]'''
269 p = json.loads(vla_byte_array)
270 msgdef = msglist.add_message(p[0], p[1:])
271 b = msglist.encode(msgdef, {'list' : [123, 456], 'oldmask': b'foobar',
272 'propercount' : 2,
273 'propermask' : [8,9]})
274 self.assertEqual(24, len(b))
275 rv = msglist.decode(msgdef, b)
276 self.assertEqual(b'foobar', rv.oldmask)
277
278 def test_old_vla_array_not_last_member(self):
279 msglist = VPP(testmode = True)
280
281 # VLA
282 vla_byte_array = '''["vla_byte_array",
283 ["u8", "oldmask", 0],
284 ["u32", "foobar"],
285 {"crc" : "0xb2739495"}
286 ]'''
287 p = json.loads(vla_byte_array)
288 self.assertRaises(ValueError, msglist.add_message, p[0], p[1:])
289
290 def test_old_vla_array_u32(self):
291 msglist = VPP(testmode = True)
292
293 # VLA
294 vla_byte_array = '''["vla_byte_array",
295 ["u32", "foobar"],
296 ["u32", "oldmask", 0],
297 {"crc" : "0xb2739495"}
298 ]'''
299 p = json.loads(vla_byte_array)
300 msgdef = msglist.add_message(p[0], p[1:])
301 b = msglist.encode(msgdef, {'foobar' : 123, 'oldmask': [123, 456, 789]})
302 self.assertEqual(16, len(b))
303 rv = msglist.decode(msgdef, b)
304 self.assertEqual([123, 456, 789], rv.oldmask)
305
306 def test_old_vla_array_compound(self):
307 msglist = VPP(testmode = True)
308
309 # VLA
310 counter_type = '''["ip4_fib_counter",
311 ["u32", "address"],
312 ["u8", "address_length"],
313 ["u64", "packets"],
314 ["u64", "bytes"],
315 {"crc" : "0xb2739495"}
316 ]'''
317
318 vla_byte_array = '''["vla_byte_array",
319 ["vl_api_ip4_fib_counter_t", "counter", 0],
320 {"crc" : "0xb2739495"}
321 ]'''
322
323 p = json.loads(counter_type)
324 msglist.add_type(p[0], p[1:])
325
326 p = json.loads(vla_byte_array)
327 with self.assertRaises(NotImplementedError):
328 msgdef = msglist.add_message(p[0], p[1:])
329
330 def test_array_count_not_previous(self):
331 msglist = VPP(testmode = True)
332
333 # VLA
334 vla_byte_array = '''["vla_byte_array",
335 ["u32", "count"],
336 ["u32", "filler"],
337 ["u32", "lst", 0, "count"],
338 {"crc" : "0xb2739495"}
339 ]'''
340
341 p = json.loads(vla_byte_array)
342 msgdef = msglist.add_message(p[0], p[1:])
343 b = msglist.encode(msgdef, {'count': 3, 'lst': [1,2,3], 'filler' : 1 })
344 rv = msglist.decode(msgdef, b)
345 self.assertEqual(rv.lst, [1,2,3])
346
Ole Troana03f4ef2016-12-02 12:53:55 +0100347 def test_argument_name(self):
Ole Troanf5984bd2016-12-18 13:15:08 +0100348 msglist = VPP(testmode=True)
Ole Troana03f4ef2016-12-02 12:53:55 +0100349
350
351 simple_name = '''["simple_name",
352 ["u32", "length"],
353 ["u8", "name"]
354 ]'''
355 p = json.loads(simple_name)
356 msgdef = msglist.add_message(p[0], p[1:])
357 b = msglist.encode(msgdef, {'length': 6, 'name': 1})
358 self.assertEqual(5, len(b))
359 rv = msglist.decode(msgdef, b)
360 self.assertEqual(6, rv.length)
361 self.assertEqual(1, rv.name)
362
363class TestConnectedPAPI(unittest.TestCase):
364 def test_request_reply_function(self):
365 vpp = VPP(jsonfiles)
366
367 vpp.connect('test_vpp_papi2')
368
369 rv = vpp.show_version()
370 self.assertEqual(0, rv.retval)
Ole Troanf5984bd2016-12-18 13:15:08 +0100371 self.assertEqual('vpe', rv.program.decode().rstrip('\0x00'))
Ole Troana03f4ef2016-12-02 12:53:55 +0100372 vpp.disconnect()
373
374
375 def test_dump_details_function(self):
376 vpp = VPP(jsonfiles)
377 vpp.connect('test_vpp_papi3')
378
379 rv = vpp.sw_interface_dump()
380 #self.assertEqual(0, rv.retval)
381 print('RV', rv)
382 vpp.disconnect()
383
384 def test_vla(self):
385 vpp = VPP(jsonfiles)
386
387 vpp.connect('test_vpp_papi3')
388
389 cmd = 'show version verbose'
390 rv = vpp.cli_inband(length=len(cmd), cmd=cmd)
391 self.assertEqual(0, rv.retval)
392 print('RV', rv.reply)
393
394 cmd = 'show vlib graph'
395 rv = vpp.cli_inband(length=len(cmd), cmd=cmd)
396 self.assertEqual(0, rv.retval)
397 print('RV', rv.reply)
398 vpp.disconnect()
399
400 def test_events(self):
401 vpp = VPP(jsonfiles)
402
403 vpp.connect('test_vpp_papi3')
404
405 vpp.register_event_callback(event_handler)
406
407 rv = vpp.want_interface_events(enable_disable = True)
408 self.assertEqual(0, rv.retval)
409 print('RV', rv)
410
411 rv = vpp.create_loopback()
412 print('RV', rv)
413 self.assertEqual(0, rv.retval)
414
415 rv = vpp.sw_interface_set_flags(sw_if_index = 1, admin_up_down = 1)
416 print('RV', rv)
417 self.assertEqual(0, rv.retval)
418 rv = vpp.sw_interface_set_flags(sw_if_index = 1, admin_up_down = 0)
419 print('RV', rv)
420 self.assertEqual(0, rv.retval)
421 self.assertEqual(papi_event.wait(10), True)
422
423 vpp.disconnect()
424
425def event_handler(msgname, result):
426 print('IN EVENT HANDLER:', msgname, result)
427 papi_event.set()
428
429class TestACL(unittest.TestCase):
430 def test_acl_create(self):
431 vpp = VPP(jsonfiles)
432
433 vpp.connect('acl-test')
434
435 rv = vpp.acl_plugin_get_version()
436 print('RV', rv)
Ole Troan85bdafa2016-12-21 11:51:05 +0100437 self.assertEqual(rv.major, 1)
Ole Troana03f4ef2016-12-02 12:53:55 +0100438 self.assertEqual(rv.minor, 1)
439
440 rv = vpp.acl_add_replace(acl_index = 0xFFFFFFFF,
441 r = [{
442 "is_permit" : 1,
443 "is_ipv6" : 0,
444 "proto" : 6,
445 "srcport_or_icmptype_first" : 80,
446 }],
447 count = 1)
448 print ('RV', rv)
449 rv = vpp.acl_add_replace(acl_index = 0xFFFFFFFF,
450 r = [{
451 "is_permit" : 1,
452 "is_ipv6" : 0,
453 "proto" : 6,
454 "srcport_or_icmptype_first" : 81,
455 }],
456 count = 1)
457 self.assertEqual(rv.retval, 0)
458 print ('RV', rv)
459 ai = rv.acl_index
460 rv = vpp.acl_dump()
461 print ('RV', rv)
462
463 #rv = vpp.acl_del(acl_index = ai)
464 #self.assertEqual(rv.retval, 0)
465
466 #rv = vpp.acl_dump()
467 #self.assertEqual([], vpp.acl_dump())
468
469 vpp.disconnect()
470
471 def test_status(self):
472 vpp = VPP(jsonfiles)
473 vpp.status()
474
Ole Troan85bdafa2016-12-21 11:51:05 +0100475 def test_acl_interface_get(self):
476 vpp = VPP(jsonfiles)
477
478 vpp.connect('test_vpp_papi2')
479
480 rv = vpp.macip_acl_interface_get()
481
482 print('RV', rv)
483
484 vpp.disconnect()
Ole Troana03f4ef2016-12-02 12:53:55 +0100485
486if __name__ == '__main__':
487 unittest.main()