| 1 |
from unittest.mock import ( |
| 2 |
patch, call, MagicMock |
| 3 |
) |
| 4 |
import io |
| 5 |
import unittest.mock as mock |
| 6 |
|
| 7 |
from kiwi.repository.apt import RepositoryApt |
| 8 |
|
| 9 |
|
| 10 |
class TestRepositoryApt: |
| 11 |
@patch('kiwi.repository.apt.Temporary.unmanaged_file') |
| 12 |
@patch('kiwi.repository.apt.PackageManagerTemplateAptGet') |
| 13 |
@patch('kiwi.repository.apt.Path.create') |
| 14 |
def setup(self, mock_path, mock_template, mock_temp): |
| 15 |
self.apt_conf = mock.Mock() |
| 16 |
mock_template.return_value = self.apt_conf |
| 17 |
|
| 18 |
template = mock.Mock() |
| 19 |
self.apt_conf.get_host_template.return_value = template |
| 20 |
|
| 21 |
tmpfile = mock.Mock() |
| 22 |
tmpfile.name = 'tmpfile' |
| 23 |
mock_temp.return_value = tmpfile |
| 24 |
root_bind = mock.Mock() |
| 25 |
root_bind.root_dir = '../data' |
| 26 |
root_bind.shared_location = '/shared-dir' |
| 27 |
|
| 28 |
with patch('builtins.open', create=True): |
| 29 |
self.repo = RepositoryApt( |
| 30 |
root_bind, custom_args=['exclude_docs'] |
| 31 |
) |
| 32 |
|
| 33 |
self.exclude_docs = True |
| 34 |
self.apt_conf.get_host_template.assert_called_once_with( |
| 35 |
self.exclude_docs |
| 36 |
) |
| 37 |
template.substitute.assert_called_once_with( |
| 38 |
{ |
| 39 |
'apt_shared_base': '/shared-dir/apt-get', |
| 40 |
'unauthenticated': 'true' |
| 41 |
} |
| 42 |
) |
| 43 |
repo = RepositoryApt( |
| 44 |
root_bind, custom_args=['check_signatures', '_target_arch%amd64'] |
| 45 |
) |
| 46 |
assert repo.custom_args == [] |
| 47 |
assert repo.unauthenticated == 'false' |
| 48 |
|
| 49 |
repo = RepositoryApt(root_bind) |
| 50 |
assert repo.custom_args == [] |
| 51 |
assert repo.unauthenticated == 'true' |
| 52 |
|
| 53 |
@patch('kiwi.repository.apt.Temporary.unmanaged_file') |
| 54 |
@patch('kiwi.repository.apt.PackageManagerTemplateAptGet') |
| 55 |
@patch('kiwi.repository.apt.Path.create') |
| 56 |
def setup_method(self, cls, mock_path, mock_template, mock_temp): |
| 57 |
self.setup() |
| 58 |
|
| 59 |
def test_use_default_location(self): |
| 60 |
template = mock.Mock() |
| 61 |
template.substitute.return_value = 'template-data' |
| 62 |
self.apt_conf.get_image_template.return_value = template |
| 63 |
with patch('builtins.open', create=True): |
| 64 |
self.repo.use_default_location() |
| 65 |
assert self.repo.shared_apt_get_dir['sources-dir'] == \ |
| 66 |
'../data/etc/apt/sources.list.d' |
| 67 |
assert self.repo.shared_apt_get_dir['preferences-dir'] == \ |
| 68 |
'../data/etc/apt/preferences.d' |
| 69 |
assert self.repo.shared_apt_get_dir['netrcparts-dir'] == \ |
| 70 |
'../data/etc/apt/auth.conf.d' |
| 71 |
self.apt_conf.get_image_template.assert_called_once_with( |
| 72 |
self.exclude_docs |
| 73 |
) |
| 74 |
template.substitute.assert_called_once_with( |
| 75 |
{'apt_shared_base': '../data/etc/apt', 'unauthenticated': 'true'} |
| 76 |
) |
| 77 |
|
| 78 |
def test_runtime_config(self): |
| 79 |
assert self.repo.runtime_config()['apt_get_args'] == \ |
| 80 |
self.repo.apt_get_args |
| 81 |
assert self.repo.runtime_config()['command_env'] == \ |
| 82 |
self.repo.command_env |
| 83 |
|
| 84 |
def test_setup_package_database_configuration(self): |
| 85 |
# just pass |
| 86 |
self.repo.setup_package_database_configuration() |
| 87 |
|
| 88 |
@patch('os.path.exists') |
| 89 |
@patch('kiwi.command.Command.run') |
| 90 |
def test_add_repo_with_priority(self, mock_Command_run, mock_exists): |
| 91 |
mock_exists.return_value = True |
| 92 |
with patch('builtins.open', create=True) as mock_open: |
| 93 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 94 |
file_handle = mock_open.return_value.__enter__.return_value |
| 95 |
self.repo.add_repo( |
| 96 |
'foo', '/srv/my-repo', 'deb', '42', 'xenial', 'a b', |
| 97 |
customization_script='custom_script' |
| 98 |
) |
| 99 |
assert mock_open.call_args_list == [ |
| 100 |
call('/shared-dir/apt-get/sources.list.d/foo.sources', 'w'), |
| 101 |
call('/shared-dir/apt-get/preferences.d/foo.pref', 'w') |
| 102 |
] |
| 103 |
assert file_handle.write.call_args_list == [ |
| 104 |
call( |
| 105 |
'Types: deb\n' |
| 106 |
'URIs: file:/srv/my-repo\n' |
| 107 |
'Suites: xenial\n' |
| 108 |
'Components: a b\n' |
| 109 |
), |
| 110 |
call('Package: *\n'), |
| 111 |
call('Pin: origin ""\n'), |
| 112 |
call('Pin-Priority: 42\n') |
| 113 |
] |
| 114 |
assert mock_Command_run.call_args_list == [ |
| 115 |
call( |
| 116 |
[ |
| 117 |
'bash', '--norc', 'custom_script', |
| 118 |
'/shared-dir/apt-get/sources.list.d/foo.sources' |
| 119 |
] |
| 120 |
), |
| 121 |
call( |
| 122 |
[ |
| 123 |
'bash', '--norc', 'custom_script', |
| 124 |
'/shared-dir/apt-get/preferences.d/foo.pref' |
| 125 |
] |
| 126 |
) |
| 127 |
] |
| 128 |
mock_exists.return_value = False |
| 129 |
with patch('builtins.open', create=True) as mock_open: |
| 130 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 131 |
file_handle = mock_open.return_value.__enter__.return_value |
| 132 |
self.repo.add_repo( |
| 133 |
'foo', |
| 134 |
'http://download.opensuse.org/repositories/V:/A:/C/Debian_9.0/', |
| 135 |
'deb', '99', 'xenial', 'a b' |
| 136 |
) |
| 137 |
assert file_handle.write.call_args_list == [ |
| 138 |
call( |
| 139 |
'Types: deb\n' |
| 140 |
'URIs: http://download.opensuse.org/repositories/V:/A:/C/Debian_9.0/\n' |
| 141 |
'Suites: xenial\n' |
| 142 |
'Components: a b\n' |
| 143 |
), |
| 144 |
call('Package: *\n'), |
| 145 |
call('Pin: origin "download.opensuse.org"\n'), |
| 146 |
call('Pin-Priority: 99\n') |
| 147 |
] |
| 148 |
|
| 149 |
@patch('os.path.exists') |
| 150 |
def test_add_repo_distribution(self, mock_exists): |
| 151 |
mock_exists.return_value = True |
| 152 |
with patch('builtins.open', create=True) as mock_open: |
| 153 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 154 |
file_handle = mock_open.return_value.__enter__.return_value |
| 155 |
self.repo.add_repo( |
| 156 |
'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial', 'a b', |
| 157 |
architectures='amd64,arm64' |
| 158 |
) |
| 159 |
file_handle.write.assert_called_once_with( |
| 160 |
'Types: deb\n' |
| 161 |
'URIs: file:/kiwi_iso_mount/uri\n' |
| 162 |
'Architectures: amd64 arm64\n' |
| 163 |
'Suites: xenial\n' |
| 164 |
'Components: a b\n' |
| 165 |
) |
| 166 |
mock_open.assert_called_once_with( |
| 167 |
'/shared-dir/apt-get/sources.list.d/foo.sources', 'w' |
| 168 |
) |
| 169 |
|
| 170 |
@patch('os.path.exists') |
| 171 |
def test_add_repo_private_with_authentication(self, mock_exists): |
| 172 |
mock_exists.return_value = False |
| 173 |
with patch('builtins.open', create=True) as mock_open: |
| 174 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 175 |
file_handle = mock_open.return_value.__enter__.return_value |
| 176 |
self.repo.add_repo( |
| 177 |
'foo', 'https://example.org/debian', 'deb', None, 'xenial', 'a b', |
| 178 |
user='some-user', secret='some-secret', |
| 179 |
architectures='amd64,arm64' |
| 180 |
) |
| 181 |
assert mock_open.call_args_list == [ |
| 182 |
call('/shared-dir/apt-get/sources.list.d/foo.sources', 'w'), |
| 183 |
call('/shared-dir/apt-get/auth.conf.d/foo.conf', 'w') |
| 184 |
] |
| 185 |
assert file_handle.write.call_args_list == [ |
| 186 |
call( |
| 187 |
'Types: deb\n' |
| 188 |
'URIs: https://example.org/debian\n' |
| 189 |
'Architectures: amd64 arm64\n' |
| 190 |
'Suites: xenial\n' |
| 191 |
'Components: a b\n' |
| 192 |
), |
| 193 |
call( |
| 194 |
'machine https://example.org/debian login ' |
| 195 |
'some-user password some-secret\n' |
| 196 |
) |
| 197 |
] |
| 198 |
file_handle.reset_mock() |
| 199 |
self.repo.add_repo( |
| 200 |
'foo', 'https://example.org/debian', 'deb', None, 'xenial', 'a b', |
| 201 |
secret='some-token', |
| 202 |
architectures='amd64,arm64' |
| 203 |
) |
| 204 |
assert file_handle.write.call_args_list == [ |
| 205 |
call( |
| 206 |
'Types: deb\n' |
| 207 |
'URIs: https://example.org/debian\n' |
| 208 |
'Architectures: amd64 arm64\n' |
| 209 |
'Suites: xenial\n' |
| 210 |
'Components: a b\n' |
| 211 |
), |
| 212 |
call( |
| 213 |
'machine https://example.org/debian login ' |
| 214 |
'some-token\n' |
| 215 |
) |
| 216 |
] |
| 217 |
|
| 218 |
@patch('os.path.exists') |
| 219 |
def test_add_repo_distribution_without_gpgchecks(self, mock_exists): |
| 220 |
mock_exists.return_value = True |
| 221 |
with patch('builtins.open', create=True) as mock_open: |
| 222 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 223 |
file_handle = mock_open.return_value.__enter__.return_value |
| 224 |
self.repo.add_repo( |
| 225 |
'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial', 'a b', |
| 226 |
repo_gpgcheck=False, pkg_gpgcheck=False |
| 227 |
) |
| 228 |
file_handle.write.assert_called_once_with( |
| 229 |
'Types: deb\n' |
| 230 |
'URIs: file:/kiwi_iso_mount/uri\n' |
| 231 |
'Suites: xenial\n' |
| 232 |
'Components: a b\n' |
| 233 |
'trusted: yes\n' |
| 234 |
'check-valid-until: no\n' |
| 235 |
) |
| 236 |
mock_open.assert_called_once_with( |
| 237 |
'/shared-dir/apt-get/sources.list.d/foo.sources', 'w' |
| 238 |
) |
| 239 |
|
| 240 |
@patch('os.path.exists') |
| 241 |
def test_add_repo_distribution_default_component(self, mock_exists): |
| 242 |
mock_exists.return_value = True |
| 243 |
with patch('builtins.open', create=True) as mock_open: |
| 244 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 245 |
file_handle = mock_open.return_value.__enter__.return_value |
| 246 |
self.repo.add_repo( |
| 247 |
'foo', '/kiwi_iso_mount/uri', 'deb', None, 'xenial' |
| 248 |
) |
| 249 |
file_handle.write.assert_called_once_with( |
| 250 |
'Types: deb\n' |
| 251 |
'URIs: file:/kiwi_iso_mount/uri\n' |
| 252 |
'Suites: xenial\n' |
| 253 |
'Components: main\n' |
| 254 |
) |
| 255 |
mock_open.assert_called_once_with( |
| 256 |
'/shared-dir/apt-get/sources.list.d/foo.sources', 'w' |
| 257 |
) |
| 258 |
|
| 259 |
@patch('os.path.exists') |
| 260 |
def test_add_repo_flat(self, mock_exists): |
| 261 |
mock_exists.return_value = False |
| 262 |
with patch('builtins.open', create=True) as mock_open: |
| 263 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 264 |
file_handle = mock_open.return_value.__enter__.return_value |
| 265 |
self.repo.add_repo( |
| 266 |
'foo', 'http://repo.com', 'deb' |
| 267 |
) |
| 268 |
file_handle.write.assert_called_once_with( |
| 269 |
'Types: deb\n' |
| 270 |
'URIs: http://repo.com\n' |
| 271 |
'Suites: ./\n' |
| 272 |
) |
| 273 |
mock_open.assert_called_once_with( |
| 274 |
'/shared-dir/apt-get/sources.list.d/foo.sources', 'w' |
| 275 |
) |
| 276 |
|
| 277 |
@patch('kiwi.repository.apt.os.unlink') |
| 278 |
@patch('kiwi.repository.apt.os.path.exists') |
| 279 |
@patch('kiwi.repository.apt.Command.run') |
| 280 |
def test_import_trusted_keys(self, mock_run, mock_exists, mock_unlink): |
| 281 |
mock_exists.return_value = True |
| 282 |
self.repo.import_trusted_keys(['key-file-a.asc', 'key-file-b.asc']) |
| 283 |
assert mock_run.call_args_list == [ |
| 284 |
call([ |
| 285 |
'gpg', '--no-options', '--no-default-keyring', |
| 286 |
'--no-auto-check-trustdb', '--trust-model', 'always', |
| 287 |
'--keyring', '/shared-dir/apt-get/trusted-keybox.gpg', |
| 288 |
'--import', '--ignore-time-conflict', 'key-file-a.asc' |
| 289 |
]), call([ |
| 290 |
'gpg', '--no-options', '--no-default-keyring', |
| 291 |
'--no-auto-check-trustdb', '--trust-model', 'always', |
| 292 |
'--keyring', '/shared-dir/apt-get/trusted-keybox.gpg', |
| 293 |
'--import', '--ignore-time-conflict', 'key-file-b.asc' |
| 294 |
]), call([ |
| 295 |
'gpg', '--no-options', '--no-default-keyring', |
| 296 |
'--no-auto-check-trustdb', '--trust-model', 'always', |
| 297 |
'--keyring', '/shared-dir/apt-get/trusted-keybox.gpg', |
| 298 |
'--export', '--yes', '--output', |
| 299 |
'/shared-dir/apt-get/trusted.gpg' |
| 300 |
]) |
| 301 |
] |
| 302 |
|
| 303 |
@patch('kiwi.path.Path.wipe') |
| 304 |
def test_delete_repo(self, mock_wipe): |
| 305 |
self.repo.delete_repo('foo') |
| 306 |
assert mock_wipe.call_args_list == [ |
| 307 |
call('/shared-dir/apt-get/sources.list.d/foo.sources'), |
| 308 |
call('/shared-dir/apt-get/auth.conf.d/foo.conf'), |
| 309 |
call('/shared-dir/apt-get/preferences.d/foo.pref') |
| 310 |
] |
| 311 |
|
| 312 |
@patch('kiwi.path.Path.wipe') |
| 313 |
@patch('os.walk') |
| 314 |
def test_cleanup_unused_repos(self, mock_walk, mock_path): |
| 315 |
mock_walk.return_value = [ |
| 316 |
('/foo', ('bar', 'baz'), ('spam', 'eggs')) |
| 317 |
] |
| 318 |
self.repo.repo_names = ['eggs'] |
| 319 |
self.repo.cleanup_unused_repos() |
| 320 |
mock_path.assert_called_once_with( |
| 321 |
'/shared-dir/apt-get/sources.list.d/spam' |
| 322 |
) |
| 323 |
|
| 324 |
@patch('kiwi.path.Path.wipe') |
| 325 |
@patch('kiwi.path.Path.create') |
| 326 |
def test_delete_all_repos(self, mock_create, mock_wipe): |
| 327 |
self.repo.delete_all_repos() |
| 328 |
assert mock_wipe.call_args_list == [ |
| 329 |
call('/shared-dir/apt-get/sources.list.d'), |
| 330 |
call('/shared-dir/apt-get/auth.conf.d'), |
| 331 |
call('/shared-dir/apt-get/preferences.d') |
| 332 |
] |
| 333 |
assert mock_create.call_args_list == [ |
| 334 |
call('/shared-dir/apt-get/sources.list.d'), |
| 335 |
call('/shared-dir/apt-get/auth.conf.d'), |
| 336 |
call('/shared-dir/apt-get/preferences.d') |
| 337 |
] |
| 338 |
|
| 339 |
@patch('kiwi.path.Path.wipe') |
| 340 |
def test_delete_repo_cache(self, mock_wipe): |
| 341 |
self.repo.delete_repo_cache('foo') |
| 342 |
assert mock_wipe.call_args_list == [ |
| 343 |
call('/shared-dir/apt-get/archives'), |
| 344 |
call('/shared-dir/apt-get/pkgcache.bin'), |
| 345 |
call('/shared-dir/apt-get/srcpkgcache.bin') |
| 346 |
] |
| 347 |
|
| 348 |
@patch('os.path.isfile') |
| 349 |
@patch('os.unlink') |
| 350 |
def test_cleanup(self, mock_os_unlink, mock_os_path_isfile): |
| 351 |
self.repo.cleanup() |
| 352 |
mock_os_unlink.assert_called_once_with('tmpfile') |