|
1 | 1 | """Unit tests for functions defined in src/lightspeed_stack.py.""" |
2 | 2 |
|
| 3 | +import logging |
3 | 4 | from pathlib import Path |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 | import yaml |
7 | 8 |
|
| 9 | +import lightspeed_stack |
| 10 | +from configuration import AppConfig |
8 | 11 | from lightspeed_stack import create_argument_parser, main |
9 | 12 |
|
| 13 | +LEGACY_DEPRECATION_MARKER = "DEPRECATED: the two-file configuration" |
| 14 | + |
| 15 | +COMMON_CONFIG_SECTIONS = """ |
| 16 | +name: test |
| 17 | +service: |
| 18 | + host: localhost |
| 19 | + port: 8080 |
| 20 | + auth_enabled: false |
| 21 | + workers: 1 |
| 22 | + color_log: true |
| 23 | + access_log: true |
| 24 | +user_data_collection: |
| 25 | + feedback_enabled: false |
| 26 | +mcp_servers: [] |
| 27 | +""" |
| 28 | + |
10 | 29 |
|
11 | 30 | def test_create_argument_parser() -> None: |
12 | 31 | """Test for create_argument_parser function. |
@@ -97,3 +116,82 @@ def test_main_migrate_config_requires_run_yaml_and_output( |
97 | 116 | ) |
98 | 117 | with pytest.raises(SystemExit): |
99 | 118 | main() |
| 119 | + |
| 120 | + |
| 121 | +def run_main_with_config( |
| 122 | + config_yaml: str, |
| 123 | + tmp_path: Path, |
| 124 | + monkeypatch: pytest.MonkeyPatch, |
| 125 | +) -> None: |
| 126 | + """Run main() against a config file, with the runners stubbed out. |
| 127 | +
|
| 128 | + Writes ``config_yaml`` to a temporary file, points argv at it, replaces |
| 129 | + the module-level configuration singleton with a fresh AppConfig (so the |
| 130 | + shared singleton is not mutated for other tests), and stubs the quota |
| 131 | + scheduler and uvicorn runners. |
| 132 | + """ |
| 133 | + cfg_file = tmp_path / "lightspeed-stack.yaml" |
| 134 | + cfg_file.write_text(config_yaml, encoding="utf-8") |
| 135 | + monkeypatch.setattr("sys.argv", ["lightspeed-stack", "-c", str(cfg_file)]) |
| 136 | + monkeypatch.setattr(lightspeed_stack, "configuration", AppConfig()) |
| 137 | + monkeypatch.setattr(lightspeed_stack, "start_quota_scheduler", lambda _: None) |
| 138 | + monkeypatch.setattr(lightspeed_stack, "start_uvicorn", lambda _: None) |
| 139 | + main() |
| 140 | + |
| 141 | + |
| 142 | +def test_main_warns_on_legacy_two_file_config( |
| 143 | + tmp_path: Path, |
| 144 | + monkeypatch: pytest.MonkeyPatch, |
| 145 | + caplog: pytest.LogCaptureFixture, |
| 146 | +) -> None: |
| 147 | + """A legacy library_client_config_path config emits one deprecation WARN.""" |
| 148 | + run_yaml = tmp_path / "run.yaml" |
| 149 | + run_yaml.write_text("version: 2\n", encoding="utf-8") |
| 150 | + config_yaml = COMMON_CONFIG_SECTIONS + f""" |
| 151 | +llama_stack: |
| 152 | + use_as_library_client: true |
| 153 | + library_client_config_path: {run_yaml} |
| 154 | +""" |
| 155 | + with caplog.at_level(logging.WARNING): |
| 156 | + run_main_with_config(config_yaml, tmp_path, monkeypatch) |
| 157 | + warnings = [ |
| 158 | + r for r in caplog.records if LEGACY_DEPRECATION_MARKER in r.getMessage() |
| 159 | + ] |
| 160 | + assert len(warnings) == 1 |
| 161 | + assert warnings[0].levelno == logging.WARNING |
| 162 | + # the WARN carries a stable link to the migration doc |
| 163 | + assert "#migration--backwards-compatibility" in warnings[0].getMessage() |
| 164 | + |
| 165 | + |
| 166 | +def test_main_does_not_warn_in_unified_mode( |
| 167 | + tmp_path: Path, |
| 168 | + monkeypatch: pytest.MonkeyPatch, |
| 169 | + caplog: pytest.LogCaptureFixture, |
| 170 | +) -> None: |
| 171 | + """A unified-mode config (llama_stack.config) emits no deprecation WARN.""" |
| 172 | + config_yaml = COMMON_CONFIG_SECTIONS + """ |
| 173 | +llama_stack: |
| 174 | + use_as_library_client: true |
| 175 | + config: |
| 176 | + baseline: default |
| 177 | +""" |
| 178 | + with caplog.at_level(logging.WARNING): |
| 179 | + run_main_with_config(config_yaml, tmp_path, monkeypatch) |
| 180 | + assert not any(LEGACY_DEPRECATION_MARKER in r.getMessage() for r in caplog.records) |
| 181 | + |
| 182 | + |
| 183 | +def test_main_does_not_warn_in_server_mode( |
| 184 | + tmp_path: Path, |
| 185 | + monkeypatch: pytest.MonkeyPatch, |
| 186 | + caplog: pytest.LogCaptureFixture, |
| 187 | +) -> None: |
| 188 | + """A server-mode config (url, no legacy fields) emits no deprecation WARN.""" |
| 189 | + config_yaml = COMMON_CONFIG_SECTIONS + """ |
| 190 | +llama_stack: |
| 191 | + use_as_library_client: false |
| 192 | + url: http://localhost:8321 |
| 193 | + api_key: xyzzy |
| 194 | +""" |
| 195 | + with caplog.at_level(logging.WARNING): |
| 196 | + run_main_with_config(config_yaml, tmp_path, monkeypatch) |
| 197 | + assert not any(LEGACY_DEPRECATION_MARKER in r.getMessage() for r in caplog.records) |
0 commit comments