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