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