Skip to content

Commit d428d08

Browse files
committed
Various fixes
* Make contract tests runnable from project root * Use public ecr version of python * Fix py3.8 issues (dict/list/set not subscriptable, need to use Dict/List/Set) * Remove `--no-isolation` from set up script (initially added as it solves a local problem with my set up, but causes general problems)
1 parent 6df3c82 commit d428d08

File tree

7 files changed

+40
-43
lines changed

7 files changed

+40
-43
lines changed

contract-tests/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ The steps to add a new test for a library or framework are:
3232

3333
Pre-requirements:
3434
* Have `docker` installed and running - verify by running the `docker` command.
35-
* Create `aws-otel-python-instrumentation/contract-tests/dist` folder
36-
* Copy the `aws_opentelemetry_distro` wheel file to `aws-otel-python-instrumentation/contract-tests/dist` folder
35+
* Ensure the `aws_opentelemetry_distro` wheel file exists in to `aws-otel-python-instrumentation/dist` folder
3736

38-
From `aws-otel-python-instrumentation/contract-tests` execute:
37+
From `aws-otel-python-instrumentation` dir, execute:
3938

4039
```
41-
./set-up-contract-tests.sh
42-
pytest tests
40+
./contract-tests/set-up-contract-tests.sh
41+
pytest contract-tests/tests
4342
```

contract-tests/images/applications/requests/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Meant to be run from aws-otel-python-instrumentation/contract-tests.
22
# Assumes existence of dist/aws_opentelemetry_distro-<pkg_version>-py3-none-any.whl.
33
# Assumes filename of aws_opentelemetry_distro-<pkg_version>-py3-none-any.whl is passed in as "DISTRO" arg.
4-
FROM python:3.9-slim
4+
FROM public.ecr.aws/docker/library/python:3.11-slim
55
WORKDIR /requests
6-
COPY ./dist /requests
7-
COPY ./images/applications/requests /requests
6+
COPY ./dist/$DISTRO /requests
7+
COPY ./contract-tests/images/applications/requests /requests
88

99
ARG DISTRO
1010
RUN pip install -r requirements.txt && pip install ${DISTRO} --force-reinstall

contract-tests/images/mock-collector/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.9-slim
1+
FROM public.ecr.aws/docker/library/python:3.11-slim
22
WORKDIR /mock-collector
33
COPY . /mock-collector
44

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from datetime import datetime, timedelta
44
from logging import Logger, getLogger
55
from time import sleep
6-
from typing import Callable, List, TypeVar
6+
from typing import Callable, List, Set, TypeVar
77

88
from google.protobuf.internal.containers import RepeatedScalarFieldContainer
99
from grpc import Channel, insecure_channel
@@ -87,7 +87,7 @@ def wait_condition(exported: List[ExportTraceServiceRequest], current: List[Expo
8787
spans.append(ResourceScopeSpan(resource_span, scope_span, span))
8888
return spans
8989

90-
def get_metrics(self, present_metrics: set[str]) -> List[ResourceScopeMetric]:
90+
def get_metrics(self, present_metrics: Set[str]) -> List[ResourceScopeMetric]:
9191
"""Get all metrics that are currently stored in the mock collector.
9292
9393
Returns:
@@ -103,7 +103,7 @@ def get_export() -> List[ExportMetricsServiceRequest]:
103103
def wait_condition(
104104
exported: List[ExportMetricsServiceRequest], current: List[ExportMetricsServiceRequest]
105105
) -> bool:
106-
received_metrics: set[str] = set()
106+
received_metrics: Set[str] = set()
107107
for exported_metric in current:
108108
for resource_metric in exported_metric.resource_metrics:
109109
for scope_metric in resource_metric.scope_metrics:
@@ -121,7 +121,7 @@ def wait_condition(
121121
return metrics
122122

123123

124-
def _wait_for_content(get_export: Callable[[], List[T]], wait_condition: Callable[[list[T], List[T]], bool]) -> List[T]:
124+
def _wait_for_content(get_export: Callable[[], List[T]], wait_condition: Callable[[List[T], List[T]], bool]) -> List[T]:
125125
# Verify that there is no more data to be received
126126
deadline: datetime = datetime.now() + _TIMEOUT_DELAY
127127
exported: List[T] = []

contract-tests/set-up-contract-tests.sh

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# Check script is running in contract-tests
33
current_path=`pwd`
44
current_dir="${current_path##*/}"
5-
if [ "$current_dir" != "contract-tests" ]; then
6-
echo "Please run from contract-tests dir"
5+
if [ "$current_dir" != "aws-otel-python-instrumentation" ]; then
6+
echo "Please run from aws-otel-python-instrumentation dir"
77
exit
88
fi
99

@@ -12,38 +12,36 @@ rm -rf dist/mock_collector*
1212
rm -rf dist/contract_tests*
1313

1414
# Create mock-collector image
15-
cd images/mock-collector
15+
cd contract-tests/images/mock-collector
1616
docker build . -t aws-appsignals-mock-collector-python
17-
cd ../..
17+
1818

1919
# Find and store aws_opentelemetry_distro whl file
20-
cd dist
21-
DISTRO=(aws_opentelemetry_distro*.whl)
22-
if [ "$DISTRO" = "aws_opentelemetry_distro*.whl" ]; then
20+
cd ../../../dist
21+
DISTRO=(aws_opentelemetry_distro-*-py3-none-any.whl)
22+
if [ "$DISTRO" = "aws_opentelemetry_distro-*-py3-none-any.whl" ]; then
2323
echo "Could not find aws_opentelemetry_distro whl file in dist dir."
2424
exit 1
2525
fi
26-
cd ..
2726

2827
# Create application images
29-
for dir in images/applications/*
28+
cd ..
29+
for dir in contract-tests/images/applications/*
3030
do
3131
application="${dir##*/}"
3232
docker build . -t aws-appsignals-tests-${application}-app -f ${dir}/Dockerfile --build-arg="DISTRO=${DISTRO}"
3333
done
3434

3535
# Build and install mock-collector
36-
cd images/mock-collector
37-
python3 -m build --outdir ../../dist --no-isolation
38-
cd ../../dist
36+
cd contract-tests/images/mock-collector
37+
python3 -m build --outdir ../../../dist
38+
cd ../../../dist
3939
pip install mock_collector-1.0.0-py3-none-any.whl --force-reinstall
4040

4141
# Build and install contract-tests
42-
cd ../tests
43-
python3 -m build --outdir ../dist --no-isolation
44-
cd ../dist
42+
cd ../contract-tests/tests
43+
python3 -m build --outdir ../../dist
44+
cd ../../dist
4545
# --force-reinstall causes `ERROR: No matching distribution found for mock-collector==1.0.0`, but uninstalling and reinstalling works pretty reliably.
4646
pip uninstall contract-tests -y
47-
pip install contract_tests-1.0.0-py3-none-any.whl
48-
49-
cd ..
47+
pip install contract_tests-1.0.0-py3-none-any.whl

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
33
from logging import INFO, Logger, getLogger
4-
from typing import List
4+
from typing import Dict, List
55
from unittest import TestCase
66

77
from docker import DockerClient
@@ -71,7 +71,7 @@ def setUp(self) -> None:
7171
.with_name(self.get_application_image_name())
7272
)
7373

74-
extra_env: dict[str, str] = self.get_application_extra_environment_variables()
74+
extra_env: Dict[str, str] = self.get_application_extra_environment_variables()
7575
for key in extra_env:
7676
self._application.with_env(key, extra_env.get(key))
7777
self._application.start()
@@ -96,7 +96,7 @@ def tearDown(self) -> None:
9696
def get_application_port(self) -> int:
9797
return 8080
9898

99-
def get_application_extra_environment_variables(self) -> dict[str, str]:
99+
def get_application_extra_environment_variables(self) -> Dict[str, str]:
100100
return {}
101101

102102
def get_application_network_aliases(self) -> List[str]:

contract-tests/tests/test/amazon/requests/requests_test.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
from typing import List
3+
from typing import Dict, List
44

55
from mock_collector_client import ResourceScopeMetric, ResourceScopeSpan
66
from requests import Response, request
@@ -37,7 +37,7 @@ def get_application_network_aliases(self) -> List[str]:
3737
return ["backend"]
3838

3939
@override
40-
def get_application_extra_environment_variables(self) -> dict[str, str]:
40+
def get_application_extra_environment_variables(self) -> Dict[str, str]:
4141
"""
4242
This does not appear to do anything, as it does not seem that OTEL supports peer service for Python. Keeping
4343
for consistency at this time.
@@ -96,7 +96,7 @@ def _assert_aws_span_attributes(
9696
self._assert_aws_attributes(target_spans[0].attributes, method, path)
9797

9898
def _assert_aws_attributes(self, attributes_list: List[KeyValue], method: str, endpoint: str) -> None:
99-
attributes_dict: dict[str, AnyValue] = self._get_attributes_dict(attributes_list)
99+
attributes_dict: Dict[str, AnyValue] = self._get_attributes_dict(attributes_list)
100100
self._assert_str_attribute(attributes_dict, AWS_LOCAL_SERVICE, self.get_application_otel_service_name())
101101
# InternalOperation as OTEL does not instrument the basic server we are using, so the client span is a local
102102
# root.
@@ -108,8 +108,8 @@ def _assert_aws_attributes(self, attributes_list: List[KeyValue], method: str, e
108108
# See comment above AWS_LOCAL_OPERATION
109109
self._assert_str_attribute(attributes_dict, AWS_SPAN_KIND, "LOCAL_ROOT")
110110

111-
def _get_attributes_dict(self, attributes_list: List[KeyValue]) -> dict[str, AnyValue]:
112-
attributes_dict: dict[str, AnyValue] = {}
111+
def _get_attributes_dict(self, attributes_list: List[KeyValue]) -> Dict[str, AnyValue]:
112+
attributes_dict: Dict[str, AnyValue] = {}
113113
for attribute in attributes_list:
114114
key: str = attribute.key
115115
value: AnyValue = attribute.value
@@ -119,13 +119,13 @@ def _get_attributes_dict(self, attributes_list: List[KeyValue]) -> dict[str, Any
119119
attributes_dict[key] = value
120120
return attributes_dict
121121

122-
def _assert_str_attribute(self, attributes_dict: dict[str, AnyValue], key: str, expected_value: str):
122+
def _assert_str_attribute(self, attributes_dict: Dict[str, AnyValue], key: str, expected_value: str):
123123
self.assertIn(key, attributes_dict)
124124
actual_value: AnyValue = attributes_dict[key]
125125
self.assertIsNotNone(actual_value)
126126
self.assertEqual(expected_value, actual_value.string_value)
127127

128-
def _assert_int_attribute(self, attributes_dict: dict[str, AnyValue], key: str, expected_value: int) -> None:
128+
def _assert_int_attribute(self, attributes_dict: Dict[str, AnyValue], key: str, expected_value: int) -> None:
129129
actual_value: AnyValue = attributes_dict[key]
130130
self.assertIsNotNone(actual_value)
131131
self.assertEqual(expected_value, actual_value.int_value)
@@ -146,7 +146,7 @@ def _assert_semantic_conventions_span_attributes(
146146
def _assert_semantic_conventions_attributes(
147147
self, attributes_list: List[KeyValue], method: str, endpoint: str, status_code: int
148148
) -> None:
149-
attributes_dict: dict[str, AnyValue] = self._get_attributes_dict(attributes_list)
149+
attributes_dict: Dict[str, AnyValue] = self._get_attributes_dict(attributes_list)
150150
# TODO: requests instrumentation is not populating net peer attributes
151151
# self._assert_str_attribute(attributes_dict, SpanAttributes.NET_PEER_NAME, "backend")
152152
# self._assert_int_attribute(attributes_dict, SpanAttributes.NET_PEER_PORT, 8080)
@@ -177,7 +177,7 @@ def _assert_metric_attributes(
177177
dp: ExponentialHistogramDataPoint = dp_list[0]
178178
if len(dp_list[1].attributes) > len(dp_list[0].attributes):
179179
dp = dp_list[1]
180-
attribute_dict: dict[str, AnyValue] = self._get_attributes_dict(dp.attributes)
180+
attribute_dict: Dict[str, AnyValue] = self._get_attributes_dict(dp.attributes)
181181
self._assert_str_attribute(attribute_dict, AWS_LOCAL_SERVICE, self.get_application_otel_service_name())
182182
# See comment on AWS_LOCAL_OPERATION in _assert_aws_attributes
183183
self._assert_str_attribute(attribute_dict, AWS_LOCAL_OPERATION, "InternalOperation")

0 commit comments

Comments
 (0)