about summary refs log tree commit diff
path: root/nixpkgs/pkgs/build-support/testers/test/default.nix
blob: fc4df4964f3981dfa75f89d5eee693880d2998ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
{ testers, lib, pkgs, hello, runCommand, ... }:
let
  pkgs-with-overlay = pkgs.extend(final: prev: {
    proof-of-overlay-hello = prev.hello;
  });

  dummyVersioning = {
    revision = "test";
    versionSuffix = "test";
    label = "test";
  };

in
lib.recurseIntoAttrs {
  hasPkgConfigModule = pkgs.callPackage ../hasPkgConfigModule/tests.nix { };

  runNixOSTest-example = pkgs-with-overlay.testers.runNixOSTest ({ lib, ... }: {
    name = "runNixOSTest-test";
    nodes.machine = { pkgs, ... }: {
      system.nixos = dummyVersioning;
      environment.systemPackages = [ pkgs.proof-of-overlay-hello pkgs.figlet ];
    };
    testScript = ''
      machine.succeed("hello | figlet >/dev/console")
    '';
  });

  # Check that the wiring of nixosTest is correct.
  # Correct operation of the NixOS test driver should be asserted elsewhere.
  nixosTest-example = pkgs-with-overlay.testers.nixosTest ({ lib, pkgs, figlet, ... }: {
    name = "nixosTest-test";
    nodes.machine = { pkgs, ... }: {
      system.nixos = dummyVersioning;
      environment.systemPackages = [ pkgs.proof-of-overlay-hello figlet ];
    };
    testScript = ''
      machine.succeed("hello | figlet >/dev/console")
    '';
  });

  testBuildFailure = lib.recurseIntoAttrs {
    happy = runCommand "testBuildFailure-happy" {
      failed = testers.testBuildFailure (runCommand "fail" {} ''
        echo ok-ish >$out

        echo failing though
        echo also stderr 1>&2
        echo 'line\nwith-\bbackslashes'
        printf "incomplete line - no newline"

        exit 3
      '');
    } ''
      grep -F 'ok-ish' $failed/result

      grep -F 'failing though' $failed/testBuildFailure.log
      grep -F 'also stderr' $failed/testBuildFailure.log
      grep -F 'line\nwith-\bbackslashes' $failed/testBuildFailure.log
      grep -F 'incomplete line - no newline' $failed/testBuildFailure.log

      [[ 3 = $(cat $failed/testBuildFailure.exit) ]]

      touch $out
    '';

    helloDoesNotFail = runCommand "testBuildFailure-helloDoesNotFail" {
      failed = testers.testBuildFailure (testers.testBuildFailure hello);

      # Add hello itself as a prerequisite, so we don't try to run this test if
      # there's an actual failure in hello.
      inherit hello;
    } ''
      echo "Checking $failed/testBuildFailure.log"
      grep -F 'testBuildFailure: The builder did not fail, but a failure was expected' $failed/testBuildFailure.log >/dev/null
      [[ 1 = $(cat $failed/testBuildFailure.exit) ]]
      touch $out
      echo 'All good.'
    '';

    multiOutput = runCommand "testBuildFailure-multiOutput" {
      failed = testers.testBuildFailure (runCommand "fail" {
        # dev will be the default output
        outputs = ["dev" "doc" "out"];
      } ''
        echo i am failing
        exit 1
      '');
    } ''
      grep -F 'i am failing' $failed/testBuildFailure.log >/dev/null
      [[ 1 = $(cat $failed/testBuildFailure.exit) ]]

      # Checking our note that dev is the default output
      echo $failed/_ | grep -- '-dev/_' >/dev/null
      echo 'All good.'
      touch $out
    '';
  };

  testEqualContents = lib.recurseIntoAttrs {
    happy = testers.testEqualContents {
      assertion = "The same directory contents at different paths are recognized as equal";
      expected = runCommand "expected" {} ''
        mkdir -p $out/c
        echo a >$out/a
        echo b >$out/b
        echo d >$out/c/d
      '';
      actual = runCommand "actual" {} ''
        mkdir -p $out/c
        echo a >$out/a
        echo b >$out/b
        echo d >$out/c/d
      '';
    };

    unequalExe =
      runCommand "testEqualContents-unequalExe" {
        log = testers.testBuildFailure (testers.testEqualContents {
          assertion = "The same directory contents at different paths are recognized as equal";
          expected = runCommand "expected" {} ''
            mkdir -p $out/c
            echo a >$out/a
            chmod a+x $out/a
            echo b >$out/b
            echo d >$out/c/d
          '';
          actual = runCommand "actual" {} ''
            mkdir -p $out/c
            echo a >$out/a
            echo b >$out/b
            chmod a+x $out/b
            echo d >$out/c/d
          '';
        });
      } ''
        (
          set -x
          grep -F -- "executable bits don't match" $log/testBuildFailure.log
          grep -E -- '+.*-actual/a' $log/testBuildFailure.log
          grep -E -- '-.*-actual/b' $log/testBuildFailure.log
          grep -F -- "--- actual-executables" $log/testBuildFailure.log
          grep -F -- "+++ expected-executables" $log/testBuildFailure.log
        ) || {
          echo "Test failed: could not find pattern in build log $log"
          exit 1
        }
        echo 'All good.'
        touch $out
      '';

    fileDiff =
      runCommand "testEqualContents-fileDiff" {
        log = testers.testBuildFailure (testers.testEqualContents {
          assertion = "The same directory contents at different paths are recognized as equal";
          expected = runCommand "expected" {} ''
            mkdir -p $out/c
            echo a >$out/a
            echo b >$out/b
            echo d >$out/c/d
          '';
          actual = runCommand "actual" {} ''
            mkdir -p $out/c
            echo a >$out/a
            echo B >$out/b
            echo d >$out/c/d
          '';
        });
      } ''
        (
          set -x
          grep -F -- "Contents must be equal but were not" $log/testBuildFailure.log
          grep -E -- '+++ .*-actual/b' $log/testBuildFailure.log
          grep -E -- '--- .*-actual/b' $log/testBuildFailure.log
          grep -F -- "-B" $log/testBuildFailure.log
          grep -F -- "+b" $log/testBuildFailure.log
        ) || {
          echo "Test failed: could not find pattern in build log $log"
          exit 1
        }
        echo 'All good.'
        touch $out
      '';

    fileMissing =
      runCommand "testEqualContents-fileMissing" {
        log = testers.testBuildFailure (testers.testEqualContents {
          assertion = "The same directory contents at different paths are recognized as equal";
          expected = runCommand "expected" {} ''
            mkdir -p $out/c
            echo a >$out/a
            echo b >$out/b
            echo d >$out/c/d
          '';
          actual = runCommand "actual" {} ''
            mkdir -p $out/c
            echo a >$out/a
            echo d >$out/c/d
          '';
        });
      } ''
        (
          set -x
          grep -F -- "Contents must be equal but were not" $log/testBuildFailure.log
          grep -E -- 'Only in .*-expected: b' $log/testBuildFailure.log
        ) || {
          echo "Test failed: could not find pattern in build log $log"
          exit 1
        }
        echo 'All good.'
        touch $out
      '';
  };
}