Ejemplo n.º 1
0
    def load(self, file_name):
        file_path = os.path.join(self._path, file_name)
        LOG.info("Loading configuration file locally: %s", file_path)

        with open(file_path, 'r') as f:
            config_dict = yaml.load(f, Loader=yaml.BaseLoader)
        # validate input data
        validate_config(config_dict)

        return UbiConfig.load_from_dict(config_dict, file_name)
Ejemplo n.º 2
0
    def load(self, file_name):
        # find the right branch from the mapping
        branch = self._files_branch_map[file_name]

        config_file_url = self._repo_api.get_file_content_api(
            file_name, branch)
        LOG.info("Loading configuration file from remote: %s", file_name)
        response = self._session.get(config_file_url)
        response.raise_for_status()

        config_dict = yaml.load(response.content, Loader=yaml.BaseLoader)
        # validate input data
        validate_config(config_dict)

        return UbiConfig.load_from_dict(config_dict, file_name)
Ejemplo n.º 3
0
    def load(self, file_name):
        file_path = os.path.join(self._path, file_name)
        LOG.info("Loading configuration file locally: %s", file_path)

        try:
            with open(file_path, 'r') as f:
                config_dict = yaml.safe_load(f)

        except yaml.YAMLError:
            LOG.error('There is syntax error in your config file %s, please fix', file_name)
            raise

        # validate input data
        validate_config(config_dict)

        return UbiConfig.load_from_dict(config_dict, file_name)
Ejemplo n.º 4
0
    def load(self, file_name):
        # find the right branch from the mapping
        branch = self._files_branch_map[file_name]

        config_file_url = self._repo_api.get_file_content_api(
            file_name, branch)
        LOG.info("Loading configuration file from remote: %s", file_name)
        response = self._session.get(config_file_url)
        response.raise_for_status()

        try:
            config_dict = yaml.safe_load(response.content)
        except yaml.YAMLError:
            LOG.error(
                'There is syntax error in your config file %s, please fix',
                config_file_url)
            raise

        # validate input data
        validate_config(config_dict)

        return UbiConfig.load_from_dict(config_dict, file_name)
Ejemplo n.º 5
0
    def load(self, file_name, version=None):
        """ Load file from remote repository.
        :param file_name: filename that is on remote repository in any branch
        """
        if file_name not in self._files_branch_map:
            raise ValueError("Couldn't find file %s from remote repo %s" %
                             (file_name, self._url))

        sha1 = self._branches.get(version)
        if version and not sha1:
            LOG.warning(
                "Couldn't find version %s from %s, will try to find %s in default",
                version,
                self._url,
                file_name,
            )

        if not version or not sha1:
            # branch is not available from the wanted version or not specified,
            # use the default version.
            for branch_sha1 in self._files_branch_map[file_name]:
                if branch_sha1[0] == "ubi7":
                    version = "ubi7"
                    break
            else:
                version = "ubi8"
            sha1 = self._branches[version]

        LOG.info("Loading config file %s from branch %s", file_name, version)
        config_file_url = self._repo_api.get_file_content_api(file_name, sha1)
        response = self._session.get(config_file_url)
        response.raise_for_status()

        config_dict = yaml.load(response.content, Loader=yaml.BaseLoader)
        # validate input data
        validate_config(config_dict)

        return UbiConfig.load_from_dict(config_dict, file_name, version[3:])
Ejemplo n.º 6
0
    def load(self, file_name, version=None):
        """Load a config file from local.

        Args:
            file_name (str): path to the config file.

            version(str):
                The version usage here is a little different from the remote loader,
                it's only used to denote the version of config file, but in remote
                loader, it's also used to find the right config.

                If version is None, we should get it from its path.
        """
        if not self._isroot:
            file_path = os.path.join(self._path, file_name)
        else:
            file_path = file_name

        if version is None:
            # get version from path, such as configs/ubi7.1/config.yaml, get
            # ubi7.1.
            version = os.path.basename(
                os.path.dirname(os.path.abspath(file_path)))

        if not re.search(r"ubi[0-9]\.[0-9]{1,2}$|ubi[0-9]$", version):
            raise ValueError(
                "Expect directories named in format ubi[0-9].([0-9]{1,2})$' or ubi[0-9]$, but got %s"
                % version)

        LOG.info("Loading configuration file locally: %s", file_path)

        with open(file_path, "r") as f:
            config_dict = yaml.load(f, Loader=yaml.BaseLoader)
        # validate input data
        validate_config(config_dict)

        return UbiConfig.load_from_dict(config_dict, file_name, version[3:])
Ejemplo n.º 7
0
def test_validate_data():
    with open(TEST_DATA) as f:
        config = yaml.safe_load(f)
    assert config_validation.validate_config(config) is None
Ejemplo n.º 8
0
def test_validate_data_pass(ubi7_1_config):
    assert config_validation.validate_config(ubi7_1_config) is None
Ejemplo n.º 9
0
def test_validate_failed_wrong_data_type(dnf7_config):
    dnf7_config['packages']['include'] = 'A string'
    with pytest.raises(ValidationError):
        config_validation.validate_config(dnf7_config)
Ejemplo n.º 10
0
def test_validate_failed_missing_package_include(dnf7_config):
    dnf7_config['packages'].pop('include')
    with pytest.raises(ValidationError):
        config_validation.validate_config(dnf7_config)
Ejemplo n.º 11
0
def test_validate_failed_extra_keyword(dnf7_config):
    dnf7_config.update({'extra': 'something should not exist'})
    with pytest.raises(ValidationError):
        config_validation.validate_config(dnf7_config)
Ejemplo n.º 12
0
def test_validate_failed_missing_content_sets(dnf7_config):
    dnf7_config.pop('content_sets')
    with pytest.raises(ValidationError):
        config_validation.validate_config(dnf7_config)
Ejemplo n.º 13
0
def test_validate_data_pass_without_module(dnf7_config):
    dnf7_config.pop('modules')
    assert config_validation.validate_config(dnf7_config) is None
Ejemplo n.º 14
0
def test_validate_data_pass(dnf7_config):
    assert config_validation.validate_config(dnf7_config) is None
Ejemplo n.º 15
0
def test_validate_failed_wrong_data_type(ubi7_1_config):
    ubi7_1_config["packages"]["include"] = "A string"
    with pytest.raises(ValidationError):
        config_validation.validate_config(ubi7_1_config)
Ejemplo n.º 16
0
def test_validate_failed_missing_package_include(ubi7_1_config):
    ubi7_1_config["packages"].pop("include")
    with pytest.raises(ValidationError):
        config_validation.validate_config(ubi7_1_config)
Ejemplo n.º 17
0
def test_validate_failed_extra_keyword(ubi7_1_config):
    ubi7_1_config.update({"extra": "something should not exist"})
    with pytest.raises(ValidationError):
        config_validation.validate_config(ubi7_1_config)
Ejemplo n.º 18
0
def test_validate_failed_missing_content_sets(ubi7_1_config):
    ubi7_1_config.pop("content_sets")
    with pytest.raises(ValidationError):
        config_validation.validate_config(ubi7_1_config)
Ejemplo n.º 19
0
def test_validate_data_pass_without_module(ubi7_1_config):
    ubi7_1_config.pop("modules")
    assert config_validation.validate_config(ubi7_1_config) is None