3 # Copyright (c) 2015 Ericsson
4 # jose.lausuch@ericsson.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
11 import re, json, os, urllib2, argparse, logging, shutil, subprocess, yaml, sys, getpass
15 from pwd import getpwuid
16 from neutronclient.v2_0 import client as neutronclient
18 actions = ['start', 'check', 'clean']
19 parser = argparse.ArgumentParser()
20 parser.add_argument("action", help="Possible actions are: '{d[0]}|{d[1]}|{d[2]}' ".format(d=actions))
21 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
22 parser.add_argument("-f", "--force", help="Force", action="store_true")
23 args = parser.parse_args()
26 """ logging configuration """
27 logger = logging.getLogger('config_functest')
28 logger.setLevel(logging.DEBUG)
30 ch = logging.StreamHandler()
32 ch.setLevel(logging.DEBUG)
34 ch.setLevel(logging.INFO)
36 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
37 ch.setFormatter(formatter)
40 REPO_PATH=os.environ['repos_dir']+'/functest/'
41 if not os.path.exists(REPO_PATH):
42 logger.error("Functest repository directory not found '%s'" % REPO_PATH)
44 sys.path.append(REPO_PATH + "testcases/")
46 with open("/home/opnfv/functest/conf/config_functest.yaml") as f:
47 functest_yaml = yaml.safe_load(f)
51 """ global variables """
53 RALLY_DIR = REPO_PATH + functest_yaml.get("general").get("directories").get("dir_rally")
54 RALLY_REPO_DIR = functest_yaml.get("general").get("directories").get("dir_repo_rally")
55 RALLY_INSTALLATION_DIR = functest_yaml.get("general").get("directories").get("dir_rally_inst")
56 RALLY_RESULT_DIR = functest_yaml.get("general").get("directories").get("dir_rally_res")
57 VPING_DIR = REPO_PATH + functest_yaml.get("general").get("directories").get("dir_vping")
58 VIMS_TEST_DIR = functest_yaml.get("general").get("directories").get("dir_repo_vims_test")
59 ODL_DIR = REPO_PATH + functest_yaml.get("general").get("directories").get("dir_odl")
60 DATA_DIR = functest_yaml.get("general").get("directories").get("dir_functest_data")
62 # Tempest/Rally configuration details
63 DEPLOYMENT_MAME = functest_yaml.get("rally").get("deployment_name")
64 RALLY_COMMIT = functest_yaml.get("general").get("repositories").get("rally_commit")
67 IMAGE_FILE_NAME = functest_yaml.get("general").get("openstack").get("image_file_name")
68 IMAGE_PATH = DATA_DIR + "/" + IMAGE_FILE_NAME
70 # NEUTRON Private Network parameters
71 NEUTRON_PRIVATE_NET_NAME = functest_yaml.get("general"). \
72 get("openstack").get("neutron_private_net_name")
73 NEUTRON_PRIVATE_SUBNET_NAME = functest_yaml.get("general"). \
74 get("openstack").get("neutron_private_subnet_name")
75 NEUTRON_PRIVATE_SUBNET_CIDR = functest_yaml.get("general"). \
76 get("openstack").get("neutron_private_subnet_cidr")
77 NEUTRON_ROUTER_NAME = functest_yaml.get("general"). \
78 get("openstack").get("neutron_router_name")
80 creds_neutron = functest_utils.get_credentials("neutron")
81 neutron_client = neutronclient.Client(**creds_neutron)
85 Start the functest environment installation
87 if not functest_utils.check_internet_connectivity():
88 logger.error("There is no Internet connectivity. Please check the network configuration.")
92 logger.info("Functest environment already installed. Nothing to do.")
96 # Clean in case there are left overs
97 logger.debug("Cleaning possible functest environment leftovers.")
99 logger.info("Starting installation of functest environment")
101 private_net = functest_utils.get_private_net(neutron_client)
102 if private_net is None:
103 # If there is no private network in the deployment we create one
104 if not create_private_neutron_net(neutron_client):
105 logger.error("There has been a problem while creating the functest network.")
109 logger.info("Private network '%s' already existing in the deployment."
110 % private_net['name'])
112 logger.info("Installing Rally...")
113 if not install_rally():
114 logger.error("There has been a problem while installing Rally.")
118 logger.info("Installing Ruby libraries for vIMS testcase...")
119 # Install ruby libraries for vims test-case
120 script = 'source /etc/profile.d/rvm.sh; '
121 script += 'cd ' + VIMS_TEST_DIR + '; '
122 script += 'rvm autolibs enable ;'
123 script += 'rvm install 1.9.3; '
124 script += 'rvm use 1.9.3;'
125 script += 'bundle install'
127 cmd = "/bin/bash -c '" + script + "'"
128 if os.environ.get("CI_DEBUG") == "false":
129 functest_utils.execute_command(cmd)
131 functest_utils.execute_command(cmd,logger)
133 # Create result folder under functest if necessary
134 if not os.path.exists(RALLY_RESULT_DIR):
135 os.makedirs(RALLY_RESULT_DIR)
138 logger.info("CI: Generate the list of executable tests.")
139 runnable_test = functest_utils.generateTestcaseList(functest_yaml)
140 logger.info("List of runnable tests generated: %s" % runnable_test)
142 logger.error("Impossible to generate the list of runnable tests")
149 Check if the functest environment is properly installed
153 logger.info("Checking current functest configuration...")
155 logger.debug("Checking script directories...")
157 dirs = [RALLY_DIR, RALLY_INSTALLATION_DIR, VPING_DIR, ODL_DIR]
159 if not os.path.exists(dir):
160 logger.debug("The directory '%s' does NOT exist." % dir)
164 logger.debug(" %s found" % dir)
166 logger.debug("...OK")
168 logger.debug("...FAIL")
171 logger.debug("Checking Rally deployment...")
172 if not check_rally():
173 logger.debug(" Rally deployment NOT installed.")
175 logger.debug("...FAIL")
177 logger.debug("...OK")
179 logger.debug("Checking Image...")
181 if not os.path.isfile(IMAGE_PATH):
182 logger.debug(" Image file '%s' NOT found." % IMAGE_PATH)
186 logger.debug(" Image file found in %s" % IMAGE_PATH)
190 logger.debug("...OK")
192 logger.debug("...FAIL")
194 #TODO: check OLD environment setup
195 return not errors_all
201 Clean the existing functest environment
203 logger.info("Removing current functest environment...")
204 if os.path.exists(RALLY_INSTALLATION_DIR):
205 logger.debug("Removing Rally installation directory %s" % RALLY_INSTALLATION_DIR)
206 shutil.rmtree(RALLY_INSTALLATION_DIR,ignore_errors=True)
208 if os.path.exists(RALLY_RESULT_DIR):
209 logger.debug("Removing Result directory")
210 shutil.rmtree(RALLY_RESULT_DIR,ignore_errors=True)
212 logger.debug("Cleaning up the OpenStack deployment...")
213 cmd='python ' + REPO_PATH + \
214 '/testcases/VIM/OpenStack/CI/libraries/clean_openstack.py'
215 functest_utils.execute_command(cmd,logger)
216 logger.info("Functest environment clean!")
222 logger.info("Rally is already installed.")
224 logger.debug("Executing %s/install_rally.sh..." %RALLY_REPO_DIR)
225 install_script = RALLY_REPO_DIR + "/install_rally.sh --yes"
226 cmd = 'sudo ' + install_script
227 if os.environ.get("CI_DEBUG") == "false":
228 functest_utils.execute_command(cmd)
230 functest_utils.execute_command(cmd,logger)
232 logger.debug("Creating Rally environment...")
233 cmd = "rally deployment create --fromenv --name="+DEPLOYMENT_MAME
234 functest_utils.execute_command(cmd,logger)
236 logger.debug("Installing tempest...")
237 cmd = "rally verify install"
238 functest_utils.execute_command(cmd,logger)
240 cmd = "rally deployment check"
241 functest_utils.execute_command(cmd,logger)
242 #TODO: check that everything is 'Available' and warn if not
244 cmd = "rally show images"
245 functest_utils.execute_command(cmd,logger)
247 cmd = "rally show flavors"
248 functest_utils.execute_command(cmd,logger)
255 Check if Rally is installed and properly configured
257 if os.path.exists(RALLY_INSTALLATION_DIR):
258 logger.debug(" Rally installation directory found in %s" % RALLY_INSTALLATION_DIR)
259 FNULL = open(os.devnull, 'w');
260 cmd="rally deployment list | grep "+DEPLOYMENT_MAME
261 logger.debug(' Executing command : {}'.format(cmd))
262 p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=FNULL);
263 #if the command does not exist or there is no deployment
264 line = p.stdout.readline()
266 logger.debug(" Rally deployment NOT found")
268 logger.debug(" Rally deployment found")
274 def create_private_neutron_net(neutron):
275 neutron.format = 'json'
276 logger.info('Creating neutron network %s...' % NEUTRON_PRIVATE_NET_NAME)
277 network_id = functest_utils. \
278 create_neutron_net(neutron, NEUTRON_PRIVATE_NET_NAME)
282 logger.debug("Network '%s' created successfully" % network_id)
284 logger.info('Updating neutron network %s...' % NEUTRON_PRIVATE_NET_NAME)
285 if functest_utils.update_neutron_net(neutron, network_id, shared=True):
286 logger.debug("Network '%s' updated successfully" % network_id)
288 logger.info('Updating neutron network %s failed' % network_id)
290 logger.debug('Creating Subnet....')
291 subnet_id = functest_utils. \
292 create_neutron_subnet(neutron,
293 NEUTRON_PRIVATE_SUBNET_NAME,
294 NEUTRON_PRIVATE_SUBNET_CIDR,
298 logger.debug("Subnet '%s' created successfully" % subnet_id)
299 logger.debug('Creating Router...')
300 router_id = functest_utils. \
301 create_neutron_router(neutron, NEUTRON_ROUTER_NAME)
306 logger.debug("Router '%s' created successfully" % router_id)
307 logger.debug('Adding router to subnet...')
309 result = functest_utils.add_interface_router(neutron, router_id, subnet_id)
314 logger.debug("Interface added successfully.")
315 network_dic = {'net_id': network_id,
316 'subnet_id': subnet_id,
317 'router_id': router_id}
322 if not (args.action in actions):
323 logger.error('argument not valid')
327 if not functest_utils.check_credentials():
328 logger.error("Please source the openrc credentials and run the script again.")
329 #TODO: source the credentials in this script
333 if args.action == "start":
336 if args.action == "check":
338 logger.info("Functest environment correctly installed")
340 logger.info("Functest environment not found or faulty")
342 if args.action == "clean":
347 print("Are you sure? [y|n]")
348 answer = raw_input("")
355 print("Invalid option.")
359 if __name__ == '__main__':