Debian Codegraph @

test/unit/xml_description_test.py

raw
1 import logging
2 from unittest.mock import patch
3 import unittest.mock as mock
4 from builtins import bytes
5 from lxml import etree
6 from pytest import raises
7 from collections import namedtuple
8 from kiwi.utils.temporary import Temporary
9 from pytest import fixture
10
11 from kiwi.xml_description import XMLDescription
12
13 from kiwi.exceptions import (
14 KiwiCommandError,
15 KiwiSchemaImportError,
16 KiwiValidationError,
17 KiwiDescriptionInvalid,
18 KiwiDataStructureError,
19 KiwiCommandNotFound,
20 KiwiExtensionError
21 )
22
23
24 class TestSchema:
25 @fixture(autouse=True)
26 def inject_fixtures(self, caplog):
27 self._caplog = caplog
28
29 def setup(self):
30 test_xml = bytes(
31 b"""<?xml version="1.0" encoding="utf-8"?>
32 <image schemaversion="7.4" name="bob">
33 <description type="system">
34 <author>John Doe</author>
35 <contact>[email protected]</contact>
36 <specification>
37 say hello
38 </specification>
39 </description>
40 <preferences>
41 <packagemanager>zypper</packagemanager>
42 <version>1.1.1</version>
43 <type image="ext3"/>
44 </preferences>
45 <repository type="rpm-md">
46 <source path="repo"/>
47 </repository>
48 </image>"""
49 )
50 test_xml_extension = bytes(
51 b"""<?xml version="1.0" encoding="utf-8"?>
52 <image schemaversion="7.4" name="bob">
53 <description type="system">
54 <author>John Doe</author>
55 <contact>[email protected]</contact>
56 <specification>
57 say hello
58 </specification>
59 </description>
60 <preferences>
61 <packagemanager>zypper</packagemanager>
62 <version>1.1.1</version>
63 <type image="ext3"/>
64 </preferences>
65 <repository type="rpm-md">
66 <source path="repo"/>
67 </repository>
68 <extension xmlns:my_plugin="http://www.my_plugin.com">
69 <my_plugin:my_feature>
70 <my_plugin:title name="cool stuff"/>
71 </my_plugin:my_feature>
72 </extension>
73 </image>"""
74 )
75 test_xml_extension_not_unique = bytes(
76 b"""<?xml version="1.0" encoding="utf-8"?>
77 <image schemaversion="7.4" name="bob">
78 <description type="system">
79 <author>John Doe</author>
80 <contact>[email protected]</contact>
81 <specification>
82 say hello
83 </specification>
84 </description>
85 <preferences>
86 <packagemanager>zypper</packagemanager>
87 <version>1.1.1</version>
88 <type image="ext3"/>
89 </preferences>
90 <repository type="rpm-md">
91 <source path="repo"/>
92 </repository>
93 <extension xmlns:my_plugin="http://www.my_plugin.com">
94 <my_plugin:toplevel_a/>
95 <my_plugin:toplevel_b/>
96 </extension>
97 </image>"""
98 )
99 test_xml_extension_invalid = bytes(
100 b"""<?xml version="1.0" encoding="utf-8"?>
101 <image schemaversion="7.4" name="bob">
102 <description type="system">
103 <author>John Doe</author>
104 <contact>[email protected]</contact>
105 <specification>
106 say hello
107 </specification>
108 </description>
109 <preferences>
110 <packagemanager>zypper</packagemanager>
111 <version>1.1.1</version>
112 <type image="ext3"/>
113 </preferences>
114 <repository type="rpm-md">
115 <source path="repo"/>
116 </repository>
117 <extension xmlns:my_plugin="http://www.my_plugin.com">
118 <my_plugin:my_feature>
119 <my_plugin:title name="cool stuff" unknown_attr="foo"/>
120 </my_plugin:my_feature>
121 </extension>
122 </image>"""
123 )
124 self.description_from_file = XMLDescription(
125 description='../data/example_config.xml'
126 )
127 test_xml_file = Temporary().new_file()
128 with open(test_xml_file.name, 'wb') as description:
129 description.write(test_xml)
130 self.description_from_data = XMLDescription(test_xml_file.name)
131
132 test_xml_extension_file = Temporary().new_file()
133 with open(test_xml_extension_file.name, 'wb') as description:
134 description.write(test_xml_extension)
135 self.extension_description_from_data = XMLDescription(
136 test_xml_extension_file.name
137 )
138
139 test_xml_extension_not_unique_file = Temporary().new_file()
140 with open(test_xml_extension_not_unique_file.name, 'wb') as description:
141 description.write(test_xml_extension_not_unique)
142 self.extension_multiple_toplevel_description_from_data = XMLDescription(
143 test_xml_extension_not_unique_file.name
144 )
145
146 test_xml_extension_invalid_file = Temporary().new_file()
147 with open(test_xml_extension_invalid_file.name, 'wb') as description:
148 description.write(test_xml_extension_invalid)
149 self.extension_invalid_description_from_data = XMLDescription(
150 test_xml_extension_invalid_file.name
151 )
152
153 def setup_method(self, cls):
154 self.setup()
155
156 def test_load_schema_from_xml_content(self):
157 schema = etree.parse('../../kiwi/schema/kiwi.rng')
158 lookup = '{http://relaxng.org/ns/structure/1.0}attribute'
159 for attribute in schema.iter(lookup):
160 if attribute.get('name') == 'schemaversion':
161 schemaversion = attribute.find(
162 '{http://relaxng.org/ns/structure/1.0}value'
163 ).text
164 parsed = self.description_from_data.load()
165 assert parsed.get_schemaversion() == schemaversion
166
167 @patch('lxml.etree.RelaxNG')
168 def test_load_schema_import_error(self, mock_relax):
169 mock_relax.side_effect = KiwiSchemaImportError(
170 'ImportError'
171 )
172 with raises(KiwiSchemaImportError):
173 self.description_from_file.load()
174
175 @patch('kiwi.xml_description.Defaults.get_schematron_module_name')
176 def test_load_schema_from_xml_content_skipping_isoschematron(
177 self, mock_get_schematron_module_name
178 ):
179 mock_get_schematron_module_name.return_value = 'bogus'
180 with self._caplog.at_level(logging.WARNING):
181 self.description_from_data.load()
182 assert 'schematron validation skipped:' in self._caplog.text
183
184 @patch('lxml.isoschematron.Schematron')
185 @patch('lxml.etree.RelaxNG')
186 @patch('lxml.etree.parse')
187 def test_load_schema_validation_error_from_file(
188 self, mock_parse, mock_relax, mock_schematron
189 ):
190 mock_validate = mock.Mock()
191 mock_validate.validate.side_effect = KiwiValidationError(
192 'ValidationError'
193 )
194 mock_relax.return_value = mock_validate
195 mock_schematron.return_value = mock_validate
196 with raises(KiwiValidationError):
197 self.description_from_file.load()
198
199 @patch('lxml.isoschematron.Schematron')
200 @patch('lxml.etree.RelaxNG')
201 @patch('lxml.etree.parse')
202 @patch('kiwi.system.setup.Command.run')
203 def test_load_schema_description_from_file_invalid(
204 self, mock_command, mock_parse, mock_relax, mock_schematron
205 ):
206 mock_rng_validate = mock.Mock()
207 mock_rng_validate.validate = mock.Mock(
208 return_value=False
209 )
210
211 mock_sch_validate = mock.Mock()
212 mock_sch_validate.validate = mock.Mock(
213 return_value=False
214 )
215
216 validation_report = namedtuple(
217 'report', ['text']
218 )
219 name_spaces = namedtuple(
220 'nspaces', ['nsmap']
221 )
222 mock_validation_report = mock.Mock()
223 mock_validation_report.getroot = mock.Mock(
224 return_value=name_spaces(nsmap="")
225 )
226 mock_validation_report.xpath = mock.Mock(
227 return_value=[
228 validation_report(text='wrong attribute 1'),
229 validation_report(text='wrong attribute 2')
230 ]
231 )
232 mock_sch_validate.validation_report = mock_validation_report
233
234 mock_relax.return_value = mock_rng_validate
235 mock_schematron.return_value = mock_sch_validate
236 mock_command.side_effect = KiwiCommandError('jing output')
237 with raises(KiwiDescriptionInvalid):
238 self.description_from_file.load()
239
240 @patch('lxml.isoschematron.Schematron')
241 @patch('lxml.etree.RelaxNG')
242 @patch('lxml.etree.parse')
243 @patch('kiwi.system.setup.Command.run')
244 def test_load_schema_description_from_data_invalid(
245 self, mock_command, mock_parse, mock_relax, mock_schematron
246 ):
247 mock_rng_validate = mock.Mock()
248 mock_rng_validate.validate = mock.Mock(
249 return_value=False
250 )
251
252 mock_sch_validate = mock.Mock()
253 mock_sch_validate.validate = mock.Mock(
254 return_value=False
255 )
256
257 validation_report = namedtuple(
258 'report', ['text']
259 )
260 name_spaces = namedtuple(
261 'nspaces', ['nsmap']
262 )
263 mock_validation_report = mock.Mock()
264 mock_validation_report.getroot = mock.Mock(
265 return_value=name_spaces(nsmap="")
266 )
267 mock_validation_report.xpath = mock.Mock(
268 return_value=[
269 validation_report(text='wrong attribute 1'),
270 validation_report(text='wrong attribute 2')
271 ]
272 )
273 mock_sch_validate.validation_report = mock_validation_report
274
275 mock_relax.return_value = mock_rng_validate
276 mock_schematron.return_value = mock_sch_validate
277 command_run = namedtuple(
278 'command', ['output', 'error', 'returncode']
279 )
280 mock_command.return_value = command_run(
281 output='jing output\n',
282 error='',
283 returncode=1
284 )
285 with raises(KiwiDescriptionInvalid):
286 self.description_from_data.load()
287
288 @patch('lxml.isoschematron.Schematron')
289 @patch('lxml.etree.RelaxNG')
290 @patch('lxml.etree.parse')
291 @patch('kiwi.system.setup.Command.run')
292 def test_load_schema_description_from_data_invalid_no_jing(
293 self, mock_command, mock_parse, mock_relax, mock_schematron
294 ):
295 mock_rng_validate = mock.Mock()
296 mock_rng_validate.validate = mock.Mock(
297 return_value=False
298 )
299
300 mock_sch_validate = mock.Mock()
301 mock_sch_validate.validate = mock.Mock(
302 return_value=True
303 )
304
305 mock_relax.return_value = mock_rng_validate
306 mock_schematron.return_value = mock_sch_validate
307 mock_command.side_effect = KiwiCommandNotFound('No jing command')
308 with raises(KiwiDescriptionInvalid):
309 self.description_from_data.load()
310
311 @patch('lxml.isoschematron.Schematron')
312 @patch('lxml.etree.RelaxNG')
313 @patch('lxml.etree.parse')
314 @patch('kiwi.xml_parse.parse')
315 def test_load_data_structure_error(
316 self, mock_xml_parse, mock_etree_parse, mock_relax, mock_schematron
317 ):
318 mock_rng_validate = mock.Mock()
319 mock_rng_validate.validate = mock.Mock(
320 return_value=True
321 )
322 mock_sch_validate = mock.Mock()
323 mock_sch_validate.validate = mock.Mock(
324 return_value=True
325 )
326 mock_relax.return_value = mock_rng_validate
327 mock_schematron.return_value = mock_sch_validate
328 mock_xml_parse.side_effect = KiwiDataStructureError(
329 'DataStructureError'
330 )
331 with raises(KiwiDataStructureError):
332 self.description_from_file.load()
333
334 @patch('kiwi.xml_description.Command.run')
335 def test_load_extension(self, mock_command):
336 command_output = mock.Mock()
337 command_output.output = 'file://../data/my_plugin.rng'
338 mock_command.return_value = command_output
339 self.extension_description_from_data.load()
340 mock_command.assert_called_once_with(
341 ['xmlcatalog', '/etc/xml/catalog', 'http://www.my_plugin.com']
342 )
343 xml_data = self.extension_description_from_data.get_extension_xml_data(
344 'my_plugin'
345 )
346 assert xml_data.getroot()[0].get('name') == 'cool stuff'
347
348 def test_load_extension_multiple_toplevel_error(self):
349 with raises(KiwiExtensionError):
350 self.extension_multiple_toplevel_description_from_data.load()
351
352 @patch('kiwi.xml_description.Command.run')
353 def test_load_extension_schema_error(self, mock_command):
354 mock_command.side_effect = Exception
355 with raises(KiwiExtensionError):
356 self.extension_description_from_data.load()
357
358 @patch('kiwi.xml_description.Command.run')
359 def test_load_extension_validation_error(self, mock_command):
360 command_output = mock.Mock()
361 command_output.output = 'file://../data/my_plugin.rng'
362 mock_command.return_value = command_output
363 with raises(KiwiExtensionError):
364 self.extension_invalid_description_from_data.load()