summary refs log tree commit diff
path: root/nixos/doc/manual/development/writing-nixos-tests.xml
blob: e5a887c18c7709e7accc26508f519a719dc58ebe (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<section xmlns="http://docbook.org/ns/docbook"
        xmlns:xlink="http://www.w3.org/1999/xlink"
        xmlns:xi="http://www.w3.org/2001/XInclude"
        version="5.0"
        xml:id="sec-writing-nixos-tests">
 <title>Writing Tests</title>

 <para>
  A NixOS test is a Nix expression that has the following structure:
<programlisting>
import ./make-test-python.nix {

  # Either the configuration of a single machine:
  machine =
    { config, pkgs, ... }:
    { <replaceable>configuration…</replaceable>
    };

  # Or a set of machines:
  nodes =
    { <replaceable>machine1</replaceable> =
        { config, pkgs, ... }: { <replaceable></replaceable> };
      <replaceable>machine2</replaceable> =
        { config, pkgs, ... }: { <replaceable></replaceable> };
          };

  testScript =
    ''
      <replaceable>Python code…</replaceable>
    '';
}
</programlisting>
  The attribute <literal>testScript</literal> is a bit of Python code that
  executes the test (described below). During the test, it will start one or
  more virtual machines, the configuration of which is described by the
  attribute <literal>machine</literal> (if you need only one machine in your
  test) or by the attribute <literal>nodes</literal> (if you need multiple
  machines). For instance,
  <filename
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/login.nix">login.nix</filename>
  only needs a single machine to test whether users can log in on the virtual
  console, whether device ownership is correctly maintained when switching
  between consoles, and so on. On the other hand,
  <filename
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/nfs.nix">nfs.nix</filename>,
  which tests NFS client and server functionality in the Linux kernel
  (including whether locks are maintained across server crashes), requires
  three machines: a server and two clients.
 </para>

 <para>
  There are a few special NixOS configuration options for test VMs:
<!-- FIXME: would be nice to generate this automatically. -->
  <variablelist>
   <varlistentry>
    <term>
     <option>virtualisation.memorySize</option>
    </term>
    <listitem>
     <para>
      The memory of the VM in megabytes.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <option>virtualisation.vlans</option>
    </term>
    <listitem>
     <para>
      The virtual networks to which the VM is connected. See
      <filename
    xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/nat.nix">nat.nix</filename>
      for an example.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <option>virtualisation.writableStore</option>
    </term>
    <listitem>
     <para>
      By default, the Nix store in the VM is not writable. If you enable this
      option, a writable union file system is mounted on top of the Nix store
      to make it appear writable. This is necessary for tests that run Nix
      operations that modify the store.
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
  For more options, see the module
  <filename
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/virtualisation/qemu-vm.nix">qemu-vm.nix</filename>.
 </para>

 <para>
  The test script is a sequence of Python statements that perform various
  actions, such as starting VMs, executing commands in the VMs, and so on. Each
  virtual machine is represented as an object stored in the variable
  <literal><replaceable>name</replaceable></literal> if this is also the
  identifier of the machine in the declarative config.
  If you didn't specify multiple machines using the <literal>nodes</literal>
  attribute, it is just <literal>machine</literal>.
  The following example starts the machine, waits until it has finished booting,
  then executes a command and checks that the output is more-or-less correct:
<programlisting>
machine.start()
machine.wait_for_unit("default.target")
if not "Linux" in machine.succeed("uname"):
  raise Exception("Wrong OS")
</programlisting>
  The first line is actually unnecessary; machines are implicitly started when
  you first execute an action on them (such as <literal>wait_for_unit</literal>
  or <literal>succeed</literal>). If you have multiple machines, you can speed
  up the test by starting them in parallel:
<programlisting>
start_all()
</programlisting>
 </para>

 <para>
  The following methods are available on machine objects:
  <variablelist>
   <varlistentry>
    <term>
     <methodname>start</methodname>
    </term>
    <listitem>
     <para>
      Start the virtual machine. This method is asynchronous  it does not
      wait for the machine to finish booting.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>shutdown</methodname>
    </term>
    <listitem>
     <para>
      Shut down the machine, waiting for the VM to exit.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>crash</methodname>
    </term>
    <listitem>
     <para>
      Simulate a sudden power failure, by telling the VM to exit immediately.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>block</methodname>
    </term>
    <listitem>
     <para>
      Simulate unplugging the Ethernet cable that connects the machine to the
      other machines.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>unblock</methodname>
    </term>
    <listitem>
     <para>
      Undo the effect of <methodname>block</methodname>.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>screenshot</methodname>
    </term>
    <listitem>
     <para>
      Take a picture of the display of the virtual machine, in PNG format. The
      screenshot is linked from the HTML log.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>get_screen_text</methodname>
    </term>
    <listitem>
     <para>
      Return a textual representation of what is currently visible on the
      machine's screen using optical character recognition.
     </para>
     <note>
      <para>
       This requires passing <option>enableOCR</option> to the test attribute
       set.
      </para>
     </note>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>send_monitor_command</methodname>
    </term>
    <listitem>
     <para>
      Send a command to the QEMU monitor. This is rarely used, but allows doing
      stuff such as attaching virtual USB disks to a running machine.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>send_keys</methodname>
    </term>
    <listitem>
     <para>
      Simulate pressing keys on the virtual keyboard, e.g.,
      <literal>send_keys("ctrl-alt-delete")</literal>.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>send_chars</methodname>
    </term>
    <listitem>
     <para>
      Simulate typing a sequence of characters on the virtual keyboard, e.g.,
      <literal>send_keys("foobar\n")</literal> will type the string
      <literal>foobar</literal> followed by the Enter key.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>execute</methodname>
    </term>
    <listitem>
     <para>
      Execute a shell command, returning a list
      <literal>(<replaceable>status</replaceable>,
      <replaceable>stdout</replaceable>)</literal>.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>succeed</methodname>
    </term>
    <listitem>
     <para>
      Execute a shell command, raising an exception if the exit status is not
      zero, otherwise returning the standard output.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>fail</methodname>
    </term>
    <listitem>
     <para>
      Like <methodname>succeed</methodname>, but raising an exception if the
      command returns a zero status.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>wait_until_succeeds</methodname>
    </term>
    <listitem>
     <para>
      Repeat a shell command with 1-second intervals until it succeeds.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>wait_until_fails</methodname>
    </term>
    <listitem>
     <para>
      Repeat a shell command with 1-second intervals until it fails.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>wait_for_unit</methodname>
    </term>
    <listitem>
     <para>
      Wait until the specified systemd unit has reached the “active” state.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>wait_for_file</methodname>
    </term>
    <listitem>
     <para>
      Wait until the specified file exists.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>wait_for_open_port</methodname>
    </term>
    <listitem>
     <para>
      Wait until a process is listening on the given TCP port (on
      <literal>localhost</literal>, at least).
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>wait_for_closed_port</methodname>
    </term>
    <listitem>
     <para>
      Wait until nobody is listening on the given TCP port.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>wait_for_x</methodname>
    </term>
    <listitem>
     <para>
      Wait until the X11 server is accepting connections.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>wait_for_text</methodname>
    </term>
    <listitem>
     <para>
      Wait until the supplied regular expressions matches the textual contents
      of the screen by using optical character recognition (see
      <methodname>get_screen_text</methodname>).
     </para>
     <note>
      <para>
       This requires passing <option>enableOCR</option> to the test attribute
       set.
      </para>
     </note>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>wait_for_window</methodname>
    </term>
    <listitem>
     <para>
      Wait until an X11 window has appeared whose name matches the given
      regular expression, e.g., <literal>wait_for_window("Terminal")</literal>.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>copy_file_from_host</methodname>
    </term>
    <listitem>
     <para>
      Copies a file from host to machine, e.g.,
      <literal>copy_file_from_host("myfile", "/etc/my/important/file")</literal>.
     </para>
     <para>
      The first argument is the file on the host. The file needs to be
      accessible while building the nix derivation. The second argument is the
      location of the file on the machine.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term>
     <methodname>systemctl</methodname>
    </term>
    <listitem>
     <para>
      Runs <literal>systemctl</literal> commands with optional support for
      <literal>systemctl --user</literal>
     </para>
     <para>
<programlisting>
machine.systemctl("list-jobs --no-pager") # runs `systemctl list-jobs --no-pager`
machine.systemctl("list-jobs --no-pager", "any-user") # spawns a shell for `any-user` and runs `systemctl --user list-jobs --no-pager`
</programlisting>
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </para>

 <para>
  To test user units declared by <literal>systemd.user.services</literal> the
  optional <literal>user</literal> argument can be used:
<programlisting>
machine.start()
machine.wait_for_x()
machine.wait_for_unit("xautolock.service", "x-session-user")
</programlisting>
  This applies to <literal>systemctl</literal>, <literal>get_unit_info</literal>,
  <literal>wait_for_unit</literal>, <literal>start_job</literal> and
  <literal>stop_job</literal>.
 </para>

 <para>
  For faster dev cycles it's also possible to disable the code-linters (this shouldn't
  be commited though):
<programlisting>
import ./make-test-python.nix {
  skipLint = true;
  machine =
    { config, pkgs, ... }:
    { <replaceable>configuration…</replaceable>
    };

  testScript =
    ''
      <replaceable>Python code…</replaceable>
    '';
}
</programlisting>
 </para>
</section>