Merge lp:~chris.macnaughton/charm-helpers/use-lsblk-to-check-mount into lp:charm-helpers

Proposed by Chris MacNaughton
Status: Merged
Merged at revision: 570
Proposed branch: lp:~chris.macnaughton/charm-helpers/use-lsblk-to-check-mount
Merge into: lp:charm-helpers
Diff against target: 92 lines (+13/-13)
2 files modified
charmhelpers/contrib/storage/linux/utils.py (+5/-5)
tests/contrib/storage/test_linux_storage_utils.py (+8/-8)
To merge this branch: bzr merge lp:~chris.macnaughton/charm-helpers/use-lsblk-to-check-mount
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+292199@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Chris Holcombe (xfactor973) wrote :

+1 from me. This looks good

539. By Chris MacNaughton

Return false in the case where lsblk gives an error

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/storage/linux/utils.py'
2--- charmhelpers/contrib/storage/linux/utils.py 2015-08-07 09:00:30 +0000
3+++ charmhelpers/contrib/storage/linux/utils.py 2016-04-19 15:19:17 +0000
4@@ -64,8 +64,8 @@
5 :returns: boolean: True if the path represents a mounted device, False if
6 it doesn't.
7 '''
8- is_partition = bool(re.search(r".*[0-9]+\b", device))
9- out = check_output(['mount']).decode('UTF-8')
10- if is_partition:
11- return bool(re.search(device + r"\b", out))
12- return bool(re.search(device + r"[0-9]*\b", out))
13+ try:
14+ out = check_output(['lsblk', '-P', device]).decode('UTF-8')
15+ except:
16+ return False
17+ return bool(re.search(r'MOUNTPOINT=".+"', out))
18
19=== modified file 'tests/contrib/storage/test_linux_storage_utils.py'
20--- tests/contrib/storage/test_linux_storage_utils.py 2015-11-02 23:06:58 +0000
21+++ tests/contrib/storage/test_linux_storage_utils.py 2016-04-19 15:19:17 +0000
22@@ -51,7 +51,7 @@
23 def test_is_device_mounted(self, check_output):
24 """It detects mounted devices as mounted."""
25 check_output.return_value = (
26- b"/dev/sda1 on / type ext4 (rw,errors=remount-ro)\n")
27+ b'NAME="sda" MAJ:MIN="8:16" RM="0" SIZE="238.5G" RO="0" TYPE="disk" MOUNTPOINT="/tmp"\n')
28 result = storage_utils.is_device_mounted('/dev/sda')
29 self.assertTrue(result)
30
31@@ -59,7 +59,7 @@
32 def test_is_device_mounted_partition(self, check_output):
33 """It detects mounted partitions as mounted."""
34 check_output.return_value = (
35- b"/dev/sda1 on / type ext4 (rw,errors=remount-ro)\n")
36+ b'NAME="sda1" MAJ:MIN="8:16" RM="0" SIZE="238.5G" RO="0" TYPE="disk" MOUNTPOINT="/tmp"\n')
37 result = storage_utils.is_device_mounted('/dev/sda1')
38 self.assertTrue(result)
39
40@@ -68,7 +68,7 @@
41 """It detects mounted devices as mounted if "mount" shows only a
42 partition as mounted."""
43 check_output.return_value = (
44- b"/dev/sda1 on / type ext4 (rw,errors=remount-ro)\n")
45+ b'NAME="sda1" MAJ:MIN="8:16" RM="0" SIZE="238.5G" RO="0" TYPE="disk" MOUNTPOINT="/tmp"\n')
46 result = storage_utils.is_device_mounted('/dev/sda')
47 self.assertTrue(result)
48
49@@ -76,7 +76,7 @@
50 def test_is_device_mounted_not_mounted(self, check_output):
51 """It detects unmounted devices as not mounted."""
52 check_output.return_value = (
53- b"/dev/foo on / type ext4 (rw,errors=remount-ro)\n")
54+ b'NAME="sda" MAJ:MIN="8:16" RM="0" SIZE="238.5G" RO="0" TYPE="disk" MOUNTPOINT=""\n')
55 result = storage_utils.is_device_mounted('/dev/sda')
56 self.assertFalse(result)
57
58@@ -84,7 +84,7 @@
59 def test_is_device_mounted_not_mounted_partition(self, check_output):
60 """It detects unmounted partitions as not mounted."""
61 check_output.return_value = (
62- b"/dev/foo on / type ext4 (rw,errors=remount-ro)\n")
63+ b'NAME="sda" MAJ:MIN="8:16" RM="0" SIZE="238.5G" RO="0" TYPE="disk" MOUNTPOINT=""\n')
64 result = storage_utils.is_device_mounted('/dev/sda1')
65 self.assertFalse(result)
66
67@@ -92,7 +92,7 @@
68 def test_is_device_mounted_full_disks(self, check_output):
69 """It detects mounted full disks as mounted."""
70 check_output.return_value = (
71- b"/dev/sda on / type ext4 (rw,errors=remount-ro)\n")
72+ b'NAME="sda" MAJ:MIN="8:16" RM="0" SIZE="238.5G" RO="0" TYPE="disk" MOUNTPOINT="/tmp"\n')
73 result = storage_utils.is_device_mounted('/dev/sda')
74 self.assertTrue(result)
75
76@@ -100,7 +100,7 @@
77 def test_is_device_mounted_cciss(self, check_output):
78 """It detects mounted cciss partitions as mounted."""
79 check_output.return_value = (
80- b"/dev/cciss/c0d0 on / type ext4 (rw,errors=remount-ro)\n")
81+ b'NAME="cciss!c0d0" MAJ:MIN="104:0" RM="0" SIZE="273.3G" RO="0" TYPE="disk" MOUNTPOINT="/root"\n')
82 result = storage_utils.is_device_mounted('/dev/cciss/c0d0')
83 self.assertTrue(result)
84
85@@ -108,6 +108,6 @@
86 def test_is_device_mounted_cciss_not_mounted(self, check_output):
87 """It detects unmounted cciss partitions as not mounted."""
88 check_output.return_value = (
89- b"/dev/cciss/c0d1 on / type ext4 (rw,errors=remount-ro)\n")
90+ b'NAME="cciss!c0d0" MAJ:MIN="104:0" RM="0" SIZE="273.3G" RO="0" TYPE="disk" MOUNTPOINT=""\n')
91 result = storage_utils.is_device_mounted('/dev/cciss/c0d0')
92 self.assertFalse(result)

Subscribers

People subscribed via source and target branches