blob: 48773c7c82dc34c55fb624884d195a546ab3e447 [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#
17
Ang Li93420002016-05-10 19:11:44 -070018"""This module is where all the test signal classes and related utilities live.
19"""
20
21import functools
22import json
23
Ang Lie2139f12016-05-12 17:39:06 -070024
Ang Li93420002016-05-10 19:11:44 -070025def GeneratedTest(func):
26 """A decorator used to suppress result reporting for the test case that
27 kicks off a group of generated test cases.
28
29 Returns:
30 What the decorated function returns.
31 """
Ang Lie2139f12016-05-12 17:39:06 -070032
Ang Li93420002016-05-10 19:11:44 -070033 @functools.wraps(func)
34 def wrapper(*args, **kwargs):
35 func(*args, **kwargs)
Ang Lie2139f12016-05-12 17:39:06 -070036 raise TestSilent("Result reporting for %s is suppressed" %
37 func.__name__)
38
Ang Li93420002016-05-10 19:11:44 -070039 return wrapper
40
Ang Lie2139f12016-05-12 17:39:06 -070041
Ang Li93420002016-05-10 19:11:44 -070042class TestSignalError(Exception):
43 """Raised when an error occurs inside a test signal."""
44
Ang Lie2139f12016-05-12 17:39:06 -070045
Ang Li93420002016-05-10 19:11:44 -070046class TestSignal(Exception):
47 """Base class for all test result control signals."""
Ang Lie2139f12016-05-12 17:39:06 -070048
Ang Li93420002016-05-10 19:11:44 -070049 def __init__(self, details, extras=None):
50 if not isinstance(details, str):
51 raise TestSignalError("Message has to be a string.")
52 super(TestSignal, self).__init__(details)
53 self.details = details
54 try:
55 json.dumps(extras)
56 self.extras = extras
57 except TypeError:
58 raise TestSignalError(("Extras must be json serializable. %s "
59 "is not.") % extras)
Ang Lie2139f12016-05-12 17:39:06 -070060
Ang Li93420002016-05-10 19:11:44 -070061 def __str__(self):
62 return "Details=%s, Extras=%s" % (self.details, self.extras)
63
Ang Lie2139f12016-05-12 17:39:06 -070064
Ang Li93420002016-05-10 19:11:44 -070065class TestFailure(TestSignal):
66 """Raised when a test has failed."""
67
Ang Lie2139f12016-05-12 17:39:06 -070068
Ang Li93420002016-05-10 19:11:44 -070069class TestPass(TestSignal):
70 """Raised when a test has passed."""
71
Ang Lie2139f12016-05-12 17:39:06 -070072
Ang Li93420002016-05-10 19:11:44 -070073class TestSkip(TestSignal):
74 """Raised when a test has been skipped."""
75
Ang Lie2139f12016-05-12 17:39:06 -070076
Ang Li93420002016-05-10 19:11:44 -070077class TestSilent(TestSignal):
78 """Raised when a test should not be reported. This should only be used for
79 generated test cases.
80 """
81
Ang Lie2139f12016-05-12 17:39:06 -070082
Ang Li93420002016-05-10 19:11:44 -070083class TestAbortClass(TestSignal):
84 """Raised when all subsequent test cases within the same test class should
85 be aborted.
86 """
87
Ang Lie2139f12016-05-12 17:39:06 -070088
Ang Li93420002016-05-10 19:11:44 -070089class TestAbortAll(TestSignal):
90 """Raised when all subsequent test cases should be aborted."""
91
Ang Lie2139f12016-05-12 17:39:06 -070092
Ang Li93420002016-05-10 19:11:44 -070093class ControllerError(Exception):
Ang Lie2139f12016-05-12 17:39:06 -070094 """Raised when an error occured in controller classes."""