Debian Codegraph @

test/unit/runtime_config_test.py

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