|
| 1 | +"""Tests for contextual_logging.execution_logging_context.""" |
| 2 | + |
| 3 | +from cloud_pipelines_backend import backend_types_sql as bts |
| 4 | +from cloud_pipelines_backend.instrumentation import contextual_logging |
| 5 | + |
| 6 | +_CLOUD_PROVIDER_KEY = "cloud-pipelines.net/orchestration/cloud_provider" |
| 7 | + |
| 8 | + |
| 9 | +def _make_execution(*, task_spec: dict | None = None) -> bts.ExecutionNode: |
| 10 | + node = bts.ExecutionNode(task_spec=task_spec or {}) |
| 11 | + node.id = "test-execution-id" |
| 12 | + node.extra_data = {} |
| 13 | + return node |
| 14 | + |
| 15 | + |
| 16 | +class TestExecutionLoggingContext: |
| 17 | + def test_always_sets_execution_id(self): |
| 18 | + execution = _make_execution() |
| 19 | + with contextual_logging.execution_logging_context(execution): |
| 20 | + assert ( |
| 21 | + contextual_logging.get_context_metadata("execution_id") |
| 22 | + == "test-execution-id" |
| 23 | + ) |
| 24 | + |
| 25 | + def test_no_cloud_provider_when_annotation_absent(self): |
| 26 | + execution = _make_execution(task_spec={}) |
| 27 | + with contextual_logging.execution_logging_context(execution): |
| 28 | + assert contextual_logging.get_context_metadata("cloud_provider") is None |
| 29 | + |
| 30 | + def test_sets_cloud_provider_from_annotation(self): |
| 31 | + execution = _make_execution( |
| 32 | + task_spec={"annotations": {_CLOUD_PROVIDER_KEY: "nebius"}} |
| 33 | + ) |
| 34 | + with contextual_logging.execution_logging_context(execution): |
| 35 | + assert contextual_logging.get_context_metadata("cloud_provider") == "nebius" |
| 36 | + |
| 37 | + def test_no_cloud_provider_when_task_spec_is_none(self): |
| 38 | + execution = _make_execution(task_spec=None) |
| 39 | + with contextual_logging.execution_logging_context(execution): |
| 40 | + assert contextual_logging.get_context_metadata("cloud_provider") is None |
| 41 | + |
| 42 | + def test_no_cloud_provider_when_annotations_is_none(self): |
| 43 | + execution = _make_execution(task_spec={"annotations": None}) |
| 44 | + with contextual_logging.execution_logging_context(execution): |
| 45 | + assert contextual_logging.get_context_metadata("cloud_provider") is None |
| 46 | + |
| 47 | + def test_context_is_cleared_after_block(self): |
| 48 | + execution = _make_execution( |
| 49 | + task_spec={"annotations": {_CLOUD_PROVIDER_KEY: "gcp"}} |
| 50 | + ) |
| 51 | + with contextual_logging.execution_logging_context(execution): |
| 52 | + pass |
| 53 | + assert contextual_logging.get_context_metadata("execution_id") is None |
| 54 | + assert contextual_logging.get_context_metadata("cloud_provider") is None |
0 commit comments