diff --git a/score/itf/core/com/ping.py b/score/itf/core/com/ping.py index 9d168f2..aa049c3 100644 --- a/score/itf/core/com/ping.py +++ b/score/itf/core/com/ping.py @@ -11,6 +11,7 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* import os +import shutil import time @@ -19,6 +20,11 @@ def _execute_command(cmd): def _ping(address, wait_ms_precision=None): + if shutil.which("ping") is None: + raise RuntimeError( + "The 'ping' utility is not installed on this system. " + "Please install it (e.g. 'apt install iputils-ping' on Debian/Ubuntu systems)." + ) timeout_command = f"timeout {wait_ms_precision} " if wait_ms_precision else "" return _execute_command(f"{timeout_command}ping -c 1 -W 1 " + address) == 0 diff --git a/test/BUILD b/test/BUILD index 02d7ef4..0bae679 100644 --- a/test/BUILD +++ b/test/BUILD @@ -133,6 +133,13 @@ py_itf_test( ], ) +py_itf_test( + name = "test_ping", + srcs = [ + "test_ping.py", + ], +) + py_itf_test( name = "test_qemu_config_schema", srcs = [ diff --git a/test/test_ping.py b/test/test_ping.py new file mode 100644 index 0000000..598f997 --- /dev/null +++ b/test/test_ping.py @@ -0,0 +1,35 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +import pytest +from unittest.mock import patch + +from score.itf.core.com.ping import ping + + +def test_ping_raises_when_ping_utility_is_missing(): + with patch("score.itf.core.com.ping.shutil.which", return_value=None): + with pytest.raises(RuntimeError, match="'ping' utility is not installed"): + ping("127.0.0.1") + + +def test_ping_returns_true_when_host_is_reachable(): + with patch("score.itf.core.com.ping.shutil.which", return_value="/usr/bin/ping"): + with patch("score.itf.core.com.ping.os.system", return_value=0): + assert ping("127.0.0.1") is True + + +def test_ping_returns_false_when_host_is_unreachable(): + with patch("score.itf.core.com.ping.shutil.which", return_value="/usr/bin/ping"): + with patch("score.itf.core.com.ping.os.system", return_value=1): + assert ping("192.0.2.1") is False