Skip to content

Commit bbd0e8d

Browse files
georgebocthpierce
andauthored
Adds contract test for CREATE DATABASE for PostgreSQL (#207)
*Issue #, if available:* N/A *Description of changes:* Adding PostgreSQL contract tests for app signals to ensure that customer experience doesn't break with any change. The new contract test covers the CREATE DATABASE SQL command. The new contract test follows the same structure as the current [Python test](https://github.com/aws-observability/aws-otel-python-instrumentation/blob/main/contract-tests/tests/test/amazon/psycopg2/psycopg2_test.py) and asserts on the same fields of the spans and attributes. Contract tests running successfully: ``` $ ./scripts/build_and_install_distro.sh ... $ ./scripts/set-up-contract-tests.sh ... $ pytest contract-tests/tests ============================= test session starts ============================== ... platform linux -- Python 3.8.11, pytest-7.1.3, pluggy-1.5.0 rootdir: /home/gbochile/aws-otel-python-instrumentation/contract-tests/tests, configfile: pyproject.toml plugins: cov-4.1.0, flaky-3.7.0 collected 3 items ../../../../home/gbochile/aws-otel-python-instrumentation/contract-tests/tests/test/amazon/psycopg2/psycopg2_test.py::Psycopg2Test::test_create_database_succeeds -------------------------------- live log setup -------------------------------- 2024-06-12 12:47:45 [ INFO] Pulling image aws-application-signals-mock-collector-python (container.py:53) 2024-06-12 12:47:45 [ INFO] Container started: d245255255f2 (container.py:64) 2024-06-12 12:47:46 [ INFO] Pulling image postgres:latest (container.py:53) 2024-06-12 12:47:47 [ INFO] Container started: 74ed6462ad77 (container.py:64) 2024-06-12 12:47:47 [ INFO] Waiting to be ready... (waiting_utils.py:46) 2024-06-12 12:47:47 [ INFO] Waiting to be ready... (waiting_utils.py:46) 2024-06-12 12:47:48 [ INFO] Waiting to be ready... (waiting_utils.py:46) -------------------------------- live log call --------------------------------- 2024-06-12 12:47:48 [ INFO] Pulling image aws-application-signals-tests-psycopg2-app (container.py:53) 2024-06-12 12:47:48 [ INFO] Container started: e46d8cee477f (container.py:64) 2024-06-12 12:47:54 [ INFO] Waiting to be ready... (waiting_utils.py:46) 2024-06-12 12:47:54 [ INFO] Waiting to be ready... (waiting_utils.py:46) 2024-06-12 12:47:55 [ INFO] Application stdout (contract_test_base.py:118) 2024-06-12 12:47:55 [ INFO] Ready (contract_test_base.py:119) 2024-06-12 12:47:55 [ INFO] Application stderr (contract_test_base.py:120) 2024-06-12 12:47:55 [ INFO] Failed to get k8s token: [Errno 2] No such file or directory: '/var/run/secrets/kubernetes.io/serviceaccount/token' AwsEcsResourceDetector failed: Missing ECS_CONTAINER_METADATA_URI therefore process is not on ECS. AwsEksResourceDetector failed: [Errno 2] No such file or directory: '/var/run/secrets/kubernetes.io/serviceaccount/token' Exception in detector <opentelemetry.sdk.extension.aws.resource.ec2.AwsEc2ResourceDetector object at 0x7fef12fac460>, ignoring AwsEc2ResourceDetector failed: timed out Configuration of configurator not loaded, aws_configurator already loaded (contract_test_base.py:121) PASSED [ 33%] ../../../../home/gbochile/aws-otel-python-instrumentation/contract-tests/tests/test/amazon/psycopg2/psycopg2_test.py::Psycopg2Test::test_drop_table_succeeds -------------------------------- live log call --------------------------------- 2024-06-12 12:47:55 [ INFO] Pulling image aws-application-signals-tests-psycopg2-app (container.py:53) 2024-06-12 12:47:55 [ INFO] Container started: d40b97a71e67 (container.py:64) 2024-06-12 12:48:01 [ INFO] Waiting to be ready... (waiting_utils.py:46) 2024-06-12 12:48:02 [ INFO] Waiting to be ready... (waiting_utils.py:46) 2024-06-12 12:48:02 [ INFO] Application stdout (contract_test_base.py:118) 2024-06-12 12:48:02 [ INFO] Ready (contract_test_base.py:119) 2024-06-12 12:48:02 [ INFO] Application stderr (contract_test_base.py:120) 2024-06-12 12:48:02 [ INFO] Failed to get k8s token: [Errno 2] No such file or directory: '/var/run/secrets/kubernetes.io/serviceaccount/token' AwsEcsResourceDetector failed: Missing ECS_CONTAINER_METADATA_URI therefore process is not on ECS. AwsEksResourceDetector failed: [Errno 2] No such file or directory: '/var/run/secrets/kubernetes.io/serviceaccount/token' Exception in detector <opentelemetry.sdk.extension.aws.resource.ec2.AwsEc2ResourceDetector object at 0x7ffadf954340>, ignoring AwsEc2ResourceDetector failed: timed out Configuration of configurator not loaded, aws_configurator already loaded (contract_test_base.py:121) PASSED [ 66%] ../../../../home/gbochile/aws-otel-python-instrumentation/contract-tests/tests/test/amazon/psycopg2/psycopg2_test.py::Psycopg2Test::test_fault -------------------------------- live log call --------------------------------- 2024-06-12 12:48:02 [ INFO] Pulling image aws-application-signals-tests-psycopg2-app (container.py:53) 2024-06-12 12:48:03 [ INFO] Container started: fce0d7a0c79d (container.py:64) 2024-06-12 12:48:09 [ INFO] Waiting to be ready... (waiting_utils.py:46) 2024-06-12 12:48:09 [ INFO] Waiting to be ready... (waiting_utils.py:46) 2024-06-12 12:48:09 [ INFO] Application stdout (contract_test_base.py:118) 2024-06-12 12:48:09 [ INFO] Ready Expected Exception with Invalid SQL occurred: relation "invalid_table" does not exist LINE 1: SELECT DISTINCT id, name FROM invalid_table ^ (contract_test_base.py:119) 2024-06-12 12:48:09 [ INFO] Application stderr (contract_test_base.py:120) 2024-06-12 12:48:09 [ INFO] Failed to get k8s token: [Errno 2] No such file or directory: '/var/run/secrets/kubernetes.io/serviceaccount/token' AwsEcsResourceDetector failed: Missing ECS_CONTAINER_METADATA_URI therefore process is not on ECS. AwsEksResourceDetector failed: [Errno 2] No such file or directory: '/var/run/secrets/kubernetes.io/serviceaccount/token' Exception in detector <opentelemetry.sdk.extension.aws.resource.ec2.AwsEc2ResourceDetector object at 0x7f22a937c940>, ignoring AwsEc2ResourceDetector failed: timed out Configuration of configurator not loaded, aws_configurator already loaded (contract_test_base.py:121) PASSED [100%] ------------------------------ live log teardown ------------------------------- 2024-06-12 12:48:10 [ INFO] MockCollector stdout (contract_test_base.py:71) 2024-06-12 12:48:10 [ INFO] Ready (contract_test_base.py:72) 2024-06-12 12:48:10 [ INFO] MockCollector stderr (contract_test_base.py:73) 2024-06-12 12:48:10 [ INFO] (contract_test_base.py:74) ... ======================== 36 passed in 310.77s (0:05:10) ======================== ``` By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. --------- Co-authored-by: Thomas Pierce <[email protected]>
1 parent 89b2146 commit bbd0e8d

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ target
6767
examples/**/build/
6868

6969
# Performance test results
70-
**/performance-tests/results/
70+
**/performance-tests/results/
71+

contract-tests/images/applications/psycopg2/psycopg2_server.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
from typing_extensions import override
1111

1212
_PORT: int = 8080
13-
_SUCCESS: str = "success"
13+
_DROP_TABLE: str = "drop_table"
1414
_ERROR: str = "error"
1515
_FAULT: str = "fault"
16+
_CREATE_DATABASE: str = "create_database"
1617

1718
_DB_HOST = os.getenv("DB_HOST")
1819
_DB_USER = os.getenv("DB_USER")
@@ -26,11 +27,17 @@ class RequestHandler(BaseHTTPRequestHandler):
2627
def do_GET(self):
2728
status_code: int = 200
2829
conn = psycopg2.connect(dbname=_DB_NAME, user=_DB_USER, password=_DB_PASS, host=_DB_HOST)
29-
if self.in_path(_SUCCESS):
30+
conn.autocommit = True # CREATE DATABASE cannot run in a transaction block
31+
if self.in_path(_DROP_TABLE):
3032
cur = conn.cursor()
3133
cur.execute("DROP TABLE IF EXISTS test_table")
3234
cur.close()
3335
status_code = 200
36+
elif self.in_path(_CREATE_DATABASE):
37+
cur = conn.cursor()
38+
cur.execute("CREATE DATABASE test_database")
39+
cur.close()
40+
status_code = 200
3441
elif self.in_path(_FAULT):
3542
cur = conn.cursor()
3643
try:

contract-tests/tests/test/amazon/psycopg2/psycopg2_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ def get_application_extra_environment_variables(self) -> Dict[str, str]:
5151
def get_application_image_name(self) -> str:
5252
return "aws-application-signals-tests-psycopg2-app"
5353

54-
def test_success(self) -> None:
54+
def test_drop_table_succeeds(self) -> None:
5555
self.mock_collector_client.clear_signals()
56-
self.do_test_requests("success", "GET", 200, 0, 0, sql_command="DROP TABLE")
56+
self.do_test_requests("drop_table", "GET", 200, 0, 0, sql_command="DROP TABLE")
57+
58+
def test_create_database_succeeds(self) -> None:
59+
self.mock_collector_client.clear_signals()
60+
self.do_test_requests("create_database", "GET", 200, 0, 0, sql_command="CREATE DATABASE")
5761

5862
def test_fault(self) -> None:
5963
self.mock_collector_client.clear_signals()

0 commit comments

Comments
 (0)