sailesh1993 / rpms / cloud-init

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