Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/framework/http.py
1956 views
1
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Wrapper over an http session with timed requests."""
4
# pylint: disable=unused-import
5
import requests
6
from requests_unixsocket import DEFAULT_SCHEME, UnixAdapter
7
8
from framework import decorators
9
10
11
class Session(requests.Session):
12
"""Wrapper over requests_unixsocket.Session limiting the call duration.
13
14
Only the API calls relevant to Firecracker (GET, PUT, PATCH) are
15
implemented.
16
"""
17
18
def __init__(self):
19
"""Create a Session object and set the is_good_response callback."""
20
super().__init__()
21
22
# The `pool_connections` argument indicates the maximum number of
23
# open connections allowed at a time. This value is set to 10 for
24
# consistency with the micro-http's `MAX_CONNECTIONS`.
25
self.mount(DEFAULT_SCHEME, UnixAdapter(pool_connections=10))
26
27
def is_good_response(response: int):
28
"""Return `True` for all HTTP 2xx response codes."""
29
return 200 <= response < 300
30
31
def is_status_ok(response: int):
32
"""Return `True` when HTTP response code is 200 OK."""
33
return response == 200
34
35
def is_status_no_content(response: int):
36
"""Return `True` when HTTP response code is 204 NoContent."""
37
return response == 204
38
39
def is_status_bad_request(response: int):
40
"""Return `True` when HTTP response code is 400 BadRequest."""
41
return response == 400
42
43
def is_status_not_found(response: int):
44
"""Return `True` when HTTP response code is 404 NotFound."""
45
return response == 404
46
47
self.is_good_response = is_good_response
48
self.is_status_ok = is_status_ok
49
self.is_status_no_content = is_status_no_content
50
self.is_status_bad_request = is_status_bad_request
51
self.is_status_not_found = is_status_not_found
52
53
@decorators.timed_request
54
def get(self, url, **kwargs):
55
"""Wrap the GET call with duration limit."""
56
# pylint: disable=method-hidden
57
# The `untime` method overrides this, and pylint disapproves.
58
return super().get(url, **kwargs)
59
60
@decorators.timed_request
61
def patch(self, url, data=None, **kwargs):
62
"""Wrap the PATCH call with duration limit."""
63
# pylint: disable=method-hidden
64
# The `untime` method overrides this, and pylint disapproves.
65
return super().patch(url, data=data, **kwargs)
66
67
@decorators.timed_request
68
def put(self, url, data=None, **kwargs):
69
"""Wrap the PUT call with duration limit."""
70
# pylint: disable=method-hidden
71
# The `untime` method overrides this, and pylint disapproves.
72
return super().put(url, data=data, **kwargs)
73
74
def untime(self):
75
"""Restore the HTTP methods to their un-timed selves."""
76
self.get = super().get
77
self.patch = super().patch
78
self.put = super().put
79
80