blob: 35fb3c0261a08fdaa55541d8e0ebec777cc3d86e [file] [log] [blame]
Filip Tehlar36217e32021-07-23 08:51:10 +00001import os
2import unittest
Pratikshya Prasai657bdf72022-08-18 11:09:38 -04003from asfframework import VppTestCase, VppTestRunner
Filip Tehlar36217e32021-07-23 08:51:10 +00004from vpp_papi import VppEnum
5import json
6
7
8class TestJsonApiTrace(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02009 """JSON API trace related tests"""
Filip Tehlar36217e32021-07-23 08:51:10 +000010
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
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020027 fname = "test_api_trace-%d.json" % self.vpp.pid
Filip Tehlar36217e32021-07-23 08:51:10 +000028 tmp_api_trace = "/tmp/%s" % fname
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020029 fpath = "%s/%s" % (self.tempdir, fname)
Filip Tehlar36217e32021-07-23 08:51:10 +000030 self.vapi.cli("api trace save-json {}".format(fname))
31 os.rename(tmp_api_trace, fpath)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020032 with open(fpath, encoding="utf-8") as f:
Filip Tehlar36217e32021-07-23 08:51:10 +000033 s = f.read()
34 trace = json.loads(s)
35 found = False
36 for o in trace:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020037 if o["_msgname"] == "show_version":
Filip Tehlar36217e32021-07-23 08:51:10 +000038 found = True
39 break
40 self.assertTrue(found)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020041 self.assertEquals(o["_msgname"], "show_version")
Filip Tehlar36217e32021-07-23 08:51:10 +000042
43 def test_json_api_trace_replay(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020044 fname = "/tmp/create_loop.json"
Filip Tehlar36217e32021-07-23 08:51:10 +000045 req = """
46[
47{
48 "_msgname": "create_loopback",
49 "_crc": "42bb5d22",
50 "mac_address": "00:00:00:00:00:00"
51}]
52"""
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020053 with open(fname, "w") as f:
Filip Tehlar36217e32021-07-23 08:51:10 +000054 f.write(req)
55 self.vapi.cli("api trace replay-json {}".format(fname))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020056 r = self.vapi.sw_interface_dump(name_filter="loop", name_filter_valid=True)
Filip Tehlar36217e32021-07-23 08:51:10 +000057 self.assertEqual(len(r), 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020058 self.assertEqual(r[0].interface_name, "loop0")
Filip Tehlar36217e32021-07-23 08:51:10 +000059
60
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020061if __name__ == "__main__":
Filip Tehlar36217e32021-07-23 08:51:10 +000062 unittest.main(testRunner=VppTestRunner)