blob: 93c19d40eb297d06136740109b84e7fce2518f72 [file] [log] [blame]
Ang Li93420002016-05-10 19:11:44 -07001#!/usr/bin/env python3.4
2#
Keun Soo Yimb05f84e2016-10-31 09:29:42 -07003# Copyright (C) 2016 The Android Open Source Project
Ang Li93420002016-05-10 19:11:44 -07004#
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 Yimb05f84e2016-10-31 09:29:42 -07009# http://www.apache.org/licenses/LICENSE-2.0
Ang Li93420002016-05-10 19:11:44 -070010#
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 Yimb05f84e2016-10-31 09:29:42 -070016#
Ang Li93420002016-05-10 19:11:44 -070017"""This module is where all the test signal classes and related utilities live.
18"""
19
20import functools
21import json
22
Ang Lie2139f12016-05-12 17:39:06 -070023
Ang Li93420002016-05-10 19:11:44 -070024def 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 Lie2139f12016-05-12 17:39:06 -070031
Ang Li93420002016-05-10 19:11:44 -070032 @functools.wraps(func)
33 def wrapper(*args, **kwargs):
34 func(*args, **kwargs)
Ang Lie2139f12016-05-12 17:39:06 -070035 raise TestSilent("Result reporting for %s is suppressed" %
36 func.__name__)
37
Ang Li93420002016-05-10 19:11:44 -070038 return wrapper
39
Ang Lie2139f12016-05-12 17:39:06 -070040
Ang Li93420002016-05-10 19:11:44 -070041class TestSignalError(Exception):
42 """Raised when an error occurs inside a test signal."""
43
Ang Lie2139f12016-05-12 17:39:06 -070044
Ang Li93420002016-05-10 19:11:44 -070045class TestSignal(Exception):
46 """Base class for all test result control signals."""
Ang Lie2139f12016-05-12 17:39:06 -070047
Ang Li93420002016-05-10 19:11:44 -070048 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 Lie2139f12016-05-12 17:39:06 -070059
Ang Li93420002016-05-10 19:11:44 -070060 def __str__(self):
61 return "Details=%s, Extras=%s" % (self.details, self.extras)
62
Ang Lie2139f12016-05-12 17:39:06 -070063
Ang Li93420002016-05-10 19:11:44 -070064class TestFailure(TestSignal):
65 """Raised when a test has failed."""
66
Ang Lie2139f12016-05-12 17:39:06 -070067
Ang Li93420002016-05-10 19:11:44 -070068class TestPass(TestSignal):
69 """Raised when a test has passed."""
70
Ang Lie2139f12016-05-12 17:39:06 -070071
Ang Li93420002016-05-10 19:11:44 -070072class TestSkip(TestSignal):
73 """Raised when a test has been skipped."""
74
Ang Lie2139f12016-05-12 17:39:06 -070075
Ang Li93420002016-05-10 19:11:44 -070076class TestSilent(TestSignal):
77 """Raised when a test should not be reported. This should only be used for
78 generated test cases.
79 """
80
Ang Lie2139f12016-05-12 17:39:06 -070081
Ang Li93420002016-05-10 19:11:44 -070082class TestAbortClass(TestSignal):
83 """Raised when all subsequent test cases within the same test class should
84 be aborted.
85 """
86
Ang Lie2139f12016-05-12 17:39:06 -070087
Ang Li93420002016-05-10 19:11:44 -070088class TestAbortAll(TestSignal):
89 """Raised when all subsequent test cases should be aborted."""
90
Ang Lie2139f12016-05-12 17:39:06 -070091
Ang Li93420002016-05-10 19:11:44 -070092class ControllerError(Exception):
Ang Lie2139f12016-05-12 17:39:06 -070093 """Raised when an error occured in controller classes."""