1b4239
From d51546dee17c9abbb9d44fb33cf81be085a46dae Mon Sep 17 00:00:00 2001
e0a765
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
1b4239
Date: Thu, 19 Jan 2023 09:40:10 +0100
1b4239
Subject: [PATCH 22/22] cc_set_hostname: ignore
1b4239
 /var/lib/cloud/data/set-hostname if it's empty (#1967)
e0a765
1b4239
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2140893
e0a765
e0a765
commit 9c7502a801763520639c66125eb373123d1e4f44
e0a765
Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
e0a765
Date:   Wed Jan 18 17:55:16 2023 +0100
e0a765
e0a765
    cc_set_hostname: ignore /var/lib/cloud/data/set-hostname if it's empty (#1967)
e0a765
e0a765
    If the file exists but is empty, do nothing.
e0a765
    Otherwise cloud-init will crash because it does not handle the empty file.
e0a765
e0a765
    RHBZ: 2140893
e0a765
e0a765
    Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
e0a765
e0a765
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
e0a765
---
e0a765
 cloudinit/config/cc_set_hostname.py            |  2 +-
e0a765
 tests/unittests/config/test_cc_set_hostname.py | 17 +++++++++++++++++
e0a765
 2 files changed, 18 insertions(+), 1 deletion(-)
e0a765
e0a765
diff --git a/cloudinit/config/cc_set_hostname.py b/cloudinit/config/cc_set_hostname.py
e0a765
index 2674fa20..7e3d5b74 100644
e0a765
--- a/cloudinit/config/cc_set_hostname.py
e0a765
+++ b/cloudinit/config/cc_set_hostname.py
e0a765
@@ -86,7 +86,7 @@ def handle(name, cfg, cloud, log, _args):
e0a765
     # distro._read_hostname implementation so we only validate  one artifact.
e0a765
     prev_fn = os.path.join(cloud.get_cpath("data"), "set-hostname")
e0a765
     prev_hostname = {}
e0a765
-    if os.path.exists(prev_fn):
e0a765
+    if os.path.exists(prev_fn) and os.stat(prev_fn).st_size > 0:
e0a765
         prev_hostname = util.load_json(util.load_file(prev_fn))
e0a765
     hostname_changed = hostname != prev_hostname.get(
e0a765
         "hostname"
e0a765
diff --git a/tests/unittests/config/test_cc_set_hostname.py b/tests/unittests/config/test_cc_set_hostname.py
e0a765
index 3d1d86ee..2c92949f 100644
e0a765
--- a/tests/unittests/config/test_cc_set_hostname.py
e0a765
+++ b/tests/unittests/config/test_cc_set_hostname.py
e0a765
@@ -5,6 +5,7 @@ import os
e0a765
 import shutil
e0a765
 import tempfile
e0a765
 from io import BytesIO
e0a765
+from pathlib import Path
e0a765
 from unittest import mock
e0a765
 
e0a765
 from configobj import ConfigObj
e0a765
@@ -242,5 +243,21 @@ class TestHostname(t_help.FilesystemMockingTestCase):
e0a765
             str(ctx_mgr.exception),
e0a765
         )
e0a765
 
e0a765
+    def test_ignore_empty_previous_artifact_file(self):
e0a765
+        cfg = {
e0a765
+            "hostname": "blah",
e0a765
+            "fqdn": "blah.blah.blah.yahoo.com",
e0a765
+        }
e0a765
+        distro = self._fetch_distro("debian")
e0a765
+        paths = helpers.Paths({"cloud_dir": self.tmp})
e0a765
+        ds = None
e0a765
+        cc = cloud.Cloud(ds, paths, {}, distro, None)
e0a765
+        self.patchUtils(self.tmp)
e0a765
+        prev_fn = Path(cc.get_cpath("data")) / "set-hostname"
e0a765
+        prev_fn.touch()
e0a765
+        cc_set_hostname.handle("cc_set_hostname", cfg, cc, LOG, [])
e0a765
+        contents = util.load_file("/etc/hostname")
e0a765
+        self.assertEqual("blah", contents.strip())
e0a765
+
e0a765
 
e0a765
 # vi: ts=4 expandtab
e0a765
-- 
e0a765
2.39.1
e0a765