Skip to content

Commit

Permalink
Add test that removing a parent USB device removes its children
Browse files Browse the repository at this point in the history
Verify that calling `remove_device()` on a parent USB device will remove
its children and generate "remove" events for their removal as well.
  • Loading branch information
bobhenz-jabil committed Jul 24, 2024
1 parent 92f7242 commit 8018e7e
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tests/test-umockdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,55 @@ def on_uevent(client, action, device, counters):
mainloop.run()
self.assertEqual(counter[syspath], [1, 1])

def test_remove_usb_parent_device(self):
'''testbed removing a USB parent device removes children'''

counter = {}

def on_uevent(client, action, device, counters):
key = device.get_sysfs_path()
counters.setdefault(key, [0, 0])
if action == 'add':
counters[key][0] += 1
elif action == 'remove':
counters[key][1] += 1

# set up listener for uevent signal
client = GUdev.Client.new(['usb'])
client.connect('uevent', on_uevent, counter)
mainloop = GLib.MainLoop()

parent_syspath = self.testbed.add_device('usb', 'myparentdev', None, ['idVendor', '0815'], ['ID_INPUT', '1'])
self.assertNotEqual(parent_syspath, None)

child1_syspath = self.testbed.add_device('usb', 'child1', parent_syspath, ['idVendor', '0815'], ['ID_INPUT', '1'])
self.assertNotEqual(child1_syspath, None)

child2_syspath = self.testbed.add_device('usb', 'child2', parent_syspath, ['idVendor', '0815'], ['ID_INPUT', '1'])
self.assertNotEqual(child2_syspath, None)

# Run the main loop for 0.5 seconds to catch the "add" uevent, which
# should be automatically generated by the call to `add_device()`.
GLib.timeout_add(500, mainloop.quit)
mainloop.run()
self.assertIn(parent_syspath, counter)
self.assertIn(child1_syspath, counter)
self.assertIn(child2_syspath, counter)
self.assertEqual(counter[parent_syspath], [1, 0])
self.assertEqual(counter[child1_syspath], [1, 0])
self.assertEqual(counter[child2_syspath], [1, 0])

# Just remove the parent, the children should also be removed.
self.testbed.remove_device(parent_syspath)

# Run the main loop for 0.5 seconds to catch the "remove" uevent, which
# should be automatically generated by the call to `remove_device()`.
GLib.timeout_add(500, mainloop.quit)
mainloop.run()
self.assertEqual(counter[parent_syspath], [1, 1])
self.assertEqual(counter[child1_syspath], [1, 1])
self.assertEqual(counter[child2_syspath], [1, 1])

def test_add_from_string(self):
self.assertTrue(self.testbed.add_from_string('''P: /devices/dev1
E: SIMPLE_PROP=1
Expand Down

0 comments on commit 8018e7e

Please sign in to comment.