def test_return_true_when_package_installed(self): stdout = dedent(""" Package: python3 Status: install ok installed Priority: optional Section: python Installed-Size: 89 Maintainer: Matthias Klose <*****@*****.**> Architecture: amd64 Multi-Arch: allowed Source: python3-defaults Version: 3.9.2-3 Replaces: python3-minimal (<< 3.1.2-2) Provides: python3-profiler Depends: python3.9 (>= 3.9.2-0~), libpython3-stdlib (= 3.9.2-3) Pre-Depends: python3-minimal (= 3.9.2-3) Suggests: python3-doc (>= 3.9.2-3), python3-tk (>= 3.9.2-0~), Description: interactive high-level object-oriented languag Python, the high-level, interactive object oriented language, includes an extensive class library with lots of goodies for network programming, system administration, sounds and graphics. . This package is a dependency package, which depends on Debian's default Python 3 version (currently v3.9). Homepage: https://www.python.org/ Cnf-Extra-Commands: python Cnf-Priority-Bonus: 5""").strip() g = None a = Apt(_mock_run(['dpkg', '-s', 'python3'], CompletedProcess(stdout, 'stderr', 0, 'cmd'))) assert a.get_package_version(g, 'python3') == '3.9.2-3'
def test_return_empty_list_if_package_not_available(self): g = None a = Apt(_mock_run( ['apt-cache', 'madison', 'cloud-init'], CompletedProcess( 'N: Unable to locate package cloud-init', '', 1, 'cmd'))) assert a.list_available_versions(g, 'cloud-init') == set()
def test_return_empty_string_when_version_not_detected(self): stdout = dedent(""" Package: python3 Cnf-Priority-Bonus: 5""").strip() g = None a = Apt(_mock_run(['dpkg', '-s', 'python3'], CompletedProcess(stdout, 'stderr', 0, 'cmd'))) assert a.get_package_version(g, 'python3') == ''
def test_skip_lines_from_madison_with_unexpected_syntax(self): g = None stdout = '\n'.join([ 'cloud-init | 21.3-1-g6803368d-0ubuntu1~20.04.3 | repo', 'cloud-init | extra bar | repo | not-recognized', 'cloud-init | missing bar', 'no bar', ]) a = Apt(_mock_run(['apt-cache', 'madison', 'cloud-init'], CompletedProcess(stdout, '', 0, 'cmd'))) assert a.list_available_versions(g, 'cloud-init') == { '21.3-1-g6803368d-0ubuntu1~20.04.3'}
def test_return_versions_if_available(self): g = None stdout = '\n'.join([ 'cloud-init | 21.3-1-g6803368d-0ubuntu1~20.04.3 | repo', 'cloud-init | 20.1-10-g71af48df-0ubuntu5 | repo', 'cloud-init | 0.7.5-0ubuntu1.23 | repo', ]) a = Apt(_mock_run(['apt-cache', 'madison', 'cloud-init'], CompletedProcess(stdout, '', 0, 'cmd'))) assert a.list_available_versions(g, 'cloud-init') == { '21.3-1-g6803368d-0ubuntu1~20.04.3', '20.1-10-g71af48df-0ubuntu5', '0.7.5-0ubuntu1.23'}
def setup_cloud_init(g: guestfs.GuestFS): """ Install cloud-init if not present, and configure to the cloud provider. Args: g: A mounted GuestFS instance. """ a = Apt(run) curr_version = a.get_package_version(g, 'cloud-init') available_versions = a.list_available_versions(g, 'cloud-init') # Try to avoid installing 21.3-1, which conflicts which the guest agent. # On first boot, systemd reaches a deadlock deadlock and doest start # its unit. If a version other than 21.3-1 isn't explicitly found, *and* # cloud-init isn't currently installed, then this allows apt to pick the # version to install. version_to_install = Apt.determine_version_to_install( curr_version, available_versions, {'21.3-1'}) pkg_to_install = '' if version_to_install: pkg_to_install = 'cloud-init=' + version_to_install elif curr_version == '': pkg_to_install = 'cloud-init' # If this block doesn't execute, it means that cloud-init was found # on the system, but there wasn't an upgrade candidate. Therefore # leave the version that's currently installed. if pkg_to_install: logging.info(pkg_to_install) utils.install_apt_packages(g, pkg_to_install) # Ubuntu 14.04's version of cloud-init doesn't have `clean`. if g.gcp_image_major > '14': run(g, 'cloud-init clean') # Remove cloud-init configs that may conflict with GCE's. # # - subiquity disables automatic network configuration # https://bugs.launchpad.net/ubuntu/+source/cloud-init/+bug/1871975 for cfg in [ 'azure', 'curtin', 'waagent', 'walinuxagent', 'aws', 'amazon', 'subiquity' ]: run(g, 'rm -f /etc/cloud/cloud.cfg.d/*%s*' % cfg) g.write('/etc/cloud/cloud.cfg.d/91-gce-system.cfg', cloud_init_config)
def test_return_empty_string_when_package_not_installed(self): g = None a = Apt(_mock_run(['dpkg', '-s', 'cloud-init'], CompletedProcess('stdout', 'stderr', 1, 'cmd'))) assert a.get_package_version(g, 'cloud-init') == ''
def test_return_empty_when_nothing_newer(self): assert Apt.determine_version_to_install( '2', {'1.1'}, set()) == ''
def test_blocklist_uses_prefix_matching(self): assert Apt.determine_version_to_install( '1', {'2.1', '2.2'}, {'2'}) == ''
def test_dont_return_blocked(self): assert Apt.determine_version_to_install( '1', {'3', '2', '1'}, {'3'}) == '2'
def test_allow_current_version_empty(self): assert Apt.determine_version_to_install( '', {'3', '2', '1'}, {}) == '3'
def test_return_greatest_match(self): assert Apt.determine_version_to_install( '1', {'3', '2', '1'}, {}) == '3'