blob: 6524878a10d2029cd0398a9d19a6924521e821f2 [file] [log] [blame]
Filip Tehlar36217e32021-07-23 08:51:10 +00001import os
2import unittest
3from framework import VppTestCase, VppTestRunner
4from vpp_papi import VppEnum
5import json
6
7
8class TestJsonApiTrace(VppTestCase):
9 """ JSON API trace related tests """
10
11 @classmethod
12 def setUpClass(cls):
13 super(TestJsonApiTrace, cls).setUpClass()
14
15 def setUp(self):
16 self.vapi.cli("api trace free")
17 self.vapi.cli("api trace on")
18 self.vapi.cli("api trace tx on")
19
20 @classmethod
21 def tearDownClass(cls):
22 super(TestJsonApiTrace, cls).tearDownClass()
23
24 def test_json_api_trace_save(self):
25 self.vapi.show_version()
26
27 fname = 'test_api_trace-%d.json' % self.vpp.pid
28 tmp_api_trace = "/tmp/%s" % fname
29 fpath = '%s/%s' % (self.tempdir, fname)
30 self.vapi.cli("api trace save-json {}".format(fname))
31 os.rename(tmp_api_trace, fpath)
32 with open(fpath, encoding='utf-8') as f:
33 s = f.read()
34 trace = json.loads(s)
35 found = False
36 for o in trace:
37 if o['_msgname'] == 'show_version':
38 found = True
39 break
40 self.assertTrue(found)
41 self.assertEquals(o['_msgname'], 'show_version')
42
43 def test_json_api_trace_replay(self):
44 fname = '/tmp/create_loop.json'
45 req = """
46[
47{
48 "_msgname": "create_loopback",
49 "_crc": "42bb5d22",
50 "mac_address": "00:00:00:00:00:00"
51}]
52"""
53 with open(fname, 'w') as f:
54 f.write(req)
55 self.vapi.cli("api trace replay-json {}".format(fname))
56 r = self.vapi.sw_interface_dump(name_filter='loop',
57 name_filter_valid=True)
58 self.assertEqual(len(r), 1)
59 self.assertEqual(r[0].interface_name, 'loop0')
60
61
62if __name__ == '__main__':
63 unittest.main(testRunner=VppTestRunner)