Conform to interface change of ODL node in Fuel Deployment
[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     openstack_nodes = test_utils.get_nodes()
36
37     (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
38      subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
39
40     try:
41         image_id = os_utils.create_glance_image(
42             conn, TESTCASE_CONFIG.image_name,
43             COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
44             container="bare", public='public')
45         image_ids.append(image_id)
46
47         network_1_id = test_utils.create_net(conn,
48                                              TESTCASE_CONFIG.net_1_name)
49         subnet_1_id = test_utils.create_subnet(conn,
50                                                TESTCASE_CONFIG.subnet_1_name,
51                                                TESTCASE_CONFIG.subnet_1_cidr,
52                                                network_1_id)
53
54         network_ids.append(network_1_id)
55         subnet_ids.append(subnet_1_id)
56
57         sg_id = os_utils.create_security_group_full(
58             conn, TESTCASE_CONFIG.secgroup_name,
59             TESTCASE_CONFIG.secgroup_descr)
60
61         # Check required number of compute nodes
62         compute_hostname = conn.compute.hypervisors().next().name
63         compute_nodes = [node for node in openstack_nodes
64                          if node.is_compute()]
65
66         av_zone_1 = "nova:" + compute_hostname
67         # List of OVS bridges to get groups
68         ovs_br = "br-int"
69         # Get a list of flows and groups, before start topology
70         initial_ovs_flows = len(test_utils.get_ovs_flows(compute_nodes,
71                                                          [ovs_br]))
72         initial_ovs_groups = len(test_utils.get_ovs_groups(compute_nodes,
73                                                            [ovs_br]))
74
75         # boot INSTANCES
76         vm_2 = test_utils.create_instance(
77             conn,
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             conn,
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
101         logger.info("Wait before subtest")
102         test_utils.wait_before_subtest()
103         # Get added OVS flows and groups
104         added_ovs_flows = len(test_utils.get_ovs_flows(compute_nodes,
105                                                        [ovs_br]))
106         added_ovs_groups = len(test_utils.get_ovs_groups(compute_nodes,
107                                                          [ovs_br]))
108         # Check if flows and groups added successfully
109         results.record_action("Check if new flows and groups were added "
110                               "to OVS")
111
112         msg = "New OVS flows added"
113         results.add_to_summary(0, "-")
114         if added_ovs_flows - initial_ovs_flows > 0:
115             results.add_success(msg)
116         else:
117             results.add_failure(msg)
118         results.add_to_summary(0, "=")
119
120         msg = "New OVS groups added"
121         results.add_to_summary(0, "-")
122         if added_ovs_groups - initial_ovs_groups > 0:
123             results.add_success(msg)
124         else:
125             results.add_failure(msg)
126         results.add_to_summary(0, "=")
127
128         get_ext_ip_cmd = "sudo ovs-vsctl get-controller {}".format(ovs_br)
129         ovs_controller_conn = (compute_nodes[0].run_cmd(get_ext_ip_cmd).
130                                strip().split('\n')[0])
131
132         for compute_node in compute_nodes:
133             # Disconnect OVS from controller
134             compute_node.run_cmd("sudo ovs-vsctl del-controller {}".
135                                  format(ovs_br))
136             test_utils.wait_before_subtest()
137             # Connect again OVS to Controller
138             compute_node.run_cmd("sudo ovs-vsctl set-controller {} {}".
139                                  format(ovs_br, ovs_controller_conn))
140
141         logger.info("Wait before subtest resync type 1")
142         test_utils.wait_before_subtest()
143         # Get OVS flows added after the reconnection
144         resynced_ovs_flows = len(test_utils.get_ovs_flows(
145             compute_nodes, [ovs_br]))
146         # Get OVS groups added after the reconnection
147         resynced_ovs_groups = len(test_utils.get_ovs_groups(
148             compute_nodes, [ovs_br]))
149
150         record_action_msg = ("Check if flows/groups are reprogrammed in OVS "
151                              "after its reconnection by del/set controller.")
152         record_test_result(added_ovs_flows, resynced_ovs_flows,
153                            added_ovs_groups, resynced_ovs_groups,
154                            record_action_msg, results)
155
156         for compute_node in compute_nodes:
157             # Disconnect OVS from controller
158             compute_node.run_cmd("sudo iptables -A OUTPUT -p tcp --dport 6653"
159                                  " -j DROP")
160             test_utils.wait_before_subtest()
161             # Connect again OVS to Controller
162             compute_node.run_cmd("sudo iptables -D OUTPUT -p tcp --dport 6653"
163                                  " -j DROP")
164
165         logger.info("Wait before subtest resync type 2")
166         test_utils.wait_before_subtest()
167         # Get OVS flows added after the reconnection
168         resynced_ovs_flows = len(test_utils.get_ovs_flows(
169             compute_nodes, [ovs_br]))
170         # Get OVS groups added after the reconnection
171         resynced_ovs_groups = len(test_utils.get_ovs_groups(
172             compute_nodes, [ovs_br]))
173
174         record_action_msg = ("Check if flows/groups are reprogrammed in OVS "
175                              "after its reconnection by firewall rule for "
176                              "OF port block/unblok")
177         record_test_result(added_ovs_flows, resynced_ovs_flows,
178                            added_ovs_groups, resynced_ovs_groups,
179                            record_action_msg, results)
180
181     except Exception as e:
182         logger.error("exception occurred while executing testcase_12: %s", e)
183         raise
184     finally:
185         # Cleanup topology
186         test_utils.cleanup_nova(conn, instance_ids)
187         test_utils.cleanup_glance(conn, image_ids)
188         test_utils.cleanup_neutron(conn, neutron_client, floatingip_ids,
189                                    bgpvpn_ids, interfaces, subnet_ids,
190                                    router_ids, network_ids)
191
192     return results.compile_summary()
193
194
195 def record_test_result(expected_flow_count, actual_flow_count,
196                        expected_group_count, actual_group_count,
197                        record_msg, results):
198     results.record_action(record_msg)
199     msg = ("OVS flows are programmed after resync expected flow count %s,"
200            " actual flow count %s" % (str(expected_flow_count),
201                                       str(actual_flow_count)))
202     results.add_to_summary(0, "-")
203     # Using <= for flow validation because ODL adds some more
204     # ARP/ICMP flows after VMs spawn up
205     if expected_flow_count <= actual_flow_count:
206         results.add_success(msg)
207     else:
208         results.add_failure(msg)
209     results.add_to_summary(0, "=")
210
211     msg = ("OVS groups are programmed after resync expected group count %s,"
212            " actual group count %s" % (str(expected_group_count),
213                                        str(actual_group_count)))
214     results.add_to_summary(0, "-")
215     if expected_group_count == actual_group_count:
216         results.add_success(msg)
217     else:
218         results.add_failure(msg)
219     results.add_to_summary(0, "=")
220
221
222 if __name__ == '__main__':
223     sys.exit(main())