Example #1
0
def cinder_unit_tests(token, test_config):
    """
    Run through the unit tests for cinder
    """
    print("[CINDER UNIT TESTS]")

    servers = nova.get_servers(token)
    for server in servers['servers']:
        nova.delete_server(token, server['id'])

    while servers['servers']:
        servers = nova.get_servers(token)
        time.sleep(0.5)

    volumes = cinder.get_volumes(token)
    print("List of volumes: %s" % volumes)

    for volume in volumes['volumes']:
        print("Deleting volume: %s" % volume)
        cinder.delete_volume(token, volume['id'])

    volume = cinder.create_volume(token, "volume-test", "", 1)
    print("Created volume: %s" % volume)

    volume = cinder.get_volume(token, volume['volume']['id'])
    print("Fetched volume: %s" % volume)
Example #2
0
def instance_get_by_name(instance_name):
    """
    Fetch instance data by the name of the instance
    """
    token = _get_token()

    servers = nova.get_servers(token).result_data
    for instance_data in servers['servers']:
        if instance_data['name'] == instance_name:
            server_data = nova.get_server(token,
                                          instance_data['id']).result_data
            return server_data['server']
Example #3
0
def process_do_setup(loads_dir, setup_data):
    """
    Test - Process Do Setup
    """
    from nfv_plugins.nfvi_plugins.openstack import cinder
    from nfv_plugins.nfvi_plugins.openstack import glance
    from nfv_plugins.nfvi_plugins.openstack import neutron
    from nfv_plugins.nfvi_plugins.openstack import nova
    from nfv_plugins.nfvi_plugins.openstack import openstack

    directory = openstack.get_directory(config,
                                        openstack.SERVICE_CATEGORY.OPENSTACK)
    token = openstack.get_token(directory)

    result = nova.get_flavors(token)
    flavors = result.result_data.get('flavors', list())

    result = glance.get_images(token)
    images = result.result_data.get('images', list())

    result = cinder.get_volumes(token)
    volumes = result.result_data.get('volumes', list())

    result = neutron.get_networks(token)
    networks = result.result_data.get('networks', list())

    result = neutron.get_subnets(token)
    subnets = result.result_data.get('subnets', list())

    result = nova.get_servers(token)
    servers = result.result_data.get('servers', list())

    for resource in setup_data['resources']:
        if 'flavor' == resource['type']:
            process_progress_marker_start("Create flavor %s " %
                                          resource['name'])
            flavor = next(
                (x for x in flavors if x['name'] == resource['name']), None)
            if flavor is None:
                nova.create_flavor(token,
                                   resource['id'],
                                   resource['name'],
                                   resource['vcpus'],
                                   resource['ram_mb'],
                                   resource['disk_gb'],
                                   ephemeral_gb=resource['ephemeral_gb'],
                                   swap_mb=resource['swap_mb'])

                if 'extra_specs' in resource:
                    for extra_spec in resource['extra_specs']:
                        nova.set_flavor_extra_specs(
                            token, resource['id'],
                            {extra_spec['key']: str(extra_spec['value'])})

            process_progress_marker_end("[OKAY]")

    result = nova.get_flavors(token)
    flavors = result.result_data.get('flavors', list())

    for resource in setup_data['resources']:
        if 'image' == resource['type']:
            process_progress_marker_start("Create image %s " %
                                          resource['name'])
            image = next((x for x in images if x['name'] == resource['name']),
                         None)
            if image is None:
                image_file = resource['file']
                if not os.path.isfile(image_file):
                    image_file = loads_dir + '/' + resource['file']
                    if not os.path.isfile(image_file):
                        process_progress_marker_end("[FAILED]")
                        print("Image file %s does not exist." %
                              resource['file'])
                        return False

                image_data = glance.create_image(
                    token, resource['name'], resource['description'],
                    resource['container_format'], resource['disk_format'],
                    resource['min_disk_size_gb'],
                    resource['min_memory_size_mb'], resource['visibility'],
                    resource['protected'], resource['properties']).result_data

                glance.upload_image_data_by_file(token, image_data['id'],
                                                 image_file)
            process_progress_marker_end("[OKAY]")

    result = glance.get_images(token)
    images = result.result_data.get('images', list())

    for resource in setup_data['resources']:
        if 'volume' == resource['type']:
            process_progress_marker_start("Create volume %s " %
                                          resource['name'])
            volume = next(
                (x for x in volumes if x['name'] == resource['name']), None)
            if volume is None:
                if resource['image_name'] is not None:
                    image = next((x for x in images
                                  if x['name'] == resource['image_name']),
                                 None)
                    if image is None:
                        process_progress_marker_end("[FAILED]")
                        print("Image %s for volume %s does not exist." %
                              (resource['image_name'], resource['name']))
                        return False

                    image_id = image['id']
                else:
                    image_id = None

                bootable = None
                if resource['bootable'] in ['YES', 'Yes', 'yes']:
                    bootable = True

                cinder.create_volume(token,
                                     resource['name'],
                                     resource['description'],
                                     resource['size_gb'],
                                     image_id,
                                     bootable=bootable)

                for _ in range(10):
                    time.sleep(5)
                    volumes = cinder.get_volumes(token).result_data
                    if volumes is not None:
                        volumes = volumes.get('volumes', list())
                        volume = next((x for x in volumes
                                       if x['name'] == resource['name']), None)
                        volume = cinder.get_volume(token,
                                                   volume['id']).result_data
                        if 'available' == volume['volume']['status']:
                            break
                else:
                    process_progress_marker_end("[FAILED]")
                    print("Volume %s not create properly." % resource['name'])
                    return False
            process_progress_marker_end("[OKAY]")

    result = cinder.get_volumes(token)
    volumes = result.result_data.get('volumes', list())

    for resource in setup_data['resources']:
        if 'network' == resource['type']:
            process_progress_marker_start("Create network %s " %
                                          resource['name'])
            network = next(
                (x for x in networks if x['name'] == resource['name']), None)
            if network is None:
                neutron.create_network(token, resource['name'],
                                       resource['network_type'],
                                       resource['segmentation_id'],
                                       resource['physical_network'],
                                       resource['shared'])
            process_progress_marker_end("[OKAY]")

    result = neutron.get_networks(token)
    networks = result.result_data.get('networks', list())

    for resource in setup_data['resources']:
        if 'subnet' == resource['type']:
            process_progress_marker_start("Create subnet %s " %
                                          resource['name'])
            subnet = next(
                (x for x in subnets if x['name'] == resource['name']), None)
            if subnet is None:
                network = next((x for x in networks
                                if x['name'] == resource['network_name']),
                               None)
                if network is None:
                    process_progress_marker_end("[FAILED]")
                    print("Network %s for subnet %s does not exist." %
                          (resource['network_name'], resource['name']))
                    return False

                neutron.create_subnet(token, network['id'], resource['name'],
                                      resource['ip_version'], resource['cidr'],
                                      resource['gateway_ip'],
                                      resource['dhcp_enabled'])
            process_progress_marker_end("[OKAY]")

    result = neutron.get_subnets(token)
    subnets = result.result_data.get('subnets', list())

    instance_created = False
    for resource in setup_data['resources']:
        if 'instance' == resource['type']:
            process_progress_marker_start("Create instance %s " %
                                          resource['name'])
            server = next(
                (x for x in servers if x['name'] == resource['name']), None)
            if server is None:
                flavor = next(
                    (x for x in flavors if x['name'] == resource['flavor']),
                    None)
                if flavor is None:
                    process_progress_marker_end("[FAILED]")
                    print("Can't find flavor %s for instance %s" %
                          (resource['flavor'], resource['name']))
                    return False

                if resource['image'] is not None:
                    image = next(
                        (x for x in images if x['name'] == resource['image']),
                        None)
                    if image is None:
                        process_progress_marker_end("[FAILED]")
                        print("Can't find image %s for instance %s" %
                              (resource['image'], resource['name']))
                        return False

                    image_id = image['id']
                else:
                    image_id = None

                block_devices = list()
                for block_device in resource['block_devices']:
                    if 'volume' == block_device['type']:
                        volume = next(
                            (x for x in volumes
                             if x['name'] == block_device['volume_name']),
                            None)
                        if volume is None:
                            process_progress_marker_end("[FAILED]")
                            print("Can't find volume %s for instance %s" %
                                  (block_device['volume_name'],
                                   resource['name']))
                            return False

                        block_devices.append({
                            'uuid':
                            volume['id'],
                            'device_name':
                            block_device['device_name'],
                            'source_type':
                            block_device['source_type'],
                            'destination_type':
                            block_device['destination_type'],
                            'boot_index':
                            block_device['boot_index']
                        })

                if 0 == len(block_devices):
                    block_devices = None

                network_ids = list()
                for network_name in resource['networks']:
                    network = next(
                        (x for x in networks if x['name'] == network_name),
                        None)
                    if network is None:
                        process_progress_marker_end("[FAILED]")
                        print("Can't find network %s for instance %s" %
                              (network_name, resource['name']))
                        return False

                    network_ids.append({'uuid': network['id']})

                if 0 == len(network_ids):
                    network_ids = None

                nova.create_server(token, resource['name'], flavor['id'],
                                   image_id, block_devices, network_ids)

                for _ in range(10):
                    time.sleep(5)
                    result = nova.get_servers(token)
                    servers = result.result_data.get('servers', list())
                    if servers:
                        server = next((x for x in servers
                                       if x['name'] == resource['name']), None)
                        server = nova.get_server(token,
                                                 server['id']).result_data
                        if 'ACTIVE' == server['server']['status']:
                            server_id = server['server']['id']
                            break
                else:
                    process_progress_marker_end("[FAILED]")
                    print("Server %s not created properly." % resource['name'])
                    return False

                for attached_volume in resource['attached_volumes']:
                    volume = next(
                        (x for x in volumes
                         if x['name'] == attached_volume['volume_name']), None)
                    if volume is None:
                        process_progress_marker_end("[FAILED]")
                        print(
                            "Can't find volume %s for instance %s" %
                            (attached_volume['volume_name'], resource['name']))
                        return False

                    nova.attach_volume(token, server_id, volume['id'],
                                       attached_volume['device_name'])

                instance_created = True

            process_progress_marker_end("[OKAY]")

    if instance_created:
        # Allow time for instances to boot and guest client to start up
        # inside the each instance (in case it is being used). Timeout is long
        # because we are usually testing in Virtual Box.
        time.sleep(90)

    return True
Example #4
0
def process_do_teardown(setup_data):
    """
    Test - Process Do Teardown
    """
    from nfv_plugins.nfvi_plugins.openstack import cinder
    from nfv_plugins.nfvi_plugins.openstack import glance
    from nfv_plugins.nfvi_plugins.openstack import neutron
    from nfv_plugins.nfvi_plugins.openstack import nova
    from nfv_plugins.nfvi_plugins.openstack import openstack

    directory = openstack.get_directory(config,
                                        openstack.SERVICE_CATEGORY.OPENSTACK)
    token = openstack.get_token(directory)

    result = nova.get_flavors(token)
    flavors = result.result_data.get('flavors', list())

    result = glance.get_images(token)
    images = result.result_data.get('images', list())

    result = cinder.get_volumes(token)
    volumes = result.result_data.get('volumes', list())

    result = neutron.get_networks(token)
    networks = result.result_data.get('networks', list())

    result = neutron.get_subnets(token)
    subnets = result.result_data.get('subnets', list())

    result = nova.get_servers(token)
    servers = result.result_data.get('servers', list())

    for resource in setup_data['resources']:
        if 'instance' == resource['type']:
            process_progress_marker_start("Delete instance %s " %
                                          resource['name'])
            server = next(
                (x for x in servers if x['name'] == resource['name']), None)
            if server is not None:
                nova.delete_server(token, server['id'])

                for _ in range(10):
                    time.sleep(5)
                    servers = nova.get_servers(token).result_data
                    if servers is not None:
                        servers = servers.get('servers', list())
                        server = next((x for x in servers
                                       if x['name'] == resource['name']), None)
                        if server is None:
                            break
                else:
                    process_progress_marker_end("[FAILED]")
                    print("Server %s not deleted." % resource['name'])
                    return False
            process_progress_marker_end("[OKAY]")

    for resource in setup_data['resources']:
        if 'subnet' == resource['type']:
            process_progress_marker_start("Delete subnet %s " %
                                          resource['name'])
            subnet = next(
                (x for x in subnets if x['name'] == resource['name']), None)
            if subnet is not None:
                neutron.delete_subnet(token, subnet['id'])
            process_progress_marker_end("[OKAY]")

    for resource in setup_data['resources']:
        if 'network' == resource['type']:
            process_progress_marker_start("Delete network %s " %
                                          resource['name'])
            network = next(
                (x for x in networks if x['name'] == resource['name']), None)
            if network is not None:
                neutron.delete_network(token, network['id'])
            process_progress_marker_end("[OKAY]")

    for resource in setup_data['resources']:
        if 'volume' == resource['type']:
            process_progress_marker_start("Delete volume %s " %
                                          resource['name'])
            volume = next(
                (x for x in volumes if x['name'] == resource['name']), None)
            if volume is not None:
                cinder.delete_volume(token, volume['id'])
            process_progress_marker_end("[OKAY]")

    for resource in setup_data['resources']:
        if 'image' == resource['type']:
            process_progress_marker_start("Delete image %s " %
                                          resource['name'])
            image = next((x for x in images if x['name'] == resource['name']),
                         None)
            if image is not None:
                glance.delete_image(token, image['id'])
            process_progress_marker_end("[OKAY]")

    for resource in setup_data['resources']:
        if 'flavor' == resource['type']:
            process_progress_marker_start("Delete flavor %s " %
                                          resource['name'])
            flavor = next(
                (x for x in flavors if x['name'] == resource['name']), None)
            if flavor is not None:
                nova.delete_flavor(token, flavor['id'])
            process_progress_marker_end("[OKAY]")

    return True
Example #5
0
def nova_unit_tests(token, test_config):
    """
    Run through the unit tests for nova
    """
    print("[NOVA UNIT TESTS]")

    response = nova.create_host_services(token, test_config['host_name'])
    print("Host-Services create response: %s" % response)

    response = nova.disable_host_services(token, test_config['host_name'])
    print("Host-Services disable response: %s" % response)

    response = nova.enable_host_services(token, test_config['host_name'])
    print("Host-Services enable response: %s" % response)

    response = nova.disable_host_services(token, test_config['host_name'])
    print("Host-Services disable response: %s" % response)

    # NOTE: if you do a delete of the host-services, the hypervisors are
    # deleted and will not be populated again until nova-compute is
    # restarted even if you re-create and enable the host-services.
    # response = nova.delete_host_services(token, test_config['host_name'])
    # print "Host-Services delete response: %s" % response

    response = nova.create_host_services(token, test_config['host_name'])
    print("Host-Services create response: %s" % response)

    response = nova.enable_host_services(token, test_config['host_name'])
    print("Host-Services enable response: %s" % response)

    hypervisors = nova.get_hypervisors(token)
    print("List of hypervisors: %s" % hypervisors)

    for hypervisor in hypervisors['hypervisors']:
        visor = nova.get_hypervisor(token, hypervisor['id'])
        print("Fetched hypervisor: %s" % visor)

    flavors = nova.get_flavors(token)
    print("List of flavors: %s" % flavors)

    for flavor in flavors['flavors']:
        print("Deleting flavor: %s" % flavor)
        nova.delete_flavor(token, flavor['id'])

    flavor = nova.create_flavor(token, 1, "flavor-test", 1, 512, 1)
    print("Created flavor: %s" % flavor)

    servers = nova.get_servers(token)
    print("List of servers: %s" % servers)

    deleted_servers = servers.copy()
    for server in servers['servers']:
        print("Deleting server: %s" % server)
        nova.delete_server(token, server['id'])

    while servers['servers']:
        servers = nova.get_servers(token)
        time.sleep(0.5)
    print("Servers deleted: %s" % deleted_servers['servers'])

    images = glance.get_images(token)
    image = next(image for image in images['images']
                 if image['name'] == 'image-test')

    networks = neutron.get_networks(token)
    network = next(network for network in networks['networks']
                   if network['name'] == 'network-test')

    networks = list()
    networks.append({'uuid': network['id']})

    server = nova.create_server(token, "server-test-with-glance",
                                flavor['flavor']['id'], image['id'],
                                networks=networks)
    print("Created server: %s" % server)

    server = nova.get_server(token, server['server']['id'])
    print("Fetched server: %s" % server)

    volumes = cinder.get_volumes(token)
    volume = next(volume for volume in volumes['volumes']
                  if volume['name'] == 'volume-test')

    block_devices = list()
    block_devices.append({'uuid': volume['id'],
                          'device_name': '/dev/sda1',
                          'source_type': 'volume',
                          'destination_type': 'volume',
                          'boot_index': '0'})

    server = nova.create_server(token, "server-test-with-cinder",
                                flavor['flavor']['id'], image['id'],
                                block_devices=block_devices,
                                networks=networks)
    status = None
    while 'ACTIVE' != status:
        server = nova.get_server(token, server['server']['id'])
        status = server['server']['status']
        time.sleep(0.5)
    print("Created server: %s" % server)

    server = nova.get_server(token, server['server']['id'])
    print("Fetched server: %s" % server)

    nova.reboot_server(token, server['server']['id'], nova.VM_REBOOT_TYPE.HARD)
    status = None
    while 'ACTIVE' != status:
        server = nova.get_server(token, server['server']['id'])
        status = server['server']['status']
        time.sleep(0.5)
    print("Hard rebooted server: %s" % server['server']['name'])

    status = None
    nova.rebuild_server(token, server['server']['id'], server['server']['name'],
                        image['id'])
    while 'ACTIVE' != status:
        server = nova.get_server(token, server['server']['id'])
        status = server['server']['status']
        time.sleep(0.5)
    print("Rebuilt server: %s" % server['server']['name'])

    servers = nova.get_servers(token)
    print("List of servers: %s" % servers)

    for server in servers['servers']:
        nova.delete_server(token, server['id'])

    while servers['servers']:
        servers = nova.get_servers(token)
        time.sleep(0.5)
    print("Servers deleted")
Example #6
0
def neutron_unit_tests(token, test_config):
    """
    Run through the unit tests for neutron
    """
    print("[NEUTRON UNIT TESTS]")

    response = neutron.create_host_services(token, test_config['host_name'],
                                            test_config['host_uuid'])
    print("Host-Services create response: %s" % response)

    response = neutron.disable_host_services(token, test_config['host_uuid'])
    print("Host-Services disable response: %s" % response)

    response = neutron.enable_host_services(token, test_config['host_uuid'])
    print("Host-Services enable response: %s" % response)

    response = neutron.disable_host_services(token, test_config['host_uuid'])
    print("Host-Services disable response: %s" % response)

    response = neutron.delete_host_services(token, test_config['host_uuid'])
    print("Host-Services delete response: %s" % response)

    response = neutron.create_host_services(token, test_config['host_name'],
                                            test_config['host_uuid'])
    print("Host-Services create response: %s" % response)

    response = neutron.enable_host_services(token, test_config['host_uuid'])
    print("Host-Services enable response: %s" % response)

    servers = nova.get_servers(token)
    for server in servers['servers']:
        nova.delete_server(token, server['id'])

    while servers['servers']:
        servers = nova.get_servers(token)
        time.sleep(0.5)

    networks = neutron.get_networks(token)
    print("List of networks: %s" % networks)

    for network in networks['networks']:
        print("Deleting network: %s" % network)
        neutron.delete_network(token, network['id'])

    network = neutron.create_network(token, 'network-test', 'vlan', 450,
                                     'physnet0', False)
    print("Created network: %s" % network)

    network = neutron.update_network(token, network['network']['id'],
                                     neutron.NETWORK_ADMIN_STATE.DOWN, True)
    print("Updated network: %s" % network)

    subnets = neutron.get_subnets(token)
    print("List of subnets: %s" % subnets)

    for subnet in subnets['subnets']:
        print("Deleting subnet: %s" % subnet)
        neutron.delete_subnet(token, subnet['id'])

    subnet = neutron.create_subnet(token, network['network']['id'],
                                   'subnet-test', 4, '192.168.101.0/24',
                                   '192.168.101.1', True)
    print("Created subnet: %s" % subnet)

    subnet = neutron.update_subnet(token, subnet['subnet']['id'],
                                   delete_gateway=True, dhcp_enabled=False)
    print("Updated subnet: %s" % subnet)

    subnet = neutron.get_subnet(token, subnet['subnet']['id'])
    print("Fetched subnet: %s" % subnet)