|
| 1 | +# Copyright 2023 The StackStorm Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from dataclasses import dataclass |
| 17 | +from textwrap import dedent |
| 18 | + |
| 19 | +from pants.backend.python.goals.pytest_runner import ( |
| 20 | + PytestPluginSetupRequest, |
| 21 | + PytestPluginSetup, |
| 22 | +) |
| 23 | +from pants.backend.python.util_rules.pex import ( |
| 24 | + PexRequest, |
| 25 | + PexRequirements, |
| 26 | + VenvPex, |
| 27 | + VenvPexProcess, |
| 28 | + rules as pex_rules, |
| 29 | +) |
| 30 | +from pants.engine.fs import CreateDigest, Digest, FileContent |
| 31 | +from pants.engine.rules import collect_rules, Get, MultiGet, rule |
| 32 | +from pants.engine.process import FallibleProcessResult, ProcessCacheScope |
| 33 | +from pants.engine.target import Target |
| 34 | +from pants.engine.unions import UnionRule |
| 35 | +from pants.util.logging import LogLevel |
| 36 | + |
| 37 | +from uses_services.exceptions import ServiceMissingError, ServiceSpecificMessages |
| 38 | +from uses_services.platform_rules import Platform |
| 39 | +from uses_services.scripts.is_redis_running import ( |
| 40 | + __file__ as is_redis_running_full_path, |
| 41 | +) |
| 42 | +from uses_services.target_types import UsesServicesField |
| 43 | + |
| 44 | + |
| 45 | +@dataclass(frozen=True) |
| 46 | +class UsesRedisRequest: |
| 47 | + """One or more targets need a running redis service using these settings. |
| 48 | +
|
| 49 | + The coord_* attributes represent the coordination settings from st2.conf. |
| 50 | + In st2 code, they come from: |
| 51 | + oslo_config.cfg.CONF.coordination.url |
| 52 | + """ |
| 53 | + |
| 54 | + # These config opts for integration tests are in: |
| 55 | + # conf/st2.dev.conf (copied to conf/st2.ci.conf) |
| 56 | + # TODO: for int tests: set url by either modifying st2.{dev,ci}.conf on the fly or via env vars. |
| 57 | + |
| 58 | + # with our version of oslo.config (newer are slower) we can't directly override opts w/ environment variables. |
| 59 | + |
| 60 | + coord_url: str = "redis://127.0.0.1:6379" |
| 61 | + |
| 62 | + |
| 63 | +@dataclass(frozen=True) |
| 64 | +class RedisIsRunning: |
| 65 | + pass |
| 66 | + |
| 67 | + |
| 68 | +class PytestUsesRedisRequest(PytestPluginSetupRequest): |
| 69 | + @classmethod |
| 70 | + def is_applicable(cls, target: Target) -> bool: |
| 71 | + if not target.has_field(UsesServicesField): |
| 72 | + return False |
| 73 | + uses = target.get(UsesServicesField).value |
| 74 | + return uses is not None and "redis" in uses |
| 75 | + |
| 76 | + |
| 77 | +@rule( |
| 78 | + desc="Ensure redis is running and accessible before running tests.", |
| 79 | + level=LogLevel.DEBUG, |
| 80 | +) |
| 81 | +async def redis_is_running_for_pytest( |
| 82 | + request: PytestUsesRedisRequest, |
| 83 | +) -> PytestPluginSetup: |
| 84 | + # this will raise an error if redis is not running |
| 85 | + _ = await Get(RedisIsRunning, UsesRedisRequest()) |
| 86 | + |
| 87 | + return PytestPluginSetup() |
| 88 | + |
| 89 | + |
| 90 | +@rule( |
| 91 | + desc="Test to see if redis is running and accessible.", |
| 92 | + level=LogLevel.DEBUG, |
| 93 | +) |
| 94 | +async def redis_is_running( |
| 95 | + request: UsesRedisRequest, platform: Platform |
| 96 | +) -> RedisIsRunning: |
| 97 | + script_path = "./is_redis_running.py" |
| 98 | + |
| 99 | + # pants is already watching this directory as it is under a source root. |
| 100 | + # So, we don't need to double watch with PathGlobs, just open it. |
| 101 | + with open(is_redis_running_full_path, "rb") as script_file: |
| 102 | + script_contents = script_file.read() |
| 103 | + |
| 104 | + script_digest, tooz_pex = await MultiGet( |
| 105 | + Get(Digest, CreateDigest([FileContent(script_path, script_contents)])), |
| 106 | + Get( |
| 107 | + VenvPex, |
| 108 | + PexRequest( |
| 109 | + output_filename="tooz.pex", |
| 110 | + internal_only=True, |
| 111 | + requirements=PexRequirements({"tooz", "redis"}), |
| 112 | + ), |
| 113 | + ), |
| 114 | + ) |
| 115 | + |
| 116 | + result = await Get( |
| 117 | + FallibleProcessResult, |
| 118 | + VenvPexProcess( |
| 119 | + tooz_pex, |
| 120 | + argv=( |
| 121 | + script_path, |
| 122 | + request.coord_url, |
| 123 | + ), |
| 124 | + input_digest=script_digest, |
| 125 | + description="Checking to see if Redis is up and accessible.", |
| 126 | + # this can change from run to run, so don't cache results. |
| 127 | + cache_scope=ProcessCacheScope.PER_SESSION, |
| 128 | + level=LogLevel.DEBUG, |
| 129 | + ), |
| 130 | + ) |
| 131 | + is_running = result.exit_code == 0 |
| 132 | + |
| 133 | + if is_running: |
| 134 | + return RedisIsRunning() |
| 135 | + |
| 136 | + # redis is not running, so raise an error with instructions. |
| 137 | + raise ServiceMissingError.generate( |
| 138 | + platform=platform, |
| 139 | + messages=ServiceSpecificMessages( |
| 140 | + service="redis", |
| 141 | + service_start_cmd_el_7="service redis start", |
| 142 | + service_start_cmd_el="systemctl start redis", |
| 143 | + not_installed_clause_el="this is one way to install it:", |
| 144 | + install_instructions_el=dedent( |
| 145 | + """\ |
| 146 | + sudo yum -y install redis |
| 147 | + # Don't forget to start redis. |
| 148 | + """ |
| 149 | + ), |
| 150 | + service_start_cmd_deb="systemctl start redis", |
| 151 | + not_installed_clause_deb="this is one way to install it:", |
| 152 | + install_instructions_deb=dedent( |
| 153 | + """\ |
| 154 | + sudo apt-get install -y mongodb redis |
| 155 | + # Don't forget to start redis. |
| 156 | + """ |
| 157 | + ), |
| 158 | + service_start_cmd_generic="systemctl start redis", |
| 159 | + ), |
| 160 | + ) |
| 161 | + |
| 162 | + |
| 163 | +def rules(): |
| 164 | + return [ |
| 165 | + *collect_rules(), |
| 166 | + UnionRule(PytestPluginSetupRequest, PytestUsesRedisRequest), |
| 167 | + *pex_rules(), |
| 168 | + ] |
0 commit comments