cloud-init/backport-fix-Ensure-fqdn-is-treated-as-string-in-get_hostname.patch

63 lines
2.4 KiB
Diff
Raw Permalink Normal View History

2025-03-05 09:30:32 +08:00
From b45d66a03659f8e4780b6b55e51edcbd2f6f012d Mon Sep 17 00:00:00 2001
From: MKhatibzadeh <32599707+masihkhatibzadeh99@users.noreply.github.com>
Date: Fri, 7 Feb 2025 18:13:43 +0330
Subject: [PATCH] fix: Ensure fqdn is treated as string in get_hostname_fqdn
(#5993)
Explicitly cast fqdn to a string before processing.
Reference:https://github.com/canonical/cloud-init/commit/b45d66a03659f8e4780b6b55e51edcbd2f6f012d
Conflict:not change .github-cla-signers
Fixes GH-5989
Co-authored-by: masih.khatibzdeh <masih.khatibzadeh@snapp.cab>
---
cloudinit/util.py | 2 +-
tests/unittests/test_util.py | 16 ++++++++++++++++
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 20b6e2e9ef4..bfcc9c8edba 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -1215,7 +1215,7 @@ def get_hostname_fqdn(cfg, cloud, metadata_only=False):
is_default = False
if "fqdn" in cfg:
# user specified a fqdn. Default hostname then is based off that
- fqdn = cfg["fqdn"]
+ fqdn = str(cfg["fqdn"])
hostname = get_cfg_option_str(cfg, "hostname", fqdn.split(".")[0])
else:
if "hostname" in cfg and cfg["hostname"].find(".") > 0:
diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py
index 8a107191b0e..7d2383f2dd6 100644
--- a/tests/unittests/test_util.py
+++ b/tests/unittests/test_util.py
@@ -799,6 +799,22 @@ def test_get_hostname_fqdn_from_without_fqdn_or_hostname(self):
mock.call(metadata_only=False),
] == cloud.get_hostname.call_args_list
+ def test_get_hostname_fqdn_from_numeric_fqdn(self):
+ """When cfg fqdn is numeric, ensure it is treated as a string."""
+ hostname, fqdn, _ = util.get_hostname_fqdn(
+ cfg={"fqdn": 12345}, cloud=None
+ )
+ self.assertEqual("12345", hostname)
+ self.assertEqual("12345", fqdn)
+
+ def test_get_hostname_fqdn_from_numeric_fqdn_with_domain(self):
+ """When cfg fqdn is numeric with a domain, ensure correct parsing."""
+ hostname, fqdn, _ = util.get_hostname_fqdn(
+ cfg={"fqdn": "12345.example.com"}, cloud=None
+ )
+ self.assertEqual("12345", hostname)
+ self.assertEqual("12345.example.com", fqdn)
+
def test_get_hostname_fqdn_from_passes_metadata_only_to_cloud(self):
"""Calls to cloud.get_hostname pass the metadata_only parameter."""
cloud = mock.MagicMock()
--
2.33.0