blob: e38b81a4c7dd05cf5f9b0acfcae54e15cd4d2a71 [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
Dmitry Valter71d02aa2023-01-27 12:49:55 +00006import shutil
Filip Tehlar36217e32021-07-23 08:51:10 +00007
8
9class TestJsonApiTrace(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020010 """JSON API trace related tests"""
Filip Tehlar36217e32021-07-23 08:51:10 +000011
12 @classmethod
13 def setUpClass(cls):
14 super(TestJsonApiTrace, cls).setUpClass()
15
16 def setUp(self):
17 self.vapi.cli("api trace free")
18 self.vapi.cli("api trace on")
19 self.vapi.cli("api trace tx on")
20
21 @classmethod
22 def tearDownClass(cls):
23 super(TestJsonApiTrace, cls).tearDownClass()
24
25 def test_json_api_trace_save(self):
26 self.vapi.show_version()
27
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020028 fname = "test_api_trace-%d.json" % self.vpp.pid
Filip Tehlar36217e32021-07-23 08:51:10 +000029 tmp_api_trace = "/tmp/%s" % fname
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020030 fpath = "%s/%s" % (self.tempdir, fname)
Filip Tehlar36217e32021-07-23 08:51:10 +000031 self.vapi.cli("api trace save-json {}".format(fname))
Dmitry Valter71d02aa2023-01-27 12:49:55 +000032 shutil.move(tmp_api_trace, fpath)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020033 with open(fpath, encoding="utf-8") as f:
Filip Tehlar36217e32021-07-23 08:51:10 +000034 s = f.read()
35 trace = json.loads(s)
36 found = False
37 for o in trace:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020038 if o["_msgname"] == "show_version":
Filip Tehlar36217e32021-07-23 08:51:10 +000039 found = True
40 break
41 self.assertTrue(found)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020042 self.assertEquals(o["_msgname"], "show_version")
Filip Tehlar36217e32021-07-23 08:51:10 +000043
44 def test_json_api_trace_replay(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020045 fname = "/tmp/create_loop.json"
Filip Tehlar36217e32021-07-23 08:51:10 +000046 req = """
47[
48{
49 "_msgname": "create_loopback",
50 "_crc": "42bb5d22",
51 "mac_address": "00:00:00:00:00:00"
52}]
53"""
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020054 with open(fname, "w") as f:
Filip Tehlar36217e32021-07-23 08:51:10 +000055 f.write(req)
56 self.vapi.cli("api trace replay-json {}".format(fname))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020057 r = self.vapi.sw_interface_dump(name_filter="loop", name_filter_valid=True)
Filip Tehlar36217e32021-07-23 08:51:10 +000058 self.assertEqual(len(r), 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020059 self.assertEqual(r[0].interface_name, "loop0")
Filip Tehlar36217e32021-07-23 08:51:10 +000060
61
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020062if __name__ == "__main__":
Filip Tehlar36217e32021-07-23 08:51:10 +000063 unittest.main(testRunner=VppTestRunner)