Skip to content
This repository was archived by the owner on Apr 7, 2022. It is now read-only.

Commit c1937a9

Browse files
authored
[1LP][RFR] Fix SSH timeout issue in verify_revert_snapshot (#9952)
* Add wait_pingable to verify_revert_snapshot * Remove data files * Remove extra .success in run_command call
1 parent b07756e commit c1937a9

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

cfme/infrastructure/virtual_machines.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,7 @@ def active(self):
812812
if self.parent_vm.provider.one_of(RHEVMProvider):
813813
child = view.tree.child_items(root_item)
814814
last_snapshot = view.tree.child_items(child[0])[0]
815-
return (len(child) == 1 and
816-
child[0].text == 'Active VM (Active)' and
815+
return (child[0].text == 'Active VM (Active)' and
817816
last_snapshot.text == title)
818817
else:
819818
return has_child(view.tree, f'{title} (Active)', root_item)

cfme/tests/infrastructure/test_snapshot.py

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1+
from contextlib import closing
2+
13
import fauxfactory
24
import pytest
35
from wrapanapi import VmState
46

57
from cfme import test_requirements
68
from cfme.automate.explorer.domain import DomainCollection
79
from cfme.automate.simulation import simulate
10+
from cfme.base.credential import SSHCredential
811
from cfme.infrastructure.provider.rhevm import RHEVMProvider
912
from cfme.infrastructure.provider.virtualcenter import VMwareProvider
1013
from cfme.infrastructure.virtual_machines import InfraVm
1114
from cfme.infrastructure.virtual_machines import InfraVmSnapshotAddView
1215
from cfme.infrastructure.virtual_machines import InfraVmSnapshotView
1316
from cfme.utils.appliance.implementations.ui import navigate_to
1417
from cfme.utils.blockers import BZ
15-
from cfme.utils.conf import credentials
1618
from cfme.utils.log import logger
1719
from cfme.utils.log_validator import LogValidator
1820
from cfme.utils.path import data_path
19-
from cfme.utils.ssh import SSHClient
21+
from cfme.utils.ssh import connect_ssh
2022
from cfme.utils.wait import wait_for
2123

2224
pytestmark = [
@@ -174,31 +176,25 @@ def test_delete_all_snapshots(create_vm, provider):
174176

175177
def verify_revert_snapshot(full_test_vm, provider, soft_assert, register_event, request,
176178
active_snapshot=False):
179+
SSH_READY_TIMEOUT = 300
177180
if provider.one_of(RHEVMProvider):
178181
# RHV snapshots have only description, no name
179182
snapshot1 = new_snapshot(full_test_vm, has_name=False)
180183
else:
181184
snapshot1 = new_snapshot(full_test_vm)
182185
full_template = getattr(provider.data.templates, 'full_template')
183-
# Define parameters of the ssh connection
184-
ssh_kwargs = {
185-
'hostname': snapshot1.parent_vm.mgmt.ip,
186-
'username': credentials[full_template.creds]['username'],
187-
'password': credentials[full_template.creds]['password']
188-
}
189-
ssh_client = SSHClient(**ssh_kwargs)
190-
# We need to wait for ssh to become available on the vm, it can take a while. Without
191-
# this wait, the ssh command would fail with 'port 22 not available' error.
192-
# Easiest way to solve this is just mask the exception with 'handle_exception = True'
193-
# and wait for successful completition of the ssh command.
194-
# The 'fail_func' ensures we close the connection that failed with exception.
195-
# Without this, the connection would hang there and wait_for would fail with timeout.
196-
wait_for(lambda: ssh_client.run_command('touch snapshot1.txt').success, num_sec=400,
197-
delay=20, handle_exception=True, fail_func=ssh_client.close(),
198-
message="Waiting for successful SSH connection")
199-
# Create first snapshot
200-
snapshot1.create()
201-
ssh_client.run_command('touch snapshot2.txt')
186+
creds = SSHCredential.from_config(full_template.creds)
187+
188+
# We need to wait for ssh to become available on the vm, it can take a while.
189+
# connect_ssh will iterate over "all_ips" on the VM and return a client when it can connect
190+
191+
with closing(connect_ssh(full_test_vm.mgmt, creds, num_sec=SSH_READY_TIMEOUT)) as ssh_client:
192+
ssh_client.run_command('touch snapshot1.txt')
193+
# Create first snapshot
194+
snapshot1.create()
195+
196+
# Assuming creating snapshot shouldn't break the ssh connection...
197+
ssh_client.run_command('touch snapshot2.txt')
202198

203199
# If we are not testing 'revert to active snapshot' situation, we create another snapshot
204200
if not active_snapshot:
@@ -212,34 +208,30 @@ def verify_revert_snapshot(full_test_vm, provider, soft_assert, register_event,
212208
if provider.one_of(RHEVMProvider):
213209
full_test_vm.power_control_from_cfme(option=full_test_vm.POWER_OFF, cancel=False)
214210
full_test_vm.wait_for_vm_state_change(
215-
desired_state=full_test_vm.STATE_OFF, timeout=900)
211+
desired_state=full_test_vm.STATE_OFF, timeout=400)
216212

217213
snapshot1.revert_to()
218214
# Wait for the snapshot to become active
219215
logger.info('Waiting for vm %s to become active', snapshot1.name)
220-
wait_for(lambda: snapshot1.active, num_sec=300, delay=20, fail_func=provider.browser.refresh,
216+
wait_for(lambda: snapshot1.active, num_sec=700, delay=30, fail_func=provider.browser.refresh,
221217
message="Waiting for the first snapshot to become active")
222218
# VM state after revert should be OFF
223219
full_test_vm.wait_for_vm_state_change(desired_state=full_test_vm.STATE_OFF, timeout=720)
224220
# Let's power it ON again
225221
full_test_vm.power_control_from_cfme(option=full_test_vm.POWER_ON, cancel=False)
226-
full_test_vm.wait_for_vm_state_change(desired_state=full_test_vm.STATE_ON, timeout=900)
222+
full_test_vm.wait_for_vm_state_change(desired_state=full_test_vm.STATE_ON, timeout=400)
227223
soft_assert(full_test_vm.mgmt.is_running, "vm not running")
228224
# Wait for successful ssh connection
229-
wait_for(lambda: ssh_client.run_command('test -e snapshot1.txt').success,
230-
num_sec=400, delay=10, handle_exception=True, fail_func=ssh_client.close(),
231-
message="Waiting for successful SSH connection after revert")
232-
try:
233-
result = ssh_client.run_command('test -e snapshot1.txt')
234-
assert result.success # file found, RC=0
235-
result = ssh_client.run_command('test -e snapshot2.txt')
236-
assert result.failed # file not found, RC=1
237-
logger.info('Revert to snapshot %s successful', snapshot1.name)
238-
except Exception:
239-
logger.exception('Revert to snapshot %s Failed', snapshot1.name)
240-
ssh_client.close()
225+
with closing(connect_ssh(full_test_vm.mgmt, creds, num_sec=SSH_READY_TIMEOUT)) as ssh_client:
226+
assert ssh_client.run_command('test -e snapshot1.txt').success
227+
# This checks the exit status is 1 -- file doesn't exist.
228+
assert ssh_client.run_command('test -e snapshot2.txt') == 1
241229

242230

231+
@pytest.mark.rhv1
232+
@pytest.mark.meta(
233+
blockers=[BZ(1805803, unblock=lambda provider: not provider.one_of(RHEVMProvider),
234+
ignore_bugs={1745065})], automates=[1805803])
243235
@pytest.mark.parametrize('create_vm', ['full_template'], indirect=True)
244236
def test_verify_revert_snapshot(create_vm, provider, soft_assert, register_event, request):
245237
"""Tests revert snapshot

0 commit comments

Comments
 (0)