actions.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 textwrap
  10. import time
  11. from inspect import getframeinfo, stack
  12. from typing import TYPE_CHECKING, Any
  13. from materialize import MZ_ROOT, spawn
  14. from materialize.checks.executors import Executor
  15. from materialize.mz_version import MzVersion
  16. if TYPE_CHECKING:
  17. from materialize.checks.scenarios import Scenario
  18. class Action:
  19. def __init__(self) -> None:
  20. self.mz_service = None
  21. self.phase = None
  22. def execute(self, e: Executor) -> None:
  23. raise NotImplementedError
  24. def join(self, e: Executor) -> None:
  25. print(f"Action {self} does not implement join()")
  26. raise NotImplementedError
  27. class Testdrive(Action):
  28. # Instruct pytest this class does not contain actual tests
  29. __test__ = False
  30. def __init__(self, input: str, dedent: bool = True) -> None:
  31. self.input = textwrap.dedent(input) if dedent else input
  32. self.handle: Any | None = None
  33. self.caller = getframeinfo(stack()[1][0])
  34. def execute(self, e: Executor, mz_service: str | None = None) -> None:
  35. """Pass testdrive actions to be run by an Executor-specific implementation."""
  36. self.handle = e.testdrive(self.input, self.caller, mz_service)
  37. def join(self, e: Executor) -> None:
  38. e.join(self.handle)
  39. class Sleep(Action):
  40. def __init__(self, interval: float) -> None:
  41. self.interval = interval
  42. def execute(self, e: Executor) -> None:
  43. print(f"Sleeping for {self.interval} seconds")
  44. time.sleep(self.interval)
  45. def join(self, e: Executor) -> None:
  46. pass
  47. class Initialize(Action):
  48. def __init__(self, scenario: "Scenario", mz_service: str | None = None) -> None:
  49. self.checks = scenario.check_objects
  50. self.mz_service = mz_service
  51. def execute(self, e: Executor) -> None:
  52. for check in self.checks:
  53. print(f"Running initialize() from {check}")
  54. check.start_initialize(e, self)
  55. def join(self, e: Executor) -> None:
  56. for check in self.checks:
  57. check.join_initialize(e)
  58. class Manipulate(Action):
  59. def __init__(
  60. self,
  61. scenario: "Scenario",
  62. phase: int | None = None,
  63. mz_service: str | None = None,
  64. ) -> None:
  65. assert phase is not None
  66. self.phase = phase - 1
  67. self.mz_service = mz_service
  68. self.checks = scenario.check_objects
  69. def execute(self, e: Executor) -> None:
  70. assert self.phase is not None
  71. for check in self.checks:
  72. print(f"Running manipulate() from {check}")
  73. check.start_manipulate(e, self)
  74. def join(self, e: Executor) -> None:
  75. assert self.phase is not None
  76. for check in self.checks:
  77. check.join_manipulate(e, self)
  78. class Validate(Action):
  79. def __init__(self, scenario: "Scenario", mz_service: str | None = None) -> None:
  80. self.checks = scenario.check_objects
  81. self.mz_service = mz_service
  82. def execute(self, e: Executor) -> None:
  83. for check in self.checks:
  84. print(f"Running validate() from {check}")
  85. check.start_validate(e, self)
  86. def join(self, e: Executor) -> None:
  87. for check in self.checks:
  88. check.join_validate(e)
  89. class BumpVersion(Action):
  90. def execute(self, e: Executor) -> None:
  91. version = MzVersion.parse_cargo().bump_minor()
  92. spawn.runv(["bin/bump-version", str(version), "--no-commit"], cwd=MZ_ROOT)
  93. def join(self, e: Executor) -> None:
  94. pass
  95. class GitResetHard(Action):
  96. def execute(self, e: Executor) -> None:
  97. MzVersion.parse_cargo().bump_minor()
  98. spawn.runv(["git", "reset", "--hard"], cwd=MZ_ROOT)
  99. def join(self, e: Executor) -> None:
  100. pass