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 def test_merge_strategy_on_self_referencing_config(self):
77 # the yaml anchor and alias syntax allows to create data
78 # that references itself. Both sides of the merge are read
79 # from the same file but as two independent objects, like
80 # it happens when two config files provide such data
81 with open('../data/kiwi_merge_recursive.yml', 'r') as config:
82 master = yaml.safe_load(config)
83 with open('../data/kiwi_merge_recursive.yml', 'r') as config:
84 slave = yaml.safe_load(config)
85 with raises(KiwiRuntimeConfigFormatError):
86 RuntimeConfig._merge(master, slave)
87
88 @patch('yaml.safe_load')
89 @patch('kiwi.defaults.CUSTOM_RUNTIME_CONFIG_FILE', 'some-custom-file')
90 def test_reading_custom_config_file(self, mock_yaml):
91 with patch('os.path.isfile', return_value=True):
92 with patch('builtins.open') as m_open:
93 RuntimeConfig(reread=True)
94 m_open.assert_has_calls(
95 [
96 call('some-custom-file', 'r')
97 ]
98 )
99
100 @pytest.mark.no_kiwi_yml_mock
101 @patch('os.path.isfile')
102 @patch('yaml.safe_load')
103 @patch('os.path.isdir')
104 @patch('os.listdir')
105 def test_reading_system_wide_config_file_from_etc(
106 self,
107 mock_os_listdir,
108 mock_os_path_isdir,
109 mock_yaml,
110 mock_os_path_isfile
111 ):
112 mock_os_listdir.return_value = ['some.yml']
113
114 def os_path_isfile(config):
115 if config == '/etc/kiwi.yml':
116 return True
117 return False
118
119 def os_path_isdir(config):
120 if config == '/etc/kiwi.yml.d':
121 return True
122 return False
123
124 mock_os_path_isfile.side_effect = os_path_isfile
125 mock_os_path_isdir.side_effect = os_path_isdir
126 with patch('builtins.open') as m_open:
127 RuntimeConfig(reread=True)
128 assert m_open.call_args_list == [
129 call('/etc/kiwi.yml', 'r'),
130 call('/etc/kiwi.yml.d/some.yml', 'r')
131 ]
132
133 @pytest.mark.no_kiwi_yml_mock
134 @patch('os.path.isfile')
135 @patch('yaml.safe_load')
136 @patch('os.path.isdir')
137 @patch('os.listdir')
138 def test_reading_system_wide_config_file_from_usr(
139 self,
140 mock_os_listdir,
141 mock_os_path_isdir,
142 mock_yaml,
143 mock_os_path_isfile
144 ):
145 mock_os_listdir.return_value = ['some.yml']
146
147 def os_path_isfile(config):
148 if config == '/usr/share/kiwi/kiwi.yml':
149 return True
150 return False
151
152 def os_path_isdir(config):
153 if config == '/usr/share/kiwi/kiwi.yml.d':
154 return True
155 return False
156
157 mock_os_path_isfile.side_effect = os_path_isfile
158 mock_os_path_isdir.side_effect = os_path_isdir
159 with patch('builtins.open') as m_open:
160 RuntimeConfig(reread=True)
161 assert m_open.call_args_list == [
162 call('/usr/share/kiwi/kiwi.yml', 'r'),
163 call('/usr/share/kiwi/kiwi.yml.d/some.yml', 'r')
164 ]
165
166 @patch('kiwi.defaults.ETC_RUNTIME_CONFIG_FILE', '../data/kiwi.yml')
167 @patch('kiwi.defaults.ETC_RUNTIME_CONFIG_DIR', '../data/kiwi.yml.d')
168 @patch('kiwi.runtime_checker.Defaults.is_buildservice_worker')
169 def test_config_sections_from_system_wide_etc_config(
170 self, mock_is_buildservice_worker
171 ):
172 mock_is_buildservice_worker.return_value = False
173 runtime_config = RuntimeConfig(reread=True)
174 assert runtime_config.get_xz_options() == ['-a', '-b', 'xxx']
175 assert runtime_config.get_mapper_tool() == 'partx'
176 assert runtime_config.get_checksum_handler(
177 '../data/metalink'
178 ).suffix == '.sha512'
179 assert runtime_config.get_checksum_handler(
180 '../data/metalink', bundle_lookup=True
181 ).suffix == '.sha256'
182
183 @patch('kiwi.defaults.ETC_RUNTIME_CONFIG_FILE', '')
184 @patch('kiwi.defaults.ETC_RUNTIME_CONFIG_DIR', '')
185 @patch('kiwi.defaults.USR_RUNTIME_CONFIG_FILE', '../data/kiwi.yml')
186 @patch('kiwi.defaults.USR_RUNTIME_CONFIG_DIR', '../data/kiwi.yml.d')
187 @patch('kiwi.runtime_checker.Defaults.is_buildservice_worker')
188 def test_config_sections_from_system_wide_usr_config(
189 self,
190 mock_is_buildservice_worker
191 ):
192 mock_is_buildservice_worker.return_value = False
193 runtime_config = RuntimeConfig(reread=True)
194 assert runtime_config.get_xz_options() == ['-a', '-b', 'xxx']
195 assert runtime_config.get_mapper_tool() == 'partx'
196
197 @patch('kiwi.runtime_checker.Defaults.is_buildservice_worker')
198 def test_config_sections_from_home_base_config(
199 self, mock_is_buildservice_worker
200 ):
201 mock_is_buildservice_worker.return_value = False
202 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/ok'}):
203 runtime_config = RuntimeConfig(reread=True)
204
205 assert runtime_config.get_xz_options() == ['-a', '-b', 'xxx']
206 assert runtime_config.is_obs_public() is True
207 assert runtime_config.get_bundle_compression() is True
208 assert runtime_config.get_obs_download_server_url() == \
209 'http://example.com'
210 assert runtime_config.get_obs_api_server_url() == \
211 'https://api.example.com'
212 assert runtime_config.get_container_compression() is False
213 assert runtime_config.get_iso_tool_category() == 'xorriso'
214 assert runtime_config.get_iso_media_tag_tool() == 'isomd5sum'
215 assert runtime_config.get_oci_archive_tool() == 'umoci'
216 assert runtime_config.get_mapper_tool() == 'partx'
217 assert runtime_config.get_package_changes() is True
218 assert runtime_config.get_disabled_runtime_checks() == [
219 'check_dracut_module_for_oem_install_in_package_list',
220 'check_container_tool_chain_installed'
221 ]
222
223 @patch('kiwi.runtime_checker.Defaults.is_buildservice_worker')
224 @patch('kiwi.runtime_checker.Defaults.get_platform_name')
225 def test_config_sections_defaults(
226 self, mock_get_platform_name, mock_is_buildservice_worker
227 ):
228 mock_get_platform_name.return_value = 's390x'
229 mock_is_buildservice_worker.return_value = True
230 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/defaults'}):
231 runtime_config = RuntimeConfig(reread=True)
232
233 assert runtime_config.get_bundle_compression(default=True) is True
234 assert runtime_config.get_bundle_compression(default=False) is False
235 assert runtime_config.is_obs_public() is True
236 assert runtime_config.get_obs_download_server_url() == \
237 Defaults.get_obs_download_server_url()
238 assert runtime_config.get_obs_api_server_url() == \
239 Defaults.get_obs_api_server_url()
240 assert runtime_config.get_container_compression() is True
241 assert runtime_config.get_iso_tool_category() == 'xorriso'
242 assert runtime_config.get_iso_media_tag_tool() == 'checkmedia'
243 assert runtime_config.get_oci_archive_tool() == 'umoci'
244 assert runtime_config.get_mapper_tool() == 'kpartx'
245 assert runtime_config.get_package_changes() is False
246 assert runtime_config.\
247 get_credentials_verification_metadata_signing_key_file() == ''
248
249 mock_get_platform_name.return_value = 'x86_64'
250 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/defaults'}):
251 runtime_config = RuntimeConfig(reread=True)
252 assert runtime_config.get_mapper_tool() == 'partx'
253 assert runtime_config.get_checksum_handler(
254 '../data/metalink'
255 ).suffix == '.sha256'
256
257 def test_config_sections_invalid(self):
258 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/invalid'}):
259 runtime_config = RuntimeConfig(reread=True)
260
261 with self._caplog.at_level(logging.WARNING):
262 assert runtime_config.get_container_compression() is True
263 assert 'Skipping invalid container compression: foo' in \
264 self._caplog.text
265 with self._caplog.at_level(logging.WARNING):
266 assert runtime_config.get_iso_tool_category() == 'xorriso'
267 assert 'Skipping invalid iso tool category: foo' in \
268 self._caplog.text
269
270 with self._caplog.at_level(logging.WARNING):
271 assert runtime_config.get_iso_media_tag_tool() == "checkmedia"
272 assert 'Skipping invalid iso media tag tool: foo' in \
273 self._caplog.text
274
275 def test_config_sections_other_settings(self):
276 with patch.dict('os.environ', {'HOME': '../data/kiwi_config/other'}):
277 runtime_config = RuntimeConfig(reread=True)
278
279 assert runtime_config.get_container_compression() is True
280 assert runtime_config.get_package_changes() is True
281 assert runtime_config.get_iso_media_tag_tool() == "checkmedia"