| 1 |
|
| 2 |
from unittest.mock import ( |
| 3 |
patch, Mock, call, mock_open |
| 4 |
) |
| 5 |
|
| 6 |
import os |
| 7 |
|
| 8 |
from kiwi.repository.pacman import RepositoryPacman |
| 9 |
|
| 10 |
|
| 11 |
class TestRepositorPacman(object): |
| 12 |
@patch('kiwi.repository.pacman.Temporary.unmanaged_file') |
| 13 |
@patch('kiwi.repository.pacman.ConfigParser') |
| 14 |
@patch('kiwi.repository.pacman.Path.create') |
| 15 |
def setup(self, mock_path, mock_config, mock_temp): |
| 16 |
runtime_pacman_config = Mock() |
| 17 |
mock_config.return_value = runtime_pacman_config |
| 18 |
tmpfile = Mock() |
| 19 |
tmpfile.name = 'tmpfile' |
| 20 |
mock_temp.return_value = tmpfile |
| 21 |
root_bind = Mock() |
| 22 |
root_bind.move_to_root = Mock( |
| 23 |
return_value=['root-moved-arguments'] |
| 24 |
) |
| 25 |
root_bind.root_dir = '../data' |
| 26 |
root_bind.shared_location = '/shared-dir' |
| 27 |
with patch('builtins.open', create=True): |
| 28 |
self.repo = RepositoryPacman(root_bind) |
| 29 |
|
| 30 |
assert runtime_pacman_config.set.call_args_list == [ |
| 31 |
call('options', 'Architecture', 'auto'), |
| 32 |
call('options', 'CacheDir', '/shared-dir/pacman/cache'), |
| 33 |
call('options', 'SigLevel', 'Never DatabaseNever'), |
| 34 |
call('options', 'LocalFileSigLevel', 'Never DatabaseNever'), |
| 35 |
call('options', 'Include', '/shared-dir/pacman/repos/*.repo') |
| 36 |
] |
| 37 |
|
| 38 |
runtime_pacman_config.reset_mock() |
| 39 |
|
| 40 |
RepositoryPacman( |
| 41 |
root_bind, custom_args=['check_signatures', '_target_arch%x86_64'] |
| 42 |
) |
| 43 |
|
| 44 |
assert runtime_pacman_config.set.call_args_list == [ |
| 45 |
call('options', 'Architecture', 'auto'), |
| 46 |
call('options', 'CacheDir', '/shared-dir/pacman/cache'), |
| 47 |
call('options', 'SigLevel', 'Required DatabaseRequired'), |
| 48 |
call( |
| 49 |
'options', 'LocalFileSigLevel', 'Required DatabaseRequired' |
| 50 |
), |
| 51 |
call('options', 'Include', '/shared-dir/pacman/repos/*.repo') |
| 52 |
] |
| 53 |
|
| 54 |
@patch('kiwi.repository.pacman.Temporary.unmanaged_file') |
| 55 |
@patch('kiwi.repository.pacman.ConfigParser') |
| 56 |
@patch('kiwi.repository.pacman.Path.create') |
| 57 |
def setup_method(self, cls, mock_path, mock_config, mock_temp): |
| 58 |
self.setup() |
| 59 |
|
| 60 |
def test_runtime_config(self): |
| 61 |
assert self.repo.runtime_config()['pacman_args'] == \ |
| 62 |
self.repo.pacman_args |
| 63 |
assert self.repo.runtime_config()['command_env'] == \ |
| 64 |
os.environ |
| 65 |
|
| 66 |
@patch('kiwi.repository.pacman.ConfigParser') |
| 67 |
@patch('os.path.exists') |
| 68 |
@patch('kiwi.command.Command.run') |
| 69 |
def test_add_local_repo_with_components( |
| 70 |
self, mock_Command_run, mock_exists, mock_config |
| 71 |
): |
| 72 |
repo_config = Mock() |
| 73 |
mock_config.return_value = repo_config |
| 74 |
mock_exists.return_value = True |
| 75 |
|
| 76 |
m_open = mock_open() |
| 77 |
with patch('builtins.open', m_open, create=True): |
| 78 |
self.repo.add_repo( |
| 79 |
'foo', '/some_uri', components='core extra', |
| 80 |
repo_gpgcheck=False, pkg_gpgcheck=True, |
| 81 |
customization_script='custom_script' |
| 82 |
) |
| 83 |
m_open.assert_called_once_with( |
| 84 |
'/shared-dir/pacman/repos/foo.repo', 'w' |
| 85 |
) |
| 86 |
|
| 87 |
assert repo_config.add_section.call_args_list == [ |
| 88 |
call('core'), call('extra') |
| 89 |
] |
| 90 |
assert repo_config.set.call_args_list == [ |
| 91 |
call('core', 'Server', 'file:///some_uri'), |
| 92 |
call('core', 'SigLevel', 'Required DatabaseNever'), |
| 93 |
call('extra', 'Server', 'file:///some_uri'), |
| 94 |
call('extra', 'SigLevel', 'Required DatabaseNever'), |
| 95 |
] |
| 96 |
mock_Command_run.assert_called_once_with( |
| 97 |
[ |
| 98 |
'bash', '--norc', 'custom_script', |
| 99 |
'/shared-dir/pacman/repos/foo.repo' |
| 100 |
] |
| 101 |
) |
| 102 |
|
| 103 |
@patch('kiwi.repository.pacman.ConfigParser') |
| 104 |
@patch('os.path.exists') |
| 105 |
def test_add_repo_with_components( |
| 106 |
self, mock_exists, mock_config |
| 107 |
): |
| 108 |
repo_config = Mock() |
| 109 |
mock_config.return_value = repo_config |
| 110 |
mock_exists.return_value = True |
| 111 |
|
| 112 |
m_open = mock_open() |
| 113 |
with patch('builtins.open', m_open, create=True): |
| 114 |
self.repo.add_repo( |
| 115 |
'foo', 'some_uri', components='core extra', |
| 116 |
repo_gpgcheck=False, pkg_gpgcheck=True |
| 117 |
) |
| 118 |
m_open.assert_called_once_with( |
| 119 |
'/shared-dir/pacman/repos/foo.repo', 'w' |
| 120 |
) |
| 121 |
|
| 122 |
assert repo_config.add_section.call_args_list == [ |
| 123 |
call('core'), call('extra') |
| 124 |
] |
| 125 |
assert repo_config.set.call_args_list == [ |
| 126 |
call('core', 'Server', 'some_uri'), |
| 127 |
call('core', 'SigLevel', 'Required DatabaseNever'), |
| 128 |
call('extra', 'Server', 'some_uri'), |
| 129 |
call('extra', 'SigLevel', 'Required DatabaseNever'), |
| 130 |
] |
| 131 |
|
| 132 |
@patch('kiwi.repository.pacman.ConfigParser') |
| 133 |
@patch('os.path.exists') |
| 134 |
def test_add_repo_without_components( |
| 135 |
self, mock_exists, mock_config |
| 136 |
): |
| 137 |
repo_config = Mock() |
| 138 |
mock_config.return_value = repo_config |
| 139 |
mock_exists.return_value = True |
| 140 |
|
| 141 |
m_open = mock_open() |
| 142 |
with patch('builtins.open', m_open, create=True): |
| 143 |
self.repo.add_repo('foo', 'some_uri', pkg_gpgcheck=False) |
| 144 |
|
| 145 |
m_open.assert_called_once_with( |
| 146 |
'/shared-dir/pacman/repos/foo.repo', 'w' |
| 147 |
) |
| 148 |
|
| 149 |
assert repo_config.add_section.call_args_list == [ |
| 150 |
call('foo') |
| 151 |
] |
| 152 |
assert repo_config.set.call_args_list == [ |
| 153 |
call('foo', 'Server', 'some_uri'), |
| 154 |
call('foo', 'SigLevel', 'Never DatabaseNever'), |
| 155 |
] |
| 156 |
|
| 157 |
@patch('kiwi.repository.pacman.Path.create') |
| 158 |
def test_setup_package_database_configuration(self, mock_path_create): |
| 159 |
self.repo.setup_package_database_configuration() |
| 160 |
mock_path_create.assert_called_once_with( |
| 161 |
'../data/var/lib/pacman' |
| 162 |
) |
| 163 |
|
| 164 |
@patch('kiwi.repository.pacman.os.path.exists') |
| 165 |
@patch('kiwi.repository.pacman.Command.run') |
| 166 |
def test_import_trusted_keys(self, mock_cmd, mock_exists): |
| 167 |
exists = [True, False] |
| 168 |
mock_exists.side_effect = lambda x: exists.pop() |
| 169 |
signing_keys = ['key-file-a.asc', 'key-file-b_ID'] |
| 170 |
self.repo.import_trusted_keys(signing_keys) |
| 171 |
assert mock_cmd.call_args_list == [ |
| 172 |
call(['pacman-key', '--add', 'key-file-a.asc']), |
| 173 |
call(['pacman-key', '--recv-keys', 'key-file-b_ID']) |
| 174 |
] |
| 175 |
|
| 176 |
@patch('kiwi.repository.pacman.Path') |
| 177 |
def test_delete_repo(self, mock_path): |
| 178 |
self.repo.delete_repo('foo') |
| 179 |
mock_path.wipe.assert_called_once_with( |
| 180 |
'/shared-dir/pacman/repos/foo.repo' |
| 181 |
) |
| 182 |
|
| 183 |
@patch('kiwi.repository.pacman.Path') |
| 184 |
def test_delete_all_repos(self, mock_path): |
| 185 |
self.repo.delete_all_repos() |
| 186 |
mock_path.wipe.assert_called_once_with('/shared-dir/pacman/repos') |
| 187 |
mock_path.create.assert_called_once_with('/shared-dir/pacman/repos') |
| 188 |
|
| 189 |
@patch('kiwi.repository.pacman.Path.wipe') |
| 190 |
@patch('kiwi.repository.pacman.os.walk') |
| 191 |
def test_cleanup_unused_repos(self, mock_os_walk, mock_wipe): |
| 192 |
mock_os_walk.return_value = [( |
| 193 |
'/shared-dir/pacman/repos', |
| 194 |
[], |
| 195 |
['foo.repo', 'bar.repo'] |
| 196 |
)] |
| 197 |
self.repo.cleanup_unused_repos() |
| 198 |
mock_os_walk.assert_called_once_with('/shared-dir/pacman/repos') |
| 199 |
assert mock_wipe.call_args_list == [ |
| 200 |
call('/shared-dir/pacman/repos/foo.repo'), |
| 201 |
call('/shared-dir/pacman/repos/bar.repo') |
| 202 |
] |
| 203 |
|
| 204 |
def test_use_default_location(self): |
| 205 |
self.repo.use_default_location() |
| 206 |
assert self.repo.shared_pacman_dir['cache-dir'] == \ |
| 207 |
'/shared-dir/pacman/cache' |
| 208 |
assert self.repo.shared_pacman_dir['repos-dir'] == \ |
| 209 |
'../data/etc/pacman.d' |
| 210 |
|
| 211 |
@patch('kiwi.repository.pacman.Path') |
| 212 |
def test_delete_repo_cache(self, mock_path): |
| 213 |
self.repo.delete_repo_cache('foo') |
| 214 |
mock_path.wipe.assert_called_once_with('/shared-dir/pacman/cache') |
| 215 |
|
| 216 |
@patch('os.path.isfile') |
| 217 |
@patch('os.unlink') |
| 218 |
def test_cleanup(self, mock_os_unlink, mock_os_path_isfile): |
| 219 |
self.repo.cleanup() |
| 220 |
mock_os_unlink.assert_called_once_with('tmpfile') |