| # -*- coding: utf-8 -*- |
| ''' |
| ''' |
| |
| import jinja2 |
| import logging |
| import re |
| import requests |
| |
| import pkg_resources |
| |
| import salt.pillar |
| |
| from runtest import tempest_sections |
| from runtest.utils import requirements |
| |
| log = logging.getLogger(__name__) |
| |
| __virtualname__ = 'runtest' |
| |
| |
| def __virtual__(): |
| return __virtualname__ |
| |
| |
| CFG_TEMPLATE = """ |
| {%- for section,options in config.items() -%} |
| [{{ section }}] |
| {%- for opt,val in options.items() %} |
| {{ opt }} = {{ val }} |
| {%- endfor %} |
| |
| {% endfor %} |
| """ |
| |
| REQUIREMENTS_LINK='https://raw.githubusercontent.com/openstack/requirements' |
| |
| def _get_pillar_for_live_minions(timeout=5, gather_job_timeout=60): |
| client = salt.client.get_local_client(__opts__['conf_file']) |
| pillars = client.cmd('*', 'pillar.items', timeout=timeout, |
| gather_job_timeout=gather_job_timeout) |
| |
| return pillars |
| |
| def generate_tempest_config(dst, *args, **kwargs): |
| |
| pillars = _get_pillar_for_live_minions() |
| this_node_pillar = __opts__['pillar'] |
| runtest_opts = this_node_pillar.get(__virtualname__, {}).get('tempest', {}) |
| config = {} |
| |
| for ts in tempest_sections.SECTIONS: |
| ts_inst = ts(pillars, runtest_opts) |
| config[ts_inst.name] = {} |
| opts = {} |
| for opt in ts_inst.options: |
| val = getattr(ts_inst, opt) |
| if val is None: |
| val = runtest_opts.get(ts_inst.name, {}).get(opt, None) |
| |
| if val is not None: |
| opts[opt] = val |
| |
| config[ts_inst.name] = opts |
| |
| data = jinja2.Environment().from_string(CFG_TEMPLATE).render(config=config) |
| with open(dst, 'w') as cfg_file: |
| cfg_file.write(data) |
| |
| return config |
| |
| def check_global_requirements(openstack_version=None, url=None): |
| """Check if our installed requirements are satisfied with upstream. |
| """ |
| |
| if openstack_version is None: |
| openstack_version = __salt__['pillar.get']('runtest:openstack_version') |
| if url is None: |
| url = '%s/%s/global-requirements.txt' % (REQUIREMENTS_LINK, openstack_version) |
| |
| resp = requests.get(url) |
| if resp.ok: |
| requirements_content = resp.text |
| else: |
| resp.raise_for_status() |
| pkgs = requirements.get_installed_distributions() |
| |
| req_deps = {} |
| freq = {} |
| for pkg in pkgs: |
| for line in requirements_content.splitlines(): |
| if re.match(r'^%s[ ><!=]' % pkg.project_name, line): |
| req_cond = line.split('#')[0].strip() |
| try: |
| log.info("Checking requirement {req_name}s, requirement" |
| " {req}s".format(req_name=pkg.project_name, |
| req=req_cond)) |
| pkg_resources.working_set.require(req_cond) |
| except pkg_resources.VersionConflict as ve: |
| log.warning("Requirement {req_name}s not satisfied. Required " |
| "version {req_cond}s. Installed version " |
| "{inst_version}s. Exception {exc}s".format( |
| req_name=pkg.project_name, req_cond=line, |
| inst_version=pkg.version, exc=ve)) |
| freq[pkg.project_name] = {'installed_version': pkg.version, |
| 'requirement': line} |
| except Exception as ex: {'error': ex, |
| 'requirement': line, |
| 'installed_version': pkg.version} |
| return freq |
| |
| def check_upper_constraints(openstack_version=None, url=None): |
| """Check if our installed requirements are satisfied with upstream upper constraints. |
| """ |
| if openstack_version is None: |
| openstack_version = __salt__['pillar.get']('runtest:openstack_version') |
| |
| if url is None: |
| url = '%s/%s/upper-constraints.txt' % (REQUIREMENTS_LINK, openstack_version) |
| |
| resp = requests.get(url) |
| if resp.ok: |
| constraints_content = resp.text |
| else: |
| resp.raise_for_status() |
| pkgs = requirements.get_installed_distributions() |
| |
| req_deps = {} |
| freq = {} |
| for pkg in pkgs: |
| for line in constraints_content.splitlines(): |
| if re.match(r'^%s[ ><!=]' % pkg.project_name, line, re.IGNORECASE): |
| req_cond = line.split('#')[0].strip() |
| try: |
| log.info("Checking requirement {req_name}s, requirement" |
| " {req}s".format(req_name=pkg.project_name, |
| req=req_cond)) |
| pkg_resources.working_set.require(req_cond) |
| except pkg_resources.VersionConflict as ve: |
| log.warning("Requirement {req_name}s not satisfied. Required " |
| "version {req_cond}s. Installed version " |
| "{inst_version}s. Exception {exc}s".format( |
| req_name=pkg.project_name, req_cond=line, |
| inst_version=pkg.version, exc=ve)) |
| freq[pkg.project_name] = {'installed_version': pkg.version, |
| 'requirement': line, |
| 'error': str(ve)} |
| except Exception as ex: |
| freq[pkg.project_name] = {'error': str(ex), |
| 'requirement': line, |
| 'installed_version': pkg.version} |
| return freq |