Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at netboot-syslinux-multiplatform 213 lines 6.8 kB view raw
1{ testers, lib, pkgs, hello, runCommand, ... }: 2let 3 pkgs-with-overlay = pkgs.extend(final: prev: { 4 proof-of-overlay-hello = prev.hello; 5 }); 6 7 dummyVersioning = { 8 revision = "test"; 9 versionSuffix = "test"; 10 label = "test"; 11 }; 12 13in 14lib.recurseIntoAttrs { 15 hasPkgConfigModule = pkgs.callPackage ../hasPkgConfigModule/tests.nix { }; 16 17 runNixOSTest-example = pkgs-with-overlay.testers.runNixOSTest ({ lib, ... }: { 18 name = "runNixOSTest-test"; 19 nodes.machine = { pkgs, ... }: { 20 system.nixos = dummyVersioning; 21 environment.systemPackages = [ pkgs.proof-of-overlay-hello pkgs.figlet ]; 22 }; 23 testScript = '' 24 machine.succeed("hello | figlet >/dev/console") 25 ''; 26 }); 27 28 # Check that the wiring of nixosTest is correct. 29 # Correct operation of the NixOS test driver should be asserted elsewhere. 30 nixosTest-example = pkgs-with-overlay.testers.nixosTest ({ lib, pkgs, figlet, ... }: { 31 name = "nixosTest-test"; 32 nodes.machine = { pkgs, ... }: { 33 system.nixos = dummyVersioning; 34 environment.systemPackages = [ pkgs.proof-of-overlay-hello figlet ]; 35 }; 36 testScript = '' 37 machine.succeed("hello | figlet >/dev/console") 38 ''; 39 }); 40 41 testBuildFailure = lib.recurseIntoAttrs { 42 happy = runCommand "testBuildFailure-happy" { 43 failed = testers.testBuildFailure (runCommand "fail" {} '' 44 echo ok-ish >$out 45 46 echo failing though 47 echo also stderr 1>&2 48 echo 'line\nwith-\bbackslashes' 49 printf "incomplete line - no newline" 50 51 exit 3 52 ''); 53 } '' 54 grep -F 'ok-ish' $failed/result 55 56 grep -F 'failing though' $failed/testBuildFailure.log 57 grep -F 'also stderr' $failed/testBuildFailure.log 58 grep -F 'line\nwith-\bbackslashes' $failed/testBuildFailure.log 59 grep -F 'incomplete line - no newline' $failed/testBuildFailure.log 60 61 [[ 3 = $(cat $failed/testBuildFailure.exit) ]] 62 63 touch $out 64 ''; 65 66 helloDoesNotFail = runCommand "testBuildFailure-helloDoesNotFail" { 67 failed = testers.testBuildFailure (testers.testBuildFailure hello); 68 69 # Add hello itself as a prerequisite, so we don't try to run this test if 70 # there's an actual failure in hello. 71 inherit hello; 72 } '' 73 echo "Checking $failed/testBuildFailure.log" 74 grep -F 'testBuildFailure: The builder did not fail, but a failure was expected' $failed/testBuildFailure.log >/dev/null 75 [[ 1 = $(cat $failed/testBuildFailure.exit) ]] 76 touch $out 77 echo 'All good.' 78 ''; 79 80 multiOutput = runCommand "testBuildFailure-multiOutput" { 81 failed = testers.testBuildFailure (runCommand "fail" { 82 # dev will be the default output 83 outputs = ["dev" "doc" "out"]; 84 } '' 85 echo i am failing 86 exit 1 87 ''); 88 } '' 89 grep -F 'i am failing' $failed/testBuildFailure.log >/dev/null 90 [[ 1 = $(cat $failed/testBuildFailure.exit) ]] 91 92 # Checking our note that dev is the default output 93 echo $failed/_ | grep -- '-dev/_' >/dev/null 94 echo 'All good.' 95 touch $out 96 ''; 97 }; 98 99 testEqualContents = lib.recurseIntoAttrs { 100 happy = testers.testEqualContents { 101 assertion = "The same directory contents at different paths are recognized as equal"; 102 expected = runCommand "expected" {} '' 103 mkdir -p $out/c 104 echo a >$out/a 105 echo b >$out/b 106 echo d >$out/c/d 107 ''; 108 actual = runCommand "actual" {} '' 109 mkdir -p $out/c 110 echo a >$out/a 111 echo b >$out/b 112 echo d >$out/c/d 113 ''; 114 }; 115 116 unequalExe = 117 runCommand "testEqualContents-unequalExe" { 118 log = testers.testBuildFailure (testers.testEqualContents { 119 assertion = "The same directory contents at different paths are recognized as equal"; 120 expected = runCommand "expected" {} '' 121 mkdir -p $out/c 122 echo a >$out/a 123 chmod a+x $out/a 124 echo b >$out/b 125 echo d >$out/c/d 126 ''; 127 actual = runCommand "actual" {} '' 128 mkdir -p $out/c 129 echo a >$out/a 130 echo b >$out/b 131 chmod a+x $out/b 132 echo d >$out/c/d 133 ''; 134 }); 135 } '' 136 ( 137 set -x 138 grep -F -- "executable bits don't match" $log/testBuildFailure.log 139 grep -E -- '+.*-actual/a' $log/testBuildFailure.log 140 grep -E -- '-.*-actual/b' $log/testBuildFailure.log 141 grep -F -- "--- actual-executables" $log/testBuildFailure.log 142 grep -F -- "+++ expected-executables" $log/testBuildFailure.log 143 ) || { 144 echo "Test failed: could not find pattern in build log $log" 145 exit 1 146 } 147 echo 'All good.' 148 touch $out 149 ''; 150 151 fileDiff = 152 runCommand "testEqualContents-fileDiff" { 153 log = testers.testBuildFailure (testers.testEqualContents { 154 assertion = "The same directory contents at different paths are recognized as equal"; 155 expected = runCommand "expected" {} '' 156 mkdir -p $out/c 157 echo a >$out/a 158 echo b >$out/b 159 echo d >$out/c/d 160 ''; 161 actual = runCommand "actual" {} '' 162 mkdir -p $out/c 163 echo a >$out/a 164 echo B >$out/b 165 echo d >$out/c/d 166 ''; 167 }); 168 } '' 169 ( 170 set -x 171 grep -F -- "Contents must be equal but were not" $log/testBuildFailure.log 172 grep -E -- '+++ .*-actual/b' $log/testBuildFailure.log 173 grep -E -- '--- .*-actual/b' $log/testBuildFailure.log 174 grep -F -- "-B" $log/testBuildFailure.log 175 grep -F -- "+b" $log/testBuildFailure.log 176 ) || { 177 echo "Test failed: could not find pattern in build log $log" 178 exit 1 179 } 180 echo 'All good.' 181 touch $out 182 ''; 183 184 fileMissing = 185 runCommand "testEqualContents-fileMissing" { 186 log = testers.testBuildFailure (testers.testEqualContents { 187 assertion = "The same directory contents at different paths are recognized as equal"; 188 expected = runCommand "expected" {} '' 189 mkdir -p $out/c 190 echo a >$out/a 191 echo b >$out/b 192 echo d >$out/c/d 193 ''; 194 actual = runCommand "actual" {} '' 195 mkdir -p $out/c 196 echo a >$out/a 197 echo d >$out/c/d 198 ''; 199 }); 200 } '' 201 ( 202 set -x 203 grep -F -- "Contents must be equal but were not" $log/testBuildFailure.log 204 grep -E -- 'Only in .*-expected: b' $log/testBuildFailure.log 205 ) || { 206 echo "Test failed: could not find pattern in build log $log" 207 exit 1 208 } 209 echo 'All good.' 210 touch $out 211 ''; 212 }; 213}