Blame tests/cloud_tests/config.py

Packit Service a04d08
# This file is part of cloud-init. See LICENSE file for license information.
Packit Service a04d08
Packit Service a04d08
"""Used to setup test configuration."""
Packit Service a04d08
Packit Service a04d08
import glob
Packit Service a04d08
import os
Packit Service a04d08
Packit Service a04d08
from cloudinit import util as c_util
Packit Service a04d08
from tests.cloud_tests import (BASE_DIR, TEST_CONF_DIR)
Packit Service a04d08
Packit Service a04d08
# conf files
Packit Service a04d08
CONF_EXT = '.yaml'
Packit Service a04d08
VERIFY_EXT = '.py'
Packit Service a04d08
PLATFORM_CONF = os.path.join(BASE_DIR, 'platforms.yaml')
Packit Service a04d08
RELEASES_CONF = os.path.join(BASE_DIR, 'releases.yaml')
Packit Service a04d08
TESTCASE_CONF = os.path.join(BASE_DIR, 'testcases.yaml')
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def get(base, key):
Packit Service a04d08
    """Get config entry 'key' from base, ensuring is dictionary."""
Packit Service a04d08
    return base[key] if key in base and base[key] is not None else {}
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def enabled(config):
Packit Service a04d08
    """Test if config item is enabled."""
Packit Service a04d08
    return isinstance(config, dict) and config.get('enabled', False)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def path_to_name(path):
Packit Service a04d08
    """Convert abs or rel path to test config to path under 'sconfigs/'."""
Packit Service a04d08
    dir_path, file_name = os.path.split(os.path.normpath(path))
Packit Service a04d08
    name = os.path.splitext(file_name)[0]
Packit Service a04d08
    return os.sep.join((os.path.basename(dir_path), name))
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def name_to_path(name):
Packit Service a04d08
    """Convert test config path under configs/ to full config path."""
Packit Service a04d08
    name = os.path.normpath(name)
Packit Service a04d08
    if not name.endswith(CONF_EXT):
Packit Service a04d08
        name = name + CONF_EXT
Packit Service a04d08
    return name if os.path.isabs(name) else os.path.join(TEST_CONF_DIR, name)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def name_sanitize(name):
Packit Service a04d08
    """Sanitize test name to be used as a module name."""
Packit Service a04d08
    return name.replace('-', '_')
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def name_to_module(name):
Packit Service a04d08
    """Convert test name to a loadable module name under 'testcases/'."""
Packit Service a04d08
    name = name_sanitize(path_to_name(name))
Packit Service a04d08
    return name.replace(os.path.sep, '.')
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def merge_config(base, override):
Packit Service a04d08
    """Merge config and base."""
Packit Service a04d08
    res = base.copy()
Packit Service a04d08
    res.update(override)
Packit Service a04d08
    res.update({k: merge_config(base.get(k, {}), v)
Packit Service a04d08
                for k, v in override.items() if isinstance(v, dict)})
Packit Service a04d08
    return res
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def merge_feature_groups(feature_conf, feature_groups, overrides):
Packit Service a04d08
    """Combine feature groups and overrides to construct a supported list.
Packit Service a04d08
Packit Service a04d08
    @param feature_conf: feature config from releases.yaml
Packit Service a04d08
    @param feature_groups: feature groups the release is a member of
Packit Service a04d08
    @param overrides: overrides specified by the release's config
Packit Service a04d08
    @return_value: dict of {feature: true/false} settings
Packit Service a04d08
    """
Packit Service a04d08
    res = dict().fromkeys(feature_conf['all'])
Packit Service a04d08
    for group in feature_groups:
Packit Service a04d08
        res.update(feature_conf['groups'][group])
Packit Service a04d08
    res.update(overrides)
Packit Service a04d08
    return res
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def load_platform_config(platform_name, require_enabled=False):
Packit Service a04d08
    """Load configuration for platform.
Packit Service a04d08
Packit Service a04d08
    @param platform_name: name of platform to retrieve config for
Packit Service a04d08
    @param require_enabled: if true, raise error if 'enabled' not True
Packit Service a04d08
    @return_value: config dict
Packit Service a04d08
    """
Packit Service a04d08
    main_conf = c_util.read_conf(PLATFORM_CONF)
Packit Service a04d08
    conf = merge_config(main_conf['default_platform_config'],
Packit Service a04d08
                        main_conf['platforms'][platform_name])
Packit Service a04d08
    if require_enabled and not enabled(conf):
Packit Service a04d08
        raise ValueError('Platform is not enabled')
Packit Service a04d08
    return conf
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def load_os_config(platform_name, os_name, require_enabled=False,
Packit Service a04d08
                   feature_overrides=None):
Packit Service a04d08
    """Load configuration for os.
Packit Service a04d08
Packit Service a04d08
    @param platform_name: platform name to load os config for
Packit Service a04d08
    @param os_name: name of os to retrieve config for
Packit Service a04d08
    @param require_enabled: if true, raise error if 'enabled' not True
Packit Service a04d08
    @param feature_overrides: feature flag overrides to merge with features
Packit Service a04d08
    @return_value: config dict
Packit Service a04d08
    """
Packit Service a04d08
    if feature_overrides is None:
Packit Service a04d08
        feature_overrides = {}
Packit Service a04d08
    main_conf = c_util.read_conf(RELEASES_CONF)
Packit Service a04d08
    default = main_conf['default_release_config']
Packit Service a04d08
    image = main_conf['releases'][os_name]
Packit Service a04d08
    conf = merge_config(merge_config(get(default, 'default'),
Packit Service a04d08
                                     get(default, platform_name)),
Packit Service a04d08
                        merge_config(get(image, 'default'),
Packit Service a04d08
                                     get(image, platform_name)))
Packit Service a04d08
Packit Service a04d08
    feature_conf = main_conf['features']
Packit Service a04d08
    feature_groups = conf.get('feature_groups', [])
Packit Service a04d08
    overrides = merge_config(get(conf, 'features'), feature_overrides)
Packit Service 9bfd13
    conf['arch'] = c_util.get_dpkg_architecture()
Packit Service a04d08
    conf['features'] = merge_feature_groups(
Packit Service a04d08
        feature_conf, feature_groups, overrides)
Packit Service a04d08
Packit Service a04d08
    if require_enabled and not enabled(conf):
Packit Service a04d08
        raise ValueError('OS is not enabled')
Packit Service a04d08
    return conf
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def load_test_config(path):
Packit Service a04d08
    """Load a test config file by either abs path or rel path."""
Packit Service a04d08
    return merge_config(c_util.read_conf(TESTCASE_CONF)['base_test_data'],
Packit Service a04d08
                        c_util.read_conf(name_to_path(path)))
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def list_feature_flags():
Packit Service a04d08
    """List all supported feature flags."""
Packit Service a04d08
    feature_conf = get(c_util.read_conf(RELEASES_CONF), 'features')
Packit Service a04d08
    return feature_conf.get('all', [])
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def list_enabled_platforms():
Packit Service a04d08
    """List all platforms enabled for testing."""
Packit Service a04d08
    platforms = get(c_util.read_conf(PLATFORM_CONF), 'platforms')
Packit Service a04d08
    return [k for k, v in platforms.items() if enabled(v)]
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def list_enabled_distros(platforms):
Packit Service a04d08
    """List all distros enabled for testing on specified platforms."""
Packit Service a04d08
    def platform_has_enabled(config):
Packit Service a04d08
        """List if platform is enabled."""
Packit Service a04d08
        return any(enabled(merge_config(get(config, 'default'),
Packit Service a04d08
                                        get(config, platform)))
Packit Service a04d08
                   for platform in platforms)
Packit Service a04d08
Packit Service a04d08
    releases = get(c_util.read_conf(RELEASES_CONF), 'releases')
Packit Service a04d08
    return [k for k, v in releases.items() if platform_has_enabled(v)]
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def list_test_configs():
Packit Service a04d08
    """List all available test config files by abspath."""
Packit Service a04d08
    return [os.path.abspath(f) for f in
Packit Service a04d08
            glob.glob(os.sep.join((TEST_CONF_DIR, '*', '*.yaml')))]
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
ENABLED_PLATFORMS = sorted(list_enabled_platforms())
Packit Service a04d08
ENABLED_DISTROS = sorted(list_enabled_distros(ENABLED_PLATFORMS))
Packit Service a04d08
Packit Service a04d08
# vi: ts=4 expandtab