Replace nova client calls with openstack sdk
[sdnvpn.git] / sdnvpn / test / functest / testcase_11.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2017 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11 import logging
12 import sys
13
14 from sdnvpn.lib import config as sdnvpn_config
15 from sdnvpn.lib import openstack_utils as os_utils
16 from sdnvpn.lib import utils as test_utils
17 from sdnvpn.lib.results import Results
18
19 logger = logging.getLogger(__name__)
20
21 COMMON_CONFIG = sdnvpn_config.CommonConfig()
22 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
23     'sdnvpn.test.functest.testcase_11')
24
25
26 def main():
27     conn = os_utils.get_os_connection()
28     results = Results(COMMON_CONFIG.line_length, conn)
29
30     results.add_to_summary(0, "=")
31     results.add_to_summary(2, "STATUS", "SUBTEST")
32     results.add_to_summary(0, "=")
33
34     neutron_client = os_utils.get_neutron_client()
35     conn = os_utils.get_os_connection()
36     openstack_nodes = test_utils.get_nodes()
37
38     (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
39      subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
40
41     try:
42         image_id = os_utils.create_glance_image(
43             conn, TESTCASE_CONFIG.image_name,
44             COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
45             container="bare", public='public')
46         image_ids.append(image_id)
47
48         network_1_id = test_utils.create_net(neutron_client,
49                                              TESTCASE_CONFIG.net_1_name)
50         subnet_1_id = test_utils.create_subnet(neutron_client,
51                                                TESTCASE_CONFIG.subnet_1_name,
52                                                TESTCASE_CONFIG.subnet_1_cidr,
53                                                network_1_id)
54
55         network_ids.append(network_1_id)
56         subnet_ids.append(subnet_1_id)
57
58         sg_id = os_utils.create_security_group_full(
59             neutron_client, TESTCASE_CONFIG.secgroup_name,
60             TESTCASE_CONFIG.secgroup_descr)
61
62         # Check required number of compute nodes
63         compute_hostname = conn.compute.hypervisors().next().name
64         compute_nodes = [node for node in openstack_nodes
65                          if node.is_compute()]
66
67         av_zone_1 = "nova:" + compute_hostname
68         # List of OVS bridges to get groups
69         ovs_br = "br-int"
70         # Get a list of groups, before start topology
71         initial_ovs_groups = test_utils.get_ovs_groups(compute_nodes,
72                                                        [ovs_br])
73
74         # boot INSTANCES
75         vm_2 = test_utils.create_instance(
76             conn,
77             TESTCASE_CONFIG.instance_2_name,
78             image_id,
79             network_1_id,
80             sg_id,
81             secgroup_name=TESTCASE_CONFIG.secgroup_name,
82             compute_node=av_zone_1)
83
84         vm_1 = test_utils.create_instance(
85             conn,
86             TESTCASE_CONFIG.instance_1_name,
87             image_id,
88             network_1_id,
89             sg_id,
90             secgroup_name=TESTCASE_CONFIG.secgroup_name,
91             compute_node=av_zone_1)
92         instance_ids.extend([vm_1.id, vm_2.id])
93
94         # Wait for VMs to get ips.
95         instances_up = test_utils.wait_for_instances_up(vm_1, vm_2)
96
97         if not instances_up:
98             logger.error("One or more instances is down")
99             # TODO: Handle this appropriately
100
101         logger.info("Wait before subtest")
102         test_utils.wait_before_subtest()
103         # Get added OVS groups
104         added_ovs_groups = (len(initial_ovs_groups) -
105                             len(test_utils.get_ovs_groups(
106                                 compute_nodes, [ovs_br])))
107         # Check if group added successfully
108         results.record_action("Check if a new group was added to OVS")
109         msg = "New OVS group added"
110         results.add_to_summary(0, "-")
111         if added_ovs_groups != 0:
112             results.add_success(msg)
113         else:
114             results.add_failure(msg)
115         results.add_to_summary(0, "=")
116         # Backup OVS controller connection info.
117         # To support HA changes should be made here.
118         get_ext_ip_cmd = "sudo ovs-vsctl get-controller {}".format(ovs_br)
119         ovs_controller_conn = (compute_nodes[0].run_cmd(get_ext_ip_cmd).
120                                strip().split('\n')[0])
121         # Disconnect OVS from controller
122         for compute_node in compute_nodes:
123             compute_node.run_cmd("sudo ovs-vsctl del-controller {}".
124                                  format(ovs_br))
125     except Exception as e:
126         logger.error("exception occurred while executing testcase_1: %s", e)
127         raise
128     finally:
129         # Cleanup topology
130         test_utils.cleanup_nova(conn, instance_ids)
131         test_utils.cleanup_glance(conn, image_ids)
132         test_utils.cleanup_neutron(neutron_client, floatingip_ids, bgpvpn_ids,
133                                    interfaces, subnet_ids, router_ids,
134                                    network_ids)
135     # Connect again OVS to Controller
136     for compute_node in compute_nodes:
137         compute_node.run_cmd("sudo ovs-vsctl set-controller {} {}".
138                              format(ovs_br, ovs_controller_conn))
139     logger.info("Wait before subtest")
140     test_utils.wait_before_subtest()
141     # Get OVS groups added after the reconnection
142     added_ovs_groups = (len(initial_ovs_groups) -
143                         len(test_utils.get_ovs_groups(
144                             compute_nodes, [ovs_br])))
145
146     # Check if group removed successfully
147     results.record_action("Check if group was removed from OVS "
148                           "after deleting the topology.")
149     msg = ""
150     # After removing the topology, groups must be equal to the initial
151     if added_ovs_groups != 0:
152         msg += " Additional group was not deleted from OVS"
153     results.add_to_summary(0, "-")
154     if len(msg) == 0:
155         msg = "Group was deleted from ovs"
156         results.add_success(msg)
157     else:
158         results.add_failure(msg)
159
160     return results.compile_summary()
161
162
163 if __name__ == '__main__':
164     sys.exit(main())