Replace nova client calls with openstack sdk
[sdnvpn.git] / sdnvpn / test / functest / testcase_12.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_12')
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 flows and groups, before start topology
71         initial_ovs_flows = len(test_utils.get_ovs_flows(compute_nodes,
72                                                          [ovs_br]))
73         initial_ovs_groups = len(test_utils.get_ovs_groups(compute_nodes,
74                                                            [ovs_br]))
75
76         # boot INSTANCES
77         vm_2 = test_utils.create_instance(
78             conn,
79             TESTCASE_CONFIG.instance_2_name,
80             image_id,
81             network_1_id,
82             sg_id,
83             secgroup_name=TESTCASE_CONFIG.secgroup_name,
84             compute_node=av_zone_1)
85
86         vm_1 = test_utils.create_instance(
87             conn,
88             TESTCASE_CONFIG.instance_1_name,
89             image_id,
90             network_1_id,
91             sg_id,
92             secgroup_name=TESTCASE_CONFIG.secgroup_name,
93             compute_node=av_zone_1)
94         instance_ids.extend([vm_1.id, vm_2.id])
95
96         # Wait for VMs to get ips.
97         instances_up = test_utils.wait_for_instances_up(vm_1, vm_2)
98
99         if not instances_up:
100             logger.error("One or more instances is down")
101
102         logger.info("Wait before subtest")
103         test_utils.wait_before_subtest()
104         # Get added OVS flows and groups
105         added_ovs_flows = len(test_utils.get_ovs_flows(compute_nodes,
106                                                        [ovs_br]))
107         added_ovs_groups = len(test_utils.get_ovs_groups(compute_nodes,
108                                                          [ovs_br]))
109         # Check if flows and groups added successfully
110         results.record_action("Check if new flows and groups were added "
111                               "to OVS")
112
113         msg = "New OVS flows added"
114         results.add_to_summary(0, "-")
115         if added_ovs_flows - initial_ovs_flows > 0:
116             results.add_success(msg)
117         else:
118             results.add_failure(msg)
119         results.add_to_summary(0, "=")
120
121         msg = "New OVS groups added"
122         results.add_to_summary(0, "-")
123         if added_ovs_groups - initial_ovs_groups > 0:
124             results.add_success(msg)
125         else:
126             results.add_failure(msg)
127         results.add_to_summary(0, "=")
128
129         get_ext_ip_cmd = "sudo ovs-vsctl get-controller {}".format(ovs_br)
130         ovs_controller_conn = (compute_nodes[0].run_cmd(get_ext_ip_cmd).
131                                strip().split('\n')[0])
132
133         for compute_node in compute_nodes:
134             # Disconnect OVS from controller
135             compute_node.run_cmd("sudo ovs-vsctl del-controller {}".
136                                  format(ovs_br))
137             test_utils.wait_before_subtest()
138             # Connect again OVS to Controller
139             compute_node.run_cmd("sudo ovs-vsctl set-controller {} {}".
140                                  format(ovs_br, ovs_controller_conn))
141
142         logger.info("Wait before subtest resync type 1")
143         test_utils.wait_before_subtest()
144         # Get OVS flows added after the reconnection
145         resynced_ovs_flows = len(test_utils.get_ovs_flows(
146             compute_nodes, [ovs_br]))
147         # Get OVS groups added after the reconnection
148         resynced_ovs_groups = len(test_utils.get_ovs_groups(
149             compute_nodes, [ovs_br]))
150
151         record_action_msg = ("Check if flows/groups are reprogrammed in OVS "
152                              "after its reconnection by del/set controller.")
153         record_test_result(added_ovs_flows, resynced_ovs_flows,
154                            added_ovs_groups, resynced_ovs_groups,
155                            record_action_msg, results)
156
157         for compute_node in compute_nodes:
158             # Disconnect OVS from controller
159             compute_node.run_cmd("sudo iptables -A OUTPUT -p tcp --dport 6653"
160                                  " -j DROP")
161             test_utils.wait_before_subtest()
162             # Connect again OVS to Controller
163             compute_node.run_cmd("sudo iptables -D OUTPUT -p tcp --dport 6653"
164                                  " -j DROP")
165
166         logger.info("Wait before subtest resync type 2")
167         test_utils.wait_before_subtest()
168         # Get OVS flows added after the reconnection
169         resynced_ovs_flows = len(test_utils.get_ovs_flows(
170             compute_nodes, [ovs_br]))
171         # Get OVS groups added after the reconnection
172         resynced_ovs_groups = len(test_utils.get_ovs_groups(
173             compute_nodes, [ovs_br]))
174
175         record_action_msg = ("Check if flows/groups are reprogrammed in OVS "
176                              "after its reconnection by firewall rule for "
177                              "OF port block/unblok")
178         record_test_result(added_ovs_flows, resynced_ovs_flows,
179                            added_ovs_groups, resynced_ovs_groups,
180                            record_action_msg, results)
181
182     except Exception as e:
183         logger.error("exception occurred while executing testcase_12: %s", e)
184         raise
185     finally:
186         # Cleanup topology
187         test_utils.cleanup_nova(conn, instance_ids)
188         test_utils.cleanup_glance(conn, image_ids)
189         test_utils.cleanup_neutron(neutron_client, floatingip_ids, bgpvpn_ids,
190                                    interfaces, subnet_ids, router_ids,
191                                    network_ids)
192
193     return results.compile_summary()
194
195
196 def record_test_result(expected_flow_count, actual_flow_count,
197                        expected_group_count, actual_group_count,
198                        record_msg, results):
199     results.record_action(record_msg)
200     msg = ("OVS flows are programmed after resync expected flow count %s,"
201            " actual flow count %s" % (str(expected_flow_count),
202                                       str(actual_flow_count)))
203     results.add_to_summary(0, "-")
204     # Using <= for flow validation because ODL adds some more
205     # ARP/ICMP flows after VMs spawn up
206     if expected_flow_count <= actual_flow_count:
207         results.add_success(msg)
208     else:
209         results.add_failure(msg)
210     results.add_to_summary(0, "=")
211
212     msg = ("OVS groups are programmed after resync expected group count %s,"
213            " actual group count %s" % (str(expected_group_count),
214                                        str(actual_group_count)))
215     results.add_to_summary(0, "-")
216     if expected_group_count == actual_group_count:
217         results.add_success(msg)
218     else:
219         results.add_failure(msg)
220     results.add_to_summary(0, "=")
221
222
223 if __name__ == '__main__':
224     sys.exit(main())