at 21.11-pre 6.7 kB view raw
1import ./make-test-python.nix ({ pkgs, lib, ... }: { 2 name = "turbovnc-headless-server"; 3 meta = { 4 maintainers = with lib.maintainers; [ nh2 ]; 5 }; 6 7 machine = { pkgs, ... }: { 8 9 environment.systemPackages = with pkgs; [ 10 glxinfo 11 procps # for `pkill`, `pidof` in the test 12 scrot # for screenshotting Xorg 13 turbovnc 14 ]; 15 16 programs.turbovnc.ensureHeadlessSoftwareOpenGL = true; 17 18 networking.firewall = { 19 # Reject instead of drop, for failures instead of hangs. 20 rejectPackets = true; 21 allowedTCPPorts = [ 22 5900 # VNC :0, for seeing what's going on in the server 23 ]; 24 }; 25 26 # So that we can ssh into the VM, see e.g. 27 # http://blog.patapon.info/nixos-local-vm/#accessing-the-vm-with-ssh 28 services.openssh.enable = true; 29 services.openssh.permitRootLogin = "yes"; 30 users.extraUsers.root.password = ""; 31 users.mutableUsers = false; 32 }; 33 34 testScript = '' 35 def wait_until_terminated_or_succeeds( 36 termination_check_shell_command, 37 success_check_shell_command, 38 get_detail_message_fn, 39 retries=60, 40 retry_sleep=0.5, 41 ): 42 def check_success(): 43 command_exit_code, _output = machine.execute(success_check_shell_command) 44 return command_exit_code == 0 45 46 for _ in range(retries): 47 exit_check_exit_code, _output = machine.execute(termination_check_shell_command) 48 is_terminated = exit_check_exit_code != 0 49 if is_terminated: 50 if check_success(): 51 return 52 else: 53 details = get_detail_message_fn() 54 raise Exception( 55 f"termination check ({termination_check_shell_command}) triggered without command succeeding ({success_check_shell_command}); details: {details}" 56 ) 57 else: 58 if check_success(): 59 return 60 time.sleep(retry_sleep) 61 62 if not check_success(): 63 details = get_detail_message_fn() 64 raise Exception( 65 f"action timed out ({success_check_shell_command}); details: {details}" 66 ) 67 68 69 # Below we use the pattern: 70 # (cmd | tee stdout.log) 3>&1 1>&2 2>&3 | tee stderr.log 71 # to capture both stderr and stdout while also teeing them, see: 72 # https://unix.stackexchange.com/questions/6430/how-to-redirect-stderr-and-stdout-to-different-files-and-also-display-in-termina/6431#6431 73 74 75 # Starts headless VNC server, backgrounding it. 76 def start_xvnc(): 77 xvnc_command = " ".join( 78 [ 79 "Xvnc", 80 ":0", 81 "-iglx", 82 "-auth /root/.Xauthority", 83 "-geometry 1240x900", 84 "-depth 24", 85 "-rfbwait 5000", 86 "-deferupdate 1", 87 "-verbose", 88 "-securitytypes none", 89 # We don't enforce localhost listening such that we 90 # can connect from outside the VM using 91 # env QEMU_NET_OPTS=hostfwd=tcp::5900-:5900 $(nix-build nixos/tests/turbovnc-headless-server.nix -A driver)/bin/nixos-test-driver 92 # for testing purposes, and so that we can in the future 93 # add another test case that connects the TurboVNC client. 94 # "-localhost", 95 ] 96 ) 97 machine.execute( 98 # Note trailing & for backgrounding. 99 f"({xvnc_command} | tee /tmp/Xvnc.stdout) 3>&1 1>&2 2>&3 | tee /tmp/Xvnc.stderr &", 100 ) 101 102 103 # Waits until the server log message that tells us that GLX is ready 104 # (requires `-verbose` above), avoiding screenshoting racing below. 105 def wait_until_xvnc_glx_ready(): 106 machine.wait_until_succeeds("test -f /tmp/Xvnc.stderr") 107 wait_until_terminated_or_succeeds( 108 termination_check_shell_command="pidof Xvnc", 109 success_check_shell_command="grep 'GLX: Initialized DRISWRAST' /tmp/Xvnc.stderr", 110 get_detail_message_fn=lambda: "Contents of /tmp/Xvnc.stderr:\n" 111 + machine.succeed("cat /tmp/Xvnc.stderr"), 112 ) 113 114 115 # Checks that we detect glxgears failing when 116 # `LIBGL_DRIVERS_PATH=/nonexistent` is set 117 # (in which case software rendering should not work). 118 def test_glxgears_failing_with_bad_driver_path(): 119 machine.execute( 120 # Note trailing & for backgrounding. 121 "(env DISPLAY=:0 LIBGL_DRIVERS_PATH=/nonexistent glxgears -info | tee /tmp/glxgears-should-fail.stdout) 3>&1 1>&2 2>&3 | tee /tmp/glxgears-should-fail.stderr &" 122 ) 123 machine.wait_until_succeeds("test -f /tmp/glxgears-should-fail.stderr") 124 wait_until_terminated_or_succeeds( 125 termination_check_shell_command="pidof glxgears", 126 success_check_shell_command="grep 'libGL error: failed to load driver: swrast' /tmp/glxgears-should-fail.stderr", 127 get_detail_message_fn=lambda: "Contents of /tmp/glxgears-should-fail.stderr:\n" 128 + machine.succeed("cat /tmp/glxgears-should-fail.stderr"), 129 ) 130 machine.wait_until_fails("pidof glxgears") 131 132 133 # Starts glxgears, backgrounding it. Waits until it prints the `GL_RENDERER`. 134 # Does not quit glxgears. 135 def test_glxgears_prints_renderer(): 136 machine.execute( 137 # Note trailing & for backgrounding. 138 "(env DISPLAY=:0 glxgears -info | tee /tmp/glxgears.stdout) 3>&1 1>&2 2>&3 | tee /tmp/glxgears.stderr &" 139 ) 140 machine.wait_until_succeeds("test -f /tmp/glxgears.stderr") 141 wait_until_terminated_or_succeeds( 142 termination_check_shell_command="pidof glxgears", 143 success_check_shell_command="grep 'GL_RENDERER' /tmp/glxgears.stdout", 144 get_detail_message_fn=lambda: "Contents of /tmp/glxgears.stderr:\n" 145 + machine.succeed("cat /tmp/glxgears.stderr"), 146 ) 147 148 149 with subtest("Start Xvnc"): 150 start_xvnc() 151 wait_until_xvnc_glx_ready() 152 153 with subtest("Ensure bad driver path makes glxgears fail"): 154 test_glxgears_failing_with_bad_driver_path() 155 156 with subtest("Run 3D application (glxgears)"): 157 test_glxgears_prints_renderer() 158 159 # Take screenshot; should display the glxgears. 160 machine.succeed("scrot --display :0 /tmp/glxgears.png") 161 162 # Copy files down. 163 machine.copy_from_vm("/tmp/glxgears.png") 164 machine.copy_from_vm("/tmp/glxgears.stdout") 165 machine.copy_from_vm("/tmp/glxgears-should-fail.stdout") 166 machine.copy_from_vm("/tmp/glxgears-should-fail.stderr") 167 machine.copy_from_vm("/tmp/Xvnc.stdout") 168 machine.copy_from_vm("/tmp/Xvnc.stderr") 169 ''; 170 171})