| 1 |
from unittest.mock import patch |
| 2 |
from pytest import raises |
| 3 |
import unittest.mock as mock |
| 4 |
|
| 5 |
from kiwi.path import Path |
| 6 |
from kiwi.package_manager.zypper import PackageManagerZypper |
| 7 |
|
| 8 |
from kiwi.exceptions import KiwiRequestError |
| 9 |
|
| 10 |
|
| 11 |
class TestPackageManagerZypper: |
| 12 |
def setup(self): |
| 13 |
repository = mock.Mock() |
| 14 |
repository.root_dir = 'root-dir' |
| 15 |
|
| 16 |
self.command_env = { |
| 17 |
'HOME': '/home/ms', 'ZYPP_CONF': 'root-dir/my/zypp.conf' |
| 18 |
} |
| 19 |
repository.runtime_config = mock.MagicMock( |
| 20 |
return_value={ |
| 21 |
'zypper_args': ['--reposd-dir', 'root-dir/my/repos'], |
| 22 |
'command_env': self.command_env |
| 23 |
} |
| 24 |
) |
| 25 |
self.manager = PackageManagerZypper(repository) |
| 26 |
|
| 27 |
self.chroot_zypper_args = Path.move_to_root( |
| 28 |
'root-dir', self.manager.zypper_args |
| 29 |
) |
| 30 |
self.chroot_command_env = self.manager.command_env |
| 31 |
zypp_conf = self.manager.command_env['ZYPP_CONF'] |
| 32 |
self.chroot_command_env['ZYPP_CONF'] = \ |
| 33 |
Path.move_to_root('root-dir', [zypp_conf])[0] |
| 34 |
|
| 35 |
def setup_method(self, cls): |
| 36 |
self.setup() |
| 37 |
|
| 38 |
def test_request_package(self): |
| 39 |
self.manager.request_package('name') |
| 40 |
assert self.manager.package_requests == ['name'] |
| 41 |
|
| 42 |
def test_request_collection(self): |
| 43 |
self.manager.request_collection('name') |
| 44 |
assert self.manager.collection_requests == ['pattern:name'] |
| 45 |
|
| 46 |
def test_request_product(self): |
| 47 |
self.manager.request_product('name') |
| 48 |
assert self.manager.product_requests == ['product:name'] |
| 49 |
|
| 50 |
def test_request_package_exclusion(self): |
| 51 |
self.manager.request_package_exclusion('name') |
| 52 |
assert self.manager.exclude_requests == ['name'] |
| 53 |
|
| 54 |
def test_setup_repository_modules(self): |
| 55 |
self.manager.setup_repository_modules({}) |
| 56 |
|
| 57 |
@patch('kiwi.command.Command.call') |
| 58 |
def test_process_install_requests_bootstrap(self, mock_call): |
| 59 |
self.manager.request_package('vim') |
| 60 |
self.manager.process_install_requests_bootstrap() |
| 61 |
mock_call.assert_called_once_with( |
| 62 |
[ |
| 63 |
'zypper', '--reposd-dir', 'root-dir/my/repos', |
| 64 |
'--root', 'root-dir', |
| 65 |
'install', '--download', 'in-advance', |
| 66 |
'--auto-agree-with-licenses' |
| 67 |
] + self.manager.custom_args + ['--', 'vim'], self.command_env |
| 68 |
) |
| 69 |
|
| 70 |
@patch('kiwi.command.Command.call') |
| 71 |
@patch('kiwi.command.Command.run') |
| 72 |
@patch('os.path.exists') |
| 73 |
@patch('kiwi.package_manager.zypper.Path.create') |
| 74 |
def test_process_install_requests( |
| 75 |
self, mock_path, mock_exists, mock_run, mock_call |
| 76 |
): |
| 77 |
mock_exists.return_value = False |
| 78 |
self.manager.request_package('vim') |
| 79 |
self.manager.request_package_exclusion('skipme') |
| 80 |
self.manager.process_install_requests() |
| 81 |
mock_path.assert_called_once_with('root-dir/etc/zypp') |
| 82 |
mock_run.assert_called_once_with( |
| 83 |
['chroot', 'root-dir', 'zypper'] + self.chroot_zypper_args + [ |
| 84 |
'al' |
| 85 |
] + self.manager.custom_args + ['skipme'], self.chroot_command_env |
| 86 |
) |
| 87 |
mock_call.assert_called_once_with( |
| 88 |
['chroot', 'root-dir', 'zypper'] + self.chroot_zypper_args + [ |
| 89 |
'install', '--download', 'in-advance', |
| 90 |
'--auto-agree-with-licenses' |
| 91 |
] + self.manager.custom_args + ['--', 'vim'], |
| 92 |
self.chroot_command_env |
| 93 |
) |
| 94 |
|
| 95 |
@patch('kiwi.command.Command.call') |
| 96 |
@patch('kiwi.command.Command.run') |
| 97 |
def test_process_delete_requests_all_installed(self, mock_run, mock_call): |
| 98 |
self.manager.request_package('vim') |
| 99 |
self.manager.process_delete_requests() |
| 100 |
mock_call.assert_called_once_with( |
| 101 |
['chroot', 'root-dir', 'zypper'] + self.chroot_zypper_args + [ |
| 102 |
'remove', '-u', '--force-resolution' |
| 103 |
] + self.manager.custom_args + ['vim'], self.chroot_command_env |
| 104 |
) |
| 105 |
|
| 106 |
@patch('kiwi.command.Command.call') |
| 107 |
@patch('kiwi.command.Command.run') |
| 108 |
def test_process_delete_requests_force(self, mock_run, mock_call): |
| 109 |
self.manager.request_package('vim') |
| 110 |
self.manager.process_delete_requests(True) |
| 111 |
mock_call.assert_called_once_with( |
| 112 |
[ |
| 113 |
'chroot', 'root-dir', 'rpm', '-e', |
| 114 |
'--nodeps', '--allmatches', '--noscripts', 'vim' |
| 115 |
], self.command_env |
| 116 |
) |
| 117 |
|
| 118 |
@patch('kiwi.command.Command.run') |
| 119 |
@patch('kiwi.command.Command.call') |
| 120 |
def test_process_delete_requests_package_missing(self, mock_call, mock_run): |
| 121 |
mock_run.side_effect = Exception |
| 122 |
self.manager.request_package('vim') |
| 123 |
with raises(KiwiRequestError): |
| 124 |
self.manager.process_delete_requests(force=True) |
| 125 |
mock_run.assert_called_once_with( |
| 126 |
['chroot', 'root-dir', 'rpm', '-q', 'vim'] |
| 127 |
) |
| 128 |
|
| 129 |
@patch('kiwi.command.Command.call') |
| 130 |
def test_update(self, mock_call): |
| 131 |
self.manager.update() |
| 132 |
mock_call.assert_called_once_with( |
| 133 |
['chroot', 'root-dir', 'zypper'] + self.chroot_zypper_args + [ |
| 134 |
'update', '--download', 'in-advance', |
| 135 |
'--auto-agree-with-licenses' |
| 136 |
] + self.manager.custom_args, self.chroot_command_env |
| 137 |
) |
| 138 |
|
| 139 |
def test_process_only_required(self): |
| 140 |
self.manager.process_only_required() |
| 141 |
assert self.manager.custom_args == ['--no-recommends'] |
| 142 |
|
| 143 |
def test_process_plus_recommended(self): |
| 144 |
self.manager.process_only_required() |
| 145 |
assert self.manager.custom_args == ['--no-recommends'] |
| 146 |
self.manager.process_plus_recommended() |
| 147 |
assert '--no-recommends' not in self.manager.custom_args |
| 148 |
|
| 149 |
def test_match_package_installed(self): |
| 150 |
assert self.manager.match_package_installed('foo', 'Installing: foo') |
| 151 |
|
| 152 |
def test_match_package_deleted(self): |
| 153 |
assert self.manager.match_package_deleted('foo', 'Removing: foo') |
| 154 |
|
| 155 |
@patch('kiwi.package_manager.zypper.RpmDataBase') |
| 156 |
def test_post_process_install_requests_bootstrap(self, mock_RpmDataBase): |
| 157 |
rpmdb = mock.Mock() |
| 158 |
rpmdb.has_rpm.return_value = True |
| 159 |
mock_RpmDataBase.return_value = rpmdb |
| 160 |
self.manager.post_process_install_requests_bootstrap() |
| 161 |
rpmdb.rebuild_database.assert_called_once_with() |
| 162 |
rpmdb.set_database_to_image_path.assert_called_once_with() |
| 163 |
|
| 164 |
def test_has_failed(self): |
| 165 |
assert self.manager.has_failed(0) is False |
| 166 |
assert self.manager.has_failed(102) is False |
| 167 |
assert self.manager.has_failed(100) is False |
| 168 |
assert self.manager.has_failed(104) is True |
| 169 |
assert self.manager.has_failed(105) is True |
| 170 |
assert self.manager.has_failed(106) is True |
| 171 |
assert self.manager.has_failed(1) is True |
| 172 |
assert self.manager.has_failed(4) is True |
| 173 |
assert self.manager.has_failed(-42) is True |
| 174 |
assert self.manager.has_failed(127) is True |
| 175 |
|
| 176 |
@patch('kiwi.package_manager.zypper.os.unlink') |
| 177 |
@patch('kiwi.package_manager.zypper.os.path.exists') |
| 178 |
@patch('kiwi.package_manager.zypper.Rpm') |
| 179 |
def test_clean_leftovers(self, mock_rpm, mock_exists, mock_unlink): |
| 180 |
mock_exists.return_value = True |
| 181 |
mock_rpm.return_value = mock.Mock() |
| 182 |
self.manager.clean_leftovers() |
| 183 |
mock_rpm.assert_called_once_with( |
| 184 |
'root-dir', 'macros.kiwi-image-config' |
| 185 |
) |
| 186 |
mock_rpm.return_value.wipe_config.assert_called_once_with() |
| 187 |
mock_exists.assert_called_once_with( |
| 188 |
'root-dir/var/lib/zypp/AnonymousUniqueId' |
| 189 |
) |
| 190 |
mock_unlink.assert_called_once_with( |
| 191 |
'root-dir/var/lib/zypp/AnonymousUniqueId' |
| 192 |
) |