The former implementation read the runtime config file every time a new RuntimeConfig instance was created. The runtime config is however static during the runtime of an image build process and not supposed to change. Therefore the file should be read in once and any new instance of RuntimConfig should just use what has been read in at the first invocation. This commit refactors the RuntimeConfig class to hold an application global RUNTIME_CONFIG variable and allows to reread the config on explicit request.
139 lines
5.4 KiB
Python
139 lines
5.4 KiB
Python
import logging
|
|
from mock import (
|
|
patch, Mock
|
|
)
|
|
from pytest import (
|
|
raises, fixture
|
|
)
|
|
|
|
from kiwi.runtime_config import RuntimeConfig
|
|
from kiwi.defaults import Defaults
|
|
|
|
from kiwi.exceptions import (
|
|
KiwiRuntimeConfigFormatError,
|
|
KiwiRuntimeConfigFileError
|
|
)
|
|
|
|
|
|
class TestRuntimeConfig:
|
|
@fixture(autouse=True)
|
|
def inject_fixtures(self, caplog):
|
|
self._caplog = caplog
|
|
|
|
@patch('kiwi.runtime_config.Cli')
|
|
def test__get_attribute_raises_on_invalid_structure(self, mock_Cli):
|
|
cli = Mock()
|
|
cli.get_global_args.return_value = {}
|
|
mock_Cli.return_value = cli
|
|
with patch.dict('os.environ', {'HOME': '../data/kiwi_config/broken'}):
|
|
runtime_config = RuntimeConfig(reread=True)
|
|
with raises(KiwiRuntimeConfigFormatError):
|
|
runtime_config._get_attribute('foo', 'bar')
|
|
|
|
@patch('kiwi.runtime_config.Cli')
|
|
def test_init_raises_custom_config_file_not_found(self, mock_Cli):
|
|
cli = Mock()
|
|
cli.get_global_args.return_value = {}
|
|
mock_Cli.return_value = cli
|
|
cli.get_global_args.return_value = {
|
|
'--config': '../data/kiwi.yml'
|
|
}
|
|
with patch('os.path.isfile', return_value=False):
|
|
with raises(KiwiRuntimeConfigFileError):
|
|
RuntimeConfig(reread=True)
|
|
|
|
@patch('os.path.exists')
|
|
@patch('yaml.safe_load')
|
|
@patch('kiwi.runtime_config.Cli')
|
|
def test_reading_system_wide_config_file(
|
|
self, mock_Cli, mock_yaml, mock_exists
|
|
):
|
|
cli = Mock()
|
|
cli.get_global_args.return_value = {}
|
|
mock_Cli.return_value = cli
|
|
exists_call_results = [True, False]
|
|
|
|
def os_path_exists(config):
|
|
return exists_call_results.pop()
|
|
|
|
mock_exists.side_effect = os_path_exists
|
|
with patch('builtins.open') as m_open:
|
|
RuntimeConfig(reread=True)
|
|
m_open.assert_called_once_with('/etc/kiwi.yml', 'r')
|
|
|
|
@patch('kiwi.runtime_config.Cli')
|
|
def test_config_sections_from_home_base_config(self, mock_Cli):
|
|
cli = Mock()
|
|
cli.get_global_args.return_value = {}
|
|
mock_Cli.return_value = cli
|
|
with patch.dict('os.environ', {'HOME': '../data/kiwi_config/ok'}):
|
|
runtime_config = RuntimeConfig(reread=True)
|
|
|
|
assert runtime_config.get_xz_options() == ['-a', '-b', 'xxx']
|
|
assert runtime_config.is_obs_public() is True
|
|
assert runtime_config.get_bundle_compression() is True
|
|
assert runtime_config.get_obs_download_server_url() == \
|
|
'http://example.com'
|
|
assert runtime_config.get_obs_api_server_url() == \
|
|
'https://api.example.com'
|
|
assert runtime_config.get_container_compression() is None
|
|
assert runtime_config.get_iso_tool_category() == 'cdrtools'
|
|
assert runtime_config.get_oci_archive_tool() == 'umoci'
|
|
assert runtime_config.get_package_changes() is True
|
|
assert runtime_config.get_disabled_runtime_checks() == [
|
|
'check_dracut_module_for_oem_install_in_package_list',
|
|
'check_container_tool_chain_installed'
|
|
]
|
|
|
|
@patch('kiwi.runtime_config.Cli')
|
|
@patch('kiwi.runtime_checker.Defaults.is_buildservice_worker')
|
|
def test_config_sections_defaults(
|
|
self, mock_is_buildservice_worker, mock_Cli
|
|
):
|
|
cli = Mock()
|
|
cli.get_global_args.return_value = {}
|
|
mock_Cli.return_value = cli
|
|
mock_is_buildservice_worker.return_value = True
|
|
with patch.dict('os.environ', {'HOME': '../data/kiwi_config/defaults'}):
|
|
runtime_config = RuntimeConfig(reread=True)
|
|
|
|
assert runtime_config.get_bundle_compression(default=True) is True
|
|
assert runtime_config.get_bundle_compression(default=False) is False
|
|
assert runtime_config.is_obs_public() is True
|
|
assert runtime_config.get_obs_download_server_url() == \
|
|
Defaults.get_obs_download_server_url()
|
|
assert runtime_config.get_obs_api_server_url() == \
|
|
Defaults.get_obs_api_server_url()
|
|
assert runtime_config.get_container_compression() == 'xz'
|
|
assert runtime_config.get_iso_tool_category() == 'xorriso'
|
|
assert runtime_config.get_oci_archive_tool() == 'umoci'
|
|
assert runtime_config.get_package_changes() is False
|
|
|
|
@patch('kiwi.runtime_config.Cli')
|
|
def test_config_sections_invalid(self, mock_Cli):
|
|
cli = Mock()
|
|
cli.get_global_args.return_value = {}
|
|
mock_Cli.return_value = cli
|
|
with patch.dict('os.environ', {'HOME': '../data/kiwi_config/invalid'}):
|
|
runtime_config = RuntimeConfig(reread=True)
|
|
|
|
with self._caplog.at_level(logging.WARNING):
|
|
assert runtime_config.get_container_compression() == 'xz'
|
|
assert 'Skipping invalid container compression: foo' in \
|
|
self._caplog.text
|
|
with self._caplog.at_level(logging.WARNING):
|
|
assert runtime_config.get_iso_tool_category() == 'xorriso'
|
|
assert 'Skipping invalid iso tool category: foo' in \
|
|
self._caplog.text
|
|
|
|
@patch('kiwi.runtime_config.Cli')
|
|
def test_config_sections_other_settings(self, mock_Cli):
|
|
cli = Mock()
|
|
cli.get_global_args.return_value = {}
|
|
mock_Cli.return_value = cli
|
|
with patch.dict('os.environ', {'HOME': '../data/kiwi_config/other'}):
|
|
runtime_config = RuntimeConfig(reread=True)
|
|
|
|
assert runtime_config.get_container_compression() == 'xz'
|
|
assert runtime_config.get_package_changes() is True
|