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 functest.utils import openstack_utils as os_utils
15 from sdnvpn.lib import config as sdnvpn_config
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_11')
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 glance_client = os_utils.get_glance_client()
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 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)
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 groups, before start topology
72 initial_ovs_groups = 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")
100 # TODO: Handle this appropriately
102 logging.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)
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 {}".
126 except Exception as e:
127 logger.error("exception occurred while executing testcase_1: %s", e)
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,
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 logging.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])))
147 # Check if group removed successfully
148 results.record_action("Check if group was removed from OVS "
149 "after deleting the topology.")
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, "-")
156 msg = "Group was deleted from ovs"
157 results.add_success(msg)
159 results.add_failure(msg)
161 return results.compile_summary()
164 if __name__ == '__main__':
165 logging.basicConfig(level=logging.INFO)