2021-07-23 08:51:10 +00:00
|
|
|
import unittest
|
2023-08-31 00:47:44 -04:00
|
|
|
from asfframework import VppAsfTestCase, VppTestRunner
|
2021-07-23 08:51:10 +00:00
|
|
|
import json
|
2023-01-27 12:49:55 +00:00
|
|
|
import shutil
|
2021-07-23 08:51:10 +00:00
|
|
|
|
|
|
|
|
2023-08-31 00:47:44 -04:00
|
|
|
class TestJsonApiTrace(VppAsfTestCase):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""JSON API trace related tests"""
|
2021-07-23 08:51:10 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(TestJsonApiTrace, cls).setUpClass()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.vapi.cli("api trace free")
|
|
|
|
self.vapi.cli("api trace on")
|
|
|
|
self.vapi.cli("api trace tx on")
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
super(TestJsonApiTrace, cls).tearDownClass()
|
|
|
|
|
|
|
|
def test_json_api_trace_save(self):
|
|
|
|
self.vapi.show_version()
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
fname = "test_api_trace-%d.json" % self.vpp.pid
|
2021-07-23 08:51:10 +00:00
|
|
|
tmp_api_trace = "/tmp/%s" % fname
|
2022-04-26 19:02:15 +02:00
|
|
|
fpath = "%s/%s" % (self.tempdir, fname)
|
2021-07-23 08:51:10 +00:00
|
|
|
self.vapi.cli("api trace save-json {}".format(fname))
|
2023-01-27 12:49:55 +00:00
|
|
|
shutil.move(tmp_api_trace, fpath)
|
2022-04-26 19:02:15 +02:00
|
|
|
with open(fpath, encoding="utf-8") as f:
|
2021-07-23 08:51:10 +00:00
|
|
|
s = f.read()
|
|
|
|
trace = json.loads(s)
|
|
|
|
found = False
|
|
|
|
for o in trace:
|
2022-04-26 19:02:15 +02:00
|
|
|
if o["_msgname"] == "show_version":
|
2021-07-23 08:51:10 +00:00
|
|
|
found = True
|
|
|
|
break
|
|
|
|
self.assertTrue(found)
|
2024-08-19 18:47:55 -04:00
|
|
|
self.assertEqual(o["_msgname"], "show_version")
|
2021-07-23 08:51:10 +00:00
|
|
|
|
|
|
|
def test_json_api_trace_replay(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
fname = "/tmp/create_loop.json"
|
2021-07-23 08:51:10 +00:00
|
|
|
req = """
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"_msgname": "create_loopback",
|
|
|
|
"_crc": "42bb5d22",
|
|
|
|
"mac_address": "00:00:00:00:00:00"
|
|
|
|
}]
|
|
|
|
"""
|
2022-04-26 19:02:15 +02:00
|
|
|
with open(fname, "w") as f:
|
2021-07-23 08:51:10 +00:00
|
|
|
f.write(req)
|
|
|
|
self.vapi.cli("api trace replay-json {}".format(fname))
|
2022-04-26 19:02:15 +02:00
|
|
|
r = self.vapi.sw_interface_dump(name_filter="loop", name_filter_valid=True)
|
2021-07-23 08:51:10 +00:00
|
|
|
self.assertEqual(len(r), 1)
|
2022-04-26 19:02:15 +02:00
|
|
|
self.assertEqual(r[0].interface_name, "loop0")
|
2021-07-23 08:51:10 +00:00
|
|
|
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
if __name__ == "__main__":
|
2021-07-23 08:51:10 +00:00
|
|
|
unittest.main(testRunner=VppTestRunner)
|