cf80c1
From c3a019b57cade8e6c3963f6bd2c7c15cd67e561c Mon Sep 17 00:00:00 2001
cf80c1
From: Eduardo Otubo <otubo@redhat.com>
cf80c1
Date: Wed, 2 Sep 2020 14:59:06 +0200
cf80c1
Subject: [PATCH] cc_mounts: fix incorrect format specifiers (#316)
cf80c1
cf80c1
RH-Author: Eduardo Otubo <otubo@redhat.com>
cf80c1
Message-id: <20200825131749.4989-1-otubo@redhat.com>
cf80c1
Patchwork-id: 98217
cf80c1
O-Subject: [RHEL-8.3.0 cloud-init PATCH] cc_mounts: fix incorrect format specifiers (#316)
cf80c1
Bugzilla: 1794664
cf80c1
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
cf80c1
RH-Acked-by: Cathy Avery <cavery@redhat.com>
cf80c1
cf80c1
Conflicts: Not exactly a conflict, but removed optional notations
cf80c1
"variable: type" and "-> type" from function header create_swapfile() as
cf80c1
it is only available on Python >= 3.5 and this patch is for RHEL-7.9
cf80c1
only (Python 2.*). The rest of the cherry-pick was clean.
cf80c1
cf80c1
commit 9d7b35ce23aaf8741dd49b16e359c96591be3c76
cf80c1
Author: Daniel Watkins <oddbloke@ubuntu.com>
cf80c1
Date:   Wed Apr 15 16:53:08 2020 -0400
cf80c1
cf80c1
    cc_mounts: fix incorrect format specifiers (#316)
cf80c1
cf80c1
    LP: #1872836
cf80c1
cf80c1
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
cf80c1
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
cf80c1
---
cf80c1
 cloudinit/config/cc_mounts.py         |  6 +++---
cf80c1
 cloudinit/config/tests/test_mounts.py | 22 ++++++++++++++++++++++
cf80c1
 2 files changed, 25 insertions(+), 3 deletions(-)
cf80c1
 create mode 100644 cloudinit/config/tests/test_mounts.py
cf80c1
cf80c1
diff --git a/cloudinit/config/cc_mounts.py b/cloudinit/config/cc_mounts.py
cf80c1
index e1c43e3..55b6770 100644
cf80c1
--- a/cloudinit/config/cc_mounts.py
cf80c1
+++ b/cloudinit/config/cc_mounts.py
cf80c1
@@ -226,17 +226,17 @@ def suggested_swapsize(memsize=None, maxsize=None, fsys=None):
cf80c1
 def create_swapfile(fname, size):
cf80c1
     """Size is in MiB."""
cf80c1
 
cf80c1
-    errmsg = "Failed to create swapfile '%s' of size %dMB via %s: %s"
cf80c1
+    errmsg = "Failed to create swapfile '%s' of size %sMB via %s: %s"
cf80c1
 
cf80c1
     def create_swap(fname, size, method):
cf80c1
         LOG.debug("Creating swapfile in '%s' on fstype '%s' using '%s'",
cf80c1
                   fname, fstype, method)
cf80c1
 
cf80c1
         if method == "fallocate":
cf80c1
-            cmd = ['fallocate', '-l', '%dM' % size, fname]
cf80c1
+            cmd = ['fallocate', '-l', '%sM' % size, fname]
cf80c1
         elif method == "dd":
cf80c1
             cmd = ['dd', 'if=/dev/zero', 'of=%s' % fname, 'bs=1M',
cf80c1
-                   'count=%d' % size]
cf80c1
+                   'count=%s' % size]
cf80c1
 
cf80c1
         try:
cf80c1
             util.subp(cmd, capture=True)
cf80c1
diff --git a/cloudinit/config/tests/test_mounts.py b/cloudinit/config/tests/test_mounts.py
cf80c1
new file mode 100644
cf80c1
index 0000000..c7dad61
cf80c1
--- /dev/null
cf80c1
+++ b/cloudinit/config/tests/test_mounts.py
cf80c1
@@ -0,0 +1,22 @@
cf80c1
+# This file is part of cloud-init. See LICENSE file for license information.
cf80c1
+from unittest import mock
cf80c1
+
cf80c1
+from cloudinit.config.cc_mounts import create_swapfile
cf80c1
+
cf80c1
+
cf80c1
+M_PATH = 'cloudinit.config.cc_mounts.'
cf80c1
+
cf80c1
+
cf80c1
+class TestCreateSwapfile:
cf80c1
+
cf80c1
+    @mock.patch(M_PATH + 'util.subp')
cf80c1
+    def test_happy_path(self, m_subp, tmpdir):
cf80c1
+        swap_file = tmpdir.join("swap-file")
cf80c1
+        fname = str(swap_file)
cf80c1
+
cf80c1
+        # Some of the calls to util.subp should create the swap file; this
cf80c1
+        # roughly approximates that
cf80c1
+        m_subp.side_effect = lambda *args, **kwargs: swap_file.write('')
cf80c1
+
cf80c1
+        create_swapfile(fname, '')
cf80c1
+        assert mock.call(['mkswap', fname]) in m_subp.call_args_list
cf80c1
-- 
cf80c1
1.8.3.1
cf80c1