at 23.11-beta 2.2 kB view raw
1import ./make-test-python.nix ({ pkgs, ... }: 2 3let 4 5 stick = pkgs.fetchurl { 6 url = "https://nixos.org/~eelco/nix/udisks-test.img.xz"; 7 sha256 = "0was1xgjkjad91nipzclaz5biv3m4b2nk029ga6nk7iklwi19l8b"; 8 }; 9 10in 11 12{ 13 name = "udisks2"; 14 meta = with pkgs.lib.maintainers; { 15 maintainers = [ eelco ]; 16 }; 17 18 nodes.machine = 19 { ... }: 20 { services.udisks2.enable = true; 21 imports = [ ./common/user-account.nix ]; 22 23 security.polkit.extraConfig = 24 '' 25 polkit.addRule(function(action, subject) { 26 if (subject.user == "alice") return "yes"; 27 }); 28 ''; 29 }; 30 31 testScript = 32 '' 33 import lzma 34 35 machine.systemctl("start udisks2") 36 machine.wait_for_unit("udisks2.service") 37 38 with lzma.open( 39 "${stick}" 40 ) as data, open(machine.state_dir / "usbstick.img", "wb") as stick: 41 stick.write(data.read()) 42 43 machine.succeed("udisksctl info -b /dev/vda >&2") 44 machine.fail("udisksctl info -b /dev/sda1") 45 46 # Attach a USB stick and wait for it to show up. 47 machine.send_monitor_command( 48 f"drive_add 0 id=stick,if=none,file={stick.name},format=raw" 49 ) 50 machine.send_monitor_command("device_add usb-storage,id=stick,drive=stick") 51 machine.wait_until_succeeds("udisksctl info -b /dev/sda1") 52 machine.succeed("udisksctl info -b /dev/sda1 | grep 'IdLabel:.*USBSTICK'") 53 54 # Mount the stick as a non-root user and do some stuff with it. 55 machine.succeed("su - alice -c 'udisksctl info -b /dev/sda1'") 56 machine.succeed("su - alice -c 'udisksctl mount -b /dev/sda1'") 57 machine.succeed( 58 "su - alice -c 'cat /run/media/alice/USBSTICK/test.txt' | grep -q 'Hello World'" 59 ) 60 machine.succeed("su - alice -c 'echo foo > /run/media/alice/USBSTICK/bar.txt'") 61 62 # Unmounting the stick should make the mountpoint disappear. 63 machine.succeed("su - alice -c 'udisksctl unmount -b /dev/sda1'") 64 machine.fail("[ -d /run/media/alice/USBSTICK ]") 65 66 # Remove the USB stick. 67 machine.send_monitor_command("device_del stick") 68 machine.wait_until_fails("udisksctl info -b /dev/sda1") 69 machine.fail("[ -e /dev/sda ]") 70 ''; 71 72})