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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
|
# This file is part of cloud-init. See LICENSE file for license information.
"""test_handler_apt_source_v1
Testing various config variations of the apt_source config
This calls all things with v1 format to stress the conversion code on top of
the actually tested code.
"""
import os
import pathlib
import re
from functools import partial
from textwrap import dedent
from unittest import mock
from unittest.mock import call
import pytest
from cloudinit import subp, util
from cloudinit.config import cc_apt_configure
from cloudinit.subp import SubpResult
from tests.unittests.util import get_cloud
original_join = os.path.join
EXPECTEDKEY = """-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1
mI0ESuZLUgEEAKkqq3idtFP7g9hzOu1a8+v8ImawQN4TrvlygfScMU1TIS1eC7UQ
NUA8Qqgr9iUaGnejb0VciqftLrU9D6WYHSKz+EITefgdyJ6SoQxjoJdsCpJ7o9Jy
8PQnpRttiFm4qHu6BVnKnBNxw/z3ST9YMqW5kbMQpfxbGe+obRox59NpABEBAAG0
HUxhdW5jaHBhZCBQUEEgZm9yIFNjb3R0IE1vc2VyiLYEEwECACAFAkrmS1ICGwMG
CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRAGILvPA2g/d3aEA/9tVjc10HOZwV29
OatVuTeERjjrIbxflO586GLA8cp0C9RQCwgod/R+cKYdQcHjbqVcP0HqxveLg0RZ
FJpWLmWKamwkABErwQLGlM/Hwhjfade8VvEQutH5/0JgKHmzRsoqfR+LMO6OS+Sm
S0ORP6HXET3+jC8BMG4tBWCTK/XEZw==
=ACB2
-----END PGP PUBLIC KEY BLOCK-----"""
ADD_APT_REPO_MATCH = r"^[\w-]+:\w"
class FakeDistro:
"""Fake Distro helper object"""
def update_package_sources(self, *, force=False):
"""Fake update_package_sources helper method"""
return
class TestAptSourceConfig:
"""TestAptSourceConfig
Main Class to test apt_source configs
"""
release = "fantastic"
matcher = re.compile(ADD_APT_REPO_MATCH).search
@pytest.fixture
def apt_lists(self, tmpdir):
p1 = os.path.join(tmpdir, "single-deb.list")
p2 = os.path.join(tmpdir, "single-deb2.list")
p3 = os.path.join(tmpdir, "single-deb3.list")
return p1, p2, p3
@pytest.fixture
def fallback_path(self, tmpdir):
return os.path.join(
tmpdir, "etc/apt/sources.list.d/", "cloud_config_sources.list"
)
@pytest.fixture(autouse=True)
def common_mocks(self, mocker):
mocker.patch(
"cloudinit.util.lsb_release",
return_value={"codename": self.release},
)
mocker.patch(
"cloudinit.util.get_dpkg_architecture", return_value="amd64"
)
mocker.patch.object(
subp, "subp", return_value=SubpResult("PPID PID", "")
)
mocker.patch("cloudinit.config.cc_apt_configure._ensure_dependencies")
def _get_default_params(self):
"""get_default_params
Get the most basic default mrror and release info to be used in tests
"""
return {
"RELEASE": self.release,
"MIRROR": "http://archive.ubuntu.com/ubuntu",
}
def wrapv1conf(self, cfg):
params = self._get_default_params()
# old v1 list format under old keys, but callabe to main handler
# disable source.list rendering and set mirror to avoid other code
return {
"apt_preserve_sources_list": True,
"apt_mirror": params["MIRROR"],
"apt_sources": cfg,
}
def myjoin(self, tmpfile, *args, **kwargs):
"""myjoin - redir into writable tmpdir"""
if (
args[0] == "/etc/apt/sources.list.d/"
and args[1] == "cloud_config_sources.list"
and len(args) == 2
):
return original_join(tmpfile, args[0].lstrip("/"), args[1])
else:
return original_join(*args, **kwargs)
def apt_src_basic(self, filename, cfg, gpg):
"""apt_src_basic
Test Fix deb source string, has to overwrite mirror conf in params
"""
cfg = self.wrapv1conf(cfg)
with mock.patch.object(cc_apt_configure, "GPG") as my_gpg:
my_gpg.return_value = gpg
cc_apt_configure.handle("test", cfg, get_cloud(), [])
assert os.path.isfile(filename)
contents = util.load_text_file(filename)
assert re.search(
r"%s %s %s %s\n"
% (
"deb",
"http://archive.ubuntu.com/ubuntu",
"karmic-backports",
"main universe multiverse restricted",
),
contents,
flags=re.IGNORECASE,
)
def test_apt_src_basic(self, apt_lists, m_gpg):
"""Test deb source string, overwrite mirror and filename"""
cfg = {
"source": (
"deb http://archive.ubuntu.com/ubuntu"
" karmic-backports"
" main universe multiverse restricted"
),
"filename": apt_lists[0],
}
self.apt_src_basic(apt_lists[0], [cfg], m_gpg)
def test_apt_src_basic_dict(self, apt_lists, m_gpg):
"""Test deb source string, overwrite mirror and filename (dict)"""
cfg = {
apt_lists[0]: {
"source": (
"deb http://archive.ubuntu.com/ubuntu"
" karmic-backports"
" main universe multiverse restricted"
)
}
}
self.apt_src_basic(apt_lists[0], cfg, m_gpg)
def apt_src_basic_tri(self, cfg, apt_lists, m_gpg):
"""apt_src_basic_tri
Test Fix three deb source string, has to overwrite mirror conf in
params. Test with filenames provided in config.
generic part to check three files with different content
"""
self.apt_src_basic(apt_lists[0], cfg, m_gpg)
# extra verify on two extra files of this test
contents = util.load_text_file(apt_lists[1])
assert re.search(
r"%s %s %s %s\n"
% (
"deb",
"http://archive.ubuntu.com/ubuntu",
"precise-backports",
"main universe multiverse restricted",
),
contents,
flags=re.IGNORECASE,
)
contents = util.load_text_file(apt_lists[2])
assert re.search(
r"%s %s %s %s\n"
% (
"deb",
"http://archive.ubuntu.com/ubuntu",
"lucid-backports",
"main universe multiverse restricted",
),
contents,
flags=re.IGNORECASE,
)
def test_apt_src_basic_tri(self, apt_lists, m_gpg):
"""Test Fix three deb source string with filenames"""
cfg1 = {
"source": (
"deb http://archive.ubuntu.com/ubuntu"
" karmic-backports"
" main universe multiverse restricted"
),
"filename": apt_lists[0],
}
cfg2 = {
"source": (
"deb http://archive.ubuntu.com/ubuntu"
" precise-backports"
" main universe multiverse restricted"
),
"filename": apt_lists[1],
}
cfg3 = {
"source": (
"deb http://archive.ubuntu.com/ubuntu"
" lucid-backports"
" main universe multiverse restricted"
),
"filename": apt_lists[2],
}
self.apt_src_basic_tri([cfg1, cfg2, cfg3], apt_lists, m_gpg)
def test_apt_src_basic_dict_tri(self, apt_lists, m_gpg):
"""Test Fix three deb source string with filenames (dict)"""
cfg = {
apt_lists[0]: {
"source": (
"deb http://archive.ubuntu.com/ubuntu"
" karmic-backports"
" main universe multiverse restricted"
)
},
apt_lists[1]: {
"source": (
"deb http://archive.ubuntu.com/ubuntu"
" precise-backports"
" main universe multiverse restricted"
)
},
apt_lists[2]: {
"source": (
"deb http://archive.ubuntu.com/ubuntu"
" lucid-backports"
" main universe multiverse restricted"
)
},
}
self.apt_src_basic_tri(cfg, apt_lists, m_gpg)
def test_apt_src_basic_nofn(self, fallback_path, tmpdir, m_gpg):
"""Test Fix three deb source string without filenames (dict)"""
cfg = {
"source": (
"deb http://archive.ubuntu.com/ubuntu"
" karmic-backports"
" main universe multiverse restricted"
)
}
with mock.patch.object(
os.path, "join", side_effect=partial(self.myjoin, tmpdir)
):
self.apt_src_basic(fallback_path, [cfg], m_gpg)
def apt_src_replacement(self, filename, cfg):
"""apt_src_replace
Test Autoreplacement of MIRROR and RELEASE in source specs
"""
cfg = self.wrapv1conf(cfg)
params = self._get_default_params()
cc_apt_configure.handle("test", cfg, get_cloud(), [])
assert os.path.isfile(filename)
contents = util.load_text_file(filename)
assert re.search(
r"%s %s %s %s\n"
% ("deb", params["MIRROR"], params["RELEASE"], "multiverse"),
contents,
flags=re.IGNORECASE,
)
def test_apt_src_replace(self, apt_lists):
"""Test Autoreplacement of MIRROR and RELEASE in source specs"""
cfg = {
"source": "deb $MIRROR $RELEASE multiverse",
"filename": apt_lists[0],
}
self.apt_src_replacement(apt_lists[0], [cfg])
def apt_src_replace_tri(self, cfg, apt_lists):
"""apt_src_replace_tri
Test three autoreplacements of MIRROR and RELEASE in source specs with
generic part
"""
self.apt_src_replacement(apt_lists[0], cfg)
# extra verify on two extra files of this test
params = self._get_default_params()
contents = util.load_text_file(apt_lists[1])
assert re.search(
r"%s %s %s %s\n"
% ("deb", params["MIRROR"], params["RELEASE"], "main"),
contents,
flags=re.IGNORECASE,
)
contents = util.load_text_file(apt_lists[2])
assert re.search(
r"%s %s %s %s\n"
% ("deb", params["MIRROR"], params["RELEASE"], "universe"),
contents,
flags=re.IGNORECASE,
)
def test_apt_src_replace_tri(self, apt_lists):
"""Test triple Autoreplacement of MIRROR and RELEASE in source specs"""
cfg1 = {
"source": "deb $MIRROR $RELEASE multiverse",
"filename": apt_lists[0],
}
cfg2 = {
"source": "deb $MIRROR $RELEASE main",
"filename": apt_lists[1],
}
cfg3 = {
"source": "deb $MIRROR $RELEASE universe",
"filename": apt_lists[2],
}
self.apt_src_replace_tri([cfg1, cfg2, cfg3], apt_lists)
def test_apt_src_replace_dict_tri(self, apt_lists):
"""Test triple Autoreplacement in source specs (dict)"""
cfg = {
apt_lists[0]: {"source": "deb $MIRROR $RELEASE multiverse"},
"notused": {
"source": "deb $MIRROR $RELEASE main",
"filename": apt_lists[1],
},
apt_lists[2]: {"source": "deb $MIRROR $RELEASE universe"},
}
self.apt_src_replace_tri(cfg, apt_lists)
def test_apt_src_replace_nofn(self, fallback_path, tmpdir):
"""Test Autoreplacement of MIRROR and RELEASE in source specs nofile"""
cfg = {"source": "deb $MIRROR $RELEASE multiverse"}
with mock.patch.object(
os.path, "join", side_effect=partial(self.myjoin, tmpdir)
):
self.apt_src_replacement(fallback_path, [cfg])
def apt_src_keyid(self, filename, cfg, keynum, gpg):
"""apt_src_keyid
Test specification of a source + keyid
"""
cfg = self.wrapv1conf(cfg)
cloud = get_cloud()
with mock.patch.object(
cc_apt_configure, "GPG"
) as this_gpg, mock.patch.object(
cc_apt_configure, "add_apt_key"
) as mockobj:
this_gpg.return_value = gpg
cc_apt_configure.handle("test", cfg, cloud, [])
# check if it added the right number of keys
calls = []
sources = cfg["apt"]["sources"]
for src in sources:
print(sources[src])
calls.append(call(sources[src], cloud, gpg))
mockobj.assert_has_calls(calls, any_order=True)
assert os.path.isfile(filename)
contents = util.load_text_file(filename)
assert re.search(
r"%s %s %s %s\n"
% (
"deb",
"http://ppa.launchpad.net/smoser/cloud-init-test/ubuntu",
"xenial",
"main",
),
contents,
flags=re.IGNORECASE,
)
def test_apt_src_keyid(self, apt_lists, m_gpg):
"""Test specification of a source + keyid with filename being set"""
cfg = {
"source": (
"deb "
"http://ppa.launchpad.net/"
"smoser/cloud-init-test/ubuntu"
" xenial main"
),
"keyid": "03683F77",
"filename": apt_lists[0],
}
self.apt_src_keyid(apt_lists[0], [cfg], 1, m_gpg)
def test_apt_src_keyid_tri(self, apt_lists, m_gpg):
"""Test 3x specification of a source + keyid with filename being set"""
cfg1 = {
"source": (
"deb "
"http://ppa.launchpad.net/"
"smoser/cloud-init-test/ubuntu"
" xenial main"
),
"keyid": "03683F77",
"filename": apt_lists[0],
}
cfg2 = {
"source": (
"deb "
"http://ppa.launchpad.net/"
"smoser/cloud-init-test/ubuntu"
" xenial universe"
),
"keyid": "03683F77",
"filename": apt_lists[1],
}
cfg3 = {
"source": (
"deb "
"http://ppa.launchpad.net/"
"smoser/cloud-init-test/ubuntu"
" xenial multiverse"
),
"keyid": "03683F77",
"filename": apt_lists[2],
}
self.apt_src_keyid(apt_lists[0], [cfg1, cfg2, cfg3], 3, m_gpg)
contents = util.load_text_file(apt_lists[1])
assert re.search(
r"%s %s %s %s\n"
% (
"deb",
"http://ppa.launchpad.net/smoser/cloud-init-test/ubuntu",
"xenial",
"universe",
),
contents,
flags=re.IGNORECASE,
)
contents = util.load_text_file(apt_lists[2])
assert re.search(
r"%s %s %s %s\n"
% (
"deb",
"http://ppa.launchpad.net/smoser/cloud-init-test/ubuntu",
"xenial",
"multiverse",
),
contents,
flags=re.IGNORECASE,
)
def test_apt_src_keyid_nofn(self, fallback_path, tmpdir, m_gpg):
"""Test specification of a source + keyid without filename being set"""
cfg = {
"source": (
"deb "
"http://ppa.launchpad.net/"
"smoser/cloud-init-test/ubuntu"
" xenial main"
),
"keyid": "03683F77",
}
with mock.patch.object(
os.path, "join", side_effect=partial(self.myjoin, tmpdir)
):
self.apt_src_keyid(fallback_path, [cfg], 1, m_gpg)
def apt_src_key(self, filename, cfg, gpg):
"""apt_src_key
Test specification of a source + key
"""
cfg = self.wrapv1conf([cfg])
cloud = get_cloud()
with mock.patch.object(
cc_apt_configure, "GPG"
) as this_gpg, mock.patch.object(
cc_apt_configure, "add_apt_key"
) as mockobj:
this_gpg.return_value = gpg
cc_apt_configure.handle("test", cfg, cloud, [])
# check if it added the right amount of keys
sources = cfg["apt"]["sources"]
calls = []
for src in sources:
calls.append(call(sources[src], cloud, gpg))
mockobj.assert_has_calls(calls, any_order=True)
assert os.path.isfile(filename)
contents = util.load_text_file(filename)
assert re.search(
r"%s %s %s %s\n"
% (
"deb",
"http://ppa.launchpad.net/smoser/cloud-init-test/ubuntu",
"xenial",
"main",
),
contents,
flags=re.IGNORECASE,
)
def test_apt_src_key(self, apt_lists, m_gpg):
"""Test specification of a source + key with filename being set"""
cfg = {
"source": (
"deb "
"http://ppa.launchpad.net/"
"smoser/cloud-init-test/ubuntu"
" xenial main"
),
"key": "fakekey 4321",
"filename": apt_lists[0],
}
self.apt_src_key(apt_lists[0], cfg, m_gpg)
def test_apt_src_key_nofn(self, fallback_path, tmpdir, m_gpg):
"""Test specification of a source + key without filename being set"""
cfg = {
"source": (
"deb "
"http://ppa.launchpad.net/"
"smoser/cloud-init-test/ubuntu"
" xenial main"
),
"key": "fakekey 4321",
}
with mock.patch.object(
os.path, "join", side_effect=partial(self.myjoin, tmpdir)
):
self.apt_src_key(fallback_path, cfg, m_gpg)
def test_apt_src_keyonly(self, apt_lists, m_gpg):
"""Test specifying key without source"""
cfg = {"key": "fakekey 4242", "filename": apt_lists[0]}
cfg = self.wrapv1conf([cfg])
with mock.patch.object(
cc_apt_configure, "GPG"
) as gpg, mock.patch.object(cc_apt_configure, "apt_key") as mockobj:
gpg.return_value = m_gpg
cc_apt_configure.handle("test", cfg, get_cloud(), [])
calls = (
call(
"add",
m_gpg,
output_file=pathlib.Path(apt_lists[0]).stem,
data="fakekey 4242",
hardened=False,
),
)
mockobj.assert_has_calls(calls, any_order=True)
# filename should be ignored on key only
assert not os.path.isfile(apt_lists[0])
def test_apt_src_keyidonly(self, apt_lists, m_gpg):
"""Test specification of a keyid without source"""
cfg = {"keyid": "03683F77", "filename": apt_lists[0]}
cfg = self.wrapv1conf([cfg])
m_gpg.getkeybyid = mock.Mock(return_value="fakekey 1212")
SAMPLE_GPG_AGENT_DIRMNGR_PIDS = dedent(
"""\
PPID PID
1 1057
1 1095
1511 2493
1511 2509
"""
)
with mock.patch.object(
subp,
"subp",
side_effect=[
SubpResult("fakekey 1212", ""),
SubpResult(SAMPLE_GPG_AGENT_DIRMNGR_PIDS, ""),
],
), mock.patch.object(
cc_apt_configure, "GPG"
) as gpg, mock.patch.object(
cc_apt_configure, "apt_key"
) as mockobj:
gpg.return_value = m_gpg
cc_apt_configure.handle("test", cfg, get_cloud(), [])
calls = (
call(
"add",
m_gpg,
output_file=pathlib.Path(apt_lists[0]).stem,
data="fakekey 1212",
hardened=False,
),
)
mockobj.assert_has_calls(calls, any_order=True)
# filename should be ignored on key only
assert not os.path.isfile(apt_lists[0])
def apt_src_keyid_real(
self, apt_lists, cfg, expectedkey, gpg, is_hardened=None
):
"""apt_src_keyid_real
Test specification of a keyid without source including
up to addition of the key (add_apt_key_raw mocked to keep the
environment as is)
"""
cfg = self.wrapv1conf([cfg])
gpg.getkeybyid = mock.Mock(return_value=expectedkey)
with mock.patch.object(cc_apt_configure, "add_apt_key_raw") as mockkey:
with mock.patch.object(cc_apt_configure, "GPG") as my_gpg:
my_gpg.return_value = gpg
cc_apt_configure.handle("test", cfg, get_cloud(), [])
if is_hardened is not None:
mockkey.assert_called_with(
expectedkey, apt_lists[0], gpg, hardened=is_hardened
)
else:
mockkey.assert_called_with(expectedkey, apt_lists[0], gpg)
# filename should be ignored on key only
assert not os.path.isfile(apt_lists[0])
def test_apt_src_keyid_real(self, apt_lists, m_gpg):
"""test_apt_src_keyid_real - Test keyid including key add"""
keyid = "03683F77"
cfg = {"keyid": keyid, "filename": apt_lists[0]}
self.apt_src_keyid_real(
apt_lists, cfg, EXPECTEDKEY, m_gpg, is_hardened=False
)
def test_apt_src_longkeyid_real(self, apt_lists, m_gpg):
"""test_apt_src_longkeyid_real - Test long keyid including key add"""
keyid = "B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77"
cfg = {"keyid": keyid, "filename": apt_lists[0]}
self.apt_src_keyid_real(
apt_lists, cfg, EXPECTEDKEY, m_gpg, is_hardened=False
)
def test_apt_src_longkeyid_ks_real(self, apt_lists, m_gpg):
"""test_apt_src_longkeyid_ks_real - Test long keyid from other ks"""
keyid = "B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77"
cfg = {
"keyid": keyid,
"keyserver": "keys.gnupg.net",
"filename": apt_lists[0],
}
self.apt_src_keyid_real(
apt_lists, cfg, EXPECTEDKEY, m_gpg, is_hardened=False
)
def test_apt_src_ppa(self, apt_lists, mocker, m_gpg):
"""Test adding a ppa"""
m_subp = mocker.patch.object(
subp, "subp", return_value=SubpResult("PPID PID", "")
)
mocker.patch("cloudinit.gpg.subp.which", return_value=False)
cfg = {
"source": "ppa:smoser/cloud-init-test",
"filename": apt_lists[0],
}
cfg = self.wrapv1conf([cfg])
with mock.patch.object(cc_apt_configure, "GPG") as my_gpg:
my_gpg.return_value = m_gpg
cc_apt_configure.handle("test", cfg, get_cloud(), [])
assert m_subp.call_args_list == [
mock.call(
[
"add-apt-repository",
"--no-update",
"ppa:smoser/cloud-init-test",
],
),
]
# adding ppa should ignore filename (uses add-apt-repository)
assert not os.path.isfile(apt_lists[0])
def test_apt_src_ppa_tri(self, apt_lists, m_gpg):
"""Test adding three ppa's"""
cfg1 = {
"source": "ppa:smoser/cloud-init-test",
"filename": apt_lists[0],
}
cfg2 = {
"source": "ppa:smoser/cloud-init-test2",
"filename": apt_lists[1],
}
cfg3 = {
"source": "ppa:smoser/cloud-init-test3",
"filename": apt_lists[2],
}
cfg = self.wrapv1conf([cfg1, cfg2, cfg3])
with mock.patch.object(
subp, "subp", return_value=SubpResult("PPID PID", "")
) as mockobj:
with mock.patch.object(cc_apt_configure, "GPG") as my_gpg:
my_gpg.return_value = m_gpg
cc_apt_configure.handle("test", cfg, get_cloud(), [])
calls = [
call(
[
"add-apt-repository",
"--no-update",
"ppa:smoser/cloud-init-test",
],
),
call(
[
"add-apt-repository",
"--no-update",
"ppa:smoser/cloud-init-test2",
],
),
call(
[
"add-apt-repository",
"--no-update",
"ppa:smoser/cloud-init-test3",
],
),
]
mockobj.assert_has_calls(calls, any_order=True)
# adding ppa should ignore all filenames (uses add-apt-repository)
assert not os.path.isfile(apt_lists[0])
assert not os.path.isfile(apt_lists[1])
assert not os.path.isfile(apt_lists[2])
def test_convert_to_new_format(self, apt_lists):
"""Test the conversion of old to new format"""
cfg1 = {
"source": "deb $MIRROR $RELEASE multiverse",
"filename": apt_lists[0],
}
cfg2 = {
"source": "deb $MIRROR $RELEASE main",
"filename": apt_lists[1],
}
cfg3 = {
"source": "deb $MIRROR $RELEASE universe",
"filename": apt_lists[2],
}
cfg = {"apt_sources": [cfg1, cfg2, cfg3]}
checkcfg = {
apt_lists[0]: {
"filename": apt_lists[0],
"source": "deb $MIRROR $RELEASE multiverse",
},
apt_lists[1]: {
"filename": apt_lists[1],
"source": "deb $MIRROR $RELEASE main",
},
apt_lists[2]: {
"filename": apt_lists[2],
"source": "deb $MIRROR $RELEASE universe",
},
}
newcfg = cc_apt_configure.convert_to_v3_apt_format(cfg)
assert newcfg["apt"]["sources"] == checkcfg
# convert again, should stay the same
newcfg2 = cc_apt_configure.convert_to_v3_apt_format(newcfg)
assert newcfg2["apt"]["sources"] == checkcfg
# should work without raising an exception
cc_apt_configure.convert_to_v3_apt_format({})
with pytest.raises(ValueError):
cc_apt_configure.convert_to_v3_apt_format({"apt_sources": 5})
def test_convert_to_new_format_collision(self):
"""Test the conversion of old to new format with collisions
That matches e.g. the MAAS case specifying old and new config"""
cfg_1_and_3 = {
"apt": {"proxy": "http://192.168.122.1:8000/"},
"apt_proxy": "http://192.168.122.1:8000/",
}
cfg_3_only = {"apt": {"proxy": "http://192.168.122.1:8000/"}}
cfgconflict = {
"apt": {"proxy": "http://192.168.122.1:8000/"},
"apt_proxy": "ftp://192.168.122.1:8000/",
}
# collision (equal)
newcfg = cc_apt_configure.convert_to_v3_apt_format(cfg_1_and_3)
assert newcfg == cfg_3_only
# collision (equal, so ok to remove)
newcfg = cc_apt_configure.convert_to_v3_apt_format(cfg_3_only)
assert newcfg == cfg_3_only
# collision (unequal)
match = "Old and New.*unequal.*apt_proxy"
with pytest.raises(ValueError, match=match):
cc_apt_configure.convert_to_v3_apt_format(cfgconflict)
def test_convert_to_new_format_dict_collision(self, apt_lists, m_gpg):
cfg1 = {
"source": "deb $MIRROR $RELEASE multiverse",
"filename": apt_lists[0],
}
cfg2 = {
"source": "deb $MIRROR $RELEASE main",
"filename": apt_lists[1],
}
cfg3 = {
"source": "deb $MIRROR $RELEASE universe",
"filename": apt_lists[2],
}
fullv3 = {
apt_lists[0]: {
"filename": apt_lists[0],
"source": "deb $MIRROR $RELEASE multiverse",
},
apt_lists[1]: {
"filename": apt_lists[1],
"source": "deb $MIRROR $RELEASE main",
},
apt_lists[2]: {
"filename": apt_lists[2],
"source": "deb $MIRROR $RELEASE universe",
},
}
cfg_3_only = {"apt": {"sources": fullv3}}
cfg_1_and_3 = {"apt_sources": [cfg1, cfg2, cfg3]}
cfg_1_and_3.update(cfg_3_only)
# collision (equal, so ok to remove)
newcfg = cc_apt_configure.convert_to_v3_apt_format(cfg_1_and_3)
assert newcfg == cfg_3_only
# no old spec (same result)
newcfg = cc_apt_configure.convert_to_v3_apt_format(cfg_3_only)
assert newcfg == cfg_3_only
diff = {
apt_lists[0]: {
"filename": apt_lists[0],
"source": "deb $MIRROR $RELEASE DIFFERENTVERSE",
},
apt_lists[1]: {
"filename": apt_lists[1],
"source": "deb $MIRROR $RELEASE main",
},
apt_lists[2]: {
"filename": apt_lists[2],
"source": "deb $MIRROR $RELEASE universe",
},
}
cfg_3_only = {"apt": {"sources": diff}}
cfg_1_and_3_different = {"apt_sources": [cfg1, cfg2, cfg3]}
cfg_1_and_3_different.update(cfg_3_only)
# collision (unequal by dict having a different entry)
with pytest.raises(ValueError):
cc_apt_configure.convert_to_v3_apt_format(cfg_1_and_3_different)
missing = {
apt_lists[0]: {
"filename": apt_lists[0],
"source": "deb $MIRROR $RELEASE multiverse",
}
}
cfg_3_only = {"apt": {"sources": missing}}
cfg_1_and_3_missing = {"apt_sources": [cfg1, cfg2, cfg3]}
cfg_1_and_3_missing.update(cfg_3_only)
# collision (unequal by dict missing an entry)
with pytest.raises(ValueError):
cc_apt_configure.convert_to_v3_apt_format(cfg_1_and_3_missing)
|