def test_create_instance_from_bootable_volume(host):
    """Test to verify that a bootable volume can be created based on a Glance image

    Args:
        host(testinfra.host.Host): A hostname in dynamic_inventory.json/molecule.yml
    """

    volume_id = helpers.get_id_by_name('volume', volume_name, host)
    assert volume_id is not None

    network_id = helpers.get_id_by_name('network', network_name, host)
    assert network_id is not None

    cmd = "{} openstack server create --volume {} --flavor {} --nic net-id={} {}'".format(
        utility_container, volume_id, flavor, network_id, instance_name)

    host.run_expect([0], cmd)

    instances = helpers.get_resource_list_by_name('server', host)
    assert instances
    instance_names = [x['Name'] for x in instances]
    assert instance_name in instance_names
    assert (helpers.get_expected_value('server', instance_name, 'status',
                                       'ACTIVE', host))
    assert (helpers.get_expected_value('server', instance_name,
                                       'OS-EXT-STS:power_state', 'Running',
                                       host))

    # Tear down
    helpers.delete_instance(instance_name, host)
    helpers.delete_volume(volume_name, host)
コード例 #2
0
def test_create_snapshot_of_an_instance(host):
    """Create an instance and then create snapshot on it"""

    data_image = {
        "instance_name": instance_name,
        "from_source": 'image',
        "source_name": image_name,
        "flavor": flavor,
        "network_name": private_net,
    }

    helpers.create_instance(data_image, host)

    # Verify the new instance is ACTIVE.
    assert (helpers.get_expected_value('server', instance_name, 'status',
                                       'ACTIVE', host, 20))

    # TODO: will find out a better way to avoid implicit sleep. 'status' is 'ACTIVE' is not enough to ensure the
    # TODO: instance is ready, there are many instance statuses that might cause the test failed.
    # TODO: run `openstack server show <instance-ID> -f json` to see all the states
    sleep(120)

    # Create snapshot from newly created/shutdown instance
    helpers.create_snapshot_from_instance(snapshot_name, instance_name, host)

    # Verify the snapshot is successfully created:
    assert (helpers.get_expected_value('image', snapshot_name, 'status',
                                       'active', host, 20))
コード例 #3
0
def attach_volume_to_server(volume, server, run_on_host):
    cmd = "{} server add volume  \
           {} \
           {} {}".format(os_pre, server, volume, os_post)
    run_on_host.run(cmd)
    return helpers.get_expected_value('volume', volume, 'status', 'in-use',
                                      run_on_host)
def test_create_floating_ip(host):
    """Create floating IP"""

    global floating_ip
    floating_ip = create_floating_ip(gateway_net, host)
    assert floating_ip

    # Before being assigned, the floating IP status should be 'DOWN'
    assert (helpers.get_expected_value('floating ip', floating_ip, 'status', 'DOWN', host))
def test_assign_floating_ip_to_instance(host):
    """Assign floating IP to an instance/server"""

    # Creating an instance from image
    data = {
        "instance_name": instance_name,
        "from_source": 'image',
        "source_name": image_name,
        "flavor": flavor,
        "network_name": private_net,
    }

    helpers.create_instance(data, host)

    # TODO: will find out a better way to avoid implicit sleep. 'status' is 'ACTIVE' is not enough to ensure the
    # TODO: instance is ready, there are many instance statuses that might cause the test failed.
    # TODO: run `openstack server show <instance-ID> -f json` to see all the states
    sleep(120)

    # Verify the new instance is ACTIVE and Running.
    assert (helpers.get_expected_value('server', instance_name, 'status', 'ACTIVE', host, 20))
    assert (helpers.get_expected_value('server', instance_name, 'OS-EXT-STS:power_state', 'Running', host, 20))

    assert floating_ip

    instance_id = helpers.get_id_by_name('server', instance_name, host)
    assert instance_id

    cmd = "{} openstack server add floating ip {} {}'".format(utility_container, instance_id, floating_ip)

    host.run_expect([0], cmd)

    # After being assigned, the floating IP status should be 'ACTIVE'
    assert (helpers.get_expected_value('floating ip', floating_ip, 'status', 'ACTIVE', host))

    # Ensure the IP can be pinged from infra1
    cmd = "ping -c1 {}".format(floating_ip)
    assert (host.run_expect([0], cmd))
コード例 #6
0
def test_create_bootable_volume(host):
    """Test to verify that a bootable volume can be created based on a Glance image

    Args:
        host(testinfra.host.Host): A hostname in dynamic_inventory.json/molecule.yml
    """

    image_id = helpers.get_id_by_name('image', image_name, host)
    assert image_id is not None

    cmd = "{} openstack volume create --size 1 --image {} --bootable {}'".format(
        utility_container, image_id, volume_name)
    host.run_expect([0], cmd)

    assert volume_name in helpers.openstack_name_list('volume', host)
    assert (helpers.get_expected_value('volume', volume_name, 'status',
                                       'available', host))
コード例 #7
0
def test_create_instance_from_snapshot(host):

    data_snapshot = {
        "instance_name": new_instance_name,
        "from_source": 'image',
        "source_name": snapshot_name,
        "flavor": flavor,
        "network_name": private_net,
    }

    # Boot new instance using the newly created snapshot:
    helpers.create_instance(data_snapshot, host)

    # Verify the new instance is successfully booted using the snapshot
    assert (helpers.get_expected_value('server', new_instance_name, 'status',
                                       'ACTIVE', host, 20))

    # Cleaning up: Delete newly created snapshot
    helpers.delete_it('image', snapshot_name, host)
コード例 #8
0
def test_hypervisor_vms(host):
    """ASC-241: Per network, spin up an instance on each hypervisor, perform
    external ping, and tear-down """

    ssh = "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
           -i ~/.ssh/rpc_support ubuntu@{}"

    vars = host.ansible('include_vars',
                        'file=./vars/main.yml')['ansible_facts']

    flavor_name = vars['flavor']['name']
    image_name = vars['image']['name']

    server_list = []
    testable_networks = []

    # get image id (sounds like a helper, or register it as an ansible value)
    cmd = "{} image list -f json {}".format(os_pre, os_post)
    res = host.run(cmd)
    images = json.loads(res.stdout)
    assert len(images) > 0
    filtered_images = list(filter(lambda d: d['Name'] == image_name, images))
    assert len(filtered_images) > 0
    image = filtered_images[-1]

    # neutron_agent connection lookup
    na_list = testinfra.utils.ansible_runner.AnsibleRunner(
        os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('neutron_agent')
    hostname = host.check_output('hostname')
    na_list = [x for x in na_list if hostname in x]
    neutron_agent = next(iter(na_list), None)
    assert neutron_agent
    if 'container' in neutron_agent:
        # lxc
        na_pre = "lxc-attach -n {} -- bash -c ".format(neutron_agent)
    else:
        # ssh
        na_pre = "ssh -o StrictHostKeyChecking=no \
                   -o UserKnownHostsFile=/dev/null \
                   {} ".format(neutron_agent)

    r = random.randint(1111, 9999)

    # get list of internal networks
    net_cmd = "{} network list -f json {}".format(os_pre, os_post)
    net_res = host.run(net_cmd)
    networks = json.loads(net_res.stdout)
    for network in networks:
        cmd = "{} network show {} -f json {}".format(os_pre, network['ID'],
                                                     os_post)
        res = host.run(cmd)
        network_detail = json.loads(res.stdout)
        if network_detail['router:external'] == 'External':
            continue
        testable_networks.append(network_detail)

    if not testable_networks:
        pytest.skip("No testable networks found")

    # iterate over internal networks
    for network in testable_networks:
        # spin up instance per hypervisor
        cmd = "{} compute service list -f json {}".format(os_pre, os_post)
        res = host.run(cmd)
        computes = json.loads(res.stdout)
        for compute in computes:
            if compute['Binary'] == 'nova-compute':
                instance_name = "rpctest-{}-{}-{}".format(
                    r, compute['Host'], network['name'])
                server = create_server_on(host, image['ID'], flavor_name,
                                          network['id'], compute['Zone'],
                                          instance_name)
                assert helpers.get_expected_value('server', server['id'],
                                                  'status', 'ACTIVE', host, 15)
                server_list.append(server['id'])

    for server in server_list:
        # test ssh
        print "DEBUG OUTPUT"
        print server
        cmd = "{} server show {} -f json {}".format(os_pre, server, os_post)
        res = host.run(cmd)
        server_detail = json.loads(res.stdout)
        network_name, ip = server_detail['addresses'].split('=')
        # get network detail (again)
        # This will include network id and subnets.
        cmd = "{} network show {} -f json {}".format(os_pre, network_name,
                                                     os_post)
        res = host.run(cmd)
        network = json.loads(res.stdout)

        # confirm SSH port access
        cmd = "{} 'ip netns exec \
               qdhcp-{} nc -w1 {} 22'".format(na_pre, network['id'], ip)
        for attempt in range(10):
            res = host.run(cmd)
            try:
                assert 'SSH' in res.stdout
            except AssertionError:
                sleep(10)
            else:
                break
        else:
            assert 'SSH' in res.stdout

        # get gateway ip via subnet detail
        cmd = "{} subnet show {} -f json {}".format(os_pre, network['subnets'],
                                                    os_post)
        res = host.run(cmd)
        sub = json.loads(res.stdout)
        if sub['gateway_ip']:
            # ping out
            cmd = "{} 'ip netns exec \
                   qdhcp-{} {} ping -c1 -w2 8.8.8.8'".format(
                na_pre, network['id'], ssh.format(ip))
            host.run_expect([0], cmd)