From aacf03969de361c50c6add15cf665335dc593a36 Mon Sep 17 00:00:00 2001 From: Emanuele Giuseppe Esposito Date: Wed, 18 Jan 2023 17:55:16 +0100 Subject: [PATCH 6/8] cc_set_hostname: ignore /var/lib/cloud/data/set-hostname if it's empty (#1967) Reference:https://github.com/canonical/cloud-init/commit/9c7502a801763520639c66125eb373123d1e4f44 Conflict:change tests/unittests/test_handler/test_handler_set_hostname.py not tests/unittests/config/test_cc_set_hostname.py. If the file exists but is empty, do nothing. Otherwise cloud-init will crash because it does not handle the empty file. RHBZ: 2140893 Signed-off-by: Emanuele Giuseppe Esposito --- cloudinit/config/cc_set_hostname.py | 2 +- .../test_handler/test_handler_set_hostname.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cloudinit/config/cc_set_hostname.py b/cloudinit/config/cc_set_hostname.py index a96bcc1..5fb8b75 100644 --- a/cloudinit/config/cc_set_hostname.py +++ b/cloudinit/config/cc_set_hostname.py @@ -84,7 +84,7 @@ def handle(name, cfg, cloud, log, _args): # distro._read_hostname implementation so we only validate one artifact. prev_fn = os.path.join(cloud.get_cpath('data'), "set-hostname") prev_hostname = {} - if os.path.exists(prev_fn): + if os.path.exists(prev_fn) and os.stat(prev_fn).st_size > 0: prev_hostname = util.load_json(util.load_file(prev_fn)) hostname_changed = (hostname != prev_hostname.get('hostname') or fqdn != prev_hostname.get('fqdn')) diff --git a/tests/unittests/test_handler/test_handler_set_hostname.py b/tests/unittests/test_handler/test_handler_set_hostname.py index 1a524c7..0ed9e03 100644 --- a/tests/unittests/test_handler/test_handler_set_hostname.py +++ b/tests/unittests/test_handler/test_handler_set_hostname.py @@ -15,6 +15,7 @@ import os import shutil import tempfile from io import BytesIO +from pathlib import Path from unittest import mock LOG = logging.getLogger(__name__) @@ -204,4 +205,21 @@ class TestHostname(t_help.FilesystemMockingTestCase): ' OOPS on: hostname1.me.com', str(ctx_mgr.exception)) + def test_ignore_empty_previous_artifact_file(self): + cfg = { + "hostname": "blah", + "fqdn": "blah.blah.blah.yahoo.com", + } + distro = self._fetch_distro("debian") + paths = helpers.Paths({"cloud_dir": self.tmp}) + ds = None + cc = cloud.Cloud(ds, paths, {}, distro, None) + self.patchUtils(self.tmp) + prev_fn = Path(cc.get_cpath("data")) / "set-hostname" + prev_fn.touch() + cc_set_hostname.handle("cc_set_hostname", cfg, cc, LOG, []) + contents = util.load_file("/etc/hostname") + self.assertEqual("blah", contents.strip()) + + # vi: ts=4 expandtab -- 2.40.0