|
| 1 | +# Copyright 2024 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.target_types import Executable |
| 24 | +from pants.backend.python.util_rules.pex import ( |
| 25 | + PexRequest, |
| 26 | + VenvPex, |
| 27 | + VenvPexProcess, |
| 28 | + rules as pex_rules, |
| 29 | +) |
| 30 | +from pants.core.goals.test import TestExtraEnv |
| 31 | +from pants.engine.fs import CreateDigest, Digest, FileContent |
| 32 | +from pants.engine.rules import collect_rules, Get, rule |
| 33 | +from pants.engine.process import FallibleProcessResult, ProcessCacheScope |
| 34 | +from pants.engine.target import Target |
| 35 | +from pants.engine.unions import UnionRule |
| 36 | +from pants.util.logging import LogLevel |
| 37 | + |
| 38 | +from uses_services.exceptions import ServiceMissingError |
| 39 | +from uses_services.platform_rules import Platform |
| 40 | +from uses_services.scripts.has_system_user import ( |
| 41 | + __file__ as has_system_user_full_path, |
| 42 | +) |
| 43 | +from uses_services.target_types import UsesServicesField |
| 44 | + |
| 45 | + |
| 46 | +@dataclass(frozen=True) |
| 47 | +class UsesSystemUserRequest: |
| 48 | + """One or more targets need the system_user (like stanley) using these settings. |
| 49 | +
|
| 50 | + The system_user attributes represent the system_user.user settings from st2.conf. |
| 51 | + In st2 code, they come from: |
| 52 | + oslo_config.cfg.CONF.system_user.user |
| 53 | + """ |
| 54 | + |
| 55 | + system_user: str = "stanley" |
| 56 | + |
| 57 | + |
| 58 | +@dataclass(frozen=True) |
| 59 | +class HasSystemUser: |
| 60 | + pass |
| 61 | + |
| 62 | + |
| 63 | +class PytestUsesSystemUserRequest(PytestPluginSetupRequest): |
| 64 | + @classmethod |
| 65 | + def is_applicable(cls, target: Target) -> bool: |
| 66 | + if not target.has_field(UsesServicesField): |
| 67 | + return False |
| 68 | + uses = target.get(UsesServicesField).value |
| 69 | + return uses is not None and "system_user" in uses |
| 70 | + |
| 71 | + |
| 72 | +@rule( |
| 73 | + desc="Ensure system_user is present before running tests.", |
| 74 | + level=LogLevel.DEBUG, |
| 75 | +) |
| 76 | +async def has_system_user_for_pytest( |
| 77 | + request: PytestUsesSystemUserRequest, |
| 78 | + test_extra_env: TestExtraEnv, |
| 79 | +) -> PytestPluginSetup: |
| 80 | + system_user = test_extra_env.env.get("ST2TESTS_SYSTEM_USER", "stanley") |
| 81 | + |
| 82 | + # this will raise an error if system_user is not present |
| 83 | + _ = await Get(HasSystemUser, UsesSystemUserRequest(system_user=system_user)) |
| 84 | + |
| 85 | + return PytestPluginSetup() |
| 86 | + |
| 87 | + |
| 88 | +@rule( |
| 89 | + desc="Test to see if system_user is present.", |
| 90 | + level=LogLevel.DEBUG, |
| 91 | +) |
| 92 | +async def has_system_user( |
| 93 | + request: UsesSystemUserRequest, platform: Platform |
| 94 | +) -> HasSystemUser: |
| 95 | + script_path = "./has_system_user.py" |
| 96 | + |
| 97 | + # pants is already watching this directory as it is under a source root. |
| 98 | + # So, we don't need to double watch with PathGlobs, just open it. |
| 99 | + with open(has_system_user_full_path, "rb") as script_file: |
| 100 | + script_contents = script_file.read() |
| 101 | + |
| 102 | + script_digest = await Get( |
| 103 | + Digest, CreateDigest([FileContent(script_path, script_contents)]) |
| 104 | + ) |
| 105 | + script_pex = await Get( |
| 106 | + VenvPex, |
| 107 | + PexRequest( |
| 108 | + output_filename="script.pex", |
| 109 | + internal_only=True, |
| 110 | + sources=script_digest, |
| 111 | + main=Executable(script_path), |
| 112 | + ), |
| 113 | + ) |
| 114 | + |
| 115 | + result = await Get( |
| 116 | + FallibleProcessResult, |
| 117 | + VenvPexProcess( |
| 118 | + script_pex, |
| 119 | + argv=(request.system_user,), |
| 120 | + description="Checking to see if system_user is present.", |
| 121 | + # this can change from run to run, so don't cache results. |
| 122 | + cache_scope=ProcessCacheScope.PER_SESSION, |
| 123 | + level=LogLevel.DEBUG, |
| 124 | + ), |
| 125 | + ) |
| 126 | + has_user = result.exit_code == 0 |
| 127 | + |
| 128 | + if has_user: |
| 129 | + return HasSystemUser() |
| 130 | + |
| 131 | + current_user = result.stdout.decode().strip() |
| 132 | + |
| 133 | + # system_user is not present, so raise an error with instructions. |
| 134 | + raise ServiceMissingError( |
| 135 | + service="system_user", |
| 136 | + platform=platform, |
| 137 | + msg=dedent( |
| 138 | + f"""\ |
| 139 | + The system_user ({request.system_user}) does not seem to be present! |
| 140 | +
|
| 141 | + Please export the ST2TESTS_SYSTEM_USER env var to specify which user |
| 142 | + tests should use as the system_user. This user must be present on |
| 143 | + your system. |
| 144 | +
|
| 145 | + To use your current user ({current_user}) as the system_user, run: |
| 146 | +
|
| 147 | + export ST2TESTS_SYSTEM_USER=$(id -un) |
| 148 | + """ |
| 149 | + ), |
| 150 | + ) |
| 151 | + |
| 152 | + |
| 153 | +def rules(): |
| 154 | + return [ |
| 155 | + *collect_rules(), |
| 156 | + UnionRule(PytestPluginSetupRequest, PytestUsesSystemUserRequest), |
| 157 | + *pex_rules(), |
| 158 | + ] |
0 commit comments