1import ./make-test.nix ({ pkgs, ... }:
2
3let
4
5 stick = pkgs.fetchurl {
6 url = http://nixos.org/~eelco/nix/udisks-test.img.xz;
7 sha256 = "0was1xgjkjad91nipzclaz5biv3m4b2nk029ga6nk7iklwi19l8b";
8 };
9
10in
11
12{
13 name = "udisks2";
14 meta = with pkgs.stdenv.lib.maintainers; {
15 maintainers = [ eelco chaoflow ];
16 };
17
18 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 my $stick = $machine->stateDir . "/usbstick.img";
34 system("xz -d < ${stick} > $stick") == 0 or die;
35
36 $machine->succeed("udisksctl info -b /dev/vda >&2");
37 $machine->fail("udisksctl info -b /dev/sda1");
38
39 # Attach a USB stick and wait for it to show up.
40 $machine->sendMonitorCommand("drive_add 0 id=stick,if=none,file=$stick,format=raw");
41 $machine->sendMonitorCommand("device_add usb-storage,id=stick,drive=stick");
42 $machine->waitUntilSucceeds("udisksctl info -b /dev/sda1");
43 $machine->succeed("udisksctl info -b /dev/sda1 | grep 'IdLabel:.*USBSTICK'");
44
45 # Mount the stick as a non-root user and do some stuff with it.
46 $machine->succeed("su - alice -c 'udisksctl info -b /dev/sda1'");
47 $machine->succeed("su - alice -c 'udisksctl mount -b /dev/sda1'");
48 $machine->succeed("su - alice -c 'cat /run/media/alice/USBSTICK/test.txt'") =~ /Hello World/ or die;
49 $machine->succeed("su - alice -c 'echo foo > /run/media/alice/USBSTICK/bar.txt'");
50
51 # Unmounting the stick should make the mountpoint disappear.
52 $machine->succeed("su - alice -c 'udisksctl unmount -b /dev/sda1'");
53 $machine->fail("[ -d /run/media/alice/USBSTICK ]");
54
55 # Remove the USB stick.
56 $machine->sendMonitorCommand("device_del stick");
57 $machine->waitUntilFails("udisksctl info -b /dev/sda1");
58 $machine->fail("[ -e /dev/sda ]");
59 '';
60
61})