Debian Codegraph @

test/unit/runtime_config_test.py

raw
1 import logging
2 import yaml
3 from unittest.mock import (
4 patch, call
5 )
6 from pytest import (
7 raises, fixture
8 )
9 import pytest
10
11 from kiwi.runtime_config import RuntimeConfig
12 from kiwi.defaults import Defaults
13
14 from kiwi.exceptions import (
15 KiwiRuntimeConfigFormatError,
16 KiwiRuntimeConfigFileError
17 )
18
19
20 class TestRuntimeConfig:
21 def setup(self):
22 Defaults.set_custom_runtime_config_file(None)
23
24 def setup_method(self, cls):
25 self.setup()
26
27 @fixture(autouse=True)
28 def inject_fixtures(self, caplog):
29 self._caplog = caplog
30
31 def test__get_attribute_raises_on_invalid_structure(self):
32 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/broken'}):
33 runtime_config = RuntimeConfig(reread=True)
34 with raises(KiwiRuntimeConfigFormatError):
35 runtime_config._get_attribute('foo', 'bar')
36
37 @patch('kiwi.defaults.CUSTOM_RUNTIME_CONFIG_FILE', 'some-config-file')
38 def test_init_raises_custom_config_file_not_found(self):
39 with patch('os.path.isfile', return_value=False):
40 with raises(KiwiRuntimeConfigFileError):
41 RuntimeConfig(reread=True)
42
43 def test_merge_strategy(self):
44 with open('../data/kiwi.yml', 'r') as config:
45 master = yaml.safe_load(config)
46 with open('../data/kiwi_merge_ok.yml', 'r') as config:
47 slave_ok = yaml.safe_load(config)
48 with open('../data/kiwi_merge_invalid_dict.yml', 'r') as config:
49 slave_invalid_dict = yaml.safe_load(config)
50 with open('../data/kiwi_merge_invalid_list.yml', 'r') as config:
51 slave_invalid_list = yaml.safe_load(config)
52 assert RuntimeConfig._merge(master, slave_ok) == {
53 'xz': {
54 'options': '--foo'
55 },
56 'shasum': {
57 'size': '256'
58 },
59 'bundle': {
60 'shasum_size': '256',
61 'compress': False
62 },
63 'runtime_checks': {
64 'disable': [
65 'check_boot_description_exists',
66 'check_container_tool_chain_installed',
67 'check_repositories_configured'
68 ]
69 }
70 }
71 with raises(NotImplementedError):
72 RuntimeConfig._merge(master, slave_invalid_dict)
73 with raises(NotImplementedError):
74 RuntimeConfig._merge(master, slave_invalid_list)
75
76 @patch('yaml.safe_load')
77 @patch('kiwi.defaults.CUSTOM_RUNTIME_CONFIG_FILE', 'some-custom-file')
78 def test_reading_custom_config_file(self, mock_yaml):
79 with patch('os.path.isfile', return_value=True):
80 with patch('builtins.open') as m_open:
81 RuntimeConfig(reread=True)
82 m_open.assert_has_calls(
83 [
84 call('some-custom-file', 'r')
85 ]
86 )
87
88 @pytest.mark.no_kiwi_yml_mock
89 @patch('os.path.exists')
90 @patch('yaml.safe_load')
91 @patch('os.path.isdir')
92 @patch('os.listdir')
93 def test_reading_system_wide_config_file_from_etc(
94 self,
95 mock_os_listdir,
96 mock_os_path_isdir,
97 mock_yaml,
98 mock_exists
99 ):
100 mock_os_listdir.return_value = ['some.yml']
101
102 def os_path_exists(config):
103 if config == '/etc/kiwi.yml':
104 return True
105 return False
106
107 def os_path_isdir(config):
108 if config == '/etc/kiwi.yml.d':
109 return True
110 return False
111
112 mock_exists.side_effect = os_path_exists
113 mock_os_path_isdir.side_effect = os_path_isdir
114 with patch('builtins.open') as m_open:
115 RuntimeConfig(reread=True)
116 assert m_open.call_args_list == [
117 call('/etc/kiwi.yml', 'r'),
118 call('/etc/kiwi.yml.d/some.yml', 'r')
119 ]
120
121 @pytest.mark.no_kiwi_yml_mock
122 @patch('os.path.exists')
123 @patch('yaml.safe_load')
124 @patch('os.path.isdir')
125 @patch('os.listdir')
126 def test_reading_system_wide_config_file_from_usr(
127 self,
128 mock_os_listdir,
129 mock_os_path_isdir,
130 mock_yaml,
131 mock_exists
132 ):
133 mock_os_listdir.return_value = ['some.yml']
134
135 def os_path_exists(config):
136 if config == '/usr/share/kiwi/kiwi.yml':
137 return True
138 return False
139
140 def os_path_isdir(config):
141 if config == '/usr/share/kiwi/kiwi.yml.d':
142 return True
143 return False
144
145 mock_exists.side_effect = os_path_exists
146 mock_os_path_isdir.side_effect = os_path_isdir
147 with patch('builtins.open') as m_open:
148 RuntimeConfig(reread=True)
149 assert m_open.call_args_list == [
150 call('/usr/share/kiwi/kiwi.yml', 'r'),
151 call('/usr/share/kiwi/kiwi.yml.d/some.yml', 'r')
152 ]
153
154 @patch('kiwi.defaults.ETC_RUNTIME_CONFIG_FILE', '../data/kiwi.yml')
155 @patch('kiwi.defaults.ETC_RUNTIME_CONFIG_DIR', '../data/kiwi.yml.d')
156 @patch('kiwi.runtime_checker.Defaults.is_buildservice_worker')
157 def test_config_sections_from_system_wide_etc_config(
158 self, mock_is_buildservice_worker
159 ):
160 mock_is_buildservice_worker.return_value = False
161 runtime_config = RuntimeConfig(reread=True)
162 assert runtime_config.get_xz_options() == ['-a', '-b', 'xxx']
163 assert runtime_config.get_mapper_tool() == 'partx'
164 assert runtime_config.get_checksum_handler(
165 '../data/metalink'
166 ).suffix == '.sha512'
167 assert runtime_config.get_checksum_handler(
168 '../data/metalink', bundle_lookup=True
169 ).suffix == '.sha256'
170
171 @patch('kiwi.defaults.ETC_RUNTIME_CONFIG_FILE', '')
172 @patch('kiwi.defaults.ETC_RUNTIME_CONFIG_DIR', '')
173 @patch('kiwi.defaults.USR_RUNTIME_CONFIG_FILE', '../data/kiwi.yml')
174 @patch('kiwi.defaults.USR_RUNTIME_CONFIG_DIR', '../data/kiwi.yml.d')
175 @patch('kiwi.runtime_checker.Defaults.is_buildservice_worker')
176 def test_config_sections_from_system_wide_usr_config(
177 self,
178 mock_is_buildservice_worker
179 ):
180 mock_is_buildservice_worker.return_value = False
181 runtime_config = RuntimeConfig(reread=True)
182 assert runtime_config.get_xz_options() == ['-a', '-b', 'xxx']
183 assert runtime_config.get_mapper_tool() == 'partx'
184
185 @patch('kiwi.runtime_checker.Defaults.is_buildservice_worker')
186 def test_config_sections_from_home_base_config(
187 self, mock_is_buildservice_worker
188 ):
189 mock_is_buildservice_worker.return_value = False
190 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/ok'}):
191 runtime_config = RuntimeConfig(reread=True)
192
193 assert runtime_config.get_xz_options() == ['-a', '-b', 'xxx']
194 assert runtime_config.is_obs_public() is True
195 assert runtime_config.get_bundle_compression() is True
196 assert runtime_config.get_obs_download_server_url() == \
197 'http://example.com'
198 assert runtime_config.get_obs_api_server_url() == \
199 'https://api.example.com'
200 assert runtime_config.get_container_compression() is False
201 assert runtime_config.get_iso_tool_category() == 'xorriso'
202 assert runtime_config.get_iso_media_tag_tool() == 'isomd5sum'
203 assert runtime_config.get_oci_archive_tool() == 'umoci'
204 assert runtime_config.get_mapper_tool() == 'partx'
205 assert runtime_config.get_package_changes() is True
206 assert runtime_config.get_disabled_runtime_checks() == [
207 'check_dracut_module_for_oem_install_in_package_list',
208 'check_container_tool_chain_installed'
209 ]
210
211 @patch('kiwi.runtime_checker.Defaults.is_buildservice_worker')
212 @patch('kiwi.runtime_checker.Defaults.get_platform_name')
213 def test_config_sections_defaults(
214 self, mock_get_platform_name, mock_is_buildservice_worker
215 ):
216 mock_get_platform_name.return_value = 's390x'
217 mock_is_buildservice_worker.return_value = True
218 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/defaults'}):
219 runtime_config = RuntimeConfig(reread=True)
220
221 assert runtime_config.get_bundle_compression(default=True) is True
222 assert runtime_config.get_bundle_compression(default=False) is False
223 assert runtime_config.is_obs_public() is True
224 assert runtime_config.get_obs_download_server_url() == \
225 Defaults.get_obs_download_server_url()
226 assert runtime_config.get_obs_api_server_url() == \
227 Defaults.get_obs_api_server_url()
228 assert runtime_config.get_container_compression() is True
229 assert runtime_config.get_iso_tool_category() == 'xorriso'
230 assert runtime_config.get_iso_media_tag_tool() == 'checkmedia'
231 assert runtime_config.get_oci_archive_tool() == 'umoci'
232 assert runtime_config.get_mapper_tool() == 'kpartx'
233 assert runtime_config.get_package_changes() is False
234 assert runtime_config.\
235 get_credentials_verification_metadata_signing_key_file() == ''
236
237 mock_get_platform_name.return_value = 'x86_64'
238 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/defaults'}):
239 runtime_config = RuntimeConfig(reread=True)
240 assert runtime_config.get_mapper_tool() == 'partx'
241 assert runtime_config.get_checksum_handler(
242 '../data/metalink'
243 ).suffix == '.sha256'
244
245 def test_config_sections_invalid(self):
246 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/invalid'}):
247 runtime_config = RuntimeConfig(reread=True)
248
249 with self._caplog.at_level(logging.WARNING):
250 assert runtime_config.get_container_compression() is True
251 assert 'Skipping invalid container compression: foo' in \
252 self._caplog.text
253 with self._caplog.at_level(logging.WARNING):
254 assert runtime_config.get_iso_tool_category() == 'xorriso'
255 assert 'Skipping invalid iso tool category: foo' in \
256 self._caplog.text
257
258 with self._caplog.at_level(logging.WARNING):
259 assert runtime_config.get_iso_media_tag_tool() == "checkmedia"
260 assert 'Skipping invalid iso media tag tool: foo' in \
261 self._caplog.text
262
263 def test_config_sections_other_settings(self):
264 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/other'}):
265 runtime_config = RuntimeConfig(reread=True)
266
267 assert runtime_config.get_container_compression() is True
268 assert runtime_config.get_package_changes() is True
269 assert runtime_config.get_iso_media_tag_tool() == "checkmedia"