Merge lp:~sylvain-pineau/checkbox/fix-1458462 into lp:checkbox

Proposed by Sylvain Pineau
Status: Merged
Approved by: Sylvain Pineau
Approved revision: 4201
Merged at revision: 4200
Proposed branch: lp:~sylvain-pineau/checkbox/fix-1458462
Merge into: lp:checkbox
Diff against target: 192 lines (+58/-23)
1 file modified
providers/plainbox-provider-checkbox/bin/removable_storage_test (+58/-23)
To merge this branch: bzr merge lp:~sylvain-pineau/checkbox/fix-1458462
Reviewer Review Type Date Requested Status
Pierre Equoy Approve
Checkbox Developers Pending
Review via email: mp+284791@code.launchpad.net

Description of the change

Fixes the linked bug where the removable storage test was run on internal eMMC drives.

We are now checking the / mount point using lsblk to discard partitions belonging to the same drive.

To post a comment you must log in.
Revision history for this message
Pierre Equoy (pieq) wrote :

+1

It works well on the two devices I tested it on, one with a NVMe drive + HDD (and a SD card reader), one with eMMC drive (and a micro SD card reader).

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'providers/plainbox-provider-checkbox/bin/removable_storage_test'
2--- providers/plainbox-provider-checkbox/bin/removable_storage_test 2014-10-19 18:18:41 +0000
3+++ providers/plainbox-provider-checkbox/bin/removable_storage_test 2016-02-02 20:32:54 +0000
4@@ -6,6 +6,8 @@
5 import hashlib
6 import logging
7 import os
8+import re
9+import shlex
10 import subprocess
11 import sys
12 import tempfile
13@@ -26,6 +28,7 @@
14 from checkbox_support.parsers.udevadm import CARD_READER_RE
15 from checkbox_support.parsers.udevadm import GENERIC_RE
16 from checkbox_support.parsers.udevadm import FLASH_RE
17+from checkbox_support.parsers.udevadm import find_pkname_is_root_mountpoint
18 from checkbox_support.udev import get_interconnect_speed
19 from checkbox_support.udev import get_udev_block_devices
20 from checkbox_support.udev import get_udev_xhci_devices
21@@ -97,7 +100,7 @@
22 class DiskTest():
23 ''' Class to contain various methods for testing removable disks '''
24
25- def __init__(self, device, memorycard):
26+ def __init__(self, device, memorycard, lsblkcommand):
27 self.rem_disks = {} # mounted before the script running
28 self.rem_disks_nm = {} # not mounted before the script running
29 self.rem_disks_memory_cards = {}
30@@ -106,8 +109,10 @@
31 # LP: #1313581, TODO: extend to be rem_disks_driver
32 self.rem_disks_xhci = {}
33 self.data = ''
34+ self.lsblk = ''
35 self.device = device
36 self.memorycard = memorycard
37+ self._run_lsblk(lsblkcommand)
38 self._probe_disks()
39
40 def read_file(self, source):
41@@ -145,6 +150,24 @@
42 logging.error("Unable to remove tempfile %s", target)
43 logging.error(" %s", exc)
44
45+ def _find_parent(self, device):
46+ if self.lsblk:
47+ pattern = re.compile('KNAME="(?P<KNAME>.*)" '
48+ 'TYPE="(?P<TYPE>.*)" '
49+ 'MOUNTPOINT="(?P<MOUNTPOINT>.*)"')
50+ for line in self.lsblk.splitlines():
51+ m = pattern.match(line)
52+ if m and device.startswith(m.group('KNAME')):
53+ return m.group('KNAME')
54+ return False
55+
56+ def _run_lsblk(self, lsblkcommand):
57+ try:
58+ self.lsblk = subprocess.check_output(shlex.split(lsblkcommand),
59+ universal_newlines=True)
60+ except subprocess.CalledProcessError as exc:
61+ raise SystemExit(exc)
62+
63 def _probe_disks(self):
64 """
65 Internal method used to probe for available disks
66@@ -223,6 +246,9 @@
67 # Get the block device pathname,
68 # to avoid the confusion, this is something like /dev/sdbX
69 dev_file = udev_device.get_device_file()
70+ parent = self._find_parent(dev_file.replace('/dev/', ''))
71+ if parent and find_pkname_is_root_mountpoint(parent, self.lsblk):
72+ continue
73 # Get the list of mount points of this block device
74 mount_points = (
75 udisks2_object[UDISKS2_FILESYSTEM_INTERFACE]['MountPoints'])
76@@ -280,15 +306,15 @@
77 parent_vendor = parent_props.Get(udisks, "DriveVendor")
78 parent_media = parent_props.Get(udisks, "DriveMedia")
79 if self.memorycard:
80- if (dev_bus != 'sdio'
81- and not FLASH_RE.search(parent_media)
82- and not CARD_READER_RE.search(parent_model)
83- and not GENERIC_RE.search(parent_vendor)):
84+ if (dev_bus != 'sdio' and not
85+ FLASH_RE.search(parent_media) and not
86+ CARD_READER_RE.search(parent_model) and not
87+ GENERIC_RE.search(parent_vendor)):
88 continue
89 else:
90- if (FLASH_RE.search(parent_media)
91- or CARD_READER_RE.search(parent_model)
92- or GENERIC_RE.search(parent_vendor)):
93+ if (FLASH_RE.search(parent_media) or
94+ CARD_READER_RE.search(parent_model) or
95+ GENERIC_RE.search(parent_vendor)):
96 continue
97 dev_file = str(device_props.Get(udisks, "DeviceFile"))
98 dev_speed = str(device_props.Get(udisks,
99@@ -376,9 +402,11 @@
100 # then compare this pci slot name to the other
101 dl = devpath.split('/')
102 s = set([x for x in dl if dl.count(x) > 1])
103- if ((pci_slot_name in dl)
104- and (dl.index(pci_slot_name) < dl.index('block'))
105- and (not(pci_slot_name in s))):
106+ if (
107+ (pci_slot_name in dl) and
108+ (dl.index(pci_slot_name) < dl.index('block')) and
109+ (not(pci_slot_name in s))
110+ ):
111 # 1. there is such pci_slot_name
112 # 2. sysfs topology looks like
113 # DEVPATH = ....../pci_slot_name/....../block/......
114@@ -451,10 +479,14 @@
115 choices=['xhci_hcd'],
116 help=("Detect the driver of the host controller."
117 "Only xhci_hcd for usb3 is supported so far."))
118+ parser.add_argument("--lsblkcommand", action='store', type=str,
119+ default="lsblk -i -n -P -o KNAME,TYPE,MOUNTPOINT",
120+ help=("Command to execute to get lsblk information. "
121+ "Only change it if you know what you're doing."))
122
123 args = parser.parse_args()
124
125- test = DiskTest(args.device, args.memorycard)
126+ test = DiskTest(args.device, args.memorycard, args.lsblkcommand)
127
128 errors = 0
129 # If we do have removable drives attached and mounted
130@@ -505,8 +537,8 @@
131 % errors_mount)
132 errors += errors_mount
133
134- disks_all = dict(list(test.rem_disks.items())
135- + list(test.rem_disks_nm.items()))
136+ disks_all = dict(list(test.rem_disks.items()) +
137+ list(test.rem_disks_nm.items()))
138
139 if len(disks_all) > 0:
140 print("Found the following mounted %s partitions:"
141@@ -528,8 +560,8 @@
142
143 disks_eligible = {disk: disks_all[disk] for disk in disks_all
144 if not args.min_speed or
145- int(test.rem_disks_speed[disk])
146- >= int(args.min_speed)}
147+ int(test.rem_disks_speed[disk]) >=
148+ int(args.min_speed)}
149 if len(disks_eligible) == 0:
150 logging.error(
151 "No %s disks with speed higher than %s bits/s",
152@@ -542,21 +574,24 @@
153 stat = os.statvfs(path)
154 disks_freespace[disk] = stat.f_bfree * stat.f_bsize
155 smallest_freespace = min(disks_freespace.values())
156+ smallest_partition = [d for d, v in disks_freespace.items() if
157+ v == smallest_freespace][0]
158 desired_size = args.size
159 if desired_size > smallest_freespace:
160 if args.auto_reduce_size:
161 min_space = HumanReadableBytes("1MiB")
162 if smallest_freespace < min_space:
163- raise IOError("Not enough space. {} is required"
164- .format(min_space))
165- new_size = HumanReadableBytes(int(0.8 * smallest_freespace))
166+ sys.exit("Not enough space. {} is required on {}"
167+ .format(min_space, smallest_partition))
168+ new_size = HumanReadableBytes(
169+ int(0.8 * smallest_freespace))
170 logging.warning("Automatically reducing test data size"
171 ". {} requested. Reducing to {}."
172 .format(desired_size, new_size))
173 desired_size = new_size
174 else:
175- raise IOError("Not enough space. {} is required"
176- .format(desired_size))
177+ sys.exit("Not enough space. {} is required on {}"
178+ .format(desired_size, smallest_partition))
179 # Generate our data file(s)
180 for count in range(args.count):
181 test_files[count] = RandomData(desired_size)
182@@ -611,8 +646,8 @@
183 # avg_write_time = total_write_time / args.count
184 try:
185 avg_write_speed = ((
186- total_write_size / total_write_time)
187- / 1024 / 1024)
188+ total_write_size / total_write_time) /
189+ 1024 / 1024)
190 except ZeroDivisionError:
191 avg_write_speed = 0.00
192 finally:

Subscribers

People subscribed via source and target branches