:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(internal_stage): rename local test function and improve object st… (#56401)
gui martins348cd32ab17f
diff --git a/products/batch_exports/backend/temporal/pipeline/internal_stage.py b/products/batch_exports/backend/temporal/pipeline/internal_stage.pyindex c876dac9eb59..b8b67eba39e6 100644--- a/products/batch_exports/backend/temporal/pipeline/internal_stage.py+++ b/products/batch_exports/backend/temporal/pipeline/internal_stage.py@@ -70,17 +70,21 @@ from products.batch_exports.backend.temporal.utils import set_status_to_running_LOGGER = get_write_only_logger()-def _is_local_or_test() -> bool:+def _is_local_dev_or_test() -> bool:return settings.DEBUG or settings.TEST+def _uses_object_storage_endpoint() -> bool:+ return _is_local_dev_or_test() or not settings.CLOUD_DEPLOYMENT++def _get_s3_endpoint_url() -> str:"""Get the S3 endpoint URL for the Temporal worker.When running the stack locally, MinIO runs in Docker but the Temporal workers run outside, so we need to pass inlocalhost URL rather than the hostname of the container."""- if _is_local_or_test():+ if _is_local_dev_or_test():return "http://localhost:19000"return settings.BATCH_EXPORT_OBJECT_STORAGE_ENDPOINT@@ -91,7 +95,7 @@ def _get_s3_credentials() -> tuple[str | None, str | None]:If keyless S3 auth is enabled, we use no credentials as the IAM role will be used to authenticate.Otherwise, we use the credentials from the object storage settings."""- use_keyless_s3_auth = not _is_local_or_test()+ use_keyless_s3_auth = not _uses_object_storage_endpoint()if use_keyless_s3_auth:aws_access_key_id = Noneaws_secret_access_key = None@@ -453,7 +457,7 @@ def _get_clickhouse_s3_staging_folder_url(folder: str) -> str:bucket = settings.BATCH_EXPORT_INTERNAL_STAGING_BUCKETregion = settings.BATCH_EXPORT_OBJECT_STORAGE_REGION# in these environments this will be a URL for MinIO- if _is_local_or_test():+ if _uses_object_storage_endpoint():base_url = f"{settings.BATCH_EXPORT_OBJECT_STORAGE_ENDPOINT}/{bucket}/"else:base_url = f"https://{bucket}.s3.{region}.amazonaws.com/"diff --git a/products/batch_exports/backend/tests/test_internal_stage_storage_config.py b/products/batch_exports/backend/tests/test_internal_stage_storage_config.pynew file mode 100644index 000000000000..2e11d9a9ccce--- /dev/null+++ b/products/batch_exports/backend/tests/test_internal_stage_storage_config.py@@ -0,0 +1,78 @@+import pytest++from django.test.utils import override_settings++from products.batch_exports.backend.temporal.pipeline.internal_stage import (+ _get_s3_credentials,+ _get_s3_endpoint_url,+ get_s3_staging_folder,+)++OBJECT_STORAGE_ENDPOINT = "http://objectstorage:19000"+OBJECT_STORAGE_BUCKET = "posthog-test"+OBJECT_STORAGE_REGION = "us-east-2"+OBJECT_STORAGE_ACCESS_KEY_ID = "object-storage-key"+OBJECT_STORAGE_SECRET_ACCESS_KEY = "object-storage-secret"+EXPECTED_STAGE_FOLDER = "batch-exports/batch-export/2026-01-01-2026-01-02/attempt_1"+++def _get_test_s3_staging_folder_url() -> str:+ return get_s3_staging_folder(+ batch_export_id="batch-export",+ data_interval_start="2026-01-01",+ data_interval_end="2026-01-02",+ attempt_number=1,+ ).url+++ ("cloud_deployment", "is_debug", "is_test"),+ [+ (None, False, False),+ (None, True, False),+ (None, False, True),+ ],+)+def test_internal_stage_uses_object_storage_endpoint_for_self_hosted_local_and_test(+ cloud_deployment: str | None, is_debug: bool, is_test: bool+) -> None:+ with override_settings(+ CLOUD_DEPLOYMENT=cloud_deployment,+ DEBUG=is_debug,+ TEST=is_test,+ BATCH_EXPORT_OBJECT_STORAGE_ENDPOINT=OBJECT_STORAGE_ENDPOINT,+ BATCH_EXPORT_INTERNAL_STAGING_BUCKET=OBJECT_STORAGE_BUCKET,+ OBJECT_STORAGE_ACCESS_KEY_ID=OBJECT_STORAGE_ACCESS_KEY_ID,+ OBJECT_STORAGE_SECRET_ACCESS_KEY=OBJECT_STORAGE_SECRET_ACCESS_KEY,+ ):+ assert (+ _get_test_s3_staging_folder_url()+ == f"{OBJECT_STORAGE_ENDPOINT}/{OBJECT_STORAGE_BUCKET}/{EXPECTED_STAGE_FOLDER}"+ )+ assert _get_s3_credentials() == (OBJECT_STORAGE_ACCESS_KEY_ID, OBJECT_STORAGE_SECRET_ACCESS_KEY)++[email protected]("cloud_deployment", ["DEV", "US", "EU", "E2E"])+def test_internal_stage_uses_aws_s3_for_cloud(cloud_deployment: str) -> None:+ with override_settings(+ CLOUD_DEPLOYMENT=cloud_deployment,+ DEBUG=False,+ TEST=False,+ BATCH_EXPORT_OBJECT_STORAGE_REGION=OBJECT_STORAGE_REGION,+ BATCH_EXPORT_INTERNAL_STAGING_BUCKET=OBJECT_STORAGE_BUCKET,+ ):+ assert (+ _get_test_s3_staging_folder_url()+ == f"https://{OBJECT_STORAGE_BUCKET}.s3.{OBJECT_STORAGE_REGION}.amazonaws.com/{EXPECTED_STAGE_FOLDER}"+ )+ assert _get_s3_credentials() == (None, None)+++def test_s3_endpoint_url_self_hosted_uses_configured_endpoint_not_localhost() -> None:+ with override_settings(+ CLOUD_DEPLOYMENT=None,+ DEBUG=False,+ TEST=False,+ BATCH_EXPORT_OBJECT_STORAGE_ENDPOINT=OBJECT_STORAGE_ENDPOINT,+ ):+ assert _get_s3_endpoint_url() == OBJECT_STORAGE_ENDPOINT