blob: acd0e632efd299c5dcf0cd583b540408502f5fcb [file] [log] [blame]
Ang Li93420002016-05-10 19:11:44 -07001#!/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 Li93420002016-05-10 19:11:44 -070016"""This module is where all the test signal classes and related utilities live.
17"""
18
19import functools
20import json
21
Ang Lie2139f12016-05-12 17:39:06 -070022
Ang Li93420002016-05-10 19:11:44 -070023def 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 Lie2139f12016-05-12 17:39:06 -070030
Ang Li93420002016-05-10 19:11:44 -070031 @functools.wraps(func)
32 def wrapper(*args, **kwargs):
33 func(*args, **kwargs)
Ang Lie2139f12016-05-12 17:39:06 -070034 raise TestSilent("Result reporting for %s is suppressed" %
35 func.__name__)
36
Ang Li93420002016-05-10 19:11:44 -070037 return wrapper
38
Ang Lie2139f12016-05-12 17:39:06 -070039
Ang Li93420002016-05-10 19:11:44 -070040class TestSignalError(Exception):
41 """Raised when an error occurs inside a test signal."""
42
Ang Lie2139f12016-05-12 17:39:06 -070043
Ang Li93420002016-05-10 19:11:44 -070044class TestSignal(Exception):
45 """Base class for all test result control signals."""
Ang Lie2139f12016-05-12 17:39:06 -070046
Ang Li93420002016-05-10 19:11:44 -070047 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 Lie2139f12016-05-12 17:39:06 -070058
Ang Li93420002016-05-10 19:11:44 -070059 def __str__(self):
60 return "Details=%s, Extras=%s" % (self.details, self.extras)
61
Ang Lie2139f12016-05-12 17:39:06 -070062
Ang Li93420002016-05-10 19:11:44 -070063class TestFailure(TestSignal):
64 """Raised when a test has failed."""
65
Ang Lie2139f12016-05-12 17:39:06 -070066
Ang Li93420002016-05-10 19:11:44 -070067class TestPass(TestSignal):
68 """Raised when a test has passed."""
69
Ang Lie2139f12016-05-12 17:39:06 -070070
Ang Li93420002016-05-10 19:11:44 -070071class TestSkip(TestSignal):
72 """Raised when a test has been skipped."""
73
Ang Lie2139f12016-05-12 17:39:06 -070074
Ang Li93420002016-05-10 19:11:44 -070075class TestSilent(TestSignal):
76 """Raised when a test should not be reported. This should only be used for
77 generated test cases.
78 """
79
Ang Lie2139f12016-05-12 17:39:06 -070080
Ang Li93420002016-05-10 19:11:44 -070081class TestAbortClass(TestSignal):
82 """Raised when all subsequent test cases within the same test class should
83 be aborted.
84 """
85
Ang Lie2139f12016-05-12 17:39:06 -070086
Ang Li93420002016-05-10 19:11:44 -070087class TestAbortAll(TestSignal):
88 """Raised when all subsequent test cases should be aborted."""
89
Ang Lie2139f12016-05-12 17:39:06 -070090
Ang Li93420002016-05-10 19:11:44 -070091class ControllerError(Exception):
Ang Lie2139f12016-05-12 17:39:06 -070092 """Raised when an error occured in controller classes."""