Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 1 | import os |
| 2 | import unittest |
Pratikshya Prasai | 657bdf7 | 2022-08-18 11:09:38 -0400 | [diff] [blame] | 3 | from asfframework import VppTestCase, VppTestRunner |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 4 | from vpp_papi import VppEnum |
| 5 | import json |
| 6 | |
| 7 | |
| 8 | class TestJsonApiTrace(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 9 | """JSON API trace related tests""" |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 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 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 27 | fname = "test_api_trace-%d.json" % self.vpp.pid |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 28 | tmp_api_trace = "/tmp/%s" % fname |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 29 | fpath = "%s/%s" % (self.tempdir, fname) |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 30 | self.vapi.cli("api trace save-json {}".format(fname)) |
| 31 | os.rename(tmp_api_trace, fpath) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 32 | with open(fpath, encoding="utf-8") as f: |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 33 | s = f.read() |
| 34 | trace = json.loads(s) |
| 35 | found = False |
| 36 | for o in trace: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 37 | if o["_msgname"] == "show_version": |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 38 | found = True |
| 39 | break |
| 40 | self.assertTrue(found) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 41 | self.assertEquals(o["_msgname"], "show_version") |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 42 | |
| 43 | def test_json_api_trace_replay(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 44 | fname = "/tmp/create_loop.json" |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 45 | req = """ |
| 46 | [ |
| 47 | { |
| 48 | "_msgname": "create_loopback", |
| 49 | "_crc": "42bb5d22", |
| 50 | "mac_address": "00:00:00:00:00:00" |
| 51 | }] |
| 52 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 53 | with open(fname, "w") as f: |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 54 | f.write(req) |
| 55 | self.vapi.cli("api trace replay-json {}".format(fname)) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 56 | r = self.vapi.sw_interface_dump(name_filter="loop", name_filter_valid=True) |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 57 | self.assertEqual(len(r), 1) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 58 | self.assertEqual(r[0].interface_name, "loop0") |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 59 | |
| 60 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 61 | if __name__ == "__main__": |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 62 | unittest.main(testRunner=VppTestRunner) |