4 # Copyright (c) 2020 Project CHIP Authors
5 # Copyright (c) 2016-2017 Nest Labs, Inc.
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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.
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)
34 import happy.HappyStateDelete
37 from happy.Utils import *
40 def cleanup_after_exception():
41 print("Deleting Happy state..")
42 opts = happy.HappyStateDelete.option()
43 state_delete = happy.HappyStateDelete.HappyStateDelete(opts)
45 print("Happy state deleted.")
50 Wrapper for unittest.main() that ensures the Happy state is deleted in case
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
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).
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).
73 except KeyboardInterrupt:
74 print("\n\nChipUtilities.run_unittest caught KeyboardInterrupt")
75 cleanup_after_exception()
78 except SystemExit as e:
79 if e.args[0] not in [0, False]:
81 "\n\nChipUtilities.run_unittest caught some kind of test error or failure")
82 cleanup_after_exception()