Skip to content

Commit 6304f50

Browse files
committed
Various fixes - mostly type hints and comments
1 parent 48e6d7e commit 6304f50

File tree

7 files changed

+39
-13
lines changed

7 files changed

+39
-13
lines changed

contract-tests/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ The frameworks and libraries that are tested in the contract tests should fall i
1414
* http-servers - applications meant to test http servers (e.g. Django).
1515
* http-clients - applications meant to test http clients (e.g. requests).
1616
* aws-sdk - Applications meant to test the AWS SDK (e.g. botocore).
17-
* database-clients - Applications meant to test database clients (e.g. asycnpg).
17+
* database-clients - Applications meant to test database clients (e.g. psychopg2).
1818

19-
When testing a framework, we will create a sample application. The sample applications are stored following this convention: `contract-tests/images/<framework-name>`.
19+
When testing a framework, we will create a sample application. The sample applications are stored following this convention: `contract-tests/images/applications/<framework-name>`.
2020

2121
# Adding tests for a new library or framework
2222

2323
The steps to add a new test for a library or framework are:
2424
* Create a sample application.
2525
* The sample application should be created in `contract-tests/images/applications/<framework-name>`.
26+
* Implement a `pyproject.toml` (to ensure code style checks run), `Dockerfile`, and `requirements.txt` file. See the `requests` application for an example of this.
2627
* Add a test class for the sample application.
2728
* The test class should be created in `contract-tests/tests/amazon/<framework-name>`.
29+
* The test class should extend `contract_test_base.py`
2830

2931
# How to run the tests locally?
3032

3133
Pre-requirements:
32-
* Have `docker` installed and running
34+
* Have `docker` installed and running - verify by running the `docker` command.
3335
* Copy the `aws_opentelemetry_distro` wheel file to each application folder under `images` (e.g. to `requests`, but not `mock-collector`)
3436

3537
From `aws-otel-python-instrumentation/contract-tests` execute:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[project]
2+
name = "requests-server"
3+
description = "Simple server that relies on requests library"
4+
version = "1.0.0"
5+
license = "Apache-2.0"
6+
requires-python = ">=3.8"
7+
8+
dependencies = [
9+
"opentelemetry-distro==0.43b0",
10+
"opentelemetry-exporter-otlp-proto-grpc==1.22.0",
11+
"typing-extensions==4.9.0",
12+
"requests==2.31.0"
13+
]

contract-tests/images/applications/requests/requests_server.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99

1010
_PORT: int = 8080
1111
_NETWORK_ALIAS: str = "backend"
12-
_SUCCESS = "success"
13-
_ERROR = "error"
14-
_FAULT = "fault"
12+
_SUCCESS: str = "success"
13+
_ERROR: str = "error"
14+
_FAULT: str = "fault"
1515

1616

1717
class RequestHandler(BaseHTTPRequestHandler):
1818
@override
19+
# pylint: disable=invalid-name
1920
def do_GET(self):
2021
self.handle_request("GET")
2122

2223
@override
24+
# pylint: disable=invalid-name
2325
def do_POST(self):
2426
self.handle_request("POST")
2527

@@ -35,21 +37,22 @@ def handle_request(self, method: str):
3537
else:
3638
status_code = 404
3739
else:
38-
response: Response = request(method, "http://backend:8080/backend{}".format(self.path))
40+
url: str = f"http://{_NETWORK_ALIAS}:{_PORT}/{_NETWORK_ALIAS}{self.path}"
41+
response: Response = request(method, url, timeout=20)
3942
status_code = response.status_code
4043
self.send_response_only(status_code)
4144
self.end_headers()
4245

4346
def in_path(self, sub_path: str):
44-
return self.path.__contains__(sub_path)
47+
return sub_path in self.path
4548

4649

4750
def main() -> None:
4851
server_address: tuple[str, int] = ("0.0.0.0", _PORT)
4952
request_handler_class: type = RequestHandler
5053
requests_server: ThreadingHTTPServer = ThreadingHTTPServer(server_address, request_handler_class)
5154
atexit.register(requests_server.shutdown)
52-
server_thread = Thread(target=requests_server.serve_forever)
55+
server_thread: Thread = Thread(target=requests_server.serve_forever)
5356
server_thread.start()
5457
print("Ready")
5558
server_thread.join()

contract-tests/images/mock-collector/mock_collector_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
_logger: Logger = getLogger(__name__)
2525
_TIMEOUT_DELAY: timedelta = timedelta(seconds=20)
2626
_WAIT_INTERVAL: float = 0.1
27-
T = TypeVar("T")
27+
T: TypeVar = TypeVar("T")
2828

2929

3030
class ResourceScopeSpan:
@@ -35,7 +35,7 @@ class ResourceScopeSpan:
3535

3636
def __init__(self, resource_spans: ResourceSpans, scope_spans: ScopeSpans, span: Span):
3737
self.resource_spans: ResourceSpans = resource_spans
38-
self.scope_spans = scope_spans
38+
self.scope_spans: ScopeSpans = scope_spans
3939
self.span: Span = span
4040

4141

contract-tests/images/mock-collector/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "mock-collector"
77
description = "Mock Collector used by contract tests for AWS OTEL Python Instrumentation"
88
version = "1.0.0"
99
license = "Apache-2.0"
10-
requires-python = ">=3.7"
10+
requires-python = ">=3.8"
1111

1212
dependencies = [
1313
"grpcio ~= 1.60.0",

contract-tests/tests/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "contract-tests"
77
description = "Contract tests for AWS OTEL Python Instrumentation"
88
version = "1.0.0"
99
license = "Apache-2.0"
10-
requires-python = ">=3.7"
10+
requires-python = ">=3.8
1111
1212
dependencies = [
1313
"opentelemetry-proto==1.22.0",

contract-tests/tests/test/amazon/base/contract_test_base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121

2222

2323
class ContractTestBase(TestCase):
24+
"""Base class for implementing a contract test.
25+
26+
This class will create all the boilerplate necessary to run a contract test. It will: 1.Create a mock collector
27+
container that receives telemetry data of the application being tested. 2. Create an application container which
28+
will be used to exercise the library under test.
29+
30+
Several methods are provided that can be overridden to customize the test scenario.
31+
"""
2432
_mock_collector_client: MockCollectorClient
2533
_application: DockerContainer
2634

0 commit comments

Comments
 (0)