From 795ecaf85189e2ffa09732607115ca3063f4f474 Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Mon, 15 Nov 2021 05:58:12 +0000 Subject: [PATCH 1/2] nixos/tests/mtp: init Adds a fully fledged NixOS VM integration test which uses jmtpfs and gvfs to test the functionality of MTP inside of NixOS. It uses USB device emulation in QEMU to create MTP device(s) which can be tested against. Co-authored-by: nixinator <33lockdown33@protonmail.com> --- nixos/tests/all-tests.nix | 1 + nixos/tests/mtp.nix | 108 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 nixos/tests/mtp.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 80645283c872..f35ecca4344a 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -277,6 +277,7 @@ in mosquitto = handleTest ./mosquitto.nix {}; mpd = handleTest ./mpd.nix {}; mpv = handleTest ./mpv.nix {}; + mtp = handleTest ./mtp.nix {}; mumble = handleTest ./mumble.nix {}; musescore = handleTest ./musescore.nix {}; munin = handleTest ./munin.nix {}; diff --git a/nixos/tests/mtp.nix b/nixos/tests/mtp.nix new file mode 100644 index 000000000000..6fa92269d41f --- /dev/null +++ b/nixos/tests/mtp.nix @@ -0,0 +1,108 @@ +import ./make-test-python.nix ({ pkgs, ... }: { + name = "mtp"; + meta = with pkgs.lib.maintainers; { + maintainers = [ matthewcroughan nixinator ]; + }; + + nodes = + { + client = { config, pkgs, ... }: { + # DBUS runs only once a user session is created, which means a user has to + # login. Here, we log in as root. Once logged in, the gvfs-daemon service runs + # as UID 0 in User-0.service + services.getty.autologinUser = "root"; + + # XDG_RUNTIME_DIR is needed for running systemd-user services such as + # gvfs-daemon as root. + environment.variables.XDG_RUNTIME_DIR = "/run/user/0"; + + environment.systemPackages = with pkgs; [ usbutils glib jmtpfs tree ]; + services.gvfs.enable = true; + + # Creates a usb-mtp device inside the VM, which is mapped to the host's + # /tmp folder, it is able to write files to this location, but only has + # permissions to read its own creations. + virtualisation.qemu.options = [ + "-usb" + "-device usb-mtp,rootdir=/tmp,readonly=false" + ]; + }; + }; + + + testScript = { nodes, ... }: + let + # Creates a list of QEMU MTP devices matching USB ID (46f4:0004). This + # value can be sourced in a shell script. This is so we can loop over the + # devices we find, as this test may want to use more than one MTP device + # in future. + mtpDevices = pkgs.writeScript "mtpDevices.sh" '' + export mtpDevices=$(lsusb -d 46f4:0004 | awk {'print $2","$4'} | sed 's/[:-]/ /g') + ''; + # Qemu is only capable of creating an MTP device with Picture Transfer + # Protocol. This means that gvfs must use gphoto2:// rather than mtp:// + # when mounting. + # https://github.com/qemu/qemu/blob/970bc16f60937bcfd334f14c614bd4407c247961/hw/usb/dev-mtp.c#L278 + gvfs = rec { + mountAllMtpDevices = pkgs.writeScript "mountAllMtpDevices.sh" '' + set -e + source ${mtpDevices} + for i in $mtpDevices + do + gio mount "gphoto2://[usb:$i]/" + done + ''; + unmountAllMtpDevices = pkgs.writeScript "unmountAllMtpDevices.sh" '' + set -e + source ${mtpDevices} + for i in $mtpDevices + do + gio mount -u "gphoto2://[usb:$i]/" + done + ''; + # gvfsTest: + # 1. Creates a 10M test file + # 2. Copies it to the device using GIO tools + # 3. Checks for corruption with `diff` + # 4. Removes the file, then unmounts the disks. + gvfsTest = pkgs.writeScript "gvfsTest.sh" '' + set -e + source ${mtpDevices} + ${mountAllMtpDevices} + dd if=/dev/urandom of=testFile10M bs=1M count=10 + for i in $mtpDevices + do + gio copy ./testFile10M gphoto2://[usb:$i]/ + ls -lah /run/user/0/gvfs/*/testFile10M + gio remove gphoto2://[usb:$i]/testFile10M + done + ${unmountAllMtpDevices} + ''; + }; + jmtpfs = { + # jmtpfsTest: + # 1. Mounts the device on a dir named `phone` using jmtpfs + # 2. Puts the current Nixpkgs libmtp version into a file + # 3. Checks for corruption with `diff` + # 4. Prints the directory tree + jmtpfsTest = pkgs.writeScript "jmtpfsTest.sh" '' + mkdir phone + jmtpfs phone + echo "${pkgs.libmtp.version}" > phone/tmp/testFile + echo "${pkgs.libmtp.version}" > testFile + diff phone/tmp/testFile testFile + tree phone + ''; + }; + in + # Using >&2 allows the results of the scripts to be printed to the terminal + # when building this test with Nix. Scripts would otherwise complete + # silently. + '' + start_all() + client.wait_for_unit("multi-user.target") + client.wait_for_unit("dbus.service") + client.succeed("${gvfs.gvfsTest} >&2") + client.succeed("${jmtpfs.jmtpfsTest} >&2") + ''; +}) From 473a571a8c99fb45d2e4ab41823a66287ff26161 Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Mon, 20 Dec 2021 22:51:25 +0000 Subject: [PATCH 2/2] nixos/tests/mtp: use QEMU v6.0.0 A change in QEMU v6.1.0 has somehow caused QEMU to behave differently enough to cause this test to fail. This commit forces the test to be ran with QEMU 6.0.0 from Nixpkgs at revision e1fc1a80a071c90ab65fb6eafae5520579163783, which is the commit prior to the QEMU 6.1.0 version bump. Co-authored-by: Julio Sueiras --- nixos/tests/all-tests.nix | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index f35ecca4344a..792965d7a10f 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -277,7 +277,13 @@ in mosquitto = handleTest ./mosquitto.nix {}; mpd = handleTest ./mpd.nix {}; mpv = handleTest ./mpv.nix {}; - mtp = handleTest ./mtp.nix {}; + mtp = let + olderQemu = (import (fetchTarball { + url = "https://github.com/nixos/nixpkgs/archive/e1fc1a80a071c90ab65fb6eafae5520579163783.tar.gz"; + sha256 = "19a0qrx31lp2r8cgk9hv4p6j6six6l82qisxr68y7wb7drw7dhkz"; + }) { inherit system; }).qemu_test; + myPkgs = import ../.. { inherit system; overlays = [ (self: super: { qemu_test = olderQemu; }) ]; }; + in handleTest ./mtp.nix { pkgs = myPkgs; }; mumble = handleTest ./mumble.nix {}; musescore = handleTest ./musescore.nix {}; munin = handleTest ./munin.nix {};