mzcompose.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. """
  10. Basic test for the Data build tool (dbt) integration of Materialize
  11. """
  12. from dataclasses import dataclass
  13. from textwrap import dedent
  14. from typing import Dict, List, Optional
  15. from materialize.mzcompose.composition import Composition, WorkflowArgumentParser
  16. from materialize.mzcompose.services.dbt import Dbt
  17. from materialize.mzcompose.services.materialized import Materialized
  18. from materialize.mzcompose.services.redpanda import Redpanda
  19. from materialize.mzcompose.services.testdrive import Testdrive
  20. SERVICES = [
  21. Materialized(),
  22. Redpanda(),
  23. Dbt(),
  24. Testdrive(seed=1),
  25. ]
  26. @dataclass
  27. class TestCase:
  28. name: str
  29. dbt_env: Dict[str, str]
  30. materialized_options: List[str]
  31. materialized_image: Optional[str] = None
  32. test_cases = [
  33. TestCase(
  34. name="no-tls-cloud",
  35. materialized_options=[],
  36. dbt_env={},
  37. ),
  38. ]
  39. def workflow_default(c: Composition, parser: WorkflowArgumentParser) -> None:
  40. parser.add_argument(
  41. "filter", nargs="?", default="", help="limit to test cases matching filter"
  42. )
  43. parser.add_argument(
  44. "-k", nargs="?", default=None, help="limit tests by keyword expressions"
  45. )
  46. parser.add_argument("-s", action="store_true", help="don't suppress output")
  47. args = parser.parse_args()
  48. c.up({"name": "dbt", "persistent": True})
  49. for test_case in test_cases:
  50. if args.filter in test_case.name:
  51. print(f"> Running test case {test_case.name}")
  52. materialized = Materialized(
  53. options=test_case.materialized_options,
  54. image=test_case.materialized_image,
  55. volumes_extra=["secrets:/secrets"],
  56. default_replication_factor=1,
  57. additional_system_parameter_defaults={
  58. "default_cluster_replication_factor": "1"
  59. },
  60. )
  61. test_args = ["dbt-materialize/tests"]
  62. if args.k:
  63. test_args.append(f"-k {args.k}")
  64. if args.s:
  65. test_args.append("-s")
  66. with c.test_case(test_case.name):
  67. with c.override(materialized):
  68. c.down()
  69. c.up(
  70. "redpanda",
  71. "materialized",
  72. {"name": "testdrive", "persistent": True},
  73. )
  74. # Create a topic that some tests rely on
  75. c.testdrive(
  76. input=dedent(
  77. """
  78. $ kafka-create-topic topic=test-source partitions=1
  79. $ kafka-create-topic topic=test-sink partitions=1
  80. """
  81. )
  82. )
  83. # Set enable_create_table_from_source to true
  84. c.testdrive(
  85. input=dedent(
  86. """
  87. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  88. ALTER SYSTEM SET enable_create_table_from_source = true
  89. """
  90. )
  91. )
  92. # Give the test harness permission to modify the built-in
  93. # objects as necessary.
  94. for what in [
  95. "DATABASE materialize",
  96. "SCHEMA materialize.public",
  97. "CLUSTER quickstart",
  98. ]:
  99. c.sql(
  100. service="materialized",
  101. user="mz_system",
  102. port=6877,
  103. sql=f"ALTER {what} OWNER TO materialize",
  104. )
  105. # Create two databases that some tests rely on
  106. c.sql(
  107. service="materialized",
  108. user="materialize",
  109. sql=dedent(
  110. """
  111. CREATE DATABASE test_database_1;
  112. CREATE DATABASE test_database_2;
  113. CREATE TABLE test_database_1.public.table1 (id int);
  114. CREATE TABLE test_database_2.public.table2 (id int);
  115. """
  116. ),
  117. )
  118. c.run(
  119. "dbt",
  120. "pytest",
  121. *test_args,
  122. env_extra={
  123. "DBT_HOST": "materialized",
  124. "KAFKA_ADDR": "redpanda:9092",
  125. "SCHEMA_REGISTRY_URL": "http://schema-registry:8081",
  126. **test_case.dbt_env,
  127. },
  128. )