3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
11 """Tempest configuration utilities."""
23 from functest.utils import config
24 from functest.utils import env
29 RALLY_CONF_PATH = "/etc/rally/rally.conf"
30 RALLY_AARCH64_PATCH_PATH = pkg_resources.resource_filename(
31 'functest', 'ci/rally_aarch64_patch.conf')
32 GLANCE_IMAGE_PATH = os.path.join(
33 getattr(config.CONF, 'dir_functest_images'),
34 getattr(config.CONF, 'openstack_image_file_name'))
35 TEMPEST_CUSTOM = pkg_resources.resource_filename(
36 'functest', 'opnfv_tests/openstack/tempest/custom_tests/test_list.txt')
37 TEMPEST_BLACKLIST = pkg_resources.resource_filename(
38 'functest', 'opnfv_tests/openstack/tempest/custom_tests/blacklist.txt')
39 TEMPEST_CONF_YAML = pkg_resources.resource_filename(
40 'functest', 'opnfv_tests/openstack/tempest/custom_tests/tempest_conf.yaml')
42 CI_INSTALLER_TYPE = env.get('INSTALLER_TYPE')
44 """ logging configuration """
45 LOGGER = logging.getLogger(__name__)
48 def create_rally_deployment():
49 """Create new rally deployment"""
50 # set the architecture to default
51 pod_arch = env.get("POD_ARCH")
52 arch_filter = ['aarch64']
54 if pod_arch and pod_arch in arch_filter:
55 LOGGER.info("Apply aarch64 specific to rally config...")
56 with open(RALLY_AARCH64_PATCH_PATH, "r") as pfile:
57 rally_patch_conf = pfile.read()
59 for line in fileinput.input(RALLY_CONF_PATH, inplace=1):
61 if "cirros|testvm" in line:
62 print rally_patch_conf
64 LOGGER.info("Creating Rally environment...")
67 cmd = ['rally', 'deployment', 'destroy',
69 str(getattr(config.CONF, 'rally_deployment_name'))]
70 output = subprocess.check_output(cmd)
71 LOGGER.info("%s\n%s", " ".join(cmd), output)
72 except subprocess.CalledProcessError:
75 cmd = ['rally', 'deployment', 'create', '--fromenv',
76 '--name', str(getattr(config.CONF, 'rally_deployment_name'))]
77 output = subprocess.check_output(cmd)
78 LOGGER.info("%s\n%s", " ".join(cmd), output)
80 cmd = ['rally', 'deployment', 'check']
81 output = subprocess.check_output(cmd)
82 LOGGER.info("%s\n%s", " ".join(cmd), output)
85 def create_verifier():
86 """Create new verifier"""
87 LOGGER.info("Create verifier from existing repo...")
88 cmd = ['rally', 'verify', 'delete-verifier',
89 '--id', str(getattr(config.CONF, 'tempest_verifier_name')),
91 output = subprocess.check_output(cmd)
92 LOGGER.info("%s\n%s", " ".join(cmd), output)
94 cmd = ['rally', 'verify', 'create-verifier',
95 '--source', str(getattr(config.CONF, 'dir_repo_tempest')),
96 '--name', str(getattr(config.CONF, 'tempest_verifier_name')),
97 '--type', 'tempest', '--system-wide']
98 output = subprocess.check_output(cmd)
99 LOGGER.info("%s\n%s", " ".join(cmd), output)
102 def get_verifier_id():
104 Returns verifier id for current Tempest
106 create_rally_deployment()
108 cmd = ("rally verify list-verifiers | awk '/" +
109 getattr(config.CONF, 'tempest_verifier_name') +
111 proc = subprocess.Popen(cmd, shell=True,
112 stdout=subprocess.PIPE,
113 stderr=subprocess.STDOUT)
114 deployment_uuid = proc.stdout.readline().rstrip()
115 if deployment_uuid == "":
116 LOGGER.error("Tempest verifier not found.")
117 raise Exception('Error with command:%s' % cmd)
118 return deployment_uuid
121 def get_verifier_deployment_id():
123 Returns deployment id for active Rally deployment
125 cmd = ("rally deployment list | awk '/" +
126 getattr(config.CONF, 'rally_deployment_name') +
128 proc = subprocess.Popen(cmd, shell=True,
129 stdout=subprocess.PIPE,
130 stderr=subprocess.STDOUT)
131 deployment_uuid = proc.stdout.readline().rstrip()
132 if deployment_uuid == "":
133 LOGGER.error("Rally deployment not found.")
134 raise Exception('Error with command:%s' % cmd)
135 return deployment_uuid
138 def get_verifier_repo_dir(verifier_id):
140 Returns installed verifier repo directory for Tempest
143 verifier_id = get_verifier_id()
145 return os.path.join(getattr(config.CONF, 'dir_rally_inst'),
147 'verifier-{}'.format(verifier_id),
151 def get_verifier_deployment_dir(verifier_id, deployment_id):
153 Returns Rally deployment directory for current verifier
156 verifier_id = get_verifier_id()
158 if not deployment_id:
159 deployment_id = get_verifier_deployment_id()
161 return os.path.join(getattr(config.CONF, 'dir_rally_inst'),
163 'verifier-{}'.format(verifier_id),
164 'for-deployment-{}'.format(deployment_id))
167 def backup_tempest_config(conf_file, res_dir):
169 Copy config file to tempest results directory
171 if not os.path.exists(res_dir):
173 shutil.copyfile(conf_file,
174 os.path.join(res_dir, 'tempest.conf'))
177 def update_tempest_conf_file(conf_file, rconfig):
178 """Update defined paramters into tempest config file"""
179 with open(TEMPEST_CONF_YAML) as yfile:
180 conf_yaml = yaml.safe_load(yfile)
182 sections = rconfig.sections()
183 for section in conf_yaml:
184 if section not in sections:
185 rconfig.add_section(section)
186 sub_conf = conf_yaml.get(section)
187 for key, value in sub_conf.items():
188 rconfig.set(section, key, value)
190 with open(conf_file, 'wb') as config_file:
191 rconfig.write(config_file)
194 def configure_tempest_update_params(tempest_conf_file, res_dir,
195 network_name=None, image_id=None,
196 flavor_id=None, compute_cnt=1):
197 # pylint: disable=too-many-branches, too-many-arguments
199 Add/update needed parameters into tempest.conf file
201 LOGGER.debug("Updating selected tempest.conf parameters...")
202 rconfig = ConfigParser.RawConfigParser()
203 rconfig.read(tempest_conf_file)
204 rconfig.set('compute', 'fixed_network_name', network_name)
205 rconfig.set('compute', 'volume_device_name', env.get('VOLUME_DEVICE_NAME'))
206 if image_id is not None:
207 rconfig.set('compute', 'image_ref', image_id)
208 if IMAGE_ID_ALT is not None:
209 rconfig.set('compute', 'image_ref_alt', IMAGE_ID_ALT)
210 if getattr(config.CONF, 'tempest_use_custom_flavors'):
211 if flavor_id is not None:
212 rconfig.set('compute', 'flavor_ref', flavor_id)
213 if FLAVOR_ID_ALT is not None:
214 rconfig.set('compute', 'flavor_ref_alt', FLAVOR_ID_ALT)
216 # enable multinode tests
217 rconfig.set('compute', 'min_compute_nodes', compute_cnt)
218 rconfig.set('compute-feature-enabled', 'live_migration', True)
220 rconfig.set('identity', 'region', os.environ.get('OS_REGION_NAME'))
221 identity_api_version = os.environ.get("OS_IDENTITY_API_VERSION", '3')
222 if identity_api_version == '3':
224 rconfig.set('identity-feature-enabled', 'api_v2', False)
227 rconfig.set('identity', 'auth_version', auth_version)
229 'validation', 'ssh_timeout',
230 getattr(config.CONF, 'tempest_validation_ssh_timeout'))
231 rconfig.set('object-storage', 'operator_role',
232 getattr(config.CONF, 'tempest_object_storage_operator_role'))
234 if os.environ.get('OS_ENDPOINT_TYPE') is not None:
235 rconfig.set('identity', 'v3_endpoint_type',
236 os.environ.get('OS_ENDPOINT_TYPE'))
238 if os.environ.get('OS_ENDPOINT_TYPE') is not None:
239 sections = rconfig.sections()
241 'compute', 'volume', 'image', 'network', 'data-processing',
242 'object-storage', 'orchestration']
243 for service in services_list:
244 if service not in sections:
245 rconfig.add_section(service)
246 rconfig.set(service, 'endpoint_type',
247 os.environ.get('OS_ENDPOINT_TYPE'))
249 LOGGER.debug('Add/Update required params defined in tempest_conf.yaml '
250 'into tempest.conf file')
251 update_tempest_conf_file(tempest_conf_file, rconfig)
253 backup_tempest_config(tempest_conf_file, res_dir)
256 def configure_verifier(deployment_dir):
258 Execute rally verify configure-verifier, which generates tempest.conf
260 cmd = ['rally', 'verify', 'configure-verifier', '--reconfigure',
261 '--id', str(getattr(config.CONF, 'tempest_verifier_name'))]
262 output = subprocess.check_output(cmd)
263 LOGGER.info("%s\n%s", " ".join(cmd), output)
265 LOGGER.debug("Looking for tempest.conf file...")
266 tempest_conf_file = os.path.join(deployment_dir, "tempest.conf")
267 if not os.path.isfile(tempest_conf_file):
268 LOGGER.error("Tempest configuration file %s NOT found.",
270 raise Exception("Tempest configuration file %s NOT found."
273 return tempest_conf_file