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