-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: apt install freeipmi-tools failed on focal #143
Merged
jneo8
merged 2 commits into
canonical:feat/life-cycle
from
jneo8:fix/focal-fail-to-install-freeipmi-tools
Dec 15, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave a comment . We can use operator-libs-linux after this issue/feature been implemented. Then we don't need to maintain the code in apt-helper.py
canonical/operator-libs-linux#113
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added related issue links in
apt_helpers
module