Merge ~paul-meyer/cloud-init:bug-1687712 into cloud-init:master

Proposed by Paul Meyer on 2017-05-03
Status: Merged
Merge reported by: Scott Moser
Merged at revision: not available
Proposed branch: ~paul-meyer/cloud-init:bug-1687712
Merge into: cloud-init:master
Diff against target: 130 lines (+66/-8)
3 files modified
cloudinit/config/cc_disk_setup.py (+14/-5)
doc/examples/cloud-config-disk-setup.txt (+3/-3)
tests/unittests/test_handler/test_handler_disk_setup.py (+49/-0)
Reviewer Review Type Date Requested Status
Chad Smith Approve on 2017-05-03
Server Team CI bot continuous-integration Approve on 2017-05-03
cloud-init commiters 2017-05-03 Pending
Review via email: mp+323532@code.launchpad.net

Description of the Change

Fixes 1687712. Added some tests for mkfs that verify bug and fix

To post a comment you must log in.
Chad Smith (chad.smith) :
Scott Moser (smoser) wrote :

Paul,
Thanks for finding this bug and fixing.

Please don't take my comments as harsh. Your help is really appreciated.

There are a few small comments inline.

Paul Meyer (paul-meyer) wrote :

Of course, all comments much appreciated. @Chad, would adding warning for options that are ignored (like having "cmd" and "extra_opts") cover your comment as well?

Chad Smith (chad.smith) wrote :

Thanks for the bug and fix here paul.

Chad Smith (chad.smith) wrote :

Minor comments inline.

Chad Smith (chad.smith) :
review: Approve
Paul Meyer (paul-meyer) wrote :

Updated code with CR suggestions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/config/cc_disk_setup.py b/cloudinit/config/cc_disk_setup.py
2index f49386e..5aeef4b 100644
3--- a/cloudinit/config/cc_disk_setup.py
4+++ b/cloudinit/config/cc_disk_setup.py
5@@ -910,12 +910,21 @@ def mkfs(fs_cfg):
6 "must be set.", label)
7
8 # Create the commands
9+ shell = False
10 if fs_cmd:
11 fs_cmd = fs_cfg['cmd'] % {
12 'label': label,
13 'filesystem': fs_type,
14 'device': device,
15 }
16+ shell = True
17+
18+ if overwrite:
19+ LOG.warning('fs_setup option "overwrite" ignored because "cmd" ' +
20+ 'was specified')
21+ if fs_opts:
22+ LOG.warning('fs_setup option "extra_opts" ignored because "cmd" ' +
23+ 'was specified')
24 else:
25 # Find the mkfs command
26 mkfs_cmd = util.which("mkfs.%s" % fs_type)
27@@ -936,14 +945,14 @@ def mkfs(fs_cfg):
28 if overwrite or device_type(device) == "disk":
29 fs_cmd.append(lookup_force_flag(fs_type))
30
31- # Add the extends FS options
32- if fs_opts:
33- fs_cmd.extend(fs_opts)
34+ # Add the extends FS options
35+ if fs_opts:
36+ fs_cmd.extend(fs_opts)
37
38 LOG.debug("Creating file system %s on %s", label, device)
39- LOG.debug(" Using cmd: %s", " ".join(fs_cmd))
40+ LOG.debug(" Using cmd: %s", str(fs_cmd))
41 try:
42- util.subp(fs_cmd)
43+ util.subp(fs_cmd, shell=shell)
44 except Exception as e:
45 raise Exception("Failed to exec of '%s':\n%s" % (fs_cmd, e))
46
47diff --git a/doc/examples/cloud-config-disk-setup.txt b/doc/examples/cloud-config-disk-setup.txt
48index 3e46a22..38ad052 100644
49--- a/doc/examples/cloud-config-disk-setup.txt
50+++ b/doc/examples/cloud-config-disk-setup.txt
51@@ -155,11 +155,11 @@ fs_setup:
52 filesystem: 'ext3'
53 device: 'ephemeral0'
54 partition: 'auto'
55- - label: mylabl2
56+ - label: mylabl2
57 filesystem: 'ext4'
58 device: '/dev/xvda1'
59- - special:
60- cmd: mkfs -t %(FILESYSTEM)s -L %(LABEL)s %(DEVICE)s
61+ - cmd: mkfs -t %(filesystem)s -L %(label)s %(device)s
62+ label: mylabl3
63 filesystem: 'btrfs'
64 device: '/dev/xvdh'
65
66diff --git a/tests/unittests/test_handler/test_handler_disk_setup.py b/tests/unittests/test_handler/test_handler_disk_setup.py
67index 7ff3922..36e53c7 100644
68--- a/tests/unittests/test_handler/test_handler_disk_setup.py
69+++ b/tests/unittests/test_handler/test_handler_disk_setup.py
70@@ -17,6 +17,10 @@ class TestIsDiskUsed(TestCase):
71 self.check_fs = self.patches.enter_context(
72 mock.patch('{0}.check_fs'.format(mod_name)))
73
74+ def tearDown(self):
75+ super(TestIsDiskUsed, self).tearDown()
76+ self.patches.close()
77+
78 def test_multiple_child_nodes_returns_true(self):
79 self.enumerate_disk.return_value = (mock.MagicMock() for _ in range(2))
80 self.check_fs.return_value = (mock.MagicMock(), None, mock.MagicMock())
81@@ -147,4 +151,49 @@ class TestUpdateFsSetupDevices(TestCase):
82 'filesystem': 'xfs'
83 }, fs_setup)
84
85+
86+@mock.patch('cloudinit.config.cc_disk_setup.find_device_node',
87+ return_value=('/dev/xdb1', False))
88+@mock.patch('cloudinit.config.cc_disk_setup.device_type', return_value=None)
89+@mock.patch('cloudinit.config.cc_disk_setup.util.subp', return_value=('', ''))
90+class TestMkfsCommandHandling(TestCase):
91+
92+ def test_with_cmd(self, subp, *args):
93+ with self.assertLogs(
94+ 'cloudinit.config.cc_disk_setup') as logs:
95+ cc_disk_setup.mkfs({
96+ 'cmd': 'mkfs -t %(filesystem)s -L %(label)s %(device)s',
97+ 'filesystem': 'ext4',
98+ 'device': '/dev/xdb1',
99+ 'label': 'with_cmd',
100+ 'extra_opts': ['should', 'generate', 'warning'],
101+ 'overwrite': 'should generate warning too'
102+ })
103+
104+ self.assertIn(
105+ 'WARNING:cloudinit.config.cc_disk_setup:' +
106+ 'fs_setup option "extra_opts" ignored because "cmd" was specified',
107+ logs.output)
108+ self.assertIn(
109+ 'WARNING:cloudinit.config.cc_disk_setup:' +
110+ 'fs_setup option "overwrite" ignored because "cmd" was specified',
111+ logs.output)
112+
113+ subp.assert_called_once_with(
114+ 'mkfs -t ext4 -L with_cmd /dev/xdb1', shell=True)
115+
116+ def test_without_cmd(self, subp, *args):
117+ cc_disk_setup.mkfs({
118+ 'filesystem': 'ext4',
119+ 'device': '/dev/xdb1',
120+ 'label': 'without_cmd',
121+ 'extra_opts': ['are', 'added'],
122+ 'overwrite': True
123+ })
124+
125+ subp.assert_called_once_with(
126+ ['/sbin/mkfs.ext4', '/dev/xdb1',
127+ '-L', 'without_cmd', '-F', 'are', 'added'],
128+ shell=False)
129+
130 # vi: ts=4 expandtab

Subscribers

People subscribed via source and target branches