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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
19 logger = logging.getLogger(__name__)
21 COMMON_CONFIG = sdnvpn_config.CommonConfig()
22 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
23 'sdnvpn.test.functest.testcase_12')
27 conn = os_utils.get_os_connection()
28 results = Results(COMMON_CONFIG.line_length, conn)
30 results.add_to_summary(0, "=")
31 results.add_to_summary(2, "STATUS", "SUBTEST")
32 results.add_to_summary(0, "=")
34 neutron_client = os_utils.get_neutron_client()
35 openstack_nodes = test_utils.get_nodes()
37 (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
38 subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
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)
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,
54 network_ids.append(network_1_id)
55 subnet_ids.append(subnet_1_id)
57 sg_id = os_utils.create_security_group_full(
58 conn, TESTCASE_CONFIG.secgroup_name,
59 TESTCASE_CONFIG.secgroup_descr)
61 # Check required number of compute nodes
62 compute_hostname = conn.compute.hypervisors().next().name
63 compute_nodes = [node for node in openstack_nodes
66 av_zone_1 = "nova:" + compute_hostname
67 # List of OVS bridges to get groups
69 # Get a list of flows and groups, before start topology
70 initial_ovs_flows = len(test_utils.get_ovs_flows(compute_nodes,
72 initial_ovs_groups = len(test_utils.get_ovs_groups(compute_nodes,
76 vm_2 = test_utils.create_instance(
78 TESTCASE_CONFIG.instance_2_name,
82 secgroup_name=TESTCASE_CONFIG.secgroup_name,
83 compute_node=av_zone_1)
85 vm_1 = test_utils.create_instance(
87 TESTCASE_CONFIG.instance_1_name,
91 secgroup_name=TESTCASE_CONFIG.secgroup_name,
92 compute_node=av_zone_1)
93 instance_ids.extend([vm_1.id, vm_2.id])
95 # Wait for VMs to get ips.
96 instances_up = test_utils.wait_for_instances_up(vm_1, vm_2)
99 logger.error("One or more instances is down")
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,
106 added_ovs_groups = len(test_utils.get_ovs_groups(compute_nodes,
108 # Check if flows and groups added successfully
109 results.record_action("Check if new flows and groups were added "
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)
117 results.add_failure(msg)
118 results.add_to_summary(0, "=")
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)
125 results.add_failure(msg)
126 results.add_to_summary(0, "=")
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])
132 for compute_node in compute_nodes:
133 # Disconnect OVS from controller
134 compute_node.run_cmd("sudo ovs-vsctl del-controller {}".
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))
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]))
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)
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"
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"
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]))
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)
181 except Exception as e:
182 logger.error("exception occurred while executing testcase_12: %s", e)
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)
192 return results.compile_summary()
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)
208 results.add_failure(msg)
209 results.add_to_summary(0, "=")
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)
218 results.add_failure(msg)
219 results.add_to_summary(0, "=")
222 if __name__ == '__main__':