3 # Copyright (c) 2015 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
13 from datetime import datetime
15 import functest.core.testcase as testcase
16 import functest.utils.openstack_utils as os_utils
17 from functest.utils.constants import CONST
20 class VPingBase(testcase.TestCase):
22 super(VPingBase, self).__init__()
24 self.functest_repo = CONST.dir_repo_functest
25 self.repo = CONST.dir_vping
26 self.vm1_name = CONST.vping_vm_name_1
27 self.vm2_name = CONST.vping_vm_name_2
28 self.vm_boot_timeout = 180
29 self.vm_delete_timeout = 100
30 self.ping_timeout = CONST.vping_ping_timeout
32 self.image_name = CONST.vping_image_name
33 self.image_filename = CONST.openstack_image_file_name
34 self.image_format = CONST.openstack_image_disk_format
35 self.image_username = CONST.openstack_image_username
36 self.image_password = CONST.openstack_image_password
37 self.image_path = os.path.join(CONST.dir_functest_data,
40 self.flavor_name = CONST.vping_vm_flavor
42 # NEUTRON Private Network parameters
43 self.private_net_name = CONST.vping_private_net_name
44 self.private_subnet_name = CONST.vping_private_subnet_name
45 self.private_subnet_cidr = CONST.vping_private_subnet_cidr
46 self.router_name = CONST.vping_router_name
47 self.sg_name = CONST.vping_sg_name
48 self.sg_desc = CONST.vping_sg_desc
49 self.neutron_client = os_utils.get_neutron_client()
50 self.glance_client = os_utils.get_glance_client()
51 self.nova_client = os_utils.get_nova_client()
53 def run(self, **kwargs):
54 if not self.check_repo_exist():
55 return testcase.TestCase.EX_RUN_ERROR
57 image_id = self.create_image()
59 return testcase.TestCase.EX_RUN_ERROR
61 flavor = self.get_flavor()
63 return testcase.TestCase.EX_RUN_ERROR
65 network_id = self.create_network_full()
67 return testcase.TestCase.EX_RUN_ERROR
69 sg_id = self.create_security_group()
71 return testcase.TestCase.EX_RUN_ERROR
73 self.delete_exist_vms()
75 self.start_time = time.time()
76 self.logger.info("vPing Start Time:'%s'" % (
77 datetime.fromtimestamp(self.start_time).strftime(
78 '%Y-%m-%d %H:%M:%S')))
80 vm1 = self.boot_vm(self.vm1_name,
87 return testcase.TestCase.EX_RUN_ERROR
89 test_ip = self.get_test_ip(vm1)
90 vm2 = self.boot_vm(self.vm2_name,
97 return testcase.TestCase.EX_RUN_ERROR
99 EXIT_CODE = self.do_vping(vm2, test_ip)
100 if EXIT_CODE == testcase.TestCase.EX_RUN_ERROR:
103 self.stop_time = time.time()
104 self.parse_result(EXIT_CODE,
107 return testcase.TestCase.EX_OK
109 def boot_vm_preparation(self, config, vmname, test_ip):
112 def do_vping(self, vm, test_ip):
113 raise NotImplementedError('vping execution is not implemented')
115 def check_repo_exist(self):
116 if not os.path.exists(self.functest_repo):
117 self.logger.error("Functest repository not found '%s'"
118 % self.functest_repo)
122 def create_image(self):
123 _, image_id = os_utils.get_or_create_image(self.image_name,
131 def get_flavor(self):
133 flavor = self.nova_client.flavors.find(name=self.flavor_name)
134 self.logger.info("Using existing Flavor '%s'..."
138 self.logger.error("Flavor '%s' not found." % self.flavor_name)
139 self.logger.info("Available flavors are: ")
140 self.pMsg(self.nova_client.flavor.list())
143 def create_network_full(self):
144 network_dic = os_utils.create_network_full(self.neutron_client,
145 self.private_net_name,
146 self.private_subnet_name,
148 self.private_subnet_cidr)
152 "There has been a problem when creating the neutron network")
154 network_id = network_dic["net_id"]
157 def create_security_group(self):
158 sg_id = os_utils.get_security_group_id(self.neutron_client,
161 self.logger.info("Using existing security group '%s'..."
164 self.logger.info("Creating security group '%s'..."
166 SECGROUP = os_utils.create_security_group(self.neutron_client,
170 self.logger.error("Failed to create the security group...")
173 sg_id = SECGROUP['id']
175 self.logger.debug("Security group '%s' with ID=%s created "
176 "successfully." % (SECGROUP['name'], sg_id))
178 self.logger.debug("Adding ICMP rules in security group '%s'..."
180 if not os_utils.create_secgroup_rule(self.neutron_client, sg_id,
182 self.logger.error("Failed to create security group rule...")
185 self.logger.debug("Adding SSH rules in security group '%s'..."
187 if not os_utils.create_secgroup_rule(self.neutron_client, sg_id,
190 self.logger.error("Failed to create security group rule...")
193 if not os_utils.create_secgroup_rule(
194 self.neutron_client, sg_id, 'egress', 'tcp', '22', '22'):
195 self.logger.error("Failed to create security group rule...")
199 def delete_exist_vms(self):
200 servers = self.nova_client.servers.list()
201 for server in servers:
202 if server.name == self.vm1_name or server.name == self.vm2_name:
203 self.logger.info("Deleting instance %s..." % server.name)
206 def boot_vm(self, vmname, image_id, flavor, network_id, test_ip, sg_id):
208 config['name'] = vmname
209 config['flavor'] = flavor
210 config['image'] = image_id
211 config['nics'] = [{"net-id": network_id}]
212 self.boot_vm_preparation(config, vmname, test_ip)
213 self.logger.info("Creating instance '%s'..." % vmname)
214 self.logger.debug("Configuration: %s" % config)
215 vm = self.nova_client.servers.create(**config)
217 # wait until VM status is active
218 if not self.waitVmActive(self.nova_client, vm):
219 vm_status = os_utils.get_instance_status(self.nova_client, vm)
220 self.logger.error("Instance '%s' cannot be booted. Status is '%s'"
221 % (vmname, vm_status))
224 self.logger.info("Instance '%s' is ACTIVE." % vmname)
226 self.add_secgroup(vmname, vm.id, sg_id)
230 def waitVmActive(self, nova, vm):
231 # sleep and wait for VM status change
233 count = self.vm_boot_timeout / sleep_time
235 status = os_utils.get_instance_status(nova, vm)
236 self.logger.debug("Status: %s" % status)
237 if status == "ACTIVE":
239 if status == "ERROR" or status == "error":
242 self.logger.debug("Booting a VM timed out...")
245 time.sleep(sleep_time)
248 def add_secgroup(self, vmname, vm_id, sg_id):
249 self.logger.info("Adding '%s' to security group '%s'..." %
250 (vmname, self.sg_name))
251 os_utils.add_secgroup_to_instance(self.nova_client, vm_id, sg_id)
253 def get_test_ip(self, vm):
254 test_ip = vm.networks.get(self.private_net_name)[0]
255 self.logger.debug("Instance '%s' got %s" % (vm.name, test_ip))
258 def parse_result(self, code, start_time, stop_time):
261 self.logger.info("vPing OK")
262 duration = round(stop_time - start_time, 1)
263 self.logger.info("vPing duration:'%s'" % duration)
267 self.logger.info("Userdata is not supported in nova boot. "
271 self.logger.error("vPing FAILED")
273 self.details = {'timestart': start_time,
274 'duration': duration,
275 'status': test_status}
276 self.criteria = test_status
280 """pretty printing"""
281 pprint.PrettyPrinter(indent=4).pprint(msg)
284 class VPingMain(object):
285 def __init__(self, vping_cls):
286 self.vping = vping_cls()
288 def main(self, **kwargs):
290 result = self.vping.run(**kwargs)
291 if result != VPingBase.EX_OK:
294 return self.vping.push_to_db()
296 return VPingBase.EX_RUN_ERROR