Skip to content

Commit defe998

Browse files
committed
Replace manual process handling by local_cmd()
1 parent 20badcf commit defe998

File tree

9 files changed

+37
-44
lines changed

9 files changed

+37
-44
lines changed

conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import lib.config as global_config
1414
from lib import pxe
15+
from lib import commands
1516
from lib.common import (
1617
callable_marker,
1718
DiskDevName,
@@ -202,7 +203,8 @@ def setup_host(hostname_or_ip, *, config=None):
202203
assert len(ips) == 1
203204
host_vm.ip = ips[0]
204205

205-
wait_for(lambda: not os.system(f"nc -zw5 {host_vm.ip} 22"),
206+
# TODO: put that in lib/netutils ?
207+
wait_for(lambda: commands.local_cmd(['nc', '-zw5', host_vm.ip, '22'], check=False),
206208
"Wait for ssh up on nested host", retry_delay_secs=5)
207209

208210
hostname_or_ip = host_vm.ip

lib/host.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,9 @@ def reboot(self, verify=False):
551551
raise
552552
if verify:
553553
wait_for_not(self.is_enabled, "Wait for host down")
554-
wait_for(lambda: not os.system(f"ping -c1 {self.hostname_or_ip} > /dev/null 2>&1"),
554+
wait_for(lambda: commands.local_cmd(['ping', '-c1', self.hostname_or_ip], check=False),
555555
"Wait for host up", timeout_secs=10 * 60, retry_delay_secs=10)
556-
wait_for(lambda: not os.system(f"nc -zw5 {self.hostname_or_ip} 22"),
556+
wait_for(lambda: commands.local_cmd(['nc', '-zw5', self.hostname_or_ip, '22'], check=False),
557557
"Wait for ssh up on host", timeout_secs=10 * 60, retry_delay_secs=5)
558558
wait_for(self.is_enabled, "Wait for XAPI to be ready", timeout_secs=30 * 60)
559559

lib/xo.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
2-
import subprocess
2+
3+
from lib import commands
34

45
from typing import Any, Dict, Literal, Union, overload
56

@@ -13,29 +14,28 @@ def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple
1314
...
1415
@overload
1516
def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple_output: Literal[False],
16-
use_json: bool = False) -> subprocess.CompletedProcess:
17+
use_json: bool = False) -> int:
1718
...
1819
@overload
1920
def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple_output: bool = True,
20-
use_json: bool = False) -> Union[subprocess.CompletedProcess, Any, str]:
21+
use_json: bool = False) -> Union[int, Any, str]:
2122
...
2223
def xo_cli(action, args={}, check=True, simple_output=True, use_json=False):
2324
run_array = ['xo-cli', action]
2425
if use_json:
2526
run_array += ['--json']
2627
run_array += ["%s=%s" % (key, value) for key, value in args.items()]
27-
res = subprocess.run(
28-
run_array,
29-
stdout=subprocess.PIPE,
30-
stderr=subprocess.STDOUT,
31-
check=check
32-
)
28+
29+
res = commands.local_cmd(run_array, check=check, decode=True)
30+
3331
if simple_output:
34-
output = res.stdout.decode().strip()
32+
output = res.output
3533
if use_json:
3634
return json.loads(output)
3735
return output
38-
return res
36+
37+
# XXX: doesn't seem to be used as by default simple_output is true
38+
return res.resultcode
3939

4040
def xo_object_exists(uuid):
4141
lst = json.loads(xo_cli('--list-objects', {'uuid': uuid}))

scripts/install_xcpng.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import os
77
import random
88
import string
9-
import subprocess
109
import sys
1110
import tempfile
1211
import time
@@ -17,7 +16,7 @@
1716
# flake8: noqa: E402
1817
sys.path.append(f"{os.path.abspath(os.path.dirname(__file__))}/..")
1918
from lib import pxe
20-
from lib.commands import SSHCommandFailed, scp, ssh
19+
from lib.commands import SSHCommandFailed, scp, ssh, local_cmd
2120
from lib.common import is_uuid, wait_for
2221
from lib.host import host_data
2322
from lib.pool import Pool
@@ -27,9 +26,7 @@
2726

2827
def generate_answerfile(directory, installer, hostname_or_ip, target_hostname, action, hdd, netinstall_gpg_check):
2928
password = host_data(hostname_or_ip)['password']
30-
cmd = ['openssl', 'passwd', '-6', password]
31-
res = subprocess.run(cmd, stdout=subprocess.PIPE)
32-
encrypted_password = res.stdout.decode().strip()
29+
encrypted_password = local_cmd(['openssl', 'passwd', '-6', password]).output.strip()
3330
if target_hostname is None:
3431
target_hostname = "xcp-ng-" + "".join(
3532
random.choice(string.ascii_lowercase) for i in range(5)
@@ -70,7 +67,9 @@ def generate_answerfile(directory, installer, hostname_or_ip, target_hostname, a
7067
raise Exception(f"Unknown action: `{action}`")
7168

7269
def is_ip_active(ip):
73-
return not os.system(f"ping -c 3 -W 10 {ip} > /dev/null 2>&1")
70+
# 3 tries with a timeout of 10 sec for each ICMP request
71+
return local_cmd(['ping', '-c', '3', '-W', '10', ip],
72+
check=False).returncode == 0
7473

7574
def is_ssh_up(ip):
7675
try:

tests/fs_diff/test_fs_diff.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
12
import pytest
23

34
import os
4-
import subprocess
5+
6+
from lib.commands import local_cmd
57

68
# Requirements:
79
# - 2 XCP-ng host of same version
@@ -14,13 +16,7 @@ def test_fs_diff(hosts):
1416

1517
fsdiff = os.path.realpath(f"{os.path.dirname(__file__)}/../../scripts/xcpng-fs-diff.py")
1618

17-
process = subprocess.Popen(
18-
[fsdiff, "--reference-host", f"{hosts[0]}", "--test-host", f"{hosts[1]}", "--json-output"],
19-
stdout=subprocess.PIPE, stderr=subprocess.PIPE
20-
)
21-
stdout, _ = process.communicate()
22-
23-
if process.returncode != 0:
24-
print(stdout.decode())
25-
26-
assert process.returncode == 0
19+
res = local_cmd([fsdiff, "--reference-host", f"{hosts[0]}",
20+
"--test-host", f"{hosts[1]}",
21+
"--json-output"])
22+
assert res.returncode == 0

tests/misc/test_access_links.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22

33
import hashlib
4-
import subprocess
54

65
from lib import commands
76

tests/network/test_vif_allowed_ip.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import ipaddress
44
import os
55

6+
from lib.commands import local_cmd
7+
68
# Requirements:
79
# - one XCP-ng host (--host) >= 8.2 (>= 8.3 for the CIDR tests) with no SDN controller configured
810
# - a VM (--vm)
911

1012
def ip_responsive(ip):
11-
return not os.system(f"ping -c 3 -W 10 {ip} > /dev/null 2>&1")
13+
# 3 tries with a timeout of 10 sec for each ICMP request
14+
return local_cmd(['ping', '-c', '3', '-W', '10', ip],
15+
check=False).returncode == 0
1216

1317
@pytest.mark.small_vm
1418
@pytest.mark.usefixtures("host_no_sdn_controller")

tests/packages/bugtool/test_bugtool.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import pytest
22

3-
import subprocess
4-
53
from lib.host import Host
64

75
# This smoke test runs xen-bugtool and verifies that the archive it generates

tests/packages/netdata/test_netdata.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import pytest
22

3-
import subprocess
4-
53
from lib.netutil import wrap_ip
4+
from lib.commands import local_cmd
65

76
# This test installs netdata and netdata-ui packages, verifies the service status,
87
# checks that the configuration is only accessible from the host, and verifies that
@@ -17,13 +16,9 @@ def __get_headers(host, port, path=None):
1716
url = f"http://{wrap_ip(host.hostname_or_ip)}:{port}"
1817
if path is not None:
1918
url += f"/{path}"
20-
process = subprocess.Popen(
21-
["curl", "-XGET", "-k", "-I", "-s", url],
22-
stdout=subprocess.PIPE,
23-
stderr=subprocess.PIPE
24-
)
25-
stdout, _ = process.communicate()
26-
return stdout.decode().splitlines()
19+
20+
res = local_cmd(["curl", "-XGET", "-k", "-I", "-s", url])
21+
return res.output.splitlines()
2722

2823
# Verify the ActiveState for the netdata service
2924
def test_netdata_service(self, host):

0 commit comments

Comments
 (0)