Tracker Miner FS has code to index removable devices, and test coverage is around 0%. I would like to improve that by adding some testcases that connect and disconnect fake removable media, and I want to check if there’s an easy way to do it.
The test must work inside a container on Gitlab CI and must not require root. The test needs to trigger GVolumeMonitor events on device added/removed. And I want to integrate it with existing testing infrastructure written in Python.
So… I tried to implement a TestVolumeMonitor class in Python and register it using Gio.ExtensionPoint.implement(), as follows:
from gi.repository import Gio
import os
# From GLib: gio/gvolumemonitor.h
G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME = "gio-native-volume-monitor"
class TestNativeVolumeMonitor(Gio.NativeVolumeMonitor):
__gtype_name__ = 'TestNativeVolumeMonitor'
def do_is_supported(self):
return True
def main():
# Trigger _g_io_modules_ensure_extension_points_registered()
vfs = Gio.Vfs.get_local()
Gio.IOExtensionPoint.implement(G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME, TestNativeVolumeMonitor.__gtype__, 'MyTest', 1)
os.environ['GIO_USE_VOLUME_MONITOR'] = 'MyTest'
vm = Gio.VolumeMonitor.get()
main()
This almost works, but do_is_selected()
doesn’t get hooked up to the TestNativeVolumeMonitor vtable, leading to a crash in giomodule.c - try_class(). PyGI only hooks up vfuncs which have a corresponding virtual-method
introspection annotation, and I guess is_selected
is considered private.
The fallback approach seems to be creating a TestVolumeMonitor class in a C library, which can be imported into Python and used from there. I hope to get some time to develop this approach further.
In the meantime, has anyone implemented something like this before? I would love to avoid writing such a thing from scratch…