Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3.4 |
| 2 | # |
Keun Soo Yim | b05f84e | 2016-10-31 09:29:42 -0700 | [diff] [blame] | 3 | # Copyright (C) 2016 The Android Open Source Project |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
Keun Soo Yim | b05f84e | 2016-10-31 09:29:42 -0700 | [diff] [blame] | 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Keun Soo Yim | b05f84e | 2016-10-31 09:29:42 -0700 | [diff] [blame] | 16 | # |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 17 | """This module is where all the test signal classes and related utilities live. |
| 18 | """ |
| 19 | |
| 20 | import functools |
| 21 | import json |
| 22 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 23 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 24 | def GeneratedTest(func): |
| 25 | """A decorator used to suppress result reporting for the test case that |
| 26 | kicks off a group of generated test cases. |
| 27 | |
| 28 | Returns: |
| 29 | What the decorated function returns. |
| 30 | """ |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 31 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 32 | @functools.wraps(func) |
| 33 | def wrapper(*args, **kwargs): |
| 34 | func(*args, **kwargs) |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 35 | raise TestSilent("Result reporting for %s is suppressed" % |
| 36 | func.__name__) |
| 37 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 38 | return wrapper |
| 39 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 40 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 41 | class TestSignalError(Exception): |
| 42 | """Raised when an error occurs inside a test signal.""" |
| 43 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 44 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 45 | class TestSignal(Exception): |
| 46 | """Base class for all test result control signals.""" |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 47 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 48 | def __init__(self, details, extras=None): |
| 49 | if not isinstance(details, str): |
| 50 | raise TestSignalError("Message has to be a string.") |
| 51 | super(TestSignal, self).__init__(details) |
| 52 | self.details = details |
| 53 | try: |
| 54 | json.dumps(extras) |
| 55 | self.extras = extras |
| 56 | except TypeError: |
| 57 | raise TestSignalError(("Extras must be json serializable. %s " |
| 58 | "is not.") % extras) |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 59 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 60 | def __str__(self): |
| 61 | return "Details=%s, Extras=%s" % (self.details, self.extras) |
| 62 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 63 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 64 | class TestFailure(TestSignal): |
| 65 | """Raised when a test has failed.""" |
| 66 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 67 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 68 | class TestPass(TestSignal): |
| 69 | """Raised when a test has passed.""" |
| 70 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 71 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 72 | class TestSkip(TestSignal): |
| 73 | """Raised when a test has been skipped.""" |
| 74 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 75 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 76 | class TestSilent(TestSignal): |
| 77 | """Raised when a test should not be reported. This should only be used for |
| 78 | generated test cases. |
| 79 | """ |
| 80 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 81 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 82 | class TestAbortClass(TestSignal): |
| 83 | """Raised when all subsequent test cases within the same test class should |
| 84 | be aborted. |
| 85 | """ |
| 86 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 87 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 88 | class TestAbortAll(TestSignal): |
| 89 | """Raised when all subsequent test cases should be aborted.""" |
| 90 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 91 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 92 | class ControllerError(Exception): |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 93 | """Raised when an error occured in controller classes.""" |