blob: 8776a79f0ac82c6c873b6b19dc189de60f3ef799 [file] [log] [blame]
Filip Tehlar36217e32021-07-23 08:51:10 +00001import unittest
Dave Wallace8800f732023-08-31 00:47:44 -04002from asfframework import VppAsfTestCase, VppTestRunner
Filip Tehlar36217e32021-07-23 08:51:10 +00003import json
Dmitry Valter71d02aa2023-01-27 12:49:55 +00004import shutil
Filip Tehlar36217e32021-07-23 08:51:10 +00005
6
Dave Wallace8800f732023-08-31 00:47:44 -04007class TestJsonApiTrace(VppAsfTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02008 """JSON API trace related tests"""
Filip Tehlar36217e32021-07-23 08:51:10 +00009
10 @classmethod
11 def setUpClass(cls):
12 super(TestJsonApiTrace, cls).setUpClass()
13
14 def setUp(self):
15 self.vapi.cli("api trace free")
16 self.vapi.cli("api trace on")
17 self.vapi.cli("api trace tx on")
18
19 @classmethod
20 def tearDownClass(cls):
21 super(TestJsonApiTrace, cls).tearDownClass()
22
23 def test_json_api_trace_save(self):
24 self.vapi.show_version()
25
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020026 fname = "test_api_trace-%d.json" % self.vpp.pid
Filip Tehlar36217e32021-07-23 08:51:10 +000027 tmp_api_trace = "/tmp/%s" % fname
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020028 fpath = "%s/%s" % (self.tempdir, fname)
Filip Tehlar36217e32021-07-23 08:51:10 +000029 self.vapi.cli("api trace save-json {}".format(fname))
Dmitry Valter71d02aa2023-01-27 12:49:55 +000030 shutil.move(tmp_api_trace, fpath)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020031 with open(fpath, encoding="utf-8") as f:
Filip Tehlar36217e32021-07-23 08:51:10 +000032 s = f.read()
33 trace = json.loads(s)
34 found = False
35 for o in trace:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020036 if o["_msgname"] == "show_version":
Filip Tehlar36217e32021-07-23 08:51:10 +000037 found = True
38 break
39 self.assertTrue(found)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020040 self.assertEquals(o["_msgname"], "show_version")
Filip Tehlar36217e32021-07-23 08:51:10 +000041
42 def test_json_api_trace_replay(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020043 fname = "/tmp/create_loop.json"
Filip Tehlar36217e32021-07-23 08:51:10 +000044 req = """
45[
46{
47 "_msgname": "create_loopback",
48 "_crc": "42bb5d22",
49 "mac_address": "00:00:00:00:00:00"
50}]
51"""
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020052 with open(fname, "w") as f:
Filip Tehlar36217e32021-07-23 08:51:10 +000053 f.write(req)
54 self.vapi.cli("api trace replay-json {}".format(fname))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020055 r = self.vapi.sw_interface_dump(name_filter="loop", name_filter_valid=True)
Filip Tehlar36217e32021-07-23 08:51:10 +000056 self.assertEqual(len(r), 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020057 self.assertEqual(r[0].interface_name, "loop0")
Filip Tehlar36217e32021-07-23 08:51:10 +000058
59
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020060if __name__ == "__main__":
Filip Tehlar36217e32021-07-23 08:51:10 +000061 unittest.main(testRunner=VppTestRunner)