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