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