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 ########################################################################
14 import os, time, subprocess, logging, argparse, yaml, pprint, sys, shutil, json, datetime
16 import keystoneclient.v2_0.client as ksclient
17 import glanceclient.client as glclient
18 import novaclient.client as nvclient
19 from neutronclient.v2_0 import client as ntclient
22 pp = pprint.PrettyPrinter(indent=4)
25 parser = argparse.ArgumentParser()
26 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
27 parser.add_argument("-r", "--report",
28 help="Create json result file",
30 args = parser.parse_args()
33 """ logging configuration """
34 logger = logging.getLogger('vIMS')
35 logger.setLevel(logging.DEBUG)
37 ch = logging.StreamHandler()
39 ch.setLevel(logging.DEBUG)
41 ch.setLevel(logging.INFO)
42 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
43 ch.setFormatter(formatter)
46 REPO_PATH=os.environ['repos_dir']+'/functest/'
47 if not os.path.exists(REPO_PATH):
48 logger.error("Functest repository directory not found '%s'" % REPO_PATH)
50 sys.path.append(REPO_PATH + "testcases/")
53 with open(REPO_PATH + "testcases/config_functest.yaml") as f:
54 functest_yaml = yaml.safe_load(f)
58 VIMS_DIR = REPO_PATH + functest_yaml.get("general").get("directories").get("dir_vIMS")
59 VIMS_DATA_DIR = functest_yaml.get("general").get("directories").get("dir_vIMS_data")+"/"
60 VIMS_TEST_DIR = functest_yaml.get("general").get("directories").get("dir_repo_vims_test")+"/"
61 TEST_DB = functest_yaml.get("results").get("test_db_url")
63 TENANT_NAME = functest_yaml.get("vIMS").get("general").get("tenant_name")
64 TENANT_DESCRIPTION = functest_yaml.get("vIMS").get("general").get("tenant_description")
65 BASE_IMAGE_URL = functest_yaml.get("vIMS").get("general").get("base_image_url")
66 BASE_IMAGE_NAME = functest_yaml.get("vIMS").get("general").get("base_image_name")
67 GLANCE_IMAGE_NAME = functest_yaml.get("vIMS").get("cloudify").get("inputs").get("image_id")
69 CFY_MANAGER_BLUEPRINT = functest_yaml.get("vIMS").get("cloudify").get("blueprint")
70 CFY_INPUTS = functest_yaml.get("vIMS").get("cloudify").get("inputs")
71 CFY_INPUTS_PATH = functest_yaml.get("vIMS").get("cloudify").get("inputs_path")
73 CW_BLUEPRINT = functest_yaml.get("vIMS").get("clearwater").get("blueprint")
74 CW_DEPLOYMENT_NAME = functest_yaml.get("vIMS").get("clearwater").get("deployment-name")
75 CW_INPUTS = functest_yaml.get("vIMS").get("clearwater").get("inputs")
76 CW_DOMAIN_NAME = functest_yaml.get("vIMS").get("clearwater").get("inputs").get("public_domain")
78 CFY_DEPLOYMENT_DURATION = 0
79 CW_DEPLOYMENT_DURATION = 0
86 def download_and_add_image_on_glance(glance, image_name, image_url):
87 dest_path = VIMS_DATA_DIR + "tmp/"
88 if not os.path.exists(dest_path):
89 os.makedirs(dest_path)
90 file_name = image_url.rsplit('/')[-1]
91 if not functest_utils.download_url(image_url, dest_path):
92 logger.error("Failed to download image %s" %file_name)
95 image = functest_utils.create_glance_image(glance, image_name, dest_path + file_name)
97 logger.error("Failed to upload image on glance")
102 def download_blueprints(blueprint_url, branch, dest_path):
103 if os.path.exists(dest_path):
104 shutil.rmtree(dest_path)
106 Repo.clone_from(blueprint_url, dest_path, branch=branch)
111 def initialize_deployments():
112 if not os.path.exists(VIMS_DATA_DIR):
113 os.makedirs(VIMS_DATA_DIR)
115 ks_creds = functest_utils.get_credentials("keystone")
116 nv_creds = functest_utils.get_credentials("nova")
117 nt_creds = functest_utils.get_credentials("neutron")
119 logger.info("Prepare OpenStack plateform (create tenant and user)")
120 keystone = ksclient.Client(**ks_creds)
122 user_id = functest_utils.get_user_id(keystone, ks_creds['username'])
124 logger.error("Error : Failed to get id of %s user" %ks_creds['username'])
127 tenant_id = functest_utils.create_tenant(keystone, TENANT_NAME, TENANT_DESCRIPTION)
129 logger.error("Error : Failed to create %s tenant" %TENANT_NAME)
133 role_id = functest_utils.get_role_id(keystone, role_name)
135 logger.error("Error : Failed to get id for %s role" %role_name)
137 if not functest_utils.add_role_user(keystone, user_id, role_id, tenant_id):
138 logger.error("Error : Failed to add %s on tenant" %ks_creds['username'])
140 user_id = functest_utils.create_user(keystone, TENANT_NAME, TENANT_NAME, None, tenant_id)
142 logger.error("Error : Failed to create %s user" %TENANT_NAME)
144 logger.info("Update OpenStack creds informations")
146 "username": TENANT_NAME,
147 "password": TENANT_NAME,
148 "tenant_name": TENANT_NAME,
152 "tenant_name": TENANT_NAME,
156 "project_id": TENANT_NAME,
159 logger.info("Upload ubuntu image if it doesn't exist")
160 glance_endpoint = keystone.service_catalog.url_for(service_type='image',
161 endpoint_type='publicURL')
162 glance = glclient.Client(1, glance_endpoint, token=keystone.auth_token)
164 image_id = functest_utils.get_image_id(glance, BASE_IMAGE_NAME)
166 logger.info("""%s image doesn't exist on glance repository.
167 Try downloading this image and upload on glance !""" %BASE_IMAGE_NAME)
168 image_id = download_and_add_image_on_glance(glance, BASE_IMAGE_NAME, BASE_IMAGE_URL)
171 logger.error("Error : Failed to find or upload required OS image for this deployment" %flavor_name)
174 logger.info("Collect flavor id for cloudify and clearwater VMs")
175 nova = nvclient.Client("2", **nv_creds)
177 flavor_name = "m1.small"
178 flavor_id = functest_utils.get_flavor_id(nova, flavor_name)
180 logger.error("Failed to find %s flavor. Try with ram range requirement !" %flavor_name)
181 flavor_id = get_flavor_id_by_ram_range(nova, 1792, 2048)
184 logger.error("Failed to find required flavor for this deployment" %flavor_name)
187 logger.info("Update security group quota for this tenant")
188 neutron = ntclient.Client(**nt_creds)
189 if not functest_utils.update_sg_quota(neutron, tenant_id, 50, 100):
190 logger.error("Failed to update security group quota for tenant %s" %TENANT_NAME)
193 ext_net = functest_utils.get_external_net(neutron)
195 logger.error("Failed to get external network")
198 logger.info("Update inputs informations")
199 CFY_INPUTS['image_id'] = image_id
200 CFY_INPUTS['flavor_id'] = flavor_id
201 CFY_INPUTS['external_network_name'] = ext_net
203 CW_INPUTS['image_id'] = image_id
204 CW_INPUTS['flavor_id'] = flavor_id
205 CW_INPUTS['external_network_name'] = ext_net
207 CFY_INPUTS['keystone_username'] = ks_creds['username']
208 CFY_INPUTS['keystone_password'] = ks_creds['password']
209 CFY_INPUTS['keystone_url'] = ks_creds['auth_url']
210 CFY_INPUTS['keystone_tenant_name'] = ks_creds['tenant_name']
212 logger.info("Prepare virtualenv for cloudify-cli")
213 cmd = "chmod +x " + VIMS_DIR + "create_venv.sh"
214 functest_utils.execute_command(cmd,logger)
215 cmd = VIMS_DIR + "create_venv.sh " + VIMS_DATA_DIR
216 functest_utils.execute_command(cmd,logger)
218 def cleanup_deployments():
219 ks_creds = functest_utils.get_credentials("keystone")
221 keystone = ksclient.Client(**ks_creds)
223 logger.info("Removing %s tenant .." %CFY_INPUTS['keystone_tenant_name'])
224 tenant_id = functest_utils.get_tenant_id(keystone, CFY_INPUTS['keystone_tenant_name'])
226 logger.error("Error : Failed to get id of %s tenant" %CFY_INPUTS['keystone_tenant_name'])
228 if not functest_utils.delete_tenant(keystone, tenant_id):
229 logger.error("Error : Failed to remove %s tenant" %CFY_INPUTS['keystone_tenant_name'])
231 logger.info("Removing %s user .." %CFY_INPUTS['keystone_username'])
232 user_id = functest_utils.get_user_id(keystone, CFY_INPUTS['keystone_username'])
234 logger.error("Error : Failed to get id of %s user" %CFY_INPUTS['keystone_username'])
236 if not functest_utils.delete_user(keystone, user_id):
237 logger.error("Error : Failed to remove %s user" %CFY_INPUTS['keystone_username'])
239 def deploy_cloudify_manager():
241 logger.info("Downloading the cloudify manager server blueprint")
242 download_result = download_blueprints(CFY_MANAGER_BLUEPRINT['url'],
243 CFY_MANAGER_BLUEPRINT['branch'],
244 VIMS_DATA_DIR + 'cloudify-manager-blueprint/')
246 if not download_result:
247 logger.error("Failed to download manager blueprint")
250 logger.info("Writing the inputs file")
251 with open( VIMS_DATA_DIR + 'cloudify-manager-blueprint/' + CFY_INPUTS_PATH, "w") as f:
252 f.write(yaml.dump(CFY_INPUTS, default_style='"') )
255 start_time_ts = time.time()
256 end_time_ts = start_time_ts
257 logger.info("Cloudify deployment Start Time:'%s'" % (
258 datetime.datetime.fromtimestamp(start_time_ts).strftime(
259 '%Y-%m-%d %H:%M:%S')))
261 logger.info("Launching the cloudify-manager deployment")
262 script = "source " + VIMS_DATA_DIR + "venv_cloudify/bin/activate; "
263 script += "cd " + VIMS_DATA_DIR + "; "
264 script += "cfy init -r; "
265 script += "cd cloudify-manager-blueprint/openstack; "
266 script += "cfy local create-requirements -o requirements.txt -p openstack-manager-blueprint.yaml; "
267 script += "pip install -r requirements.txt; "
268 script += "cfy bootstrap --install-plugins -p openstack-manager-blueprint.yaml -i inputs.yaml; "
269 cmd = "/bin/bash -c '" + script + "'"
270 functest_utils.execute_command(cmd, logger)
272 logger.info("Cloudify-manager server is UP !")
274 global CFY_DEPLOYMENT_DURATION
275 end_time_ts = time.time()
276 CFY_DEPLOYMENT_DURATION = round(end_time_ts - start_time_ts, 1)
277 logger.info("Cloudify deployment duration:'%s'" %CFY_DEPLOYMENT_DURATION)
279 def undeploy_cloudify_manager():
281 logger.info("Launching the cloudify-manager undeployment")
282 script = "source " + VIMS_DATA_DIR + "venv_cloudify/bin/activate; "
283 script += "cd " + VIMS_DATA_DIR + "; "
284 script += "cfy teardown -f; "
285 cmd = "/bin/bash -c '" + script + "'"
286 functest_utils.execute_command(cmd, logger)
288 logger.info("Cloudify-manager server has been successfully removed!")
290 def deploy_clearwater():
292 logger.info("Downloading the {0} blueprint".format(CW_BLUEPRINT['file_name']))
293 download_result = download_blueprints(CW_BLUEPRINT['url'], CW_BLUEPRINT['branch'],
294 VIMS_DATA_DIR + CW_BLUEPRINT['destination_folder'])
296 if not download_result:
297 logger.error("Failed to download blueprint {0}".format(CW_BLUEPRINT['file_name']))
300 logger.info("Writing the inputs file")
301 with open(VIMS_DATA_DIR + CW_BLUEPRINT['destination_folder'] + "/inputs.yaml", "w") as f:
302 f.write(yaml.dump(CW_INPUTS, default_style='"') )
307 start_time_ts = time.time()
308 end_time_ts = start_time_ts
309 logger.info("vIMS VNF deployment Start Time:'%s'" % (
310 datetime.datetime.fromtimestamp(start_time_ts).strftime(
311 '%Y-%m-%d %H:%M:%S')))
313 logger.info("Launching the {0} deployment".format(CW_BLUEPRINT['name']))
314 script = "source " + VIMS_DATA_DIR + "venv_cloudify/bin/activate; "
315 script += "cd " + VIMS_DATA_DIR + CW_BLUEPRINT['destination_folder'] + "; "
316 script += "cfy blueprints upload -b " + CW_BLUEPRINT['name'] + " -p openstack-blueprint.yaml; "
317 script += "cfy deployments create -b " + CW_BLUEPRINT['name'] + " -d " + CW_DEPLOYMENT_NAME + " --inputs inputs.yaml; "
318 script += "cfy executions start -w install -d " + CW_DEPLOYMENT_NAME + " --timeout 1800; "
320 cmd = "/bin/bash -c '" + script + "'"
321 functest_utils.execute_command(cmd, logger)
323 logger.info("Clearwater vIMS is UP !")
325 global CW_DEPLOYMENT_DURATION
326 end_time_ts = time.time()
327 CW_DEPLOYMENT_DURATION = round(end_time_ts - start_time_ts, 1)
328 logger.info("vIMS VNF deployment duration:'%s'" %CW_DEPLOYMENT_DURATION)
330 def undeploy_clearwater():
332 logger.info("Launching the {0} undeployment".format(CW_BLUEPRINT['name']))
333 script = "source " + VIMS_DATA_DIR + "venv_cloudify/bin/activate; "
334 script += "cd " + VIMS_DATA_DIR + "; "
335 script += "cfy executions start -w uninstall -d " + CW_DEPLOYMENT_NAME + " --timeout 1800 ; "
336 script += "cfy deployments delete -d " + CW_DEPLOYMENT_NAME + "; "
338 cmd = "/bin/bash -c '" + script + "'"
339 functest_utils.execute_command(cmd, logger)
341 def test_clearwater():
345 script = "source " + VIMS_DATA_DIR + "venv_cloudify/bin/activate; "
346 script += "cd " + VIMS_DATA_DIR + "; "
347 script += "cfy deployments outputs -d " + CW_DEPLOYMENT_NAME + " | grep Value: | sed \"s/ *Value: //g\";"
348 cmd = "/bin/bash -c '" + script + "'"
351 logger.debug("Trying to get clearwater nameserver IP ... ")
352 dns_ip = os.popen(cmd).read()
353 dns_ip = dns_ip.splitlines()[0]
355 logger.error("Unable to retrieve the IP of the DNS server !")
357 start_time_ts = time.time()
358 end_time_ts = start_time_ts
359 logger.info("vIMS functional test Start Time:'%s'" % (
360 datetime.datetime.fromtimestamp(start_time_ts).strftime(
361 '%Y-%m-%d %H:%M:%S')))
364 script = 'echo -e "nameserver ' + dns_ip + '\nnameserver 8.8.8.8\nnameserver 8.8.4.4" > /etc/resolv.conf; '
365 script += 'source /etc/profile.d/rvm.sh; '
366 script += 'cd ' + VIMS_TEST_DIR + '; '
367 script += 'rake test[' + CW_INPUTS["public_domain"] + '] SIGNUP_CODE="secret"'
369 cmd = "/bin/bash -c '" + script + "'"
370 output_file = "output.txt"
371 f = open(output_file, 'w+')
372 p = subprocess.call(cmd, shell=True, stdout=f, stderr=subprocess.STDOUT)
374 end_time_ts = time.time()
375 duration = round(end_time_ts - start_time_ts, 1)
376 logger.info("vIMS functional test duration:'%s'" %duration)
377 f = open(output_file, 'r')
379 if result != "" and logger:
384 logger.debug("Trying to load test results")
385 with open(VIMS_TEST_DIR + "temp.json") as f:
386 vims_test_result = json.load(f)
389 logger.error("Unable to retrieve test results")
391 if vims_test_result != "" & args.report:
392 logger.debug("Push result into DB")
393 logger.debug("Pushing results to DB....")
394 git_version = functest_utils.get_git_branch(REPO_PATH)
395 functest_utils.push_results_to_db(db_url=TEST_DB, case_name="vIMS",
396 logger=logger, pod_name="opnfv-jump-2", git_version=git_version,
397 payload={'orchestrator':{'duration': CFY_DEPLOYMENT_DURATION,
399 'vIMS': {'duration': CW_DEPLOYMENT_DURATION,
401 'sig_test': {'duration': duration,
402 'result': vims_test_result}})
404 os.remove(VIMS_TEST_DIR + "temp.json")
406 logger.error("Deleting file failed")
409 initialize_deployments()
410 deploy_cloudify_manager()
415 undeploy_clearwater()
416 undeploy_cloudify_manager()
417 cleanup_deployments()
419 if __name__ == '__main__':