Skip to content

Commit c9ebfa7

Browse files
committed
fix(trainer): keep the server in UNC file URIs and warn on HTTP source failures
Address review feedback: - Path.as_uri() writes a UNC path as file://server/share/dir. Rebuild it as //server/share/dir instead of turning it into a relative path. - Log a configured HTTP(S) source that can't be fetched at WARNING, so it isn't silently replaced by the built-in runtimes. - Add type hints to the new tests. Signed-off-by: Ashish Ranjan <ashishranjaniitp@gmail.com>
1 parent b655a1c commit c9ebfa7

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎kubeflow/trainer/backends/container/runtime_loader.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ def _parse_source_url(source: str) -> tuple[str, str]:
452452
return ("http", source)
453453
elif source.startswith("file://"):
454454
path = urllib.parse.unquote(source[7:]) # Remove 'file://', decode e.g. %20
455+
if not path.startswith("/") and not _WINDOWS_DRIVE_PATH.match(path):
456+
# UNC form, as produced by Path.as_uri(): file://server/share/dir -> //server/share/dir
457+
return ("file", "//" + path)
455458
# RFC 8089 Windows form, as produced by Path.as_uri(): file:///C:/dir -> C:/dir
456459
if path.startswith("/") and _WINDOWS_DRIVE_PATH.match(path[1:]):
457460
path = path[1:]
@@ -536,7 +539,9 @@ def _load_from_http_url(url: str) -> list[base_types.Runtime]:
536539
runtimes.append(runtime)
537540
logger.debug(f"Loaded runtime from HTTP: {runtime.name}")
538541
except Exception as e:
539-
logger.debug(f"Failed to load runtime from HTTP {url}: {e}")
542+
# An HTTP(S) source is configured explicitly, so don't fall back to the built-in
543+
# runtimes silently.
544+
logger.warning(f"Failed to load runtime from HTTP {url}: {e}")
540545

541546
return runtimes
542547

‎kubeflow/trainer/backends/container/runtime_loader_test.py‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""
2020

2121
import logging
22+
from pathlib import Path
2223
from unittest.mock import MagicMock, patch
2324

2425
import pytest
@@ -177,6 +178,15 @@
177178
"expected_path": "\\\\server\\share\\runtimes",
178179
},
179180
),
181+
TestCase(
182+
name="parse windows unc file url from Path.as_uri()",
183+
expected_status=SUCCESS,
184+
config={
185+
"url": "file://server/share/my%20runtimes",
186+
"expected_type": "file",
187+
"expected_path": "//server/share/my runtimes",
188+
},
189+
),
180190
TestCase(
181191
name="parse home-relative path",
182192
expected_status=SUCCESS,
@@ -714,7 +724,7 @@ def test_parse_runtime_yaml_extracts_image(test_case):
714724

715725

716726
@pytest.mark.parametrize("as_uri", [False, True], ids=["native path", "file uri"])
717-
def test_list_training_runtimes_from_local_path(tmp_path, as_uri):
727+
def test_list_training_runtimes_from_local_path(tmp_path: Path, as_uri: bool) -> None:
718728
r"""A local runtime directory is loaded whether given as a native path or a file:// URI.
719729
720730
On a Windows client this covers drive paths (C:\...) and file:///C:/... URIs.
@@ -731,11 +741,28 @@ def test_list_training_runtimes_from_local_path(tmp_path, as_uri):
731741
assert "my-custom-runtime" in [r.name for r in runtimes]
732742

733743

734-
def test_list_training_runtimes_warns_on_unusable_source(caplog):
744+
def test_list_training_runtimes_warns_on_unusable_source(
745+
caplog: pytest.LogCaptureFixture,
746+
) -> None:
735747
"""An unusable source is reported at WARNING, not hidden behind the built-in runtimes."""
736748
with caplog.at_level(logging.WARNING, logger=runtime_loader.__name__):
737749
runtimes = runtime_loader.list_training_runtimes_from_sources(["relative/runtimes"])
738750

739751
assert "Failed to load from source relative/runtimes" in caplog.text
740752
# Built-in runtimes are still returned as the fallback.
741753
assert runtimes
754+
755+
756+
def test_load_from_http_url_warns_on_failure(caplog: pytest.LogCaptureFixture) -> None:
757+
"""A configured HTTP(S) source that can't be fetched is reported at WARNING."""
758+
with (
759+
patch(
760+
"urllib.request.urlopen",
761+
side_effect=runtime_loader.urllib.error.URLError("connection refused"),
762+
),
763+
caplog.at_level(logging.WARNING, logger=runtime_loader.__name__),
764+
):
765+
runtimes = runtime_loader._load_from_http_url("https://example.com/runtime.yaml")
766+
767+
assert runtimes == []
768+
assert "Failed to load runtime from HTTP https://example.com/runtime.yaml" in caplog.text

0 commit comments

Comments
 (0)