Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/framework/decorators.py
1956 views
1
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Module for declaring decorators used throughout integration tests."""
4
5
import time
6
7
from framework.defs import MAX_API_CALL_DURATION_MS
8
9
10
def timed_request(method):
11
"""Decorate functions to monitor their duration."""
12
class ApiTimeoutException(Exception):
13
"""A custom exception containing the details of the failed API call."""
14
15
def __init__(self, duration, method, resource, payload):
16
"""Compose the error message from the API call components."""
17
super().__init__(
18
'API call exceeded maximum duration: {:.2f} ms.\n'
19
'Call: {} {} {}'.format(duration, method, resource, payload)
20
)
21
22
def timed(*args, **kwargs):
23
"""Raise an exception if method's duration exceeds the max value."""
24
start = time.time()
25
result = method(*args, **kwargs)
26
duration_ms = (time.time() - start) * 1000
27
28
if duration_ms > MAX_API_CALL_DURATION_MS:
29
try:
30
# The positional arguments are:
31
# 1. The Session object
32
# 2. The URL from which we extract the resource for readability
33
resource = args[1][(args[1].rfind("/")):]
34
except IndexError:
35
# Ignore formatting errors.
36
resource = ''
37
38
# The payload is JSON-encoded and passed as an argument.
39
payload = kwargs['json'] if 'json' in kwargs else ''
40
41
raise ApiTimeoutException(
42
duration_ms,
43
method.__name__.upper(),
44
resource,
45
payload
46
)
47
48
return result
49
50
return timed
51
52
53
def test_context(cap, count=1):
54
"""Set the image capability and vm count attribute for individual tests."""
55
def wrap(func):
56
setattr(func, '_capability', cap)
57
setattr(func, '_pool_size', count)
58
return func
59
return wrap
60
61