Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions score/itf/core/com/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************
import os
import shutil
import time


Expand All @@ -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

Expand Down
7 changes: 7 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
35 changes: 35 additions & 0 deletions test/test_ping.py
Original file line number Diff line number Diff line change
@@ -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