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 results = Results(COMMON_CONFIG.line_length)
29 results.add_to_summary(0, "=")
30 results.add_to_summary(2, "STATUS", "SUBTEST")
31 results.add_to_summary(0, "=")
33 nova_client = os_utils.get_nova_client()
34 neutron_client = os_utils.get_neutron_client()
35 conn = os_utils.get_os_connection()
36 openstack_nodes = test_utils.get_nodes()
38 (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
39 subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
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)
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,
55 network_ids.append(network_1_id)
56 subnet_ids.append(subnet_1_id)
58 sg_id = os_utils.create_security_group_full(
59 neutron_client, TESTCASE_CONFIG.secgroup_name,
60 TESTCASE_CONFIG.secgroup_descr)
62 # Check required number of compute nodes
64 nova_client.hypervisors.list()[0].hypervisor_hostname)
65 compute_nodes = [node for node in openstack_nodes
68 av_zone_1 = "nova:" + compute_hostname
69 # List of OVS bridges to get groups
71 # Get a list of flows and groups, before start topology
72 initial_ovs_flows = len(test_utils.get_ovs_flows(compute_nodes,
74 initial_ovs_groups = len(test_utils.get_ovs_groups(compute_nodes,
78 vm_2 = test_utils.create_instance(
80 TESTCASE_CONFIG.instance_2_name,
84 secgroup_name=TESTCASE_CONFIG.secgroup_name,
85 compute_node=av_zone_1)
87 vm_1 = test_utils.create_instance(
89 TESTCASE_CONFIG.instance_1_name,
93 secgroup_name=TESTCASE_CONFIG.secgroup_name,
94 compute_node=av_zone_1)
95 instance_ids.extend([vm_1.id, vm_2.id])
97 # Wait for VMs to get ips.
98 instances_up = test_utils.wait_for_instances_up(vm_1, vm_2)
101 logger.error("One or more instances is down")
103 logger.info("Wait before subtest")
104 test_utils.wait_before_subtest()
105 # Get added OVS flows and groups
106 added_ovs_flows = len(test_utils.get_ovs_flows(compute_nodes,
108 added_ovs_groups = len(test_utils.get_ovs_groups(compute_nodes,
110 # Check if flows and groups added successfully
111 results.record_action("Check if new flows and groups were added "
114 msg = "New OVS flows added"
115 results.add_to_summary(0, "-")
116 if added_ovs_flows - initial_ovs_flows > 0:
117 results.add_success(msg)
119 results.add_failure(msg)
120 results.add_to_summary(0, "=")
122 msg = "New OVS groups added"
123 results.add_to_summary(0, "-")
124 if added_ovs_groups - initial_ovs_groups > 0:
125 results.add_success(msg)
127 results.add_failure(msg)
128 results.add_to_summary(0, "=")
130 get_ext_ip_cmd = "sudo ovs-vsctl get-controller {}".format(ovs_br)
131 ovs_controller_conn = (compute_nodes[0].run_cmd(get_ext_ip_cmd).
132 strip().split('\n')[0])
134 for compute_node in compute_nodes:
135 # Disconnect OVS from controller
136 compute_node.run_cmd("sudo ovs-vsctl del-controller {}".
138 test_utils.wait_before_subtest()
139 # Connect again OVS to Controller
140 compute_node.run_cmd("sudo ovs-vsctl set-controller {} {}".
141 format(ovs_br, ovs_controller_conn))
143 logger.info("Wait before subtest resync type 1")
144 test_utils.wait_before_subtest()
145 # Get OVS flows added after the reconnection
146 resynced_ovs_flows = len(test_utils.get_ovs_flows(
147 compute_nodes, [ovs_br]))
148 # Get OVS groups added after the reconnection
149 resynced_ovs_groups = len(test_utils.get_ovs_groups(
150 compute_nodes, [ovs_br]))
152 record_action_msg = ("Check if flows/groups are reprogrammed in OVS "
153 "after its reconnection by del/set controller.")
154 record_test_result(added_ovs_flows, resynced_ovs_flows,
155 added_ovs_groups, resynced_ovs_groups,
156 record_action_msg, results)
158 for compute_node in compute_nodes:
159 # Disconnect OVS from controller
160 compute_node.run_cmd("sudo iptables -A OUTPUT -p tcp --dport 6653"
162 test_utils.wait_before_subtest()
163 # Connect again OVS to Controller
164 compute_node.run_cmd("sudo iptables -D OUTPUT -p tcp --dport 6653"
167 logger.info("Wait before subtest resync type 2")
168 test_utils.wait_before_subtest()
169 # Get OVS flows added after the reconnection
170 resynced_ovs_flows = len(test_utils.get_ovs_flows(
171 compute_nodes, [ovs_br]))
172 # Get OVS groups added after the reconnection
173 resynced_ovs_groups = len(test_utils.get_ovs_groups(
174 compute_nodes, [ovs_br]))
176 record_action_msg = ("Check if flows/groups are reprogrammed in OVS "
177 "after its reconnection by firewall rule for "
178 "OF port block/unblok")
179 record_test_result(added_ovs_flows, resynced_ovs_flows,
180 added_ovs_groups, resynced_ovs_groups,
181 record_action_msg, results)
183 except Exception as e:
184 logger.error("exception occurred while executing testcase_12: %s", e)
188 test_utils.cleanup_nova(nova_client, instance_ids)
189 test_utils.cleanup_glance(conn, image_ids)
190 test_utils.cleanup_neutron(neutron_client, floatingip_ids, bgpvpn_ids,
191 interfaces, subnet_ids, router_ids,
194 return results.compile_summary()
197 def record_test_result(expected_flow_count, actual_flow_count,
198 expected_group_count, actual_group_count,
199 record_msg, results):
200 results.record_action(record_msg)
201 msg = ("OVS flows are programmed after resync expected flow count %s,"
202 " actual flow count %s" % (str(expected_flow_count),
203 str(actual_flow_count)))
204 results.add_to_summary(0, "-")
205 # Using <= for flow validation because ODL adds some more
206 # ARP/ICMP flows after VMs spawn up
207 if expected_flow_count <= actual_flow_count:
208 results.add_success(msg)
210 results.add_failure(msg)
211 results.add_to_summary(0, "=")
213 msg = ("OVS groups are programmed after resync expected group count %s,"
214 " actual group count %s" % (str(expected_group_count),
215 str(actual_group_count)))
216 results.add_to_summary(0, "-")
217 if expected_group_count == actual_group_count:
218 results.add_success(msg)
220 results.add_failure(msg)
221 results.add_to_summary(0, "=")
224 if __name__ == '__main__':