Skip to content

Commit 555345c

Browse files
author
Jamie Smith
authored
Fix the USB Mass Storage Device test so it can run as a CI user without root (#65)
1 parent ffc3367 commit 555345c

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

drivers/usb/tests/TESTS/host_tests/pyusb_msd.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,30 @@ def _disk_path_windows(serial):
196196

197197
@staticmethod
198198
def _disk_path_linux(serial):
199-
output = subprocess.check_output(['lsblk', '-dnoserial,mountpoint']).split(b'\n')
199+
200+
# This generates a table of serial number, mount point (e.g. /media/foo/DISK), and path (e.g. /dev/sdd)
201+
output = subprocess.check_output(['lsblk', '-dnoserial,mountpoint,path']).decode("UTF-8").split('\n')
200202
for line in output:
201-
serial_and_mount_point = line.split()
202-
if len(serial_and_mount_point) == 2:
203-
if serial_and_mount_point[0] == str(serial):
204-
return serial_and_mount_point[1]
203+
fields = line.split()
204+
205+
if len(fields) >= 2 and fields[0] == str(serial):
206+
# Found the correct device
207+
208+
if len(fields) == 2:
209+
# "mountpoint" column (idx 1) is empty, meaning the device is not mounted.
210+
# Ask the OS to mount it under our user account.
211+
# Note: This requires that no other processes are trying to automount disks at the same
212+
# time -- this often requires changing file manager settings.
213+
subprocess.check_call(['udisksctl', 'mount', '-b', fields[1]])
214+
215+
# The OS will now mount the disk. Return so that the query can be run again and we'll get it
216+
# next time.
217+
return None
218+
219+
else:
220+
# Disk has a mount point, return it
221+
return fields[1]
222+
205223
return None
206224

207225
@staticmethod

drivers/usb/tests/TESTS/usb_device/README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Testing the Mbed OS USB device
22

33
## Setup
4-
Before running tests, please make sure to use a
5-
[top-level requirements.txt][LN-requirements] file to install all the
4+
Before running tests, please make sure to install all the
65
required Python modules.
76

87
```
9-
pip install -r requirements.txt
8+
pip install -r mbed-os/tools/requirements-ci-build.txt
109
```
1110

1211
Additional, platform-specific setup is described below.
@@ -29,7 +28,7 @@ See also [Known issues](#known-issues).
2928
1. Plug both USB interfaces (*DAPLink* and *USB device*).
3029

3130
### Linux
32-
1. Install the `hidapi` Python module, otherwise some USB HID test cases will
31+
1. Install the `hidapi` Python module, otherwise some USB HID test cases will
3332
be skipped. This module is not installed during the initial setup due to
3433
external dependencies for Linux.
3534

@@ -47,21 +46,26 @@ See also [Known issues](#known-issues).
4746
```bash
4847
pip install -r TESTS/usb_device/hid/requirements.txt
4948
```
50-
51-
1. Update the `udev` rules for Mbed USB CDC device as follows
52-
([source][LN-udev_rules]):
49+
2. Add your user to the `plugdev` group with `sudo usermod -G plugdev <your username>`
50+
3. Update the `udev` rules for the USB VIDs/PIDs used in the test as follows:
5351

5452
```bash
5553
sudo tee /etc/udev/rules.d/99-ttyacms.rules >/dev/null <<EOF
56-
ATTRS{idVendor}=="1f00" ATTRS{idProduct}=="2013", ENV{ID_MM_DEVICE_IGNORE}="1"
57-
ATTRS{idVendor}=="1f00" ATTRS{idProduct}=="2012", ENV{ID_MM_DEVICE_IGNORE}="1"
54+
# Mbed OS USB Device test suite
55+
ATTRS{idVendor}=="0d28", ATTRS{idProduct}=="0007", MODE="660", GROUP="plugdev", TAG+="uaccess"
56+
ATTRS{idVendor}=="0d28", ATTRS{idProduct}=="0205", MODE="660", GROUP="plugdev", TAG+="uaccess"
57+
ATTRS{idVendor}=="0d28", ATTRS{idProduct}=="0206", MODE="660", GROUP="plugdev", TAG+="uaccess"
58+
ATTRS{idVendor}=="1f00" ATTRS{idProduct}=="2013", ENV{ID_MM_DEVICE_IGNORE}="1", MODE="660", GROUP="plugdev"
59+
ATTRS{idVendor}=="1f00" ATTRS{idProduct}=="2012", ENV{ID_MM_DEVICE_IGNORE}="1", MODE="660", GROUP="plugdev"
5860
EOF
5961
sudo udevadm control --reload-rules
62+
sudo udevadm trigger
6063
```
6164
62-
This will prevent the `ModemManager` daemon from automatically opening the
65+
Among other things, this will [prevent][LN-udev_rules] the `ModemManager` daemon from automatically opening the
6366
port and sending the `AT commands`, which it does for every new
6467
`/dev/ttyACM` device registered in system.
68+
4. Install the `udisks2` package, which the test script uses to mount USB disks. Additionally, you may need to disable any automounting of disks provided by your file manager / distro.
6569
6670
### Mac
6771
No setup method has been verified for this platform.
@@ -152,7 +156,6 @@ To prevent Windows from writing to removable drives on connect drive indexing ca
152156
You may want to connect the device directly to the host machine with no hubs on the way.
153157
154158
<!-- LINKS -->
155-
[LN-requirements]: ../../requirements.txt
156159
[LN-zadig]: https://zadig.akeo.ie/
157160
[LN-zadig_conf1]: basic/zadig_conf/mbed_os-usb_test_device1.cfg
158161
[LN-zadig_conf2]: basic/zadig_conf/mbed_os-usb_test_device2.cfg

0 commit comments

Comments
 (0)