Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3.4 |
| 2 | # |
| 3 | # Copyright 2016 - The Android Open Source Project |
| 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 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 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. |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 16 | """This module is where all the test signal classes and related utilities live. |
| 17 | """ |
| 18 | |
| 19 | import functools |
| 20 | import json |
| 21 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 22 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 23 | def GeneratedTest(func): |
| 24 | """A decorator used to suppress result reporting for the test case that |
| 25 | kicks off a group of generated test cases. |
| 26 | |
| 27 | Returns: |
| 28 | What the decorated function returns. |
| 29 | """ |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 30 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 31 | @functools.wraps(func) |
| 32 | def wrapper(*args, **kwargs): |
| 33 | func(*args, **kwargs) |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 34 | raise TestSilent("Result reporting for %s is suppressed" % |
| 35 | func.__name__) |
| 36 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 37 | return wrapper |
| 38 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 39 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 40 | class TestSignalError(Exception): |
| 41 | """Raised when an error occurs inside a test signal.""" |
| 42 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 43 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 44 | class TestSignal(Exception): |
| 45 | """Base class for all test result control signals.""" |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 46 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 47 | def __init__(self, details, extras=None): |
| 48 | if not isinstance(details, str): |
| 49 | raise TestSignalError("Message has to be a string.") |
| 50 | super(TestSignal, self).__init__(details) |
| 51 | self.details = details |
| 52 | try: |
| 53 | json.dumps(extras) |
| 54 | self.extras = extras |
| 55 | except TypeError: |
| 56 | raise TestSignalError(("Extras must be json serializable. %s " |
| 57 | "is not.") % extras) |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 58 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 59 | def __str__(self): |
| 60 | return "Details=%s, Extras=%s" % (self.details, self.extras) |
| 61 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 62 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 63 | class TestFailure(TestSignal): |
| 64 | """Raised when a test has failed.""" |
| 65 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 66 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 67 | class TestPass(TestSignal): |
| 68 | """Raised when a test has passed.""" |
| 69 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 70 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 71 | class TestSkip(TestSignal): |
| 72 | """Raised when a test has been skipped.""" |
| 73 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 74 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 75 | class TestSilent(TestSignal): |
| 76 | """Raised when a test should not be reported. This should only be used for |
| 77 | generated test cases. |
| 78 | """ |
| 79 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 80 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 81 | class TestAbortClass(TestSignal): |
| 82 | """Raised when all subsequent test cases within the same test class should |
| 83 | be aborted. |
| 84 | """ |
| 85 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 86 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 87 | class TestAbortAll(TestSignal): |
| 88 | """Raised when all subsequent test cases should be aborted.""" |
| 89 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 90 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 91 | class ControllerError(Exception): |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame^] | 92 | """Raised when an error occured in controller classes.""" |