-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: apt install freeipmi-tools failed on focal (#143)
* fix: apt install freeipmi-tools failed on focal Fix: #128
- Loading branch information
Showing
4 changed files
with
101 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Apt helper module for missing features in operator_libs_linux.""" | ||
import re | ||
from subprocess import PIPE, CalledProcessError, check_output | ||
from typing import Optional | ||
|
||
from charms.operator_libs_linux.v0 import apt | ||
|
||
|
||
def get_candidate_version(package: str) -> Optional[str]: | ||
"""Get candiate version of package from apt-cache. | ||
Related issue: https://github.com/canonical/operator-libs-linux/issues/113 | ||
""" | ||
try: | ||
output = check_output( | ||
["apt-cache", "policy", package], stderr=PIPE, universal_newlines=True | ||
) | ||
except CalledProcessError as e: | ||
raise apt.PackageError(f"Could not list packages in apt-cache: {e.output}") from None | ||
|
||
lines = [line.strip() for line in output.strip().split("\n")] | ||
for line in lines: | ||
candidate_matcher = re.compile(r"^Candidate:\s(?P<version>(.*))") | ||
matches = candidate_matcher.search(line) | ||
if matches: | ||
return matches.groupdict().get("version") | ||
raise apt.PackageError(f"Could not find candidate version package in apt-cache: {output}") | ||
|
||
|
||
def add_pkg_with_candidate_version(pkg: str) -> None: | ||
"""Install package with apt-cache candidate version. | ||
Related issue: https://github.com/canonical/operator-libs-linux/issues/113 | ||
""" | ||
version = get_candidate_version(pkg) | ||
apt.add_package(pkg, version=version, update_cache=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import unittest | ||
from subprocess import CalledProcessError | ||
from unittest import mock | ||
|
||
from charms.operator_libs_linux.v0 import apt | ||
|
||
import apt_helpers | ||
|
||
APT_CACHE_POLICY_FREEIPMI_TOOLS_OUTPUT = """freeipmi-tools: | ||
Installed: (none) | ||
Candidate: 1.6.4-3ubuntu1.1 | ||
Version table: | ||
1.6.9-2~bpo20.04.1 100 | ||
100 http://tw.archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages | ||
1.6.4-3ubuntu1.1 500 | ||
500 http://tw.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages | ||
100 /var/lib/dpkg/status | ||
1.6.4-3ubuntu1 500 | ||
500 http://tw.archive.ubuntu.com/ubuntu focal/main amd64 Packages | ||
""" | ||
|
||
|
||
class TestGetCandidateVersion(unittest.TestCase): | ||
@mock.patch("apt_helpers.check_output") | ||
def test_install_freeipmi_tools_on_focal(self, mock_check_output): | ||
mock_check_output.return_value = APT_CACHE_POLICY_FREEIPMI_TOOLS_OUTPUT | ||
version = apt_helpers.get_candidate_version("freeipmi-tools") | ||
self.assertEqual(version, "1.6.4-3ubuntu1.1") | ||
|
||
@mock.patch("apt_helpers.check_output") | ||
def test_checkoutput_failed(self, mock_check_output): | ||
mock_check_output.side_effect = CalledProcessError(-1, "cmd") | ||
|
||
with self.assertRaises(apt.PackageError): | ||
apt_helpers.get_candidate_version("freeipmi-tools") | ||
|
||
@mock.patch("apt_helpers.check_output") | ||
def test_checkoutput_version_not_found_error(self, mock_check_output): | ||
fake_output = APT_CACHE_POLICY_FREEIPMI_TOOLS_OUTPUT.replace("Candidate", "NotCandidate") | ||
mock_check_output.return_value = fake_output | ||
|
||
with self.assertRaises(apt.PackageError): | ||
apt_helpers.get_candidate_version("freeipmi-tools") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters