Skip to content

Commit

Permalink
Various fixes - mostly type hints and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thpierce committed Feb 6, 2024
1 parent 490ca12 commit 56d66fe
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 13 deletions.
8 changes: 5 additions & 3 deletions contract-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ The frameworks and libraries that are tested in the contract tests should fall i
* http-servers - applications meant to test http servers (e.g. Django).
* http-clients - applications meant to test http clients (e.g. requests).
* aws-sdk - Applications meant to test the AWS SDK (e.g. botocore).
* database-clients - Applications meant to test database clients (e.g. asycnpg).
* database-clients - Applications meant to test database clients (e.g. psychopg2).

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

# Adding tests for a new library or framework

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

# How to run the tests locally?

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

From `aws-otel-python-instrumentation/contract-tests` execute:
Expand Down
13 changes: 13 additions & 0 deletions contract-tests/images/applications/requests/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[project]
name = "requests-server"
description = "Simple server that relies on requests library"
version = "1.0.0"
license = "Apache-2.0"
requires-python = ">=3.8"

dependencies = [
"opentelemetry-distro==0.43b0",
"opentelemetry-exporter-otlp-proto-grpc==1.22.0",
"typing-extensions==4.9.0",
"requests==2.31.0"
]
15 changes: 9 additions & 6 deletions contract-tests/images/applications/requests/requests_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

_PORT: int = 8080
_NETWORK_ALIAS: str = "backend"
_SUCCESS = "success"
_ERROR = "error"
_FAULT = "fault"
_SUCCESS: str = "success"
_ERROR: str = "error"
_FAULT: str = "fault"


class RequestHandler(BaseHTTPRequestHandler):
@override
# pylint: disable=invalid-name
def do_GET(self):
self.handle_request("GET")

@override
# pylint: disable=invalid-name
def do_POST(self):
self.handle_request("POST")

Expand All @@ -35,21 +37,22 @@ def handle_request(self, method: str):
else:
status_code = 404
else:
response: Response = request(method, "http://backend:8080/backend{}".format(self.path))
url: str = f"http://{_NETWORK_ALIAS}:{_PORT}/{_NETWORK_ALIAS}{self.path}"
response: Response = request(method, url, timeout=20)
status_code = response.status_code
self.send_response_only(status_code)
self.end_headers()

def in_path(self, sub_path: str):
return self.path.__contains__(sub_path)
return sub_path in self.path


def main() -> None:
server_address: tuple[str, int] = ("0.0.0.0", _PORT)
request_handler_class: type = RequestHandler
requests_server: ThreadingHTTPServer = ThreadingHTTPServer(server_address, request_handler_class)
atexit.register(requests_server.shutdown)
server_thread = Thread(target=requests_server.serve_forever)
server_thread: Thread = Thread(target=requests_server.serve_forever)
server_thread.start()
print("Ready")
server_thread.join()
Expand Down
4 changes: 2 additions & 2 deletions contract-tests/images/mock-collector/mock_collector_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
_logger: Logger = getLogger(__name__)
_TIMEOUT_DELAY: timedelta = timedelta(seconds=20)
_WAIT_INTERVAL: float = 0.1
T = TypeVar("T")
T: TypeVar = TypeVar("T")


class ResourceScopeSpan:
Expand All @@ -35,7 +35,7 @@ class ResourceScopeSpan:

def __init__(self, resource_spans: ResourceSpans, scope_spans: ScopeSpans, span: Span):
self.resource_spans: ResourceSpans = resource_spans
self.scope_spans = scope_spans
self.scope_spans: ScopeSpans = scope_spans
self.span: Span = span


Expand Down
2 changes: 1 addition & 1 deletion contract-tests/images/mock-collector/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "mock-collector"
description = "Mock Collector used by contract tests for AWS OTEL Python Instrumentation"
version = "1.0.0"
license = "Apache-2.0"
requires-python = ">=3.7"
requires-python = ">=3.8"

dependencies = [
"grpcio ~= 1.60.0",
Expand Down
2 changes: 1 addition & 1 deletion contract-tests/tests/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "contract-tests"
description = "Contract tests for AWS OTEL Python Instrumentation"
version = "1.0.0"
license = "Apache-2.0"
requires-python = ">=3.7"
requires-python = ">=3.8
dependencies = [
"opentelemetry-proto==1.22.0",
Expand Down
8 changes: 8 additions & 0 deletions contract-tests/tests/test/amazon/base/contract_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@


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

Expand Down

0 comments on commit 56d66fe

Please sign in to comment.