def disable_forgery_protection(): starttime = time.time() ssh_client = SSHClient() logger.info('Turning off "allow_forgery_protection"') ssh_client.run_command( "sed -i \'s/allow_forgery_protection = true/allow_forgery_protection = false/\' " "/var/www/miq/vmdb/config/environments/production.rb") ssh_client.run_command("service evmserverd restart") ssh_client.close() timediff = time.time() - starttime logger.info( 'Turned off "allow_forgery_protection" in: {}'.format(timediff)) yield starttime = time.time() ssh_client = SSHClient() logger.info('Turning on "allow_forgery_protection"') ssh_client.run_command( "sed -i \'s/allow_forgery_protection = false/allow_forgery_protection = true/\' " "/var/www/miq/vmdb/config/environments/production.rb") ssh_client.run_command("service evmserverd restart") ssh_client.close() timediff = time.time() - starttime logger.info('Turned on "allow_forgery_protection" in: {}'.format(timediff))
def net_check_remote(port, addr=None, machine_addr=None, ssh_creds=None, force=False): """Checks the availability of a port from outside using another machine (over SSH)""" from cfme.utils.ssh import SSHClient port = int(port) if not addr: addr = my_ip_address() if port not in _ports[addr] or force: if not machine_addr: machine_addr = store.current_appliance.hostname if not ssh_creds: ssh_client = store.current_appliance.ssh_client else: ssh_client = SSHClient(hostname=machine_addr, username=ssh_creds['username'], password=ssh_creds['password']) with ssh_client: # on exception => fails with return code 1 cmd = '''python2 -c " import sys, socket addr = socket.gethostbyname('%s') socket.create_connection((addr, %d), timeout=10) sys.exit(0) "''' % (addr, port) result = ssh_client.run_command(cmd) _ports[addr][port] = result.success return _ports[addr][port]
def get_config_list(ssh_client: SSHClient): # Obtain network configuration script for eth0 and store it in a list network_cfg_cmd = ssh_client.run_command( 'cat /etc/sysconfig/network-scripts/ifcfg-eth0') assert network_cfg_cmd.success config_list = network_cfg_cmd.output.split('\n') return config_list
def net_check_remote(port, addr=None, machine_addr=None, ssh_creds=None, force=False): """Checks the availability of a port from outside using another machine (over SSH)""" from cfme.utils.ssh import SSHClient port = int(port) if not addr: addr = my_ip_address() if port not in _ports[addr] or force: if not machine_addr: machine_addr = urlparse.urlparse(store.base_url).hostname if not ssh_creds: ssh_client = store.current_appliance.ssh_client else: ssh_client = SSHClient( hostname=machine_addr, username=ssh_creds['username'], password=ssh_creds['password'] ) with ssh_client: # on exception => fails with return code 1 cmd = '''python -c " import sys, socket addr = socket.gethostbyname('%s') socket.create_connection((addr, %d), timeout=10) sys.exit(0) "''' % (addr, port) ret, out = ssh_client.run_command(cmd) if ret == 0: _ports[addr][port] = True else: _ports[addr][port] = False return _ports[addr][port]
def checks(self, ssh_client: SSHClient, config_list: List[str] = None): if config_list is None: config_list = self.get_config_list(ssh_client) # Compare contents of network script with cloud-init payload assert f'BOOTPROTO={self.bootproto}' in config_list # Check that correct hostname has been set hostname_cmd = ssh_client.run_command('hostname') assert hostname_cmd.success assert hostname_cmd.output.strip() == self.payload['hostname']
def verify_revert_snapshot(full_test_vm, provider, soft_assert, register_event, request, active_snapshot=False): if provider.one_of(RHEVMProvider): # RHV snapshots have only description, no name snapshot1 = new_snapshot(full_test_vm, has_name=False) else: snapshot1 = new_snapshot(full_test_vm) full_template = getattr(provider.data.templates, 'full_template') # Define parameters of the ssh connection ssh_kwargs = { 'hostname': snapshot1.vm.provider.mgmt.get_ip_address(snapshot1.vm.name), 'username': credentials[full_template.creds]['username'], 'password': credentials[full_template.creds]['password'] } ssh_client = SSHClient(**ssh_kwargs) # We need to wait for ssh to become available on the vm, it can take a while. Without # this wait, the ssh command would fail with 'port 22 not available' error. # Easiest way to solve this is just mask the exception with 'handle_exception = True' # and wait for successful completition of the ssh command. # The 'fail_func' ensures we close the connection that failed with exception. # Without this, the connection would hang there and wait_for would fail with timeout. wait_for(lambda: ssh_client.run_command('touch snapshot1.txt').rc == 0, num_sec=400, delay=20, handle_exception=True, fail_func=ssh_client.close()) # Create first snapshot snapshot1.create() ssh_client.run_command('touch snapshot2.txt') # If we are not testing 'revert to active snapshot' situation, we create another snapshot if not active_snapshot: if provider.one_of(RHEVMProvider): snapshot2 = new_snapshot(full_test_vm, has_name=False) else: snapshot2 = new_snapshot(full_test_vm) snapshot2.create() # VM on RHV provider must be powered off before snapshot revert if provider.one_of(RHEVMProvider): full_test_vm.power_control_from_cfme(option=full_test_vm.POWER_OFF, cancel=False) full_test_vm.wait_for_vm_state_change( desired_state=full_test_vm.STATE_OFF, timeout=900) snapshot1.revert_to() # Wait for the snapshot to become active logger.info('Waiting for vm %s to become active', snapshot1.name) wait_for(lambda: snapshot1.active, num_sec=300, delay=20, fail_func=provider.browser.refresh) # VM state after revert should be OFF full_test_vm.wait_for_vm_state_change(desired_state=full_test_vm.STATE_OFF, timeout=720) # Let's power it ON again full_test_vm.power_control_from_cfme(option=full_test_vm.POWER_ON, cancel=False) full_test_vm.wait_for_vm_state_change(desired_state=full_test_vm.STATE_ON, timeout=900) soft_assert(full_test_vm.provider.mgmt.is_vm_running(full_test_vm.name), "vm not running") # Wait for successful ssh connection wait_for(lambda: ssh_client.run_command('test -e snapshot1.txt').rc == 0, num_sec=400, delay=20, handle_exception=True, fail_func=ssh_client.close()) try: result = ssh_client.run_command('test -e snapshot1.txt') assert not result.rc result = ssh_client.run_command('test -e snapshot2.txt') assert result.rc logger.info('Revert to snapshot %s successful', snapshot1.name) except: logger.exception('Revert to snapshot %s Failed', snapshot1.name) ssh_client.close()
def verify_revert_snapshot(full_test_vm, provider, soft_assert, register_event, request, active_snapshot=False): if provider.one_of(RHEVMProvider): # RHV snapshots have only description, no name snapshot1 = new_snapshot(full_test_vm, has_name=False) else: snapshot1 = new_snapshot(full_test_vm) full_template = getattr(provider.data.templates, 'full_template') # Define parameters of the ssh connection ssh_kwargs = { 'hostname': snapshot1.parent_vm.mgmt.ip, 'username': credentials[full_template.creds]['username'], 'password': credentials[full_template.creds]['password'] } ssh_client = SSHClient(**ssh_kwargs) # We need to wait for ssh to become available on the vm, it can take a while. Without # this wait, the ssh command would fail with 'port 22 not available' error. # Easiest way to solve this is just mask the exception with 'handle_exception = True' # and wait for successful completition of the ssh command. # The 'fail_func' ensures we close the connection that failed with exception. # Without this, the connection would hang there and wait_for would fail with timeout. wait_for(lambda: ssh_client.run_command('touch snapshot1.txt').success, num_sec=400, delay=20, handle_exception=True, fail_func=ssh_client.close(), message="Waiting for successful SSH connection") # Create first snapshot snapshot1.create() ssh_client.run_command('touch snapshot2.txt') # If we are not testing 'revert to active snapshot' situation, we create another snapshot if not active_snapshot: if provider.one_of(RHEVMProvider): snapshot2 = new_snapshot(full_test_vm, has_name=False) else: snapshot2 = new_snapshot(full_test_vm) snapshot2.create() # VM on RHV provider must be powered off before snapshot revert if provider.one_of(RHEVMProvider): full_test_vm.power_control_from_cfme(option=full_test_vm.POWER_OFF, cancel=False) full_test_vm.wait_for_vm_state_change( desired_state=full_test_vm.STATE_OFF, timeout=900) snapshot1.revert_to() # Wait for the snapshot to become active logger.info('Waiting for vm %s to become active', snapshot1.name) wait_for(lambda: snapshot1.active, num_sec=300, delay=20, fail_func=provider.browser.refresh, message="Waiting for the first snapshot to become active") # VM state after revert should be OFF full_test_vm.wait_for_vm_state_change(desired_state=full_test_vm.STATE_OFF, timeout=720) # Let's power it ON again full_test_vm.power_control_from_cfme(option=full_test_vm.POWER_ON, cancel=False) full_test_vm.wait_for_vm_state_change(desired_state=full_test_vm.STATE_ON, timeout=900) soft_assert(full_test_vm.mgmt.is_running, "vm not running") # Wait for successful ssh connection wait_for(lambda: ssh_client.run_command('test -e snapshot1.txt').success, num_sec=400, delay=10, handle_exception=True, fail_func=ssh_client.close(), message="Waiting for successful SSH connection after revert") try: result = ssh_client.run_command('test -e snapshot1.txt') assert result.success # file found, RC=0 result = ssh_client.run_command('test -e snapshot2.txt') assert result.failed # file not found, RC=1 logger.info('Revert to snapshot %s successful', snapshot1.name) except Exception: logger.exception('Revert to snapshot %s Failed', snapshot1.name) ssh_client.close()
def test_verify_revert_snapshot(full_test_vm, provider, soft_assert, register_event): """Tests revert snapshot Metadata: test_flag: snapshot, provision """ if provider.one_of(RHEVMProvider): snapshot1 = new_snapshot(full_test_vm, has_name=False) else: snapshot1 = new_snapshot(full_test_vm) full_template = getattr(provider.data.templates, 'full_template') ssh_kwargs = { 'hostname': snapshot1.vm.provider.mgmt.get_ip_address(snapshot1.vm.name), 'username': credentials[full_template.creds]['username'], 'password': credentials[full_template.creds]['password'] } ssh_client = SSHClient(**ssh_kwargs) # We need to wait for ssh to become available on the vm, it can take a while. Without # this wait, the ssh command would fail with 'port 22 not available' error. # Easiest way to solve this is just mask the exception with 'handle_exception = True' # and wait for successful completition of the ssh command. # The 'fail_func' ensures we close the connection that failed with exception. # Without this, the connection would hang there and wait_for would fail with timeout. wait_for(lambda: ssh_client.run_command('touch snapshot1.txt').rc == 0, num_sec=300, delay=20, handle_exception=True, fail_func=ssh_client.close()) snapshot1.create() register_event(target_type='VmOrTemplate', target_name=full_test_vm.name, event_type='vm_snapshot_complete') register_event(target_type='VmOrTemplate', target_name=full_test_vm.name, event_type='vm_snapshot') ssh_client.run_command('touch snapshot2.txt') if provider.one_of(RHEVMProvider): snapshot2 = new_snapshot(full_test_vm, has_name=False) else: snapshot2 = new_snapshot(full_test_vm) snapshot2.create() if provider.one_of(RHEVMProvider): full_test_vm.power_control_from_cfme(option=full_test_vm.POWER_OFF, cancel=False) full_test_vm.wait_for_vm_state_change( desired_state=full_test_vm.STATE_OFF, timeout=900) snapshot1.revert_to() # Wait for the snapshot to become active logger.info('Waiting for vm %s to become active', snapshot1.name) wait_for(lambda: snapshot1.active, num_sec=300, delay=20, fail_func=provider.browser.refresh) full_test_vm.wait_for_vm_state_change(desired_state=full_test_vm.STATE_OFF, timeout=720) full_test_vm.power_control_from_cfme(option=full_test_vm.POWER_ON, cancel=False) full_test_vm.wait_for_vm_state_change(desired_state=full_test_vm.STATE_ON, timeout=900) current_state = full_test_vm.find_quadicon().data['state'] soft_assert(current_state.startswith('currentstate-on'), "Quadicon state is {}".format(current_state)) soft_assert(full_test_vm.provider.mgmt.is_vm_running(full_test_vm.name), "vm not running") wait_for(lambda: ssh_client.run_command('test -e snapshot1.txt').rc == 0, num_sec=400, delay=20, handle_exception=True, fail_func=ssh_client.close()) try: result = ssh_client.run_command('test -e snapshot1.txt') assert not result.rc result = ssh_client.run_command('test -e snapshot2.txt') assert result.rc logger.info('Revert to snapshot %s successful', snapshot1.name) except: logger.exception('Revert to snapshot %s Failed', snapshot1.name) ssh_client.close()