Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / test_driver / happy / lib / ChipUtilities.py
1 #!/usr/bin/env python3
2
3 #
4 #    Copyright (c) 2020 Project CHIP Authors
5 #    Copyright (c) 2016-2017 Nest Labs, Inc.
6 #    All rights reserved.
7 #
8 #    Licensed under the Apache License, Version 2.0 (the "License");
9 #    you may not use this file except in compliance with the License.
10 #    You may obtain a copy of the License at
11 #
12 #        http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #    Unless required by applicable law or agreed to in writing, software
15 #    distributed under the License is distributed on an "AS IS" BASIS,
16 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #    See the License for the specific language governing permissions and
18 #    limitations under the License.
19 #
20
21 #
22 #    @file
23 #       Implements utilities to parse standard content output by the
24 #       Chip standalone test clients and servers (resource stats, leak
25 #       detection, fault-injection, etc)
26 #
27
28 import sys
29 import re
30 import pprint
31 import traceback
32 import unittest
33
34 import happy.HappyStateDelete
35
36
37 from happy.Utils import *
38
39
40 def cleanup_after_exception():
41     print("Deleting Happy state..")
42     opts = happy.HappyStateDelete.option()
43     state_delete = happy.HappyStateDelete.HappyStateDelete(opts)
44     state_delete.run()
45     print("Happy state deleted.")
46
47
48 def run_unittest():
49     """
50     Wrapper for unittest.main() that ensures the Happy state is deleted in case
51     of failure or error.
52     This is meant to be used with mock-to-mock test cases that always delete the topology they create.
53     If the test case can reuse an existing topology and therefore does not always delete the topology,
54     it should handle exceptions in setUp and tearDown explicitly.
55     Unittest traps all exceptions but not KeyboardInterrupt.
56     So, a KeyboardInterrupt will cause the Happy state not to be deleted.
57     An exception raised during a test results in the test ending with an "error"
58     as opposed to a "failure" (see the docs for more details). tearDown is invoked
59     in that case.
60     If an exception is raised during setUp, tearDown is not invoked, and so the
61     Happy state is not cleaned up.
62     Note that an invocation of sys.exit() from the Happy code results in an
63     exception itself (and then it depends if that is happening during setUp,
64     during the test, or during tearDown).
65     So,
66       1. we must cleanup in case of KeyboardInterrupt, and
67       2. to keep it simple, we cleanup in case of any SystemExit that carries
68          an error (we don't care if unittest was able to run tearDown or not).
69     """
70     try:
71         unittest.main()
72
73     except KeyboardInterrupt:
74         print("\n\nChipUtilities.run_unittest caught KeyboardInterrupt")
75         cleanup_after_exception()
76         raise
77
78     except SystemExit as e:
79         if e.args[0] not in [0, False]:
80             print(
81                 "\n\nChipUtilities.run_unittest caught some kind of test error or failure")
82             cleanup_after_exception()
83         raise e