|
18 | 18 | """ |
19 | 19 |
|
20 | 20 | import json |
| 21 | +import shutil |
21 | 22 | from collections.abc import Generator |
22 | 23 | from pathlib import Path |
23 | 24 | from typing import Any |
24 | 25 |
|
25 | 26 | import pytest |
26 | 27 | from fit_scenario import FitScenario, temp_dir_common |
| 28 | +from testing_utils import BazelTools |
27 | 29 |
|
28 | 30 |
|
29 | 31 | def read_launch_manager_config(config_path: Path) -> dict[str, Any]: |
@@ -102,6 +104,170 @@ def create_launch_manager_config(config_path: Path, components: dict[str, Any], |
102 | 104 | return config_path |
103 | 105 |
|
104 | 106 |
|
| 107 | +def create_daemon_integrated_config( |
| 108 | + config_path: Path, |
| 109 | + bin_dir: Path, |
| 110 | + components: dict[str, Any], |
| 111 | + run_targets: dict[str, Any] | None = None, |
| 112 | + enable_health_monitoring: bool = True, |
| 113 | +) -> Path: |
| 114 | + """ |
| 115 | + Create a Launch Manager configuration for daemon integration tests. |
| 116 | +
|
| 117 | + Parameters |
| 118 | + ---------- |
| 119 | + config_path : Path |
| 120 | + Path where the configuration file should be created. |
| 121 | + bin_dir : Path |
| 122 | + Directory containing application binaries. |
| 123 | + components : dict |
| 124 | + Component definitions with supervised applications. |
| 125 | + run_targets : dict, optional |
| 126 | + Run target definitions. If None, uses default startup/running/fallback. |
| 127 | + enable_health_monitoring : bool |
| 128 | + Whether to enable alive supervision for components. |
| 129 | +
|
| 130 | + Returns |
| 131 | + ------- |
| 132 | + Path |
| 133 | + Path to the created configuration file. |
| 134 | + """ |
| 135 | + if run_targets is None: |
| 136 | + run_targets = { |
| 137 | + "startup": {"description": "System startup", "depends_on": []}, |
| 138 | + "running": {"description": "Normal operation", "depends_on": []}, |
| 139 | + "fallback": {"description": "Fallback mode", "depends_on": [], "transition_timeout": 5}, |
| 140 | + } |
| 141 | + |
| 142 | + alive_supervision = {} |
| 143 | + if enable_health_monitoring: |
| 144 | + alive_supervision = { |
| 145 | + "alive_supervision": { |
| 146 | + "reporting_cycle": 0.1, |
| 147 | + "min_indications": 1, |
| 148 | + "max_indications": 3, |
| 149 | + "failed_cycles_tolerance": 2, |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + config = { |
| 154 | + "schema_version": 1, |
| 155 | + "defaults": { |
| 156 | + "deployment_config": { |
| 157 | + "bin_dir": str(bin_dir) + "/", |
| 158 | + "ready_recovery_action": {"restart": {"number_of_attempts": 3, "delay_before_restart": 0.5}}, |
| 159 | + "sandbox": { |
| 160 | + "uid": 0, |
| 161 | + "gid": 0, |
| 162 | + "supplementary_group_ids": [], |
| 163 | + "scheduling_policy": "SCHED_OTHER", |
| 164 | + "scheduling_priority": 1, |
| 165 | + }, |
| 166 | + }, |
| 167 | + "component_properties": { |
| 168 | + "application_profile": { |
| 169 | + "application_type": "Reporting", |
| 170 | + "is_self_terminating": False, |
| 171 | + **alive_supervision, |
| 172 | + }, |
| 173 | + "depends_on": [], |
| 174 | + "process_arguments": [], |
| 175 | + "ready_condition": {"process_state": "Running"}, |
| 176 | + }, |
| 177 | + "run_target": { |
| 178 | + "transition_timeout": 10, |
| 179 | + "recovery_action": {"switch_run_target": {"run_target": "fallback"}}, |
| 180 | + }, |
| 181 | + }, |
| 182 | + "components": components, |
| 183 | + "run_targets": run_targets, |
| 184 | + "initial_run_target": "startup", |
| 185 | + "fallback_run_target": {"description": "Fallback state", "depends_on": [], "transition_timeout": 1.5}, |
| 186 | + } |
| 187 | + config_path.write_text(json.dumps(config, indent=2)) |
| 188 | + return config_path |
| 189 | + |
| 190 | + |
| 191 | +def add_supervised_component( |
| 192 | + component_name: str, |
| 193 | + binary_name: str, |
| 194 | + app_type: str = "Reporting", |
| 195 | + depends_on: list[str] | None = None, |
| 196 | + process_args: list[str] | None = None, |
| 197 | + env_vars: dict[str, str] | None = None, |
| 198 | +) -> dict[str, Any]: |
| 199 | + """ |
| 200 | + Create a component configuration for a supervised application. |
| 201 | +
|
| 202 | + Parameters |
| 203 | + ---------- |
| 204 | + component_name : str |
| 205 | + Unique component identifier. |
| 206 | + binary_name : str |
| 207 | + Name of the binary to execute. |
| 208 | + app_type : str |
| 209 | + Application type (Reporting, State_Manager, Reporting_And_Supervised, etc.). |
| 210 | + depends_on : list[str], optional |
| 211 | + List of component names this component depends on. |
| 212 | + process_args : list[str], optional |
| 213 | + Command-line arguments for the process. |
| 214 | + env_vars : dict[str, str], optional |
| 215 | + Environment variables to set. |
| 216 | +
|
| 217 | + Returns |
| 218 | + ------- |
| 219 | + dict |
| 220 | + Component configuration suitable for Launch Manager config. |
| 221 | + """ |
| 222 | + component = { |
| 223 | + "description": f"{component_name} supervised application", |
| 224 | + "component_properties": { |
| 225 | + "binary_name": binary_name, |
| 226 | + "application_profile": {"application_type": app_type}, |
| 227 | + "depends_on": depends_on or [], |
| 228 | + "process_arguments": process_args or [], |
| 229 | + }, |
| 230 | + } |
| 231 | + |
| 232 | + if env_vars: |
| 233 | + component["deployment_config"] = {"environmental_variables": env_vars} |
| 234 | + |
| 235 | + return component |
| 236 | + |
| 237 | + |
| 238 | +def copy_test_app_to_daemon_workspace(daemon_info: dict[str, Any], app_name: str, version: str = "rust") -> Path: |
| 239 | + """ |
| 240 | + Copy a test application binary to the daemon workspace. |
| 241 | +
|
| 242 | + Parameters |
| 243 | + ---------- |
| 244 | + daemon_info : dict |
| 245 | + Daemon information from launch_manager_daemon fixture. |
| 246 | + app_name : str |
| 247 | + Name of the test application (e.g., "supervised_test_app"). |
| 248 | + version : str |
| 249 | + Implementation version: "rust" or "cpp". |
| 250 | +
|
| 251 | + Returns |
| 252 | + ------- |
| 253 | + Path |
| 254 | + Path to the copied binary in daemon workspace. |
| 255 | + """ |
| 256 | + # Build the test application |
| 257 | + tools = BazelTools(option_prefix=version) |
| 258 | + target_suffix = "_rust" if version == "rust" else "_cpp" |
| 259 | + target = f"//feature_integration_tests/test_apps:{app_name}{target_suffix}" |
| 260 | + tools.build(target) |
| 261 | + source_path = tools.find_target_path(target) |
| 262 | + |
| 263 | + # Copy to daemon bin directory |
| 264 | + dest_path = daemon_info["bin_dir"] / (app_name if version == "rust" else f"{app_name}_cpp") |
| 265 | + shutil.copy2(source_path, dest_path) |
| 266 | + dest_path.chmod(0o755) |
| 267 | + |
| 268 | + return dest_path |
| 269 | + |
| 270 | + |
105 | 271 | class LifecycleScenario(FitScenario): |
106 | 272 | """ |
107 | 273 | Base class for lifecycle feature integration tests. |
|
0 commit comments