Skip to content

Commit

Permalink
apt: change private cache list files to deb822
Browse files Browse the repository at this point in the history
In this case we can do it for all releases, as there is no dependency on
it outside of the client.

Signed-off-by: Renan Rodrigo <[email protected]>
  • Loading branch information
renanrodrigo authored and orndorffgrant committed Jan 9, 2024
1 parent 2d693ff commit 4c39b48
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 64 deletions.
9 changes: 0 additions & 9 deletions uaclient/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@
Signed-By: {keyrings_dir}/{keyring_file}
"""

ESM_REPO_FILE_CONTENT = """\
# Written by ubuntu-advantage-tools
deb https://esm.ubuntu.com/{name}/ubuntu {series}-{name}-security main
# deb-src https://esm.ubuntu.com/{name}/ubuntu {series}-{name}-security main
deb https://esm.ubuntu.com/{name}/ubuntu {series}-{name}-updates main
# deb-src https://esm.ubuntu.com/{name}/ubuntu {series}-{name}-updates main
"""

ESM_BASIC_FILE_STRUCTURE = {
"files": [
Expand Down
57 changes: 39 additions & 18 deletions uaclient/entitlements/esm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
from typing import Tuple, Type, Union

from uaclient import gpg, messages, system
from uaclient.apt import APT_KEYS_DIR, ESM_REPO_FILE_CONTENT, KEYRINGS_DIR
from uaclient import messages, system
from uaclient.apt import APT_KEYS_DIR, DEB822_REPO_FILE_CONTENT, KEYRINGS_DIR
from uaclient.defaults import ESM_APT_ROOTDIR
from uaclient.entitlements import repo
from uaclient.entitlements.base import UAEntitlement
from uaclient.entitlements.entitlement_status import CanDisableFailure
from uaclient.util import set_filename_extension


class ESMBaseEntitlement(repo.RepoEntitlement):
Expand Down Expand Up @@ -35,37 +36,57 @@ def setup_local_esm_repo(self) -> None:
# Ugly? Yes, but so is python < 3.8 without removeprefix
assert self.name.startswith("esm-")
esm_name = self.name[len("esm-") :]
repo_filename = os.path.normpath(
ESM_APT_ROOTDIR + self.repo_file,
sources_repo_filename = set_filename_extension(
os.path.normpath(
ESM_APT_ROOTDIR + self.repo_file,
),
"sources",
)
list_repo_filename = set_filename_extension(
os.path.normpath(
ESM_APT_ROOTDIR + self.repo_file,
),
"list",
)
keyring_file = self.repo_key_file

# No need to create if already present
if os.path.exists(repo_filename):
# No need to create if any format already present
if os.path.exists(sources_repo_filename) or os.path.exists(
list_repo_filename
):
return

system.write_file(
repo_filename,
ESM_REPO_FILE_CONTENT.format(name=esm_name, series=series),
esm_url = "https://esm.ubuntu.com/{name}/ubuntu".format(name=esm_name)
suites = "{series}-{name}-security {series}-{name}-updates".format(
series=series, name=esm_name
)

# Set up GPG key
source_keyring_file = os.path.join(KEYRINGS_DIR, keyring_file)
destination_keyring_file = os.path.normpath(
ESM_APT_ROOTDIR + APT_KEYS_DIR + keyring_file
# When writing, use the sources format by default
system.write_file(
sources_repo_filename,
DEB822_REPO_FILE_CONTENT.format(
url=esm_url,
suites=suites,
keyrings_dir=KEYRINGS_DIR,
keyring_file=self.repo_key_file,
),
)
os.makedirs(os.path.dirname(destination_keyring_file), exist_ok=True)
gpg.export_gpg_key(source_keyring_file, destination_keyring_file)

def disable_local_esm_repo(self) -> None:
keyring_file = os.path.normpath(
ESM_APT_ROOTDIR + APT_KEYS_DIR + self.repo_key_file
)
system.ensure_file_absent(keyring_file)

repo_filename = os.path.normpath(
ESM_APT_ROOTDIR + self.repo_file,
)
system.ensure_file_absent(repo_filename)
system.ensure_file_absent(keyring_file)
# Remove any instance of the file present in the folder
system.ensure_file_absent(
set_filename_extension(repo_filename, "sources")
)
system.ensure_file_absent(
set_filename_extension(repo_filename, "list")
)


class ESMAppsEntitlement(ESMBaseEntitlement):
Expand Down
69 changes: 32 additions & 37 deletions uaclient/entitlements/tests/test_esm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from uaclient import apt
from uaclient.entitlements.esm import ESMAppsEntitlement, ESMInfraEntitlement
from uaclient.util import set_filename_extension

M_PATH = "uaclient.entitlements.esm.ESMInfraEntitlement."
M_REPOPATH = "uaclient.entitlements.repo."
Expand Down Expand Up @@ -98,12 +99,8 @@ class TestUpdateESMCaches:
@mock.patch("uaclient.apt.os.path.exists")
@mock.patch("uaclient.apt.system.get_release_info")
@mock.patch("uaclient.apt.system.write_file")
@mock.patch("uaclient.apt.os.makedirs")
@mock.patch("uaclient.apt.gpg.export_gpg_key")
def test_setup_local_esm_repo(
self,
m_export_gpg,
m_makedirs,
m_write_file,
m_get_release_info,
m_exists,
Expand All @@ -117,57 +114,55 @@ def test_setup_local_esm_repo(

if file_exists:
assert m_write_file.call_count == 0
assert m_makedirs.call_count == 0
assert m_export_gpg.call_count == 0

else:
suites = "{series}-{name}-security {series}-{name}-updates".format(
name=entitlement.name[4:], series="example"
)
assert m_write_file.call_args_list == [
mock.call(
os.path.normpath(
apt.ESM_APT_ROOTDIR + entitlement.repo_file,
),
apt.ESM_REPO_FILE_CONTENT.format(
name=entitlement.name[4:], series="example"
),
)
]

assert m_makedirs.call_args_list == [
mock.call(
os.path.dirname(
set_filename_extension(
os.path.normpath(
apt.ESM_APT_ROOTDIR
+ apt.APT_KEYS_DIR
+ entitlement.repo_key_file
apt.ESM_APT_ROOTDIR + entitlement.repo_file,
),
"sources",
),
exist_ok=True,
)
]

assert m_export_gpg.call_args_list == [
mock.call(
os.path.join(apt.KEYRINGS_DIR, entitlement.repo_key_file),
os.path.normpath(
apt.ESM_APT_ROOTDIR
+ apt.APT_KEYS_DIR
+ entitlement.repo_key_file
apt.DEB822_REPO_FILE_CONTENT.format(
url="https://esm.ubuntu.com/{name}/ubuntu".format(
name=entitlement.name[4:]
),
suites=suites,
keyrings_dir=apt.KEYRINGS_DIR,
keyring_file=entitlement.repo_key_file,
),
)
]

@mock.patch("uaclient.apt.system.ensure_file_absent")
def disable_local_esm_repo(self, m_ensure_file_absent, entitlement):
def test_disable_local_esm_repo(self, m_ensure_file_absent, entitlement):
entitlement.disable_local_esm_repo()
assert m_ensure_file_absent.call_args_list == [
mock.call(
os.path.normpath(
apt.ESM_APT_ROOTDIR + self.repo_file,
apt.ESM_APT_ROOTDIR
+ apt.APT_KEYS_DIR
+ entitlement.repo_key_file
)
),
mock.call(
os.path.normpath(
apt.ESM_APT_ROOTDIR + apt.APT_KEYS_DIR + self.repo_key_file
)
set_filename_extension(
os.path.normpath(
apt.ESM_APT_ROOTDIR + entitlement.repo_file,
),
"sources",
),
),
mock.call(
set_filename_extension(
os.path.normpath(
apt.ESM_APT_ROOTDIR + entitlement.repo_file,
),
"list",
),
),
]

0 comments on commit 4c39b48

Please sign in to comment.