tests: Add platform handling for FreeBSD
[vpp.git] / test / asf / test_api_trace.py
1 import unittest
2 from asfframework import VppAsfTestCase, VppTestRunner
3 import json
4 import shutil
5
6
7 class TestJsonApiTrace(VppAsfTestCase):
8     """JSON API trace related tests"""
9
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
26         fname = "test_api_trace-%d.json" % self.vpp.pid
27         tmp_api_trace = "/tmp/%s" % fname
28         fpath = "%s/%s" % (self.tempdir, fname)
29         self.vapi.cli("api trace save-json {}".format(fname))
30         shutil.move(tmp_api_trace, fpath)
31         with open(fpath, encoding="utf-8") as f:
32             s = f.read()
33         trace = json.loads(s)
34         found = False
35         for o in trace:
36             if o["_msgname"] == "show_version":
37                 found = True
38                 break
39         self.assertTrue(found)
40         self.assertEquals(o["_msgname"], "show_version")
41
42     def test_json_api_trace_replay(self):
43         fname = "/tmp/create_loop.json"
44         req = """
45 [
46 {
47         "_msgname": "create_loopback",
48         "_crc": "42bb5d22",
49         "mac_address": "00:00:00:00:00:00"
50 }]
51 """
52         with open(fname, "w") as f:
53             f.write(req)
54         self.vapi.cli("api trace replay-json {}".format(fname))
55         r = self.vapi.sw_interface_dump(name_filter="loop", name_filter_valid=True)
56         self.assertEqual(len(r), 1)
57         self.assertEqual(r[0].interface_name, "loop0")
58
59
60 if __name__ == "__main__":
61     unittest.main(testRunner=VppTestRunner)