Skip to content

Commit

Permalink
Handle URL params for AptRepoFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
hstct committed Mar 6, 2023
1 parent 82e4bf9 commit 0043600
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/subscription_manager/repofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,19 +463,22 @@ def sections(self):

def fix_content(self, content):
# Luckily apt ignores all Fields it does not recognize
baseurl = content["baseurl"]
url_with_params = content["baseurl"]
baseurl = url_with_params.rpartition("/")[0]
url_res = re.match(r"^https?://(?P<location>.*)$", baseurl)
ent_res = re.match(r"^/etc/pki/entitlement/(?P<entitlement>.*).pem$", content["sslclientcert"])
if url_res and ent_res:
location = url_res.group("location")
entitlement = ent_res.group("entitlement")
baseurl = "katello://{}@{}".format(entitlement, location)

parsed_url = urlparse(url_with_params)
query = parse_qs(parsed_url.query)
apt_cont = content.copy()
apt_cont["Types"] = "deb"
apt_cont["URIs"] = baseurl
apt_cont["Suites"] = "default"
apt_cont["Components"] = "all"
apt_cont["Suites"] = query["rel"][0].replace(",", " ") if "rel" in query else "default"
apt_cont["Components"] = query["comp"][0].replace(",", " ") if "comp" in query else "all"
apt_cont["Trusted"] = "yes"

if apt_cont["arches"] is None or apt_cont["arches"] == ["ALL"]:
Expand Down
58 changes: 58 additions & 0 deletions test/test_repolib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,64 @@ def test_fix_content_default(self, mock_file):
self.assertIn(key, act_content)
self.assertEqual(act_content[key], exp_params[key])

@patch("builtins.open", new_callable=mock_open, read_data="data")
def test_fix_content_url_params(self, mock_file):
# Setup mock and expected values
assert open("/etc/apt/sources.list.d/rhsm.sources").read() == "data"
mock_file.assert_called_with("/etc/apt/sources.list.d/rhsm.sources")
exp_params = {
"arches": "none",
"Types": "deb",
"URIs": "https://example.site.org/",
"Suites": "focal",
"Components": "puppet7",
"Trusted": "yes",
"sslclientcert": "mypem.pem",
}
ar = self._helper_stub_repofile()
repo_mock = self._helper_stub_repo(
"mock",
existing_values=[
("baseurl", "https://example.site.org/?comp=puppet7&rel=focal"),
("sslclientcert", exp_params["ssclientcert"]),
],
)
# Modify data
act_content = ar.fix_content(repo_mock)
# Test modification by comparing with expected values
for key in exp_params:
self.assertIn(key, act_content)
self.assertEqual(act_content[key], exp_params[key])

@patch("builtins.open", new_callable=mock_open, read_data="data")
def test_fix_content_url_params_multi(self, mock_file):
# Setup mock and expected values
assert open("/etc/apt/sources.list.d/rhsm.sources").read() == "data"
mock_file.assert_called_with("/etc/apt/sources.list.d/rhsm.sources")
exp_params = {
"arches": "none",
"Types": "deb",
"URIs": "https://example.site.org/",
"Suites": "focal jammy",
"Components": "puppet7 puppet6",
"Trusted": "yes",
"sslclientcert": "mypem.pem",
}
ar = self._helper_stub_repofile()
repo_mock = self._helper_stub_repo(
"mock",
existing_values=[
("baseurl", "https://example.site.org/?comp=puppet7,puppet6&rel=focal,jammy"),
("sslclientcert", exp_params["ssclientcert"]),
],
)
# Modify data
act_content = ar.fix_content(repo_mock)
# Test modification by comparing with expected values
for key in exp_params:
self.assertIn(key, act_content)
self.assertEqual(act_content[key], exp_params[key])

@patch("builtins.open", new_callable=mock_open, read_data="data")
def test_fix_content_arches(self, mock_file):
# Setup mock and expected values
Expand Down

0 comments on commit 0043600

Please sign in to comment.