3 #######################################################################
5 # Copyright (c) 2015 Orange
6 # valentin.boucher@orange.com
8 # All rights reserved. This program and the accompanying materials
9 # are made available under the terms of the Apache License, Version 2.0
10 # which accompanies this distribution, and is available at
11 # http://www.apache.org/licenses/LICENSE-2.0
12 ########################################################################
24 import keystoneclient.v2_0.client as ksclient
25 import glanceclient.client as glclient
26 import novaclient.client as nvclient
27 from neutronclient.v2_0 import client as ntclient
29 from clearwater import clearwater
30 from orchestrator import orchestrator
32 import functest.utils.functest_logger as ft_logger
33 import functest.utils.functest_utils as functest_utils
34 import functest.utils.openstack_utils as os_utils
37 pp = pprint.PrettyPrinter(indent=4)
40 parser = argparse.ArgumentParser()
41 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
42 parser.add_argument("-r", "--report",
43 help="Create json result file",
45 parser.add_argument("-n", "--noclean",
46 help="Don't clean the created resources for this test.",
48 args = parser.parse_args()
50 """ logging configuration """
51 logger = ft_logger.Logger("vIMS").getLogger()
53 REPO_PATH = os.environ['repos_dir'] + '/functest/'
54 if not os.path.exists(REPO_PATH):
55 logger.error("Functest repository directory not found '%s'" % REPO_PATH)
58 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
59 functest_yaml = yaml.safe_load(f)
63 VIMS_DIR = (REPO_PATH +
64 functest_yaml.get("general").get("directories").get("dir_vIMS"))
65 VIMS_DATA_DIR = functest_yaml.get("general").get(
66 "directories").get("dir_vIMS_data") + "/"
67 VIMS_TEST_DIR = functest_yaml.get("general").get(
68 "directories").get("dir_repo_vims_test") + "/"
69 DB_URL = functest_yaml.get("results").get("test_db_url")
71 TENANT_NAME = functest_yaml.get("vIMS").get("general").get("tenant_name")
72 TENANT_DESCRIPTION = functest_yaml.get("vIMS").get(
73 "general").get("tenant_description")
74 IMAGES = functest_yaml.get("vIMS").get("general").get("images")
76 CFY_MANAGER_BLUEPRINT = functest_yaml.get(
77 "vIMS").get("cloudify").get("blueprint")
78 CFY_MANAGER_REQUIERMENTS = functest_yaml.get(
79 "vIMS").get("cloudify").get("requierments")
80 CFY_INPUTS = functest_yaml.get("vIMS").get("cloudify").get("inputs")
82 CW_BLUEPRINT = functest_yaml.get("vIMS").get("clearwater").get("blueprint")
83 CW_DEPLOYMENT_NAME = functest_yaml.get("vIMS").get(
84 "clearwater").get("deployment-name")
85 CW_INPUTS = functest_yaml.get("vIMS").get("clearwater").get("inputs")
86 CW_REQUIERMENTS = functest_yaml.get("vIMS").get(
87 "clearwater").get("requierments")
89 CFY_DEPLOYMENT_DURATION = 0
90 CW_DEPLOYMENT_DURATION = 0
92 TESTCASE_START_TIME = time.time()
93 RESULTS = {'orchestrator': {'duration': 0, 'result': ''},
94 'vIMS': {'duration': 0, 'result': ''},
95 'sig_test': {'duration': 0, 'result': ''}}
98 def download_and_add_image_on_glance(glance, image_name, image_url):
99 dest_path = VIMS_DATA_DIR + "tmp/"
100 if not os.path.exists(dest_path):
101 os.makedirs(dest_path)
102 file_name = image_url.rsplit('/')[-1]
103 if not functest_utils.download_url(image_url, dest_path):
104 logger.error("Failed to download image %s" % file_name)
107 image = os_utils.create_glance_image(
108 glance, image_name, dest_path + file_name)
110 logger.error("Failed to upload image on glance")
116 def step_failure(step_name, error_msg):
117 logger.error(error_msg)
118 set_result(step_name, 0, error_msg)
120 # in case of failure starting and stoping time are not correct
121 stop_time = time.time()
122 if step_name == "sig_test":
124 functest_utils.push_results_to_db("functest",
134 def set_result(step_name, duration=0, result=""):
135 RESULTS[step_name] = {'duration': duration, 'result': result}
138 def test_clearwater():
139 script = "source " + VIMS_DATA_DIR + "venv_cloudify/bin/activate; "
140 script += "cd " + VIMS_DATA_DIR + "; "
141 script += "cfy status | grep -Eo \"([0-9]{1,3}\.){3}[0-9]{1,3}\""
142 cmd = "/bin/bash -c '" + script + "'"
145 logger.debug("Trying to get clearwater manager IP ... ")
146 mgr_ip = os.popen(cmd).read()
147 mgr_ip = mgr_ip.splitlines()[0]
149 step_failure("sig_test", "Unable to retrieve the IP of the "
150 "cloudify manager server !")
152 api_url = "http://" + mgr_ip + "/api/v2"
153 dep_outputs = requests.get(api_url + "/deployments/" +
154 CW_DEPLOYMENT_NAME + "/outputs")
155 dns_ip = dep_outputs.json()['outputs']['dns_ip']
156 ellis_ip = dep_outputs.json()['outputs']['ellis_ip']
158 ellis_url = "http://" + ellis_ip + "/"
159 url = ellis_url + "accounts"
161 params = {"password": "functest",
162 "full_name": "opnfv functest user",
163 "email": "functest@opnfv.fr",
164 "signup_code": "secret"}
166 rq = requests.post(url, data=params)
168 while rq.status_code != 201 and i > 0:
169 rq = requests.post(url, data=params)
173 if rq.status_code == 201:
174 url = ellis_url + "session"
175 rq = requests.post(url, data=params)
178 url = ellis_url + "accounts/" + params['email'] + "/numbers"
180 rq = requests.post(url, cookies=cookies)
182 while rq.status_code != 200 and i > 0:
183 rq = requests.post(url, cookies=cookies)
187 if rq.status_code != 200:
188 step_failure("sig_test", "Unable to create a number: %s"
189 % rq.json()['reason'])
191 start_time_ts = time.time()
192 end_time_ts = start_time_ts
193 logger.info("vIMS functional test Start Time:'%s'" % (
194 datetime.datetime.fromtimestamp(start_time_ts).strftime(
195 '%Y-%m-%d %H:%M:%S')))
196 nameservers = functest_utils.get_resolvconf_ns()
198 for ns in nameservers:
199 resolvconf += "\nnameserver " + ns
202 script = ('echo -e "nameserver ' + dns_ip + resolvconf +
203 '" > /etc/resolv.conf; ')
204 script += 'source /etc/profile.d/rvm.sh; '
205 script += 'cd ' + VIMS_TEST_DIR + '; '
206 script += ('rake test[' + CW_INPUTS["public_domain"] +
207 '] SIGNUP_CODE="secret"')
209 cmd = "/bin/bash -c '" + script + "'"
210 output_file = "output.txt"
211 f = open(output_file, 'w+')
212 subprocess.call(cmd, shell=True, stdout=f,
213 stderr=subprocess.STDOUT)
215 end_time_ts = time.time()
216 duration = round(end_time_ts - start_time_ts, 1)
217 logger.info("vIMS functional test duration:'%s'" % duration)
218 f = open(output_file, 'r')
220 if result != "" and logger:
223 vims_test_result = ""
225 logger.debug("Trying to load test results")
226 with open(VIMS_TEST_DIR + "temp.json") as f:
227 vims_test_result = json.load(f)
230 logger.error("Unable to retrieve test results")
232 set_result("sig_test", duration, vims_test_result)
234 # success criteria for vIMS (for Brahmaputra)
235 # - orchestrator deployed
237 # TODO use test criteria defined in config file
240 if (RESULTS['orchestrator']['duration'] > 0 and
241 RESULTS['vIMS']['duration'] > 0):
244 logger.error("Unable to set test status")
246 functest_utils.push_results_to_db("functest",
255 os.remove(VIMS_TEST_DIR + "temp.json")
257 logger.error("Deleting file failed")
262 # ############### GENERAL INITIALISATION ################
264 if not os.path.exists(VIMS_DATA_DIR):
265 os.makedirs(VIMS_DATA_DIR)
267 ks_creds = os_utils.get_credentials("keystone")
268 nv_creds = os_utils.get_credentials("nova")
269 nt_creds = os_utils.get_credentials("neutron")
271 logger.info("Prepare OpenStack plateform (create tenant and user)")
272 keystone = ksclient.Client(**ks_creds)
274 user_id = os_utils.get_user_id(keystone, ks_creds['username'])
276 step_failure("init", "Error : Failed to get id of " +
277 ks_creds['username'])
279 tenant_id = os_utils.create_tenant(
280 keystone, TENANT_NAME, TENANT_DESCRIPTION)
282 step_failure("init", "Error : Failed to create " +
283 TENANT_NAME + " tenant")
285 roles_name = ["admin", "Admin"]
287 for role_name in roles_name:
289 role_id = os_utils.get_role_id(keystone, role_name)
292 logger.error("Error : Failed to get id for %s role" % role_name)
294 if not os_utils.add_role_user(keystone, user_id, role_id, tenant_id):
295 logger.error("Error : Failed to add %s on tenant" %
296 ks_creds['username'])
298 user_id = os_utils.create_user(
299 keystone, TENANT_NAME, TENANT_NAME, None, tenant_id)
301 logger.error("Error : Failed to create %s user" % TENANT_NAME)
303 logger.info("Update OpenStack creds informations")
305 "username": TENANT_NAME,
306 "password": TENANT_NAME,
307 "tenant_name": TENANT_NAME,
311 "tenant_name": TENANT_NAME,
315 "project_id": TENANT_NAME,
318 logger.info("Upload some OS images if it doesn't exist")
319 glance_endpoint = keystone.service_catalog.url_for(
320 service_type='image', endpoint_type='publicURL')
321 glance = glclient.Client(1, glance_endpoint, token=keystone.auth_token)
323 for img in IMAGES.keys():
324 image_name = IMAGES[img]['image_name']
325 image_url = IMAGES[img]['image_url']
327 image_id = os_utils.get_image_id(glance, image_name)
330 logger.info("""%s image doesn't exist on glance repository. Try
331 downloading this image and upload on glance !""" % image_name)
332 image_id = download_and_add_image_on_glance(
333 glance, image_name, image_url)
338 "Error : Failed to find or upload required OS "
339 "image for this deployment")
341 nova = nvclient.Client("2", **nv_creds)
343 logger.info("Update security group quota for this tenant")
344 neutron = ntclient.Client(**nt_creds)
345 if not os_utils.update_sg_quota(neutron, tenant_id, 50, 100):
348 "Failed to update security group quota for tenant " + TENANT_NAME)
350 logger.info("Update cinder quota for this tenant")
351 from cinderclient import client as cinderclient
353 creds_cinder = os_utils.get_credentials("cinder")
354 cinder_client = cinderclient.Client('1', creds_cinder['username'],
355 creds_cinder['api_key'],
356 creds_cinder['project_id'],
357 creds_cinder['auth_url'],
358 service_type="volume")
359 if not os_utils.update_cinder_quota(cinder_client, tenant_id, 20, 10, 150):
361 "init", "Failed to update cinder quota for tenant " + TENANT_NAME)
363 # ############### CLOUDIFY INITIALISATION ################
365 cfy = orchestrator(VIMS_DATA_DIR, CFY_INPUTS, logger)
367 cfy.set_credentials(username=ks_creds['username'], password=ks_creds[
368 'password'], tenant_name=ks_creds['tenant_name'],
369 auth_url=ks_creds['auth_url'])
371 logger.info("Collect flavor id for cloudify manager server")
372 nova = nvclient.Client("2", **nv_creds)
374 flavor_name = "m1.medium"
375 flavor_id = os_utils.get_flavor_id(nova, flavor_name)
376 for requirement in CFY_MANAGER_REQUIERMENTS:
377 if requirement == 'ram_min':
378 flavor_id = os_utils.get_flavor_id_by_ram_range(
379 nova, CFY_MANAGER_REQUIERMENTS['ram_min'], 8196)
383 "Failed to find %s flavor. "
384 "Try with ram range default requirement !" % flavor_name)
385 flavor_id = os_utils.get_flavor_id_by_ram_range(nova, 4000, 8196)
388 step_failure("orchestrator",
389 "Failed to find required flavor for this deployment")
391 cfy.set_flavor_id(flavor_id)
393 image_name = "centos_7"
394 image_id = os_utils.get_image_id(glance, image_name)
395 for requirement in CFY_MANAGER_REQUIERMENTS:
396 if requirement == 'os_image':
397 image_id = os_utils.get_image_id(
398 glance, CFY_MANAGER_REQUIERMENTS['os_image'])
403 "Error : Failed to find required OS image for cloudify manager")
405 cfy.set_image_id(image_id)
407 ext_net = os_utils.get_external_net(neutron)
409 step_failure("orchestrator", "Failed to get external network")
411 cfy.set_external_network_name(ext_net)
413 ns = functest_utils.get_resolvconf_ns()
415 cfy.set_nameservers(ns)
417 logger.info("Prepare virtualenv for cloudify-cli")
418 cmd = "chmod +x " + VIMS_DIR + "create_venv.sh"
419 functest_utils.execute_command(cmd, logger)
421 cmd = VIMS_DIR + "create_venv.sh " + VIMS_DATA_DIR
422 functest_utils.execute_command(cmd, logger)
424 cfy.download_manager_blueprint(
425 CFY_MANAGER_BLUEPRINT['url'], CFY_MANAGER_BLUEPRINT['branch'])
427 # ############### CLOUDIFY DEPLOYMENT ################
428 start_time_ts = time.time()
429 end_time_ts = start_time_ts
430 logger.info("Cloudify deployment Start Time:'%s'" % (
431 datetime.datetime.fromtimestamp(start_time_ts).strftime(
432 '%Y-%m-%d %H:%M:%S')))
434 error = cfy.deploy_manager()
436 step_failure("orchestrator", error)
438 end_time_ts = time.time()
439 duration = round(end_time_ts - start_time_ts, 1)
440 logger.info("Cloudify deployment duration:'%s'" % duration)
441 set_result("orchestrator", duration, "")
443 # ############### CLEARWATER INITIALISATION ################
445 cw = clearwater(CW_INPUTS, cfy, logger)
447 logger.info("Collect flavor id for all clearwater vm")
448 nova = nvclient.Client("2", **nv_creds)
450 flavor_name = "m1.small"
451 flavor_id = os_utils.get_flavor_id(nova, flavor_name)
452 for requirement in CW_REQUIERMENTS:
453 if requirement == 'ram_min':
454 flavor_id = os_utils.get_flavor_id_by_ram_range(
455 nova, CW_REQUIERMENTS['ram_min'], 8196)
459 "Failed to find %s flavor. Try with ram range "
460 "default requirement !" % flavor_name)
461 flavor_id = os_utils.get_flavor_id_by_ram_range(nova, 4000, 8196)
465 "vIMS", "Failed to find required flavor for this deployment")
467 cw.set_flavor_id(flavor_id)
469 image_name = "ubuntu_14.04"
470 image_id = os_utils.get_image_id(glance, image_name)
471 for requirement in CW_REQUIERMENTS:
472 if requirement == 'os_image':
473 image_id = os_utils.get_image_id(
474 glance, CW_REQUIERMENTS['os_image'])
479 "Error : Failed to find required OS image for cloudify manager")
481 cw.set_image_id(image_id)
483 ext_net = os_utils.get_external_net(neutron)
485 step_failure("vIMS", "Failed to get external network")
487 cw.set_external_network_name(ext_net)
489 # ############### CLEARWATER DEPLOYMENT ################
491 start_time_ts = time.time()
492 end_time_ts = start_time_ts
493 logger.info("vIMS VNF deployment Start Time:'%s'" % (
494 datetime.datetime.fromtimestamp(start_time_ts).strftime(
495 '%Y-%m-%d %H:%M:%S')))
497 error = cw.deploy_vnf(CW_BLUEPRINT)
499 step_failure("vIMS", error)
501 end_time_ts = time.time()
502 duration = round(end_time_ts - start_time_ts, 1)
503 logger.info("vIMS VNF deployment duration:'%s'" % duration)
504 set_result("vIMS", duration, "")
506 # ############### CLEARWATER TEST ################
510 # ########## CLEARWATER UNDEPLOYMENT ############
514 # ########### CLOUDIFY UNDEPLOYMENT #############
516 cfy.undeploy_manager()
518 # ############## GENERAL CLEANUP ################
522 ks_creds = os_utils.get_credentials("keystone")
524 keystone = ksclient.Client(**ks_creds)
526 logger.info("Removing %s tenant .." % CFY_INPUTS['keystone_tenant_name'])
527 tenant_id = os_utils.get_tenant_id(
528 keystone, CFY_INPUTS['keystone_tenant_name'])
530 logger.error("Error : Failed to get id of %s tenant" %
531 CFY_INPUTS['keystone_tenant_name'])
533 if not os_utils.delete_tenant(keystone, tenant_id):
534 logger.error("Error : Failed to remove %s tenant" %
535 CFY_INPUTS['keystone_tenant_name'])
537 logger.info("Removing %s user .." % CFY_INPUTS['keystone_username'])
538 user_id = os_utils.get_user_id(
539 keystone, CFY_INPUTS['keystone_username'])
541 logger.error("Error : Failed to get id of %s user" %
542 CFY_INPUTS['keystone_username'])
544 if not os_utils.delete_user(keystone, user_id):
545 logger.error("Error : Failed to remove %s user" %
546 CFY_INPUTS['keystone_username'])
549 if __name__ == '__main__':