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
18 import openstack_utils
19 from neutronclient.v2_0 import client as neutronclient
21 actions = ['start', 'check', 'clean']
22 parser = argparse.ArgumentParser()
23 parser.add_argument("action", help="Possible actions are: \
24 '{d[0]}|{d[1]}|{d[2]}' ".format(d=actions))
25 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
26 parser.add_argument("-f", "--force", help="Force", action="store_true")
27 args = parser.parse_args()
30 """ logging configuration """
31 logger = logging.getLogger('config_functest')
32 logger.setLevel(logging.DEBUG)
34 ch = logging.StreamHandler()
36 ch.setLevel(logging.DEBUG)
38 ch.setLevel(logging.INFO)
40 formatter = logging.Formatter('%(asctime)s - %(name)s - '
41 '%(levelname)s - %(message)s')
42 ch.setFormatter(formatter)
45 REPOS_DIR = os.environ['repos_dir']
46 FUNCTEST_REPO = REPOS_DIR + '/functest/'
47 if not os.path.exists(FUNCTEST_REPO):
48 logger.error("Functest repository directory not found '%s'"
51 sys.path.append(FUNCTEST_REPO + "testcases/")
53 with open("/home/opnfv/functest/conf/config_functest.yaml") as f:
54 functest_yaml = yaml.safe_load(f)
58 """ global variables """
60 RALLY_DIR = FUNCTEST_REPO + functest_yaml.get("general").\
61 get("directories").get("dir_rally")
62 RALLY_REPO_DIR = functest_yaml.get("general").\
63 get("directories").get("dir_repo_rally")
64 RALLY_INSTALLATION_DIR = functest_yaml.get("general").\
65 get("directories").get("dir_rally_inst")
66 RALLY_RESULT_DIR = functest_yaml.get("general").\
67 get("directories").get("dir_rally_res")
68 TEMPEST_REPO_DIR = functest_yaml.get("general").\
69 get("directories").get("dir_repo_tempest")
70 VPING_DIR = FUNCTEST_REPO + functest_yaml.\
71 get("general").get("directories").get("dir_vping")
72 ODL_DIR = FUNCTEST_REPO + functest_yaml.\
73 get("general").get("directories").get("dir_odl")
74 DATA_DIR = functest_yaml.get("general").\
75 get("directories").get("dir_functest_data")
77 # Tempest/Rally configuration details
78 DEPLOYMENT_MAME = functest_yaml.get("rally").get("deployment_name")
81 IMAGE_FILE_NAME = functest_yaml.get("general").\
82 get("openstack").get("image_file_name")
83 IMAGE_PATH = DATA_DIR + "/" + IMAGE_FILE_NAME
85 # NEUTRON Private Network parameters
86 NEUTRON_PRIVATE_NET_NAME = functest_yaml.get("general"). \
87 get("openstack").get("neutron_private_net_name")
88 NEUTRON_PRIVATE_SUBNET_NAME = functest_yaml.get("general"). \
89 get("openstack").get("neutron_private_subnet_name")
90 NEUTRON_PRIVATE_SUBNET_CIDR = functest_yaml.get("general"). \
91 get("openstack").get("neutron_private_subnet_cidr")
92 NEUTRON_ROUTER_NAME = functest_yaml.get("general"). \
93 get("openstack").get("neutron_router_name")
95 creds_neutron = openstack_utils.get_credentials("neutron")
96 neutron_client = neutronclient.Client(**creds_neutron)
101 Start the functest environment installation
103 if not functest_utils.check_internet_connectivity():
104 logger.info("No Internet connectivity. "
105 "This may affect some test case suites.")
108 logger.info("Functest environment already installed. Nothing to do.")
112 # Clean in case there are left overs
113 logger.debug("Cleaning possible functest environment leftovers.")
115 logger.info("Starting installation of functest environment")
117 private_net = openstack_utils.get_private_net(neutron_client)
118 if private_net is None:
119 # If there is no private network in the deployment we create one
120 if not create_private_neutron_net(neutron_client):
121 logger.error("There has been a problem while "
122 "creating the functest network.")
126 logger.info("Private network '%s' already existing in "
127 "the deployment." % private_net['name'])
129 logger.info("Installing Rally...")
130 if not install_rally():
131 logger.error("There has been a problem while installing Rally.")
135 # Create result folder under functest if necessary
136 if not os.path.exists(RALLY_RESULT_DIR):
137 os.makedirs(RALLY_RESULT_DIR)
140 logger.info("CI: Generate the list of executable tests.")
141 runnable_test = functest_utils.generateTestcaseList(functest_yaml)
142 logger.info("List of runnable tests generated: %s" % runnable_test)
144 logger.error("Impossible to generate the list of runnable tests")
151 Check if the functest environment is properly installed
154 logger.info("Checking current functest configuration...")
156 logger.debug("Checking script directories...")
158 dirs = [RALLY_DIR, RALLY_INSTALLATION_DIR, VPING_DIR, ODL_DIR]
160 if not os.path.exists(dir):
161 logger.debug(" %s NOT found" % dir)
164 logger.debug(" %s found" % dir)
166 logger.debug("Checking Rally deployment...")
167 if not check_rally():
168 logger.debug(" Rally deployment NOT installed.")
171 logger.debug("Checking Image...")
172 if not os.path.isfile(IMAGE_PATH):
173 logger.debug(" Image file '%s' NOT found." % IMAGE_PATH)
176 logger.debug(" Image file found in %s" % IMAGE_PATH)
178 # TODO: check OLD environment setup
179 return not errors_all
184 Clean the existing functest environment
186 logger.info("Removing current functest environment...")
187 if os.path.exists(RALLY_INSTALLATION_DIR):
188 logger.debug("Removing Rally installation directory %s"
189 % RALLY_INSTALLATION_DIR)
190 shutil.rmtree(RALLY_INSTALLATION_DIR, ignore_errors=True)
192 if os.path.exists(RALLY_RESULT_DIR):
193 logger.debug("Removing Result directory")
194 shutil.rmtree(RALLY_RESULT_DIR, ignore_errors=True)
196 logger.info("Functest environment clean!")
201 logger.info("Rally is already installed.")
203 logger.debug("Creating Rally environment...")
204 cmd = "rally deployment create --fromenv --name=" + DEPLOYMENT_MAME
205 functest_utils.execute_command(cmd, logger)
207 logger.debug("Installing tempest from existing repo...")
208 cmd = "rally verify install --source " + TEMPEST_REPO_DIR + \
210 functest_utils.execute_command(cmd, logger)
212 cmd = "rally deployment check"
213 functest_utils.execute_command(cmd, logger)
214 # TODO: check that everything is 'Available' and warn if not
216 cmd = "rally show images"
217 functest_utils.execute_command(cmd, logger)
219 cmd = "rally show flavors"
220 functest_utils.execute_command(cmd, logger)
227 Check if Rally is installed and properly configured
229 if os.path.exists(RALLY_INSTALLATION_DIR):
230 logger.debug(" Rally installation directory found in %s"
231 % RALLY_INSTALLATION_DIR)
232 FNULL = open(os.devnull, 'w')
233 cmd = "rally deployment list | grep " + DEPLOYMENT_MAME
234 logger.debug(' Executing command : {}'.format(cmd))
235 p = subprocess.Popen(cmd, shell=True,
236 stdout=subprocess.PIPE, stderr=FNULL)
237 # if the command does not exist or there is no deployment
238 line = p.stdout.readline()
240 logger.debug(" Rally deployment NOT found")
242 logger.debug(" Rally deployment found")
248 def create_private_neutron_net(neutron):
249 neutron.format = 'json'
250 logger.info("Creating network '%s'..." % NEUTRON_PRIVATE_NET_NAME)
251 network_id = openstack_utils. \
252 create_neutron_net(neutron, NEUTRON_PRIVATE_NET_NAME)
256 logger.debug("Network '%s' created successfully." % network_id)
258 logger.info("Updating network '%s' with shared=True..."
259 % NEUTRON_PRIVATE_NET_NAME)
260 if openstack_utils.update_neutron_net(neutron, network_id, shared=True):
261 logger.debug("Network '%s' updated successfully." % network_id)
263 logger.info("Updating neutron network '%s' failed" % network_id)
265 logger.info("Creating Subnet....")
266 subnet_id = openstack_utils. \
267 create_neutron_subnet(neutron,
268 NEUTRON_PRIVATE_SUBNET_NAME,
269 NEUTRON_PRIVATE_SUBNET_CIDR,
273 logger.debug("Subnet '%s' created successfully." % subnet_id)
274 logger.info("Creating Router...")
275 router_id = openstack_utils. \
276 create_neutron_router(neutron, NEUTRON_ROUTER_NAME)
281 logger.debug("Router '%s' created successfully." % router_id)
282 logger.info("Adding router to subnet...")
284 result = openstack_utils.add_interface_router(neutron,
291 logger.debug("Interface added successfully.")
296 if not (args.action in actions):
297 logger.error('argument not valid')
300 if not openstack_utils.check_credentials():
301 logger.error("Please source the openrc credentials and "
302 "run the script again.")
303 # TODO: source the credentials in this script
306 if args.action == "start":
309 if args.action == "check":
311 logger.info("Functest environment correctly installed")
313 logger.info("Functest environment not found or faulty")
315 if args.action == "clean":
320 print("Are you sure? [y|n]")
321 answer = raw_input("")
328 print("Invalid option.")
332 if __name__ == '__main__':