Skip to content

Commit b811a25

Browse files
committed
[Storage] Use ssh retries in run_ssh_commands
Signed-off-by: Jenia Peimer <jpeimer@redhat.com>
1 parent 58a668a commit b811a25

File tree

10 files changed

+51
-36
lines changed

10 files changed

+51
-36
lines changed

tests/storage/online_resize/utils.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pyhelper_utils.shell import run_ssh_commands
1414
from timeout_sampler import TimeoutExpiredError, TimeoutSampler
1515

16-
from utilities.constants import TIMEOUT_4MIN
16+
from utilities.constants import TIMEOUT_2MIN, TIMEOUT_4MIN, TIMEOUT_5SEC
1717
from utilities.storage import create_dv
1818
from utilities.virt import running_vm
1919

@@ -61,9 +61,16 @@ def cksum_file(vm, filename, create=False):
6161
run_ssh_commands(
6262
host=vm.ssh_exec,
6363
commands=shlex.split(f"dd if=/dev/urandom of={filename} count=100 && sync"),
64+
wait_timeout=TIMEOUT_2MIN,
65+
sleep=TIMEOUT_5SEC,
6466
)
6567

66-
out = run_ssh_commands(host=vm.ssh_exec, commands=shlex.split(f"sha256sum {filename}"))[0]
68+
out = run_ssh_commands(
69+
host=vm.ssh_exec,
70+
commands=shlex.split(f"sha256sum {filename}"),
71+
wait_timeout=TIMEOUT_2MIN,
72+
sleep=TIMEOUT_5SEC,
73+
)[0]
6774
sha256sum = out.split()[0]
6875
LOGGER.info(f"File sha256sum is {sha256sum}")
6976
return sha256sum
@@ -104,7 +111,7 @@ def expand_pvc(dv, size_change):
104111

105112
def get_resize_count(vm):
106113
commands = shlex.split("sudo dmesg | grep -c 'new size' || true")
107-
result = run_ssh_commands(host=vm.ssh_exec, commands=commands)[0]
114+
result = run_ssh_commands(host=vm.ssh_exec, commands=commands, wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC)[0]
108115

109116
return int(result)
110117

@@ -136,7 +143,9 @@ def wait_for_resize(vm, count=1):
136143
if current_resize_count in (desired_count, desired_count + 1):
137144
break
138145
except TimeoutExpiredError:
139-
dmesg = run_ssh_commands(host=vm.ssh_exec, commands=shlex.split("dmesg"))[0]
146+
dmesg = run_ssh_commands(
147+
host=vm.ssh_exec, commands=shlex.split("dmesg"), wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC
148+
)[0]
140149
LOGGER.error(f"Failed to reach resize count {desired_count}.\ndmesg:\n{dmesg}")
141150
raise
142151

tests/storage/snapshots/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
create_windows_directory,
2121
set_permissions,
2222
)
23-
from utilities.constants import TIMEOUT_10MIN, UNPRIVILEGED_USER
23+
from utilities.constants import TIMEOUT_2MIN, TIMEOUT_5SEC, TIMEOUT_10MIN, UNPRIVILEGED_USER
2424

2525
LOGGER = logging.getLogger(__name__)
2626

@@ -88,7 +88,7 @@ def snapshot_dirctory_removed(windows_vm_for_snapshot, windows_snapshot):
8888
cmd = shlex.split(
8989
f'powershell -command "Remove-Item -Path {WINDOWS_DIRECTORY_PATH} -Recurse"',
9090
)
91-
run_ssh_commands(host=windows_vm_for_snapshot.ssh_exec, commands=cmd)
91+
run_ssh_commands(host=windows_vm_for_snapshot.ssh_exec, commands=cmd, wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC)
9292
assert_windows_directory_existence(
9393
expected_result=False,
9494
windows_vm=windows_vm_for_snapshot,
@@ -103,6 +103,6 @@ def file_created_during_snapshot(windows_vm_for_snapshot, windows_snapshot):
103103
cmd = shlex.split(
104104
f'powershell -command "for($i=1; $i -le 100; $i++){{$i| Out-File -FilePath {file} -Append}}"',
105105
)
106-
run_ssh_commands(host=windows_vm_for_snapshot.ssh_exec, commands=cmd)
106+
run_ssh_commands(host=windows_vm_for_snapshot.ssh_exec, commands=cmd, wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC)
107107
windows_snapshot.wait_snapshot_done(timeout=TIMEOUT_10MIN)
108108
windows_vm_for_snapshot.stop(wait=True)

tests/storage/snapshots/test_snapshots.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
from tests.storage.snapshots.utils import (
2323
expected_output_after_restore,
2424
fail_to_create_snapshot_no_permissions,
25-
run_command_on_vm_and_check_output,
2625
start_windows_vm_after_restore,
2726
)
2827
from tests.storage.utils import assert_windows_directory_existence
2928
from utilities.constants import LS_COMMAND, TIMEOUT_1MIN, TIMEOUT_10SEC
29+
from utilities.storage import run_command_on_vm_and_check_output
3030
from utilities.virt import restart_vm_wait_for_running_vm, running_vm
3131

3232
LOGGER = logging.getLogger(__name__)

tests/storage/snapshots/utils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import shlex
2-
31
import pytest
42
from kubernetes.client.rest import ApiException
53
from ocp_resources.virtual_machine_snapshot import VirtualMachineSnapshot
6-
from pyhelper_utils.shell import run_ssh_commands
74

85
from tests.storage.snapshots.constants import ERROR_MSG_USER_CANNOT_CREATE_VM_SNAPSHOTS
96
from utilities.constants import TIMEOUT_10MIN

tests/storage/storage_migration/conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
OS_FLAVOR_FEDORA,
3131
OS_FLAVOR_RHEL,
3232
OS_FLAVOR_WINDOWS,
33+
TIMEOUT_2MIN,
34+
TIMEOUT_5SEC,
3335
U1_SMALL,
3436
Images,
3537
)
@@ -336,6 +338,8 @@ def vm_with_mounted_hotplugged_disk(vm_for_storage_class_migration_with_hotplugg
336338
f"sudo mount {HOTPLUGGED_DEVICE} {MOUNT_HOTPLUGGED_DEVICE_PATH}",
337339
]
338340
],
341+
wait_timeout=TIMEOUT_2MIN,
342+
sleep=TIMEOUT_5SEC,
339343
)
340344
yield vm_for_storage_class_migration_with_hotplugged_volume
341345

@@ -347,6 +351,8 @@ def written_file_to_the_mounted_hotplugged_disk(vm_with_mounted_hotplugged_disk)
347351
commands=shlex.split(
348352
f"echo '{CONTENT}' | sudo tee {MOUNT_HOTPLUGGED_DEVICE_PATH}/{FILE_BEFORE_STORAGE_MIGRATION}"
349353
),
354+
wait_timeout=TIMEOUT_2MIN,
355+
sleep=TIMEOUT_5SEC,
350356
)
351357
yield vm_with_mounted_hotplugged_disk
352358

@@ -400,7 +406,7 @@ def written_file_to_windows_vms_before_migration(booted_vms_for_storage_class_mi
400406
cmd = shlex.split(
401407
f'powershell -command "\\"{CONTENT}\\" | Out-File -FilePath {WINDOWS_FILE_WITH_PATH} -Append"'
402408
)
403-
run_ssh_commands(host=vm.ssh_exec, commands=cmd)
409+
run_ssh_commands(host=vm.ssh_exec, commands=cmd, wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC)
404410
yield booted_vms_for_storage_class_migration
405411

406412

tests/storage/storage_migration/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
NO_STORAGE_CLASS_FAILURE_MESSAGE,
1414
)
1515
from tests.storage.utils import check_file_in_vm
16-
from utilities.constants import TIMEOUT_10MIN, TIMEOUT_10SEC
16+
from utilities.constants import TIMEOUT_2MIN, TIMEOUT_5SEC, TIMEOUT_10MIN, TIMEOUT_10SEC
1717
from utilities.exceptions import StorageMigrationError
1818
from utilities.virt import VirtualMachineForTests, get_vm_boot_time
1919

@@ -89,14 +89,19 @@ def get_storage_class_for_storage_migration(storage_class: str, cluster_storage_
8989

9090
def verify_file_in_hotplugged_disk(vm: VirtualMachineForTests, file_name: str, file_content: str) -> None:
9191
output = run_ssh_commands(
92-
host=vm.ssh_exec, commands=shlex.split(f"cat {MOUNT_HOTPLUGGED_DEVICE_PATH}/{file_name}")
92+
host=vm.ssh_exec,
93+
commands=shlex.split(f"cat {MOUNT_HOTPLUGGED_DEVICE_PATH}/{file_name}"),
94+
wait_timeout=TIMEOUT_2MIN,
95+
sleep=TIMEOUT_5SEC,
9396
)[0]
9497
assert output.strip() == file_content, f"'{output}' does not equal '{file_content}'"
9598

9699

97100
def verify_file_in_windows_vm(windows_vm: VirtualMachineForTests, file_name_with_path: str, file_content: str) -> None:
98101
cmd = shlex.split(f'powershell -command "Get-Content {file_name_with_path}"')
99-
out = run_ssh_commands(host=windows_vm.ssh_exec, commands=cmd)[0].strip()
102+
out = run_ssh_commands(host=windows_vm.ssh_exec, commands=cmd, wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC)[
103+
0
104+
].strip()
100105
assert out.strip() == file_content, f"'{out}' does not equal '{file_content}'"
101106

102107

tests/storage/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
CDI_UPLOADPROXY,
3737
LS_COMMAND,
3838
TIMEOUT_2MIN,
39+
TIMEOUT_5SEC,
3940
TIMEOUT_20SEC,
4041
TIMEOUT_30MIN,
4142
Images,
@@ -468,15 +469,17 @@ def assert_windows_directory_existence(
468469
expected_result: bool, windows_vm: VirtualMachineForTests, directory_path: str
469470
) -> None:
470471
cmd = shlex.split(f'powershell -command "Test-Path -Path {directory_path}"')
471-
out = run_ssh_commands(host=windows_vm.ssh_exec, commands=cmd)[0].strip()
472+
out = run_ssh_commands(host=windows_vm.ssh_exec, commands=cmd, wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC)[
473+
0
474+
].strip()
472475
assert expected_result == ast.literal_eval(out), f"Directory exist: {out}, expected result: {expected_result}"
473476

474477

475478
def create_windows_directory(windows_vm: VirtualMachineForTests, directory_path: str) -> None:
476479
cmd = shlex.split(
477480
f'powershell -command "New-Item -Path {directory_path} -ItemType Directory"',
478481
)
479-
run_ssh_commands(host=windows_vm.ssh_exec, commands=cmd)
482+
run_ssh_commands(host=windows_vm.ssh_exec, commands=cmd, wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC)
480483
assert_windows_directory_existence(
481484
expected_result=True,
482485
windows_vm=windows_vm,

tests/storage/vm_export/conftest.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import base64
6-
import shlex
76
from subprocess import check_output
87

98
import pytest
@@ -18,13 +17,12 @@
1817
)
1918
from ocp_resources.virtual_machine_export import VirtualMachineExport
2019
from ocp_resources.virtual_machine_snapshot import VirtualMachineSnapshot
21-
from pyhelper_utils.shell import run_ssh_commands
2220

2321
from tests.storage.constants import TEST_FILE_CONTENT, TEST_FILE_NAME
2422
from tests.storage.vm_export.utils import create_blank_dv_by_specific_user, get_manifest_from_vmexport, get_manifest_url
2523
from utilities.constants import OS_FLAVOR_RHEL, U1_SMALL, UNPRIVILEGED_PASSWORD, UNPRIVILEGED_USER
2624
from utilities.infra import create_ns, login_with_user_password
27-
from utilities.storage import data_volume_template_with_source_ref_dict
25+
from utilities.storage import data_volume_template_with_source_ref_dict, write_file_via_ssh
2826
from utilities.virt import VirtualMachineForTests, running_vm
2927

3028

@@ -218,10 +216,7 @@ def rhel_vm_for_snapshot_with_content(
218216
),
219217
) as vm:
220218
running_vm(vm=vm)
221-
222-
cmd = shlex.split(f"echo '{TEST_FILE_CONTENT}' > {TEST_FILE_NAME} && sync")
223-
run_ssh_commands(host=vm.ssh_exec, commands=cmd)
224-
219+
write_file_via_ssh(vm=vm, filename=TEST_FILE_CONTENT, content=TEST_FILE_NAME)
225220
yield vm
226221

227222

tests/storage/vm_export/test_vm_export.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from ocp_resources.persistent_volume_claim import PersistentVolumeClaim
1010
from ocp_resources.resource import Resource
1111
from ocp_resources.virtual_machine_export import VirtualMachineExport
12-
from pyhelper_utils.shell import run_ssh_commands
1312

1413
from tests.storage.constants import TEST_FILE_CONTENT, TEST_FILE_NAME
1514
from utilities.infra import run_virtctl_command
15+
from utilities.storage import run_command_on_vm_and_check_output
1616
from utilities.virt import running_vm
1717

1818
VIRTUALMACHINEEXPORTS = "virtualmachineexports"
@@ -62,12 +62,8 @@ def test_vmexport_snapshot_manifests(
6262
vm_from_vmexport,
6363
):
6464
running_vm(vm=vm_from_vmexport)
65-
66-
result = run_ssh_commands(host=vm_from_vmexport.ssh_exec, commands=shlex.split(f"cat {TEST_FILE_NAME}"))
67-
file_content = result[0].strip()
68-
69-
assert file_content == TEST_FILE_CONTENT, (
70-
f"Unexpected content in {TEST_FILE_NAME}: got '{file_content}', expected '{TEST_FILE_CONTENT}'"
65+
run_command_on_vm_and_check_output(
66+
vm=vm_from_vmexport, command=f"cat {TEST_FILE_NAME}", expected_result=TEST_FILE_CONTENT
7167
)
7268

7369

utilities/storage.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,7 @@ def write_file_via_ssh(vm: virt_util.VirtualMachineForTests, filename: str, cont
643643
content: Content to write to the file
644644
"""
645645
cmd = shlex.split(f"echo {shlex.quote(content)} > {shlex.quote(filename)} && sync")
646-
run_ssh_commands(host=vm.ssh_exec, commands=cmd)
647-
646+
run_ssh_commands(host=vm.ssh_exec, commands=cmd, wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC)
648647

649648

650649
def run_command_on_vm_and_check_output(vm, command, expected_result):
@@ -661,6 +660,8 @@ def run_command_on_vm_and_check_output(vm, command, expected_result):
661660
cmd_output = run_ssh_commands(
662661
host=vm.ssh_exec,
663662
commands=shlex.split(f"bash -c {shlex.quote(command)}"),
663+
wait_timeout=TIMEOUT_2MIN,
664+
sleep=TIMEOUT_5SEC,
664665
)[0].strip()
665666
expected_result = expected_result.strip()
666667
assert expected_result in cmd_output, f"Expected '{expected_result}' in output '{cmd_output}'"
@@ -673,9 +674,10 @@ def run_command_on_cirros_vm_and_check_output(vm, command, expected_result):
673674

674675

675676
def assert_disk_serial(vm, command=shlex.split("sudo ls /dev/disk/by-id")):
676-
assert HOTPLUG_DISK_SERIAL in run_ssh_commands(host=vm.ssh_exec, commands=command)[0], (
677-
f"hotplug disk serial id {HOTPLUG_DISK_SERIAL} is not in VM"
678-
)
677+
assert (
678+
HOTPLUG_DISK_SERIAL
679+
in run_ssh_commands(host=vm.ssh_exec, commands=command, wait_timeout=TIMEOUT_2MIN, sleep=TIMEOUT_5SEC)[0]
680+
), f"hotplug disk serial id {HOTPLUG_DISK_SERIAL} is not in VM"
679681

680682

681683
def assert_hotplugvolume_nonexist(vm):
@@ -811,6 +813,8 @@ def check_disk_count_in_vm(vm):
811813
out = run_ssh_commands(
812814
host=vm.ssh_exec,
813815
commands=[shlex.split("lsblk | grep disk | grep -v SWAP| wc -l")],
816+
wait_timeout=TIMEOUT_2MIN,
817+
sleep=TIMEOUT_5SEC,
814818
)[0].strip()
815819
assert out == str(len(vm.instance.spec.template.spec.domain.devices.disks)), (
816820
"Failed to verify actual disk count against VMI"

0 commit comments

Comments
 (0)