Skip to content

Commit bc4dcd8

Browse files
committed
fix: raise clear error when ping utility is missing
Previously, if the system ping binary was not installed, os.system() returned exit code 127 (shell command not found), which was silently treated as a failed ping attempt. This caused a misleading assertion failure like "Target is not pingable within expected time frame" instead of indicating that ping itself is missing. Fix by checking shutil.which("ping") before invoking the binary and raising a RuntimeError with an actionable message if it is absent. Add unit tests covering the missing-binary error, reachable host, and unreachable host cases. Closes #63
1 parent bb9a22b commit bc4dcd8

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

score/itf/core/com/ping.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# SPDX-License-Identifier: Apache-2.0
1212
# *******************************************************************************
1313
import os
14+
import shutil
1415
import time
1516

1617

@@ -19,6 +20,11 @@ def _execute_command(cmd):
1920

2021

2122
def _ping(address, wait_ms_precision=None):
23+
if shutil.which("ping") is None:
24+
raise RuntimeError(
25+
"The 'ping' utility is not installed on this system. "
26+
"Please install it (e.g. 'apt install iputils-ping' on Debian/Ubuntu systems)."
27+
)
2228
timeout_command = f"timeout {wait_ms_precision} " if wait_ms_precision else ""
2329
return _execute_command(f"{timeout_command}ping -c 1 -W 1 " + address) == 0
2430

test/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ py_itf_test(
133133
],
134134
)
135135

136+
py_itf_test(
137+
name = "test_ping",
138+
srcs = [
139+
"test_ping.py",
140+
],
141+
)
142+
136143
py_itf_test(
137144
name = "test_qemu_config_schema",
138145
srcs = [

test/test_ping.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# *******************************************************************************
2+
# Copyright (c) 2026 Contributors to the Eclipse Foundation
3+
#
4+
# See the NOTICE file(s) distributed with this work for additional
5+
# information regarding copyright ownership.
6+
#
7+
# This program and the accompanying materials are made available under the
8+
# terms of the Apache License Version 2.0 which is available at
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# SPDX-License-Identifier: Apache-2.0
12+
# *******************************************************************************
13+
14+
import pytest
15+
from unittest.mock import patch
16+
17+
from score.itf.core.com.ping import ping
18+
19+
20+
def test_ping_raises_when_ping_utility_is_missing():
21+
with patch("score.itf.core.com.ping.shutil.which", return_value=None):
22+
with pytest.raises(RuntimeError, match="'ping' utility is not installed"):
23+
ping("127.0.0.1")
24+
25+
26+
def test_ping_returns_true_when_host_is_reachable():
27+
with patch("score.itf.core.com.ping.shutil.which", return_value="/usr/bin/ping"):
28+
with patch("score.itf.core.com.ping.os.system", return_value=0):
29+
assert ping("127.0.0.1") is True
30+
31+
32+
def test_ping_returns_false_when_host_is_unreachable():
33+
with patch("score.itf.core.com.ping.shutil.which", return_value="/usr/bin/ping"):
34+
with patch("score.itf.core.com.ping.os.system", return_value=1):
35+
assert ping("192.0.2.1") is False

0 commit comments

Comments
 (0)