test_analytics_db_config.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. import os
  10. from materialize import buildkite, ui
  11. from materialize.mz_env_util import get_cloud_hostname
  12. from materialize.mzcompose.composition import Composition
  13. from materialize.test_analytics.config.mz_db_config import MzDbConfig
  14. def create_test_analytics_config(c: Composition) -> MzDbConfig:
  15. """This requires the "mz" service in the composition."""
  16. app_password = os.getenv("PRODUCTION_ANALYTICS_APP_PASSWORD")
  17. if app_password is not None:
  18. try:
  19. hostname = get_cloud_hostname(c, app_password=app_password)
  20. except ui.CommandFailureCausedUIError as e:
  21. # TODO: Remove when database-issues#8592 is fixed
  22. print(f"Failed to get cloud hostname ({e}), using fallback value")
  23. hostname = "7vifiksqeftxc6ld3r6zvc8n2.lb.us-east-1.aws.materialize.cloud"
  24. else:
  25. hostname = "unknown"
  26. return create_test_analytics_config_with_hostname(hostname)
  27. def create_test_analytics_config_with_hostname(hostname: str) -> MzDbConfig:
  28. username = os.getenv("PRODUCTION_ANALYTICS_USERNAME", "infra+bot@materialize.com")
  29. app_password = os.getenv("PRODUCTION_ANALYTICS_APP_PASSWORD")
  30. config = create_test_analytics_config_with_credentials(
  31. hostname, username, app_password
  32. )
  33. if hostname == "unknown":
  34. print("Disabling test-analytics, host is unknown")
  35. config.enabled = False
  36. return config
  37. def create_test_analytics_config_with_credentials(
  38. hostname: str, username: str, app_password: str | None
  39. ) -> MzDbConfig:
  40. database = "raw"
  41. search_path = "test_analytics"
  42. cluster = "test_analytics"
  43. # disable test_analytics access when running locally
  44. enabled = buildkite.is_in_buildkite()
  45. return MzDbConfig(
  46. hostname=hostname,
  47. username=username,
  48. app_password=app_password,
  49. database=database,
  50. search_path=search_path,
  51. cluster=cluster,
  52. enabled=enabled,
  53. application_name="test-analytics",
  54. )
  55. def create_dummy_test_analytics_config() -> MzDbConfig:
  56. config = create_test_analytics_config_with_credentials("<none>", "local", None)
  57. config.enabled = False
  58. return config