~1chb1n/openstack-mojo-specs/mitaka-metal-and-stuff

« back to all changes in this revision

Viewing changes to helper/tests/test_vrrp_ha.py

  • Committer: David Ames
  • Date: 2015-11-18 15:52:18 UTC
  • mfrom: (236.1.5 vrrp)
  • Revision ID: david.ames@canonical.com-20151118155218-l60h221qxx0rhpt3
[gnuoy, r=thedac] VRRP spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
import time
 
3
import sys
 
4
import utils.mojo_utils as mojo_utils
 
5
import utils.mojo_os_utils as mojo_os_utils
 
6
import logging
 
7
 
 
8
 
 
9
def lookup_cirros_server(cloud_auth):
 
10
    cirros_images = get_cirros_images(cloud_auth)
 
11
    nova_client = mojo_os_utils.get_nova_client(cloud_auth)
 
12
    for server in nova_client.servers.list():
 
13
        if server.image['id'] in cirros_images:
 
14
            return server
 
15
 
 
16
 
 
17
def get_cirros_server(cloud_auth, image_password):
 
18
    logging.info('Looking for existing cirros server')
 
19
    cirros_server = lookup_cirros_server(cloud_auth)
 
20
    if cirros_server:
 
21
        ip = get_server_floating_ip(cirros_server)
 
22
        logging.info('Checking connectivity to cirros guest')
 
23
        if not mojo_os_utils.ssh_test('cirros', ip, cirros_server.name,
 
24
                                      password=image_password):
 
25
            raise Exception('Cirros guest inaccessable')
 
26
    else:
 
27
        nova_client = mojo_os_utils.get_nova_client(cloud_auth)
 
28
        logging.info('Creating new cirros guest')
 
29
        mojo_os_utils.boot_and_test(
 
30
            nova_client,
 
31
            image_name='cirros',
 
32
            flavor_name='m1.small',
 
33
            number=1,
 
34
            privkey=None,
 
35
        )
 
36
        cirros_server = lookup_cirros_server(cloud_auth)
 
37
        ip = get_server_floating_ip(cirros_server)
 
38
    return cirros_server, ip
 
39
 
 
40
 
 
41
def get_cirros_images(cloud_auth):
 
42
    logging.info('Getting list of cirros images')
 
43
    glance_client = mojo_os_utils.get_glance_client(cloud_auth)
 
44
    cirros_images = []
 
45
    for image in glance_client.images.list():
 
46
        if 'cirros' in image.name:
 
47
            cirros_images.append(image.id)
 
48
    return cirros_images
 
49
 
 
50
 
 
51
def check_server_state(nova_client, state, server_id=None, server_name=None,
 
52
                       wait_time=120):
 
53
    sleep_time = 2
 
54
    if server_name:
 
55
        server_id = nova_client.servers.find(name=server_name).id
 
56
    server = nova_client.servers.find(id=server_id)
 
57
    while server.status != state and wait_time > 0:
 
58
        time.sleep(sleep_time)
 
59
        server = nova_client.servers.find(id=server_id)
 
60
        wait_time -= sleep_time
 
61
    return server.status == state
 
62
 
 
63
 
 
64
def check_neutron_agent_states(neutron_client, host_name, wait_time=120):
 
65
    sleep_time = 2
 
66
    statuses = [False]
 
67
    while False in statuses and wait_time > 0:
 
68
        time.sleep(sleep_time)
 
69
        statuses = []
 
70
        for agent in neutron_client.list_agents()['agents']:
 
71
            if agent['host'] == host_name:
 
72
                statuses.append(agent['admin_state_up'])
 
73
                statuses.append(agent['alive'])
 
74
        wait_time -= sleep_time
 
75
    return False not in statuses
 
76
 
 
77
 
 
78
def get_server_floating_ip(server):
 
79
    for addr in server.addresses['private']:
 
80
        if addr['OS-EXT-IPS:type'] == 'floating':
 
81
            return addr['addr']
 
82
 
 
83
 
 
84
def main(argv):
 
85
    logging.basicConfig(level=logging.INFO)
 
86
    logging.getLogger("urllib3").setLevel(logging.WARNING)
 
87
    overcloud_novarc = mojo_utils.get_overcloud_auth()
 
88
    undercloud_novarc = mojo_utils.get_undercloud_auth()
 
89
    under_novac = mojo_os_utils.get_nova_client(undercloud_novarc)
 
90
    over_neutronc = mojo_os_utils.get_neutron_client(overcloud_novarc)
 
91
    image_config = mojo_utils.get_mojo_config('images.yaml')
 
92
    image_password = image_config['cirros']['password']
 
93
    # Look for existing Cirros guest
 
94
    server, ip = get_cirros_server(overcloud_novarc, image_password)
 
95
    router = over_neutronc.list_routers(name='provider-router')['routers'][0]
 
96
    l3_agents = over_neutronc.list_l3_agent_hosting_routers(
 
97
        router=router['id'])['agents']
 
98
    logging.info('Checking there are multiple L3 agents running tenant router')
 
99
    if len(l3_agents) != 2:
 
100
        raise Exception('Unexpected number of l3 agents')
 
101
    for agent in l3_agents:
 
102
        gateway_hostname = agent['host']
 
103
        gateway_server = under_novac.servers.find(name=gateway_hostname)
 
104
        logging.info('Shutting down neutron gateway {} ({})'.format(
 
105
            gateway_hostname,
 
106
            gateway_server.id)
 
107
        )
 
108
        gateway_server.stop()
 
109
        if not check_server_state(under_novac, 'SHUTOFF',
 
110
                                  server_name=gateway_hostname):
 
111
            raise Exception('Server failed to reach SHUTOFF state')
 
112
        logging.info('Neutron gateway %s has shutdown' % (gateway_hostname))
 
113
        logging.info('Checking connectivity to cirros guest')
 
114
        if not mojo_os_utils.wait_for_ping(ip, 90):
 
115
            raise Exception('Cirros guest not responding to ping')
 
116
        if not mojo_os_utils.ssh_test('cirros', ip, server.name,
 
117
                                      password=image_password):
 
118
            raise Exception('Cirros guest issh connection failed')
 
119
        logging.info('Starting neutron gateway: ' + gateway_hostname)
 
120
        gateway_server.start()
 
121
        if not check_server_state(under_novac, 'ACTIVE',
 
122
                                  server_name=gateway_hostname):
 
123
            raise Exception('Server failed to reach SHUTOFF state')
 
124
        if not check_neutron_agent_states(over_neutronc, gateway_hostname):
 
125
            raise Exception('Server agents failed to reach active state')
 
126
 
 
127
if __name__ == "__main__":
 
128
    sys.exit(main(sys.argv))