Skip to content

Commit f38eae4

Browse files
committed
refactor(tests): move return code check for ssh into ssh.run
Ssh connection should be considered an error by default. In order to unify handling of this case, move check for non zero return code into the function itself. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent d27e697 commit f38eae4

File tree

9 files changed

+32
-54
lines changed

9 files changed

+32
-54
lines changed

tests/framework/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,7 @@ def get_free_mem_ssh(ssh_connection):
350350
:param ssh_connection: connection to the guest
351351
:return: available mem column output of 'free'
352352
"""
353-
_, stdout, stderr = ssh_connection.run("cat /proc/meminfo | grep MemAvailable")
354-
assert stderr == ""
353+
stdout, _ = ssh_connection.run("cat /proc/meminfo | grep MemAvailable")
355354

356355
# Split "MemAvailable: 123456 kB" and validate it
357356
meminfo_data = stdout.split()
@@ -531,7 +530,7 @@ def generate_mmds_session_token(ssh_connection, ipv4_address, token_ttl):
531530
cmd += " -X PUT"
532531
cmd += ' -H "X-metadata-token-ttl-seconds: {}"'.format(token_ttl)
533532
cmd += " http://{}/latest/api/token".format(ipv4_address)
534-
_, stdout, _ = ssh_connection.run(cmd)
533+
stdout, _ = ssh_connection.run(cmd)
535534
token = stdout
536535

537536
return token
@@ -625,8 +624,7 @@ def guest_run_fio_iteration(ssh_connection, iteration):
625624
--output /tmp/fio{} > /dev/null &""".format(
626625
iteration
627626
)
628-
exit_code, _, stderr = ssh_connection.run(fio)
629-
assert exit_code == 0, stderr
627+
ssh_connection.run(fio)
630628

631629

632630
def check_filesystem(ssh_connection, disk_fmt, disk):

tests/framework/utils_vsock.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ def _copy_vsock_data_to_guest(ssh_connection, blob_path, vm_blob_path, vsock_hel
214214
# Copy the data file and a vsock helper to the guest.
215215

216216
cmd = "mkdir -p /tmp/vsock"
217-
ecode, _, _ = ssh_connection.run(cmd)
218-
assert ecode == 0, "Failed to set up tmpfs drive on the guest."
217+
ssh_connection.run(cmd)
219218

220219
ssh_connection.scp_put(vsock_helper, "/tmp/vsock_helper")
221220
ssh_connection.scp_put(blob_path, vm_blob_path)

tests/host_tools/network.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ def run(self, cmd_string, timeout=None, *, check=False, debug=False):
122122
if debug:
123123
command.insert(1, "-vvv")
124124

125-
return self._exec(command, timeout, check=check)
125+
exit_code, stdout, stderr = self._exec(command, timeout, check=check)
126+
if exit_code != 0:
127+
logger = logging.getLogger("ssh connection")
128+
logger.error("while running: %s", cmd)
129+
logger.error("stdout: %s", stdout)
130+
logger.error("stderr: %s", stderr)
131+
assert False, "ssh failed"
132+
return stdout, stderr
126133

127134
def check_output(self, cmd_string, timeout=None, *, debug=False):
128135
"""Same as `run`, but raises an exception on non-zero return code of remote command"""
@@ -185,7 +192,7 @@ def mac_from_ip(ip_address):
185192
def get_guest_net_if_name(ssh_connection, guest_ip):
186193
"""Get network interface name based on its IPv4 address."""
187194
cmd = "ip a s | grep '{}' | tr -s ' ' | cut -d' ' -f6".format(guest_ip)
188-
_, guest_if_name, _ = ssh_connection.run(cmd)
195+
guest_if_name, _ = ssh_connection.run(cmd)
189196
if_name = guest_if_name.strip()
190197
return if_name if if_name != "" else None
191198

tests/integration_tests/functional/test_balloon.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,11 @@ def get_rss_from_pmap():
4141

4242
def lower_ssh_oom_chance(ssh_connection):
4343
"""Lure OOM away from ssh process"""
44-
logger = logging.getLogger("lower_ssh_oom_chance")
45-
4644
cmd = "cat /run/sshd.pid"
47-
exit_code, stdout, stderr = ssh_connection.run(cmd)
48-
# add something to the logs for troubleshooting
49-
if exit_code != 0:
50-
logger.error("while running: %s", cmd)
51-
logger.error("stdout: %s", stdout)
52-
logger.error("stderr: %s", stderr)
53-
45+
stdout, _ = ssh_connection.run(cmd)
5446
for pid in stdout.split(" "):
5547
cmd = f"choom -n -1000 -p {pid}"
56-
exit_code, stdout, stderr = ssh_connection.run(cmd)
57-
if exit_code != 0:
58-
logger.error("while running: %s", cmd)
59-
logger.error("stdout: %s", stdout)
60-
logger.error("stderr: %s", stderr)
48+
ssh_connection.run(cmd)
6149

6250

6351
def make_guest_dirty_memory(ssh_connection, amount_mib=32):
@@ -68,12 +56,7 @@ def make_guest_dirty_memory(ssh_connection, amount_mib=32):
6856

6957
cmd = f"/usr/local/bin/fillmem {amount_mib}"
7058
try:
71-
exit_code, stdout, stderr = ssh_connection.run(cmd, timeout=1.0)
72-
# add something to the logs for troubleshooting
73-
if exit_code != 0:
74-
logger.error("while running: %s", cmd)
75-
logger.error("stdout: %s", stdout)
76-
logger.error("stderr: %s", stderr)
59+
ssh_connection.run(cmd, timeout=1.0)
7760
except TimeoutExpired:
7861
# It's ok if this expires. Sometimes the SSH connection
7962
# gets killed by the OOM killer *after* the fillmem program

tests/integration_tests/functional/test_drive_vhost_user.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ def _check_block_size(ssh_connection, dev_path, size):
1515
"""
1616
Checks the size of the block device.
1717
"""
18-
_, stdout, stderr = ssh_connection.run("blockdev --getsize64 {}".format(dev_path))
19-
assert stderr == ""
18+
stdout, _ = ssh_connection.run("blockdev --getsize64 {}".format(dev_path))
2019
assert stdout.strip() == str(size)
2120

2221

tests/integration_tests/functional/test_drive_virtio.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,12 @@ def test_flush(uvm_plain_rw, io_engine):
354354

355355

356356
def _check_block_size(ssh_connection, dev_path, size):
357-
_, stdout, stderr = ssh_connection.run("blockdev --getsize64 {}".format(dev_path))
358-
assert stderr == ""
357+
stdout, _ = ssh_connection.run("blockdev --getsize64 {}".format(dev_path))
359358
assert stdout.strip() == str(size)
360359

361360

362361
def _check_file_size(ssh_connection, dev_path, size):
363-
_, stdout, stderr = ssh_connection.run("stat --format=%s {}".format(dev_path))
364-
assert stderr == ""
362+
stdout, _ = ssh_connection.run("stat --format=%s {}".format(dev_path))
365363
assert stdout.strip() == str(size)
366364

367365

@@ -379,7 +377,5 @@ def _check_drives(test_microvm, assert_dict, keys_array):
379377

380378

381379
def _check_mount(ssh_connection, dev_path):
382-
_, _, stderr = ssh_connection.run(f"mount {dev_path} /tmp", timeout=30.0)
383-
assert stderr == ""
384-
_, _, stderr = ssh_connection.run("umount /tmp", timeout=30.0)
385-
assert stderr == ""
380+
ssh_connection.run(f"mount {dev_path} /tmp", timeout=30.0)
381+
ssh_connection.run("umount /tmp", timeout=30.0)

tests/integration_tests/functional/test_mmds.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,14 @@ def test_guest_mmds_hang(uvm_plain, version):
503503
get_cmd += f" http://{DEFAULT_IPV4}/"
504504

505505
if version == "V1":
506-
_, stdout, _ = ssh_connection.run(get_cmd)
506+
stdout, _ = ssh_connection.run(get_cmd)
507507
assert "Invalid request" in stdout
508508
else:
509509
# Generate token.
510510
token = generate_mmds_session_token(ssh_connection, DEFAULT_IPV4, token_ttl=60)
511511

512512
get_cmd += ' -H "X-metadata-token: {}"'.format(token)
513-
_, stdout, _ = ssh_connection.run(get_cmd)
513+
stdout, _ = ssh_connection.run(get_cmd)
514514
assert "Invalid request" in stdout
515515

516516
# Do the same for a PUT request.
@@ -522,7 +522,7 @@ def test_guest_mmds_hang(uvm_plain, version):
522522
cmd += ' -d "some body"'
523523
cmd += " http://{}/".format(DEFAULT_IPV4)
524524

525-
_, stdout, _ = ssh_connection.run(cmd)
525+
stdout, _ = ssh_connection.run(cmd)
526526
assert "Invalid request" in stdout
527527

528528

@@ -705,7 +705,7 @@ def test_mmds_v2_negative(uvm_plain):
705705
run_guest_cmd(ssh_connection, put_cmd.format(ttl), expected)
706706

707707
# Valid `PUT` request to generate token.
708-
_, stdout, _ = ssh_connection.run(put_cmd.format(1))
708+
stdout, _ = ssh_connection.run(put_cmd.format(1))
709709
token = stdout
710710
assert len(token) > 0
711711

tests/integration_tests/functional/test_net_config_space.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ def _find_iomem_range(ssh_connection, dev_name):
219219
# its contents and grep for the VirtIO device name, which
220220
# with ACPI is "LNRO0005:XY".
221221
cmd = f"cat /proc/iomem | grep -m 1 {dev_name}"
222-
rc, stdout, stderr = ssh_connection.run(cmd)
223-
assert rc == 0, stderr
222+
stdout, _ = ssh_connection.run(cmd)
224223

225224
# Take range in the form 'start-end' from line. The line looks like this:
226225
# d00002000-d0002fff : LNRO0005:02
@@ -246,7 +245,7 @@ def _get_net_mem_addr_base_x86_acpi(ssh_connection, if_name):
246245
# that corresponds to the virtio-net device.
247246
cmd = "ls {}/{}/virtio{}/net"
248247
for idx, dev in enumerate(virtio_devs):
249-
_, guest_if_name, _ = ssh_connection.run(
248+
guest_if_name, _ = ssh_connection.run(
250249
cmd.format(sys_virtio_mmio_cmdline, dev, idx)
251250
)
252251
if guest_if_name.strip() == if_name:
@@ -259,8 +258,7 @@ def _get_net_mem_addr_base_x86_cmdline(ssh_connection, if_name):
259258
"""Check for net device memory start address via command line arguments"""
260259
sys_virtio_mmio_cmdline = "/sys/devices/virtio-mmio-cmdline/"
261260
cmd = "ls {} | grep virtio-mmio. | sed 's/virtio-mmio.//'"
262-
exit_code, stdout, stderr = ssh_connection.run(cmd.format(sys_virtio_mmio_cmdline))
263-
assert exit_code == 0, stderr
261+
stdout, _ = ssh_connection.run(cmd.format(sys_virtio_mmio_cmdline))
264262
virtio_devs_idx = stdout.strip().split()
265263

266264
cmd = "cat /proc/cmdline"
@@ -278,7 +276,7 @@ def _get_net_mem_addr_base_x86_cmdline(ssh_connection, if_name):
278276

279277
cmd = "ls {}/virtio-mmio.{}/virtio{}/net"
280278
for idx in virtio_devs_idx:
281-
_, guest_if_name, _ = ssh_connection.run(
279+
guest_if_name, _ = ssh_connection.run(
282280
cmd.format(sys_virtio_mmio_cmdline, idx, idx)
283281
)
284282
if guest_if_name.strip() == if_name:
@@ -299,8 +297,7 @@ def _get_net_mem_addr_base(ssh_connection, if_name):
299297
if platform.machine() == "aarch64":
300298
sys_virtio_mmio_cmdline = "/sys/devices/platform"
301299
cmd = "ls {} | grep .virtio_mmio".format(sys_virtio_mmio_cmdline)
302-
rc, stdout, _ = ssh_connection.run(cmd)
303-
assert rc == 0
300+
stdout, _ = ssh_connection.run(cmd)
304301

305302
virtio_devs = stdout.split()
306303
devs_addr = list(map(lambda dev: dev.split(".")[0], virtio_devs))
@@ -310,7 +307,7 @@ def _get_net_mem_addr_base(ssh_connection, if_name):
310307
# accordingly when parsed inside `change_config_space.c`.
311308
hex_prefix = "0x"
312309
for idx, dev in enumerate(virtio_devs):
313-
_, guest_if_name, _ = ssh_connection.run(
310+
guest_if_name, _ = ssh_connection.run(
314311
cmd.format(sys_virtio_mmio_cmdline, dev, idx)
315312
)
316313
if guest_if_name.strip() == if_name:

tests/integration_tests/functional/test_snapshot_basic.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def _get_guest_drive_size(ssh_connection, guest_dev_name="/dev/vdb"):
4444
# `lsblk` command outputs 2 lines to STDOUT:
4545
# "SIZE" and the size of the device, in bytes.
4646
blksize_cmd = "LSBLK_DEBUG=all lsblk -b {} --output SIZE".format(guest_dev_name)
47-
rc, stdout, stderr = ssh_connection.run(blksize_cmd)
48-
assert rc == 0, stderr
47+
stdout, _ = ssh_connection.run(blksize_cmd)
4948
lines = stdout.split("\n")
5049
return lines[1].strip()
5150

0 commit comments

Comments
 (0)