@@ -205,6 +205,83 @@ def test_acifragmentgen_fragment_push(docker_image, cert_chain, capsysbinary):
205205 assert f .read () == signed_fragment
206206
207207
208+ # Unit tests for ORAS path resolution
209+
210+
211+ def test_resolve_oras_path_uses_absolute_path_entries (monkeypatch , tmp_path ):
212+ """A planted CWD executable is ignored in favor of another absolute PATH entry."""
213+ from azext_confcom .oras_proxy import resolve_oras_path
214+
215+ current_directory = tmp_path / "work"
216+ trusted_directory = tmp_path / "tools"
217+ current_directory .mkdir ()
218+ trusted_directory .mkdir ()
219+ oras_name = "oras.exe" if os .name == "nt" else "oras"
220+ planted_oras = current_directory / oras_name
221+ trusted_oras = trusted_directory / oras_name
222+ planted_oras .touch ()
223+ trusted_oras .touch ()
224+ planted_oras .chmod (0o755 )
225+ trusted_oras .chmod (0o755 )
226+
227+ monkeypatch .chdir (current_directory )
228+ if os .name == "nt" :
229+ monkeypatch .setenv ("PATHEXT" , ".EXE" )
230+ monkeypatch .setenv (
231+ "PATH" ,
232+ os .pathsep .join (["" , "." , str (current_directory ), "relative-tools" , str (trusted_directory )]),
233+ )
234+
235+ assert resolve_oras_path () == os .path .realpath (trusted_oras )
236+
237+
238+ def test_resolve_oras_path_errors_when_no_safe_candidate (monkeypatch , tmp_path ):
239+ """Resolution fails instead of executing ORAS from the current directory."""
240+ from azext_confcom .oras_proxy import resolve_oras_path
241+
242+ monkeypatch .chdir (tmp_path )
243+ monkeypatch .setenv ("PATH" , os .pathsep .join (["" , "." , str (tmp_path ), "relative-tools" ]))
244+
245+ with patch ("azext_confcom.oras_proxy.shutil.which" ) as mock_which :
246+ with pytest .raises (SystemExit ):
247+ resolve_oras_path ()
248+
249+ mock_which .assert_not_called ()
250+
251+
252+ def test_call_oras_cli_executes_resolved_absolute_path ():
253+ """The shared ORAS runner never passes a bare executable name to subprocess."""
254+ from azext_confcom .oras_proxy import call_oras_cli
255+
256+ with patch (
257+ "azext_confcom.oras_proxy.resolve_oras_path" ,
258+ return_value = os .path .abspath (os .path .join ("trusted-tools" , "oras.exe" )),
259+ ) as mock_resolve , patch ("azext_confcom.oras_proxy.subprocess.run" ) as mock_run :
260+ call_oras_cli (["discover" , "example.azurecr.io/image:tag" ])
261+
262+ mock_resolve .assert_called_once_with ()
263+ command = mock_run .call_args .args [0 ]
264+ assert os .path .isabs (command [0 ])
265+ assert command [1 :] == ["discover" , "example.azurecr.io/image:tag" ]
266+
267+
268+ @patch ("azext_confcom.command.fragment_push.subprocess.run" )
269+ def test_oras_push_executes_resolved_absolute_path (mock_run ):
270+ """The fragment push command executes the resolved ORAS path."""
271+ from azext_confcom .command .fragment_push import oras_push
272+
273+ resolved_path = os .path .abspath (os .path .join ("trusted-tools" , "oras.exe" ))
274+ with patch (
275+ "azext_confcom.command.fragment_push.oras_proxy.resolve_oras_path" ,
276+ return_value = resolved_path ,
277+ ):
278+ oras_push ("fragment.rego.cose" , "example.azurecr.io/fragments:test" )
279+
280+ command = mock_run .call_args .args [0 ]
281+ assert command [0 ] == resolved_path
282+ assert command [1 ] == "push"
283+
284+
208285# ── Unit tests for prepend_docker_registry ─────────────────────────────────
209286
210287
@@ -361,14 +438,19 @@ def test_oras_attach_explicit_platform(mock_run):
361438
362439 mock_run .return_value = MagicMock (returncode = 0 )
363440
364- oras_attach (
365- signed_fragment_path = "/tmp/fragment.cose" ,
366- manifest_tag = "myregistry.io/myimage:latest" ,
367- platform = "linux/amd64" ,
368- )
441+ with patch (
442+ "azext_confcom.command.fragment_attach.oras_proxy.resolve_oras_path" ,
443+ return_value = os .path .abspath (os .path .join ("trusted-tools" , "oras.exe" )),
444+ ):
445+ oras_attach (
446+ signed_fragment_path = "/tmp/fragment.cose" ,
447+ manifest_tag = "myregistry.io/myimage:latest" ,
448+ platform = "linux/amd64" ,
449+ )
369450
370451 mock_run .assert_called_once ()
371452 cmd = mock_run .call_args [0 ][0 ]
453+ assert os .path .isabs (cmd [0 ])
372454 assert "--platform" in cmd
373455 assert "linux/amd64" in cmd
374456 assert "application/cose-x509+rego" in " " .join (cmd )
@@ -385,13 +467,18 @@ def test_oras_attach_auto_detected_platform(mock_platforms, mock_run):
385467
386468 mock_run .return_value = MagicMock (returncode = 0 )
387469
388- oras_attach (
389- signed_fragment_path = "/tmp/fragment.cose" ,
390- manifest_tag = "myregistry.io/myimage:latest" ,
391- )
470+ with patch (
471+ "azext_confcom.command.fragment_attach.oras_proxy.resolve_oras_path" ,
472+ return_value = os .path .abspath (os .path .join ("trusted-tools" , "oras.exe" )),
473+ ):
474+ oras_attach (
475+ signed_fragment_path = "/tmp/fragment.cose" ,
476+ manifest_tag = "myregistry.io/myimage:latest" ,
477+ )
392478
393479 mock_run .assert_called_once ()
394480 cmd = mock_run .call_args [0 ][0 ]
481+ assert os .path .isabs (cmd [0 ])
395482 assert "--platform" in cmd
396483 assert "linux/amd64" in cmd
397484 assert "application/cose-x509+rego" in " " .join (cmd )
0 commit comments