3 # Author: Jose Lausuch (jose.lausuch@ericsson.com)
5 # Installs the Functest framework within the Docker container
6 # and run the tests automatically
9 # All rights reserved. This program and the accompanying materials
10 # are made available under the terms of the Apache License, Version 2.0
11 # which accompanies this distribution, and is available at
12 # http://www.apache.org/licenses/LICENSE-2.0
25 import functest.utils.functest_logger as ft_logger
26 import functest.utils.functest_utils as ft_utils
27 import functest.utils.openstack_utils as os_utils
29 actions = ['start', 'check']
30 parser = argparse.ArgumentParser()
31 parser.add_argument("action", help="Possible actions are: "
32 "'{d[0]}|{d[1]}' ".format(d=actions))
33 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
34 args = parser.parse_args()
37 """ logging configuration """
38 logger = ft_logger.Logger("prepare_env").getLogger()
41 """ global variables """
42 INSTALLERS = ['fuel', 'compass', 'apex', 'joid']
43 CI_INSTALLER_TYPE = ""
47 CONFIG_FUNCTEST_PATH = os.environ["CONFIG_FUNCTEST_YAML"]
48 CONFIG_PATCH_PATH = os.path.join(os.path.dirname(
49 CONFIG_FUNCTEST_PATH), "config_patch.yaml")
51 functest_yaml = ft_utils.get_functest_yaml()
53 with open(CONFIG_PATCH_PATH) as f:
54 functest_patch_yaml = yaml.safe_load(f)
56 FUNCTEST_CONF_DIR = functest_yaml.get("general").get(
57 "directories").get("dir_functest_conf")
59 FUNCTEST_DATA_DIR = functest_yaml.get("general").get(
60 "directories").get("dir_functest_data")
61 FUNCTEST_RESULTS_DIR = functest_yaml.get("general").get(
62 "directories").get("dir_results")
63 DEPLOYMENT_MAME = functest_yaml.get("rally").get("deployment_name")
64 TEMPEST_REPO_DIR = functest_yaml.get("general").get(
65 "directories").get("dir_repo_tempest")
67 ENV_FILE = FUNCTEST_CONF_DIR + "/env_active"
70 def print_separator():
71 logger.info("==============================================")
74 def check_env_variables():
76 logger.info("Checking environment variables...")
77 global CI_INSTALLER_TYPE
78 global CI_INSTALLER_IP
81 CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
82 CI_INSTALLER_IP = os.getenv('INSTALLER_IP')
83 CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
84 CI_NODE = os.getenv('NODE_NAME')
85 CI_BUILD_TAG = os.getenv('BUILD_TAG')
86 CI_DEBUG = os.getenv('CI_DEBUG')
88 if CI_INSTALLER_TYPE is None:
89 logger.warning("The env variable 'INSTALLER_TYPE' is not defined.")
90 CI_INSTALLER_TYPE = "undefined"
92 if os.getenv('INSTALLER_TYPE') not in INSTALLERS:
93 logger.warning("INSTALLER_TYPE=%s is not a valid OPNFV installer. "
94 "Available OPNFV Installers are : %s."
95 "Setting INSTALLER_TYPE=undefined." % INSTALLERS)
96 CI_INSTALLER_TYPE = "undefined"
98 logger.info(" INSTALLER_TYPE=%s" % CI_INSTALLER_TYPE)
100 if CI_INSTALLER_IP is None:
101 logger.warning("The env variable 'INSTALLER_IP' is not defined. "
102 "It is needed to fetch the OpenStack credentials. "
103 "If the credentials are not provided to the "
104 "container as a volume, please add this env variable "
105 "to the 'docker run' command.")
107 logger.info(" INSTALLER_IP=%s" % CI_INSTALLER_IP)
109 if CI_SCENARIO is None:
110 logger.warning("The env variable 'DEPLOY_SCENARIO' is not defined. "
111 "Setting CE_SCENARIO=undefined.")
112 CI_SCENARIO = "undefined"
114 logger.info(" DEPLOY_SCENARIO=%s" % CI_SCENARIO)
116 logger.info(" CI_DEBUG=%s" % CI_DEBUG)
119 logger.info(" NODE_NAME=%s" % CI_NODE)
122 logger.info(" BUILD_TAG=%s" % CI_BUILD_TAG)
125 def create_directories():
127 logger.info("Creating needed directories...")
128 if not os.path.exists(FUNCTEST_CONF_DIR):
129 os.makedirs(FUNCTEST_CONF_DIR)
130 logger.info(" %s created." % FUNCTEST_CONF_DIR)
132 logger.debug(" %s already exists." % FUNCTEST_CONF_DIR)
134 if not os.path.exists(FUNCTEST_DATA_DIR):
135 os.makedirs(FUNCTEST_DATA_DIR)
136 logger.info(" %s created." % FUNCTEST_DATA_DIR)
138 logger.debug(" %s already exists." % FUNCTEST_DATA_DIR)
141 def source_rc_file():
143 logger.info("Fetching RC file...")
144 rc_file = os.getenv('creds')
146 logger.warning("The environment variable 'creds' must be set and"
147 "pointing to the local RC file. Using default: "
148 "/home/opnfv/functest/conf/openstack.creds ...")
149 rc_file = "/home/opnfv/functest/conf/openstack.creds"
151 if not os.path.isfile(rc_file):
152 logger.info("RC file not provided. "
153 "Fetching it from the installer...")
154 if CI_INSTALLER_IP is None:
155 logger.error("The env variable CI_INSTALLER_IP must be provided in"
156 " order to fetch the credentials from the installer.")
157 sys.exit("Missing CI_INSTALLER_IP.")
158 if CI_INSTALLER_TYPE not in INSTALLERS:
159 logger.error("Cannot fetch credentials. INSTALLER_TYPE=%s is "
160 "not a valid OPNFV installer. Available "
161 "installers are : %s." % INSTALLERS)
162 sys.exit("Wrong INSTALLER_TYPE.")
164 cmd = ("/home/opnfv/repos/releng/utils/fetch_os_creds.sh "
166 % (rc_file, CI_INSTALLER_TYPE, CI_INSTALLER_IP))
167 logger.debug("Executing command: %s" % cmd)
168 p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
169 output = p.communicate()[0]
170 logger.debug("\n%s" % output)
171 if p.returncode != 0:
172 logger.error("Failed to fetch credentials from installer.")
175 logger.info("RC file provided in %s." % rc_file)
176 if os.path.getsize(rc_file) == 0:
177 logger.error("The file %s is empty." % rc_file)
180 logger.info("Sourcing the OpenStack RC file...")
181 creds = os_utils.source_credentials(rc_file)
183 for key, value in creds.iteritems():
184 if re.search("OS_", key):
185 str += "\n\t\t\t\t\t\t " + key + "=" + value
186 logger.debug("Used credentials: %s" % str)
189 def patch_config_file():
191 for key in functest_patch_yaml:
192 if key in CI_SCENARIO:
193 new_functest_yaml = dict(ft_utils.merge_dicts(
194 functest_yaml, functest_patch_yaml[key]))
198 os.remove(CONFIG_FUNCTEST_PATH)
199 with open(CONFIG_FUNCTEST_PATH, "w") as f:
200 f.write(yaml.dump(new_functest_yaml, default_style='"'))
204 def verify_deployment():
206 logger.info("Verifying OpenStack services...")
207 cmd = ("%s/ci/check_os.sh" % ft_utils.FUNCTEST_REPO)
209 logger.debug("Executing command: %s" % cmd)
210 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
212 while p.poll() is None:
213 line = p.stdout.readline().rstrip()
216 sys.exit("Problem while running 'check_os.sh'.")
222 logger.info("Creating Rally environment...")
224 cmd = "rally deployment destroy opnfv-rally"
225 ft_utils.execute_command(cmd, exit_on_error=False,
226 error_msg=("Deployment %s does not exist."
227 % DEPLOYMENT_MAME), verbose=False)
228 rally_conf = os_utils.get_credentials_for_rally()
229 with open('rally_conf.json', 'w') as fp:
230 json.dump(rally_conf, fp)
231 cmd = "rally deployment create --file=rally_conf.json --name="
232 cmd += DEPLOYMENT_MAME
233 ft_utils.execute_command(cmd,
234 error_msg="Problem creating Rally deployment")
236 logger.info("Installing tempest from existing repo...")
237 cmd = ("rally verify install --source " + TEMPEST_REPO_DIR +
239 ft_utils.execute_command(cmd,
240 error_msg="Problem installing Tempest.")
242 cmd = "rally deployment check"
243 ft_utils.execute_command(cmd,
244 error_msg=("OpenStack not responding or "
245 "faulty Rally deployment."))
247 cmd = "rally show images"
248 ft_utils.execute_command(cmd,
249 error_msg=("Problem while listing "
250 "OpenStack images."))
252 cmd = "rally show flavors"
253 ft_utils.execute_command(cmd,
254 error_msg=("Problem while showing "
255 "OpenStack flavors."))
258 def check_environment():
259 msg_not_active = "The Functest environment is not installed."
260 if not os.path.isfile(ENV_FILE):
261 logger.error(msg_not_active)
264 with open(ENV_FILE, "r") as env_file:
266 if not re.search("1", s):
267 logger.error(msg_not_active)
270 logger.info("Functest environment installed.")
274 if not (args.action in actions):
275 logger.error('Argument not valid.')
278 if args.action == "start":
279 logger.info("######### Preparing Functest environment #########\n")
280 check_env_variables()
287 with open(ENV_FILE, "w") as env_file:
292 if args.action == "check":
297 if __name__ == '__main__':