Esempio n. 1
0
def _vm_args_to_params(**vm_args):  # noqa - ignore mccabe warning
    """
    Convert fabric-style simple arguments into an oVirt VM parameters structure

    All parameters are as defined in the 'create' task for customizing the pool
    VMs

    :returns: an oVirt VM paameters structure or None if not customization was
              requested
    :rtype: oVirtObjects.VM
    """
    vm_args_supported = (
        'custom_serial_number',
        'memory',
        'memory_guaranteed',
        'memory_balooning',
        'vcpus',
    )
    vm_args = dict((key, value) for key, value in vm_args.iteritems()
                   if key in vm_args_supported and value is not None)
    if not vm_args:
        return None
    vm_params = oVirtParams.VM()
    memory = None
    if 'memory' in vm_args:
        memory = int(vm_args['memory'])
        vm_params.memory = memory
    mem_policy = None
    if 'memory_guaranteed' in vm_args or 'memory_balooning' in vm_args:
        mem_policy = oVirtParams.MemoryPolicy()
        if 'memory_guaranteed' in vm_args:
            mem_policy.guaranteed = int(vm_args['memory_guaranteed'])
        if 'memory_balooning' in vm_args:
            mem_policy.ballooning = bool(vm_args['balooning'])
    # oVirt sets guaranteed to 1G by default so we need to set it for smaller
    # VMs. This is a work-around for oVirt BZ#1333369
    if memory and memory < 1 * GiB:
        if mem_policy is None:
            mem_policy = oVirtParams.MemoryPolicy(guaranteed=memory)
        elif mem_policy.guaranteed is None:
            mem_policy.guaranteed = memory
    vm_params.memory_policy = mem_policy
    if 'vcpus' in vm_args:
        vm_params.cpu = oVirtParams.CPU(topology=oVirtParams.CpuTopology(
            sockets=int(vm_args['vcpus'])))
    if 'custom_serial_number' in vm_args:
        vm_params.serial_number = oVirtParams.SerialNumber(
            policy='custom',
            value=vm_args['custom_serial_number'],
        )
    return vm_params
Esempio n. 2
0
def add_vm_template(api):
    #TODO: Fix the exported domain generation.
    #For the time being, add VM from Glance imported template.
    if api.templates.get(name=TEMPLATE_CIRROS) is None:
        raise SkipTest('%s: template %s not available.' % (add_vm_template.__name__, TEMPLATE_CIRROS))

    vm_memory = 512 * MB
    vm_params = params.VM(
        name=VM1_NAME,
        description='CirrOS imported from Glance as Template',
        memory=vm_memory,
        cluster=params.Cluster(
            name=TEST_CLUSTER,
        ),
        template=params.Template(
            name=TEMPLATE_CIRROS,
        ),
        display=params.Display(
            type_='vnc',
        ),
        memory_policy=params.MemoryPolicy(
            guaranteed=vm_memory / 2,
            ballooning=False,
        ),
        os=params.OperatingSystem(
            type_='other_linux',
        ),
        timezone='Etc/GMT',
        type_='server',
        serial_number=params.SerialNumber(
            policy='custom',
            value='12345678',
        ),
        cpu=params.CPU(
            architecture='X86_64',
            topology=params.CpuTopology(
                cores=1,
                threads=2,
                sockets=1,
            ),
        ),
    )
    api.vms.add(vm_params)
    testlib.assert_true_within_long(
        lambda: api.vms.get(VM1_NAME).status.state == 'down',
    )
    disk_name = api.vms.get(VM1_NAME).disks.list()[0].name
    testlib.assert_true_within_long(
        lambda:
        api.vms.get(VM1_NAME).disks.get(disk_name).status.state == 'ok'
    )