Make sdnvpn logging proper
[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 sys
12
13 from sdnvpn.lib import config as sdnvpn_config
14 from sdnvpn.lib import openstack_utils as os_utils
15 from sdnvpn.lib import utils as test_utils
16 from sdnvpn.lib.results import Results
17 from sdnvpn.lib import logutil
18
19 logger = logutil.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     results = Results(COMMON_CONFIG.line_length)
28
29     results.add_to_summary(0, "=")
30     results.add_to_summary(2, "STATUS", "SUBTEST")
31     results.add_to_summary(0, "=")
32
33     nova_client = os_utils.get_nova_client()
34     neutron_client = os_utils.get_neutron_client()
35     glance_client = os_utils.get_glance_client()
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             glance_client, 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 = (
64             nova_client.hypervisors.list()[0].hypervisor_hostname)
65         compute_nodes = [node for node in openstack_nodes
66                          if node.is_compute()]
67
68         av_zone_1 = "nova:" + compute_hostname
69         # List of OVS bridges to get groups
70         ovs_br = "br-int"
71         # Get a list of groups, before start topology
72         initial_ovs_groups = test_utils.get_ovs_groups(compute_nodes,
73                                                        [ovs_br])
74
75         # boot INSTANCES
76         vm_2 = test_utils.create_instance(
77             nova_client,
78             TESTCASE_CONFIG.instance_2_name,
79             image_id,
80             network_1_id,
81             sg_id,
82             secgroup_name=TESTCASE_CONFIG.secgroup_name,
83             compute_node=av_zone_1)
84
85         vm_1 = test_utils.create_instance(
86             nova_client,
87             TESTCASE_CONFIG.instance_1_name,
88             image_id,
89             network_1_id,
90             sg_id,
91             secgroup_name=TESTCASE_CONFIG.secgroup_name,
92             compute_node=av_zone_1)
93         instance_ids.extend([vm_1.id, vm_2.id])
94
95         # Wait for VMs to get ips.
96         instances_up = test_utils.wait_for_instances_up(vm_1, vm_2)
97
98         if not instances_up:
99             logger.error("One or more instances is down")
100             # TODO: Handle this appropriately
101
102         logger.info("Wait before subtest")
103         test_utils.wait_before_subtest()
104         # Get added OVS groups
105         added_ovs_groups = (len(initial_ovs_groups) -
106                             len(test_utils.get_ovs_groups(
107                                 compute_nodes, [ovs_br])))
108         # Check if group added successfully
109         results.record_action("Check if a new group was added to OVS")
110         msg = "New OVS group added"
111         results.add_to_summary(0, "-")
112         if added_ovs_groups != 0:
113             results.add_success(msg)
114         else:
115             results.add_failure(msg)
116         results.add_to_summary(0, "=")
117         # Backup OVS controller connection info.
118         # To support HA changes should be made here.
119         get_ext_ip_cmd = "sudo ovs-vsctl get-controller {}".format(ovs_br)
120         ovs_controller_conn = (compute_nodes[0].run_cmd(get_ext_ip_cmd).
121                                strip().split('\n')[0])
122         # Disconnect OVS from controller
123         for compute_node in compute_nodes:
124             compute_node.run_cmd("sudo ovs-vsctl del-controller {}".
125                                  format(ovs_br))
126     except Exception as e:
127         logger.error("exception occurred while executing testcase_1: %s", e)
128         raise
129     finally:
130         # Cleanup topology
131         test_utils.cleanup_nova(nova_client, instance_ids)
132         test_utils.cleanup_glance(glance_client, image_ids)
133         test_utils.cleanup_neutron(neutron_client, floatingip_ids, bgpvpn_ids,
134                                    interfaces, subnet_ids, router_ids,
135                                    network_ids)
136     # Connect again OVS to Controller
137     for compute_node in compute_nodes:
138         compute_node.run_cmd("sudo ovs-vsctl set-controller {} {}".
139                              format(ovs_br, ovs_controller_conn))
140     logger.info("Wait before subtest")
141     test_utils.wait_before_subtest()
142     # Get OVS groups added after the reconnection
143     added_ovs_groups = (len(initial_ovs_groups) -
144                         len(test_utils.get_ovs_groups(
145                             compute_nodes, [ovs_br])))
146
147     # Check if group removed successfully
148     results.record_action("Check if group was removed from OVS "
149                           "after deleting the topology.")
150     msg = ""
151     # After removing the topology, groups must be equal to the initial
152     if added_ovs_groups != 0:
153         msg += " Additional group was not deleted from OVS"
154     results.add_to_summary(0, "-")
155     if len(msg) == 0:
156         msg = "Group was deleted from ovs"
157         results.add_success(msg)
158     else:
159         results.add_failure(msg)
160
161     return results.compile_summary()
162
163
164 if __name__ == '__main__':
165     sys.exit(main())