3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
19 from functest.ci import check_deployment
20 import functest.utils.functest_utils as ft_utils
21 import functest.utils.openstack_utils as os_utils
22 from functest.utils.constants import CONST
24 actions = ['start', 'check']
26 """ logging configuration """
27 logger = logging.getLogger('functest.ci.prepare_env')
29 # set the architecture to default
30 pod_arch = os.getenv("POD_ARCH", None)
31 arch_filter = ['aarch64']
33 CONFIG_FUNCTEST_PATH = pkg_resources.resource_filename(
34 'functest', 'ci/config_functest.yaml')
35 CONFIG_PATCH_PATH = pkg_resources.resource_filename(
36 'functest', 'ci/config_patch.yaml')
37 CONFIG_AARCH64_PATCH_PATH = pkg_resources.resource_filename(
38 'functest', 'ci/config_aarch64_patch.yaml')
41 class PrepareEnvParser(object):
44 self.parser = argparse.ArgumentParser()
45 self.parser.add_argument("action", help="Possible actions are: "
46 "'{d[0]}|{d[1]}' ".format(d=actions),
48 self.parser.add_argument("-d", "--debug", help="Debug mode",
51 def parse_args(self, argv=[]):
52 return vars(self.parser.parse_args(argv))
55 def print_separator():
56 logger.info("==============================================")
62 logger.info("Sourcing the OpenStack RC file...")
63 os_utils.source_credentials(CONST.__getattribute__('openstack_creds'))
64 for key, value in os.environ.iteritems():
65 if re.search("OS_", key):
66 if key == 'OS_AUTH_URL':
67 CONST.__setattr__('OS_AUTH_URL', value)
68 elif key == 'OS_USERNAME':
69 CONST.__setattr__('OS_USERNAME', value)
70 elif key == 'OS_TENANT_NAME':
71 CONST.__setattr__('OS_TENANT_NAME', value)
72 elif key == 'OS_PASSWORD':
73 CONST.__setattr__('OS_PASSWORD', value)
76 def update_config_file():
77 patch_file(CONFIG_PATCH_PATH)
79 if pod_arch and pod_arch in arch_filter:
80 patch_file(CONFIG_AARCH64_PATCH_PATH)
82 if "TEST_DB_URL" in os.environ:
86 def patch_file(patch_file_path):
87 logger.debug('Updating file: %s', patch_file_path)
88 with open(patch_file_path) as f:
89 patch_file = yaml.safe_load(f)
92 for key in patch_file:
93 if key in CONST.__getattribute__('DEPLOY_SCENARIO'):
94 new_functest_yaml = dict(ft_utils.merge_dicts(
95 ft_utils.get_functest_yaml(), patch_file[key]))
99 os.remove(CONFIG_FUNCTEST_PATH)
100 with open(CONFIG_FUNCTEST_PATH, "w") as f:
101 f.write(yaml.dump(new_functest_yaml, default_style='"'))
105 with open(CONFIG_FUNCTEST_PATH) as f:
106 functest_yaml = yaml.safe_load(f)
108 with open(CONFIG_FUNCTEST_PATH, "w") as f:
109 functest_yaml["results"]["test_db_url"] = os.environ.get('TEST_DB_URL')
110 f.write(yaml.dump(functest_yaml, default_style='"'))
113 def verify_deployment():
115 logger.info("Verifying OpenStack deployment...")
116 deployment = check_deployment.CheckDeployment()
117 deployment.check_all()
121 _, flavor_id = os_utils.get_or_create_flavor('m1.tiny',
126 if flavor_id is None:
127 raise Exception('Failed to create flavor')
130 def check_environment():
131 msg_not_active = "The Functest environment is not installed."
132 if not os.path.isfile(CONST.__getattribute__('env_active')):
133 raise Exception(msg_not_active)
135 with open(CONST.__getattribute__('env_active'), "r") as env_file:
137 if not re.search("1", s):
138 raise Exception(msg_not_active)
140 logger.info("Functest environment is installed.")
143 def prepare_env(**kwargs):
145 if not (kwargs['action'] in actions):
146 logger.error('Argument not valid.')
148 elif kwargs['action'] == "start":
149 logger.info("######### Preparing Functest environment #########\n")
154 with open(CONST.__getattribute__('env_active'), "w") as env_file:
157 elif kwargs['action'] == "check":
159 except Exception as e:
166 logging.config.fileConfig(pkg_resources.resource_filename(
167 'functest', 'ci/logging.ini'))
168 logging.captureWarnings(True)
169 parser = PrepareEnvParser()
170 args = parser.parse_args(sys.argv[1:])
171 return prepare_env(**args)