at 23.11-beta 6.6 kB view raw
1# Miscellaneous small tests that don't warrant their own VM run. 2 3import ./make-test-python.nix ({ lib, pkgs, ...} : let 4 foo = pkgs.writeText "foo" "Hello World"; 5in { 6 name = "misc"; 7 meta.maintainers = with lib.maintainers; [ eelco ]; 8 9 nodes.machine = 10 { lib, ... }: 11 { swapDevices = lib.mkOverride 0 12 [ { device = "/root/swapfile"; size = 128; } ]; 13 environment.variables.EDITOR = lib.mkOverride 0 "emacs"; 14 documentation.nixos.enable = lib.mkOverride 0 true; 15 systemd.tmpfiles.rules = [ "d /tmp 1777 root root 10d" ]; 16 systemd.tmpfiles.settings."10-test"."/tmp/somefile".d = {}; 17 virtualisation.fileSystems = { "/tmp2" = 18 { fsType = "tmpfs"; 19 options = [ "mode=1777" "noauto" ]; 20 }; 21 # Tests https://discourse.nixos.org/t/how-to-make-a-derivations-executables-have-the-s-permission/8555 22 "/user-mount/point" = { 23 device = "/user-mount/source"; 24 fsType = "none"; 25 options = [ "bind" "rw" "user" "noauto" ]; 26 }; 27 "/user-mount/denied-point" = { 28 device = "/user-mount/denied-source"; 29 fsType = "none"; 30 options = [ "bind" "rw" "noauto" ]; 31 }; 32 }; 33 systemd.automounts = lib.singleton 34 { wantedBy = [ "multi-user.target" ]; 35 where = "/tmp2"; 36 }; 37 users.users.sybil = { isNormalUser = true; group = "wheel"; }; 38 users.users.alice = { isNormalUser = true; }; 39 security.sudo = { enable = true; wheelNeedsPassword = false; }; 40 boot.kernel.sysctl."vm.swappiness" = 1; 41 boot.kernelParams = [ "vsyscall=emulate" ]; 42 system.extraDependencies = [ foo ]; 43 }; 44 45 testScript = 46 '' 47 import json 48 49 50 def get_path_info(path): 51 result = machine.succeed(f"nix --option experimental-features nix-command path-info --json {path}") 52 parsed = json.loads(result) 53 return parsed 54 55 56 with subtest("nix-db"): 57 info = get_path_info("${foo}") 58 print(info) 59 60 if ( 61 info[0]["narHash"] 62 != "sha256-BdMdnb/0eWy3EddjE83rdgzWWpQjfWPAj3zDIFMD3Ck=" 63 ): 64 raise Exception("narHash not set") 65 66 if info[0]["narSize"] != 128: 67 raise Exception("narSize not set") 68 69 with subtest("nixos-version"): 70 machine.succeed("[ `nixos-version | wc -w` = 2 ]") 71 72 with subtest("nixos-rebuild"): 73 assert "NixOS module" in machine.succeed("nixos-rebuild --help") 74 75 with subtest("Sanity check for uid/gid assignment"): 76 assert "4" == machine.succeed("id -u messagebus").strip() 77 assert "4" == machine.succeed("id -g messagebus").strip() 78 assert "users:x:100:" == machine.succeed("getent group users").strip() 79 80 with subtest("Regression test for GMP aborts on QEMU."): 81 machine.succeed("expr 1 + 2") 82 83 with subtest("the swap file got created"): 84 machine.wait_for_unit("root-swapfile.swap") 85 machine.succeed("ls -l /root/swapfile | grep 134217728") 86 87 with subtest("whether kernel.poweroff_cmd is set"): 88 machine.succeed('[ -x "$(cat /proc/sys/kernel/poweroff_cmd)" ]') 89 90 with subtest("whether the io cgroupv2 controller is properly enabled"): 91 machine.succeed("grep -q '\\bio\\b' /sys/fs/cgroup/cgroup.controllers") 92 93 with subtest("whether we have a reboot record in wtmp"): 94 machine.shutdown 95 machine.wait_for_unit("multi-user.target") 96 machine.succeed("last | grep reboot >&2") 97 98 with subtest("whether we can override environment variables"): 99 machine.succeed('[ "$EDITOR" = emacs ]') 100 101 with subtest("whether hostname (and by extension nss_myhostname) works"): 102 assert "machine" == machine.succeed("hostname").strip() 103 assert "machine" == machine.succeed("hostname -s").strip() 104 105 with subtest("whether systemd-udevd automatically loads modules for our hardware"): 106 machine.succeed("systemctl start systemd-udev-settle.service") 107 machine.wait_for_unit("systemd-udev-settle.service") 108 assert "mousedev" in machine.succeed("lsmod") 109 110 with subtest("whether systemd-tmpfiles-clean works"): 111 machine.succeed( 112 "touch /tmp/foo", "systemctl start systemd-tmpfiles-clean", "[ -e /tmp/foo ]" 113 ) 114 # move into the future 115 machine.succeed( 116 'date -s "@$(($(date +%s) + 1000000))"', 117 "systemctl start systemd-tmpfiles-clean", 118 ) 119 machine.fail("[ -e /tmp/foo ]") 120 121 with subtest("whether systemd-tmpfiles settings works"): 122 machine.succeed("[ -e /tmp/somefile ]") 123 124 with subtest("whether automounting works"): 125 machine.fail("grep '/tmp2 tmpfs' /proc/mounts") 126 machine.succeed("touch /tmp2/x") 127 machine.succeed("grep '/tmp2 tmpfs' /proc/mounts") 128 129 with subtest( 130 "Whether mounting by a user is possible with the `user` option in fstab (#95444)" 131 ): 132 machine.succeed("mkdir -p /user-mount/source") 133 machine.succeed("touch /user-mount/source/file") 134 machine.succeed("chmod -R a+Xr /user-mount/source") 135 machine.succeed("mkdir /user-mount/point") 136 machine.succeed("chown alice:users /user-mount/point") 137 machine.succeed("su - alice -c 'mount /user-mount/point'") 138 machine.succeed("su - alice -c 'ls /user-mount/point/file'") 139 with subtest( 140 "Whether mounting by a user is denied without the `user` option in fstab" 141 ): 142 machine.succeed("mkdir -p /user-mount/denied-source") 143 machine.succeed("touch /user-mount/denied-source/file") 144 machine.succeed("chmod -R a+Xr /user-mount/denied-source") 145 machine.succeed("mkdir /user-mount/denied-point") 146 machine.succeed("chown alice:users /user-mount/denied-point") 147 machine.fail("su - alice -c 'mount /user-mount/denied-point'") 148 149 with subtest("shell-vars"): 150 machine.succeed('[ -n "$NIX_PATH" ]') 151 152 with subtest("nix-db"): 153 machine.succeed("nix-store -qR /run/current-system | grep nixos-") 154 155 with subtest("Test sysctl"): 156 machine.wait_for_unit("systemd-sysctl.service") 157 assert "1" == machine.succeed("sysctl -ne vm.swappiness").strip() 158 machine.execute("sysctl vm.swappiness=60") 159 assert "60" == machine.succeed("sysctl -ne vm.swappiness").strip() 160 161 with subtest("Test boot parameters"): 162 assert "vsyscall=emulate" in machine.succeed("cat /proc/cmdline") 163 ''; 164})