227 lines
9.0 KiB
Python
227 lines
9.0 KiB
Python
# Copyright 2021 Red Hat, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import configparser
|
|
import copy
|
|
import ddt
|
|
import os
|
|
from unittest import mock
|
|
|
|
from . import fakes
|
|
from . import test_main
|
|
import tripleo_repos.yum_config.constants as const
|
|
import tripleo_repos.yum_config.exceptions as exc
|
|
import tripleo_repos.yum_config.yum_config as yum_cfg
|
|
|
|
|
|
@ddt.ddt
|
|
class TestTripleOYumConfig(test_main.TestTripleoYumConfigBase):
|
|
"""Tests for TripleYumConfig class and its methods."""
|
|
|
|
def _create_yum_config_obj(self, dir_path=None, valid_options=None,
|
|
file_extension=None):
|
|
self.mock_object(os.path, 'isfile')
|
|
self.mock_object(os, 'access')
|
|
self.mock_object(os.path, 'isdir')
|
|
return yum_cfg.TripleOYumConfig(dir_path=dir_path,
|
|
valid_options=valid_options,
|
|
file_extension=file_extension)
|
|
|
|
def test_tripleo_yum_config_invalid_dir_path(self):
|
|
self.mock_object(os.path, 'isdir',
|
|
mock.Mock(return_value=False))
|
|
|
|
self.assertRaises(exc.TripleOYumConfigNotFound,
|
|
yum_cfg.TripleOYumConfig,
|
|
dir_path='fake_dir_path')
|
|
|
|
def test_read_config_file_path(self):
|
|
yum_config = self._create_yum_config_obj()
|
|
|
|
parser_mock = mock.Mock()
|
|
self.mock_object(configparser, 'ConfigParser',
|
|
mock.Mock(return_value=parser_mock))
|
|
read_mock = self.mock_object(parser_mock, 'read')
|
|
self.mock_object(parser_mock, 'sections',
|
|
mock.Mock(return_value=fakes.FAKE_SECTIONS))
|
|
|
|
config_parser, file_path = yum_config._read_config_file(
|
|
fakes.FAKE_FILE_PATH,
|
|
section=fakes.FAKE_SECTION1
|
|
)
|
|
|
|
self.assertEqual(parser_mock, config_parser)
|
|
self.assertEqual(fakes.FAKE_FILE_PATH, file_path)
|
|
read_mock.assert_called_once_with(fakes.FAKE_FILE_PATH)
|
|
|
|
def test_read_config_file_path_parse_error(self):
|
|
yum_config = self._create_yum_config_obj()
|
|
|
|
parser_mock = mock.Mock()
|
|
self.mock_object(configparser, 'ConfigParser',
|
|
mock.Mock(return_value=parser_mock))
|
|
read_mock = self.mock_object(parser_mock, 'read',
|
|
mock.Mock(side_effect=configparser.Error))
|
|
|
|
self.assertRaises(exc.TripleOYumConfigFileParseError,
|
|
yum_config._read_config_file,
|
|
fakes.FAKE_FILE_PATH,
|
|
section=fakes.FAKE_SECTION1)
|
|
|
|
read_mock.assert_called_once_with(fakes.FAKE_FILE_PATH)
|
|
|
|
def test_read_config_file_path_invalid_section(self):
|
|
yum_config = self._create_yum_config_obj()
|
|
|
|
parser_mock = mock.Mock()
|
|
self.mock_object(configparser, 'ConfigParser',
|
|
mock.Mock(return_value=parser_mock))
|
|
read_mock = self.mock_object(parser_mock, 'read')
|
|
self.mock_object(parser_mock, 'sections',
|
|
mock.Mock(return_value=fakes.FAKE_SECTIONS))
|
|
|
|
self.assertRaises(exc.TripleOYumConfigInvalidSection,
|
|
yum_config._read_config_file,
|
|
fakes.FAKE_FILE_PATH,
|
|
section='invalid_section')
|
|
|
|
read_mock.assert_called_once_with(fakes.FAKE_FILE_PATH)
|
|
|
|
def test_get_config_files(self):
|
|
yum_config = self._create_yum_config_obj(
|
|
dir_path=fakes.FAKE_DIR_PATH,
|
|
file_extension='.conf')
|
|
parser_mocks = []
|
|
for i in range(3):
|
|
parser_mock = mock.Mock()
|
|
parser_mocks.append(parser_mock)
|
|
self.mock_object(parser_mock, 'read')
|
|
|
|
self.mock_object(parser_mocks[0], 'sections',
|
|
mock.Mock(return_value=[]))
|
|
# second file inside dir will have the expected sections
|
|
self.mock_object(parser_mocks[1], 'sections',
|
|
mock.Mock(return_value=fakes.FAKE_SECTIONS))
|
|
self.mock_object(parser_mocks[2], 'sections',
|
|
mock.Mock(return_value=[]))
|
|
self.mock_object(os, 'listdir',
|
|
mock.Mock(return_value=fakes.FAKE_DIR_FILES))
|
|
self.mock_object(os, 'access', mock.Mock(return_value=True))
|
|
self.mock_object(configparser, 'ConfigParser',
|
|
mock.Mock(side_effect=parser_mocks))
|
|
|
|
result = yum_config._get_config_files(fakes.FAKE_SECTION1)
|
|
expected_dir_path = [os.path.join(fakes.FAKE_DIR_PATH,
|
|
fakes.FAKE_DIR_FILES[1])]
|
|
|
|
self.assertEqual(expected_dir_path, result)
|
|
|
|
@mock.patch('builtins.open')
|
|
def test_update_section(self, open):
|
|
yum_config = self._create_yum_config_obj(
|
|
valid_options=fakes.FAKE_SUPP_OPTIONS)
|
|
config_parser = fakes.FakeConfigParser({fakes.FAKE_SECTION1: {}})
|
|
|
|
mock_read_config = self.mock_object(
|
|
yum_config, '_read_config_file',
|
|
mock.Mock(return_value=(config_parser, fakes.FAKE_FILE_PATH)))
|
|
|
|
updates = {fakes.FAKE_OPTION1: 'new_fake_value'}
|
|
|
|
yum_config.update_section(fakes.FAKE_SECTION1, updates,
|
|
file_path=fakes.FAKE_FILE_PATH)
|
|
|
|
mock_read_config.assert_called_once_with(fakes.FAKE_FILE_PATH,
|
|
section=fakes.FAKE_SECTION1)
|
|
|
|
def test_update_section_invalid_options(self):
|
|
yum_config = self._create_yum_config_obj(
|
|
valid_options=fakes.FAKE_SUPP_OPTIONS)
|
|
|
|
updates = {'invalid_option': 'new_fake_value'}
|
|
|
|
self.assertRaises(exc.TripleOYumConfigInvalidOption,
|
|
yum_config.update_section,
|
|
fakes.FAKE_SECTION1,
|
|
updates,
|
|
file_path=fakes.FAKE_FILE_PATH)
|
|
|
|
def test_update_section_file_not_found(self):
|
|
yum_config = self._create_yum_config_obj(
|
|
valid_options=fakes.FAKE_SUPP_OPTIONS)
|
|
mock_get_configs = self.mock_object(
|
|
yum_config, '_get_config_files',
|
|
mock.Mock(return_value=[]))
|
|
|
|
updates = {fakes.FAKE_OPTION1: 'new_fake_value'}
|
|
|
|
self.assertRaises(exc.TripleOYumConfigNotFound,
|
|
yum_config.update_section,
|
|
fakes.FAKE_SECTION1,
|
|
updates)
|
|
mock_get_configs.assert_called_once_with(fakes.FAKE_SECTION1)
|
|
|
|
|
|
@ddt.ddt
|
|
class TestTripleOYumRepoConfig(test_main.TestTripleoYumConfigBase):
|
|
"""Tests for TripleOYumRepoConfig class and its methods."""
|
|
|
|
@ddt.data(True, False, None)
|
|
def test_yum_repo_config_update_section(self, enable):
|
|
self.mock_object(os.path, 'isfile')
|
|
self.mock_object(os, 'access')
|
|
self.mock_object(os.path, 'isdir')
|
|
cfg_obj = yum_cfg.TripleOYumRepoConfig()
|
|
|
|
mock_update = self.mock_object(yum_cfg.TripleOYumConfig,
|
|
'update_section')
|
|
|
|
updates = {fakes.FAKE_OPTION1: 'new_fake_value'}
|
|
expected_updates = copy.copy(updates)
|
|
if enable is not None:
|
|
expected_updates['enabled'] = '1' if enable else '0'
|
|
|
|
cfg_obj.update_section(fakes.FAKE_SECTION1, set_dict=updates,
|
|
file_path=fakes.FAKE_FILE_PATH, enabled=enable)
|
|
|
|
mock_update.assert_called_once_with(fakes.FAKE_SECTION1,
|
|
expected_updates,
|
|
file_path=fakes.FAKE_FILE_PATH)
|
|
|
|
|
|
@ddt.ddt
|
|
class TestTripleOYumGlobalConfig(test_main.TestTripleoYumConfigBase):
|
|
"""Tests for TripleOYumGlobalConfig class and its methods."""
|
|
|
|
@mock.patch('builtins.open')
|
|
def test_create_yum_global_config_create_yum_conf(self, open):
|
|
self.mock_object(os, 'access')
|
|
self.mock_object(os.path, 'isdir')
|
|
self.mock_object(os.path, 'isfile',
|
|
mock.Mock(side_effect=[False, True]))
|
|
|
|
fake_cfg_parser = mock.Mock()
|
|
mock_read = self.mock_object(fake_cfg_parser, 'read')
|
|
mock_add = self.mock_object(fake_cfg_parser, 'add_section')
|
|
mock_write = self.mock_object(fake_cfg_parser, 'write')
|
|
self.mock_object(configparser, 'ConfigParser',
|
|
mock.Mock(return_value=fake_cfg_parser))
|
|
|
|
cfg_obj = yum_cfg.TripleOYumGlobalConfig()
|
|
|
|
self.assertIsNotNone(cfg_obj)
|
|
mock_read.assert_called_once_with(const.YUM_GLOBAL_CONFIG_FILE_PATH)
|
|
mock_add.assert_called_once_with('main')
|
|
mock_write.assert_called_once()
|