def utility_vm():
    """ Deploy an utility vm for tests to use.

    This fixture creates a vm on provider and then receives its ip.
    After the test run vm is deleted from provider.
    """
    try:
        data = cfme_data['utility_vm']
        injected_user_cred = SSHCredential.from_config(
            data['injected_credentials'])
        try:
            with open(os.path.expanduser('~/.ssh/id_rsa.pub')) as f:
                authorized_ssh_keys = f.read()
        except FileNotFoundError:
            authorized_ssh_keys = None
        vm = deploy_template(
            data.provider,
            random_vm_name('proxy'),
            template_name=data.template_name,
            # The naming is not great. It comes from
            # https://access.redhat.com/documentation/en-us/red_hat_virtualization/4.2/
            # html-single/python_sdk_guide/index#Starting_a_Virtual_Machine_with_Cloud-Init
            initialization=dict(user_name=injected_user_cred.principal,
                                root_password=injected_user_cred.secret,
                                authorized_ssh_keys=authorized_ssh_keys))
    except AttributeError:
        msg = 'Missing utility_vm data from cfme_data.yaml, cannot deploy the utility vm.'
        logger.exception(msg)
        pytest.skip(msg)

    yield vm, injected_user_cred, data

    vm.delete()
Example #2
0
    def get_credentials(cls, credential_dict, cred_type=None):
        """Processes a credential dictionary into a credential object.

        Args:
            credential_dict: A credential dictionary.
            cred_type: Type of credential (None, token, ssh, amqp, ...)

        Returns:
            A :py:class:`cfme.base.credential.Credential` instance.
        """
        domain = credential_dict.get('domain')
        token = credential_dict.get('token')
        if not cred_type:
            return Credential(principal=credential_dict['username'],
                              secret=credential_dict['password'],
                              domain=domain)
        elif cred_type == 'amqp':
            return EventsCredential(principal=credential_dict['username'],
                                    secret=credential_dict['password'])

        elif cred_type == 'ssh':
            return SSHCredential(principal=credential_dict['username'],
                                 secret=credential_dict['password'])
        elif cred_type == 'candu':
            return CANDUCredential(principal=credential_dict['username'],
                                   secret=credential_dict['password'])
        elif cred_type == 'token':
            return TokenCredential(token=token)
Example #3
0
def verify_revert_snapshot(full_test_vm, provider, soft_assert, register_event, request,
                           active_snapshot=False):
    SSH_READY_TIMEOUT = 300
    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')
    creds = SSHCredential.from_config(full_template.creds)

    # We need to wait for ssh to become available on the vm, it can take a while.
    # connect_ssh will iterate over "all_ips" on the VM and return a client when it can connect

    with closing(connect_ssh(full_test_vm.mgmt, creds, num_sec=SSH_READY_TIMEOUT)) as ssh_client:
        ssh_client.run_command('touch snapshot1.txt')
        # Create first snapshot
        snapshot1.create()

        # Assuming creating snapshot shouldn't break the ssh connection...
        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=400)

    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=700, delay=30, 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=400)
    soft_assert(full_test_vm.mgmt.is_running, "vm not running")
    # Wait for successful ssh connection
    with closing(connect_ssh(full_test_vm.mgmt, creds, num_sec=SSH_READY_TIMEOUT)) as ssh_client:
        assert ssh_client.run_command('test -e snapshot1.txt').success
        # This checks the exit status is 1 -- file doesn't exist.
        assert ssh_client.run_command('test -e snapshot2.txt') == 1