Exemplo n.º 1
0
def _create_vm(request, template_type, provider, vm_name):
    """Look up template in provider yaml based on the template_type,
    and provision a VM/Instance from it on the provider.
    """
    if template_type:
        template = _get_template(provider, template_type)
        template_name = template.name
    else:
        template_name = None

    collection = provider.appliance.provider_based_collection(provider)
    vm_obj = collection.instantiate(vm_name,
                                    provider,
                                    template_name=template_name)
    vm_obj.create_on_provider(allow_skip='default')

    @request.addfinalizer
    def _cleanup():
        vm_obj.cleanup_on_provider()
        try:
            provider.refresh_provider_relationships()
        except Exception as e:
            if e.args[0] != "Provider collection empty":
                raise

    vm_obj.mgmt.ensure_state(VmState.RUNNING)

    if not vm_obj.exists:
        provider.refresh_provider_relationships()
        vm_obj.wait_to_appear()

    return vm_obj
def test_migration_long_name(request, appliance, provider, source_provider):
    """Test to check VM name with 64 character should work

    Polarion:
        assignee: sshveta
        initialEstimate: 1/2h
        casecomponent: V2V
    """
    source_datastores_list = source_provider.data.get("datastores", [])
    source_datastore = [
        d.name for d in source_datastores_list if d.type == "nfs"
    ][0]
    collection = appliance.provider_based_collection(source_provider)

    # Following code will create vm name with 64 characters
    vm_name = "{vm_name}{extra_words}".format(
        vm_name=random_vm_name(context="v2v"),
        extra_words=fauxfactory.gen_alpha(51))
    template = _get_template(source_provider, Templates.RHEL7_MINIMAL)
    vm_obj = collection.instantiate(
        name=vm_name,
        provider=source_provider,
        template_name=template.name,
    )
    vm_obj.create_on_provider(timeout=2400,
                              find_in_cfme=True,
                              allow_skip="default",
                              datastore=source_datastore)
    request.addfinalizer(lambda: vm_obj.cleanup_on_provider())
    mapping_data = infra_mapping_default_data(source_provider, provider)

    infrastructure_mapping_collection = appliance.collections.v2v_infra_mappings
    mapping = infrastructure_mapping_collection.create(**mapping_data)

    @request.addfinalizer
    def _cleanup():
        infrastructure_mapping_collection.delete(mapping)

    migration_plan_collection = appliance.collections.v2v_migration_plans
    migration_plan = migration_plan_collection.create(
        name=fauxfactory.gen_alphanumeric(20, start="long_name_"),
        description=fauxfactory.gen_alphanumeric(25, start="desc_long_name_"),
        infra_map=mapping.name,
        vm_list=[vm_obj],
        target_provider=provider)
    assert migration_plan.wait_for_state("Started")
    assert migration_plan.wait_for_state("In_Progress")
    assert migration_plan.wait_for_state("Completed")
    assert migration_plan.wait_for_state("Successful")

    migrated_vm = get_migrated_vm(vm_obj, provider)
    assert vm_obj.mac_address == migrated_vm.mac_address
def get_vm(request,
           appliance,
           source_provider,
           template_type,
           datastore='nfs'):
    """ Helper method that takes template , source provider and datastore
        and creates VM on source provider to migrate .

    Args:
        request
        appliance:
        source_provider: Provider on which vm is created
        template: Template used for creating VM
        datastore: datastore in which VM is created. If no datastore
                   is provided then by default VM is created on nfs datastore

        returns: Vm object
    """
    source_datastores_list = source_provider.data.get("datastores", [])
    source_datastore = [
        d.name for d in source_datastores_list if d.type == datastore
    ][0]
    collection = source_provider.appliance.provider_based_collection(
        source_provider)
    vm_name = random_vm_name("v2v-auto")
    template = _get_template(source_provider, template_type)
    vm_obj = collection.instantiate(vm_name,
                                    source_provider,
                                    template_name=template.name)
    power_on_vm = True
    if 'win10' in template.name:
        # TODO Get the VM to the correct power state within the fixture/test, not here
        # the fixture or test
        # Need to leave this off, otherwise migration fails
        # because when migration process tries to power off the VM if it is powered off
        # and for win10, it hibernates and that state of filesystem is unsupported
        power_on_vm = False
    vm_obj.create_on_provider(
        timeout=2400,
        find_in_cfme=True,
        allow_skip="default",
        datastore=source_datastore,
        power_on=power_on_vm,
    )
    request.addfinalizer(lambda: vm_obj.cleanup_on_provider())
    return vm_obj
def create_catalog_item(appliance,
                        provider,
                        provisioning,
                        dialog,
                        catalog,
                        vm_count='1',
                        console_test=False):
    provision_type, template, host, datastore, iso_file, vlan = map(
        provisioning.get, ('provision_type', 'template', 'host', 'datastore',
                           'iso_file', 'vlan'))
    if console_test:
        template = _get_template(provider, 'console_template').name
        logger.info(f"Console template name : {template}")
    item_name = dialog.label
    if provider.one_of(InfraProvider):
        catalog_name = template
        provisioning_data = {
            'catalog': {
                'catalog_name': {
                    'name': catalog_name,
                    'provider': provider.name
                },
                'vm_name': random_vm_name('serv'),
                'provision_type': provision_type,
                'num_vms': vm_count
            },
            'environment': {
                'host_name': {
                    'name': host
                },
                'datastore_name': {
                    'name': datastore
                }
            },
            'network': {
                'vlan': partial_match(vlan)
            },
        }
    elif provider.one_of(CloudProvider):
        catalog_name = provisioning['image']['name']
        provisioning_data = {
            'catalog': {
                'catalog_name': {
                    'name': catalog_name,
                    'provider': provider.name
                },
                'vm_name': random_vm_name('serv')
            },
            'properties': {
                'instance_type':
                partial_match(provisioning.get('instance_type', None)),
                'guest_keypair':
                provisioning.get('guest_keypair', None)
            },
        }
        # Azure specific
        if provider.one_of(AzureProvider):
            recursive_update(
                provisioning_data, {
                    'customize': {
                        'admin_username': provisioning['customize_username'],
                        'root_password': provisioning['customize_password']
                    },
                    'environment': {
                        'security_groups': provisioning['security_group'],
                        'cloud_network': provisioning['cloud_network'],
                        'cloud_subnet': provisioning['cloud_subnet'],
                        'resource_groups': provisioning['resource_group']
                    },
                })
        # GCE specific
        if provider.one_of(GCEProvider):
            recursive_update(
                provisioning_data, {
                    'properties': {
                        'boot_disk_size': provisioning['boot_disk_size'],
                        'is_preemptible': True
                    },
                    'environment': {
                        'availability_zone': provisioning['availability_zone'],
                        'cloud_network': provisioning['cloud_network']
                    },
                })
        # EC2 specific
        if provider.one_of(EC2Provider):
            recursive_update(
                provisioning_data, {
                    'environment': {
                        'availability_zone': provisioning['availability_zone'],
                        'cloud_network': provisioning['cloud_network'],
                        'cloud_subnet': provisioning['cloud_subnet'],
                        'security_groups': provisioning['security_group'],
                    },
                })
            # OpenStack specific
        if provider.one_of(OpenStackProvider):
            recursive_update(
                provisioning_data, {
                    'environment': {
                        'availability_zone': provisioning['availability_zone'],
                        'cloud_network': provisioning['cloud_network'],
                        'cloud_tenant': provisioning['cloud_tenant'],
                        'security_groups': provisioning['security_group'],
                    },
                })

    catalog_item = appliance.collections.catalog_items.create(
        provider.catalog_item_type,
        name=item_name,
        description="my catalog",
        display_in=True,
        catalog=catalog,
        dialog=dialog,
        prov_data=provisioning_data,
        provider=provider)
    return catalog_item