| 1 |
from unittest.mock import patch |
| 2 |
from pytest import raises |
| 3 |
import sys |
| 4 |
import unittest.mock as mock |
| 5 |
import kiwi |
| 6 |
|
| 7 |
from ..test_helper import argv_kiwi_tests |
| 8 |
|
| 9 |
from kiwi.defaults import Defaults |
| 10 |
from kiwi.builder.archive import ArchiveBuilder |
| 11 |
|
| 12 |
from kiwi.exceptions import KiwiArchiveSetupError |
| 13 |
|
| 14 |
|
| 15 |
class TestArchiveBuilder: |
| 16 |
def setup(self): |
| 17 |
Defaults.set_platform_name('x86_64') |
| 18 |
self.xml_state = mock.Mock() |
| 19 |
self.xml_state.profiles = None |
| 20 |
self.xml_state.get_image_version = mock.Mock( |
| 21 |
return_value='1.2.3' |
| 22 |
) |
| 23 |
self.xml_state.get_build_type_name = mock.Mock( |
| 24 |
return_value='tbz' |
| 25 |
) |
| 26 |
self.xml_state.xml_data.get_name = mock.Mock( |
| 27 |
return_value='myimage' |
| 28 |
) |
| 29 |
self.setup = mock.Mock() |
| 30 |
kiwi.builder.archive.SystemSetup = mock.Mock( |
| 31 |
return_value=self.setup |
| 32 |
) |
| 33 |
self.archive = ArchiveBuilder( |
| 34 |
self.xml_state, 'target_dir', 'root_dir' |
| 35 |
) |
| 36 |
|
| 37 |
def setup_method(self, cls): |
| 38 |
self.setup() |
| 39 |
|
| 40 |
def test_create_unknown_archive_type(self): |
| 41 |
xml_state = mock.Mock() |
| 42 |
xml_state.get_build_type_name = mock.Mock( |
| 43 |
return_value='bogus' |
| 44 |
) |
| 45 |
xml_state.get_image_version = mock.Mock( |
| 46 |
return_value='1.2.3' |
| 47 |
) |
| 48 |
xml_state.xml_data.get_name = mock.Mock( |
| 49 |
return_value='myimage' |
| 50 |
) |
| 51 |
archive = ArchiveBuilder( |
| 52 |
xml_state, 'target_dir', 'root_dir' |
| 53 |
) |
| 54 |
with raises(KiwiArchiveSetupError): |
| 55 |
archive.create() |
| 56 |
|
| 57 |
@patch('kiwi.builder.archive.ArchiveTar') |
| 58 |
def test_create_tar(self, mock_tar): |
| 59 |
Defaults.set_platform_name('x86_64') |
| 60 |
self.archive.requested_archive_type = 'tbz' |
| 61 |
archive = mock.Mock() |
| 62 |
mock_tar.return_value = archive |
| 63 |
self.archive.create() |
| 64 |
mock_tar.assert_called_once_with( |
| 65 |
'target_dir/myimage.x86_64-1.2.3.tar' |
| 66 |
) |
| 67 |
archive.create_xz_compressed.assert_called_once_with( |
| 68 |
'root_dir', exclude=[ |
| 69 |
'image', '.kconfig', 'run/*', 'tmp/*', |
| 70 |
'.buildenv', 'var/cache/kiwi' |
| 71 |
], xz_options=None |
| 72 |
) |
| 73 |
self.setup.export_package_verification.assert_called_once_with( |
| 74 |
'target_dir' |
| 75 |
) |
| 76 |
self.setup.export_package_list.assert_called_once_with( |
| 77 |
'target_dir' |
| 78 |
) |
| 79 |
|
| 80 |
@patch('kiwi.builder.archive.ArchiveCpio') |
| 81 |
def test_create_cpio(self, mock_cpio): |
| 82 |
Defaults.set_platform_name('x86_64') |
| 83 |
self.archive.requested_archive_type = 'cpio' |
| 84 |
archive = mock.Mock() |
| 85 |
mock_cpio.return_value = archive |
| 86 |
self.archive.create() |
| 87 |
mock_cpio.assert_called_once_with( |
| 88 |
'target_dir/myimage.x86_64-1.2.3.cpio' |
| 89 |
) |
| 90 |
archive.create.assert_called_once_with( |
| 91 |
'root_dir', exclude=[ |
| 92 |
'image', '.kconfig', 'run/*', 'tmp/*', |
| 93 |
'.buildenv', 'var/cache/kiwi' |
| 94 |
] |
| 95 |
) |
| 96 |
self.setup.export_package_verification.assert_called_once_with( |
| 97 |
'target_dir' |
| 98 |
) |
| 99 |
self.setup.export_package_list.assert_called_once_with( |
| 100 |
'target_dir' |
| 101 |
) |
| 102 |
|
| 103 |
def teardown(self): |
| 104 |
sys.argv = argv_kiwi_tests |
| 105 |
|
| 106 |
def teardown_method(self, cls): |
| 107 |
self.teardown() |