···159159`execute`
160160161161: Execute a shell command, returning a list `(status, stdout)`.
162162+163163+ Commands are run with `set -euo pipefail` set:
164164+165165+ - If several commands are separated by `;` and one fails, the
166166+ command as a whole will fail.
167167+168168+ - For pipelines, the last non-zero exit status will be returned
169169+ (if there is one; otherwise zero will be returned).
170170+171171+ - Dereferencing unset variables fails the command.
172172+173173+ - It will wait for stdout to be closed.
174174+162175 If the command detaches, it must close stdout, as `execute` will wait
163176 for this to consume all output reliably. This can be achieved by
164177 redirecting stdout to stderr `>&2`, to `/dev/console`, `/dev/null` or
165178 a file. Examples of detaching commands are `sleep 365d &`, where the
166179 shell forks a new process that can write to stdout and `xclip -i`, where
167180 the `xclip` command itself forks without closing stdout.
181181+168182 Takes an optional parameter `check_return` that defaults to `True`.
169183 Setting this parameter to `False` will not check for the return code
170184 and return -1 instead. This can be used for commands that shut down
171185 the VM and would therefore break the pipe that would be used for
172186 retrieving the return code.
173187188188+ A timeout for the command can be specified (in seconds) using the optional
189189+ `timeout` parameter, e.g., `execute(cmd, timeout=10)` or
190190+ `execute(cmd, timeout=None)`. The default is 900 seconds.
191191+174192`succeed`
175193176194: Execute a shell command, raising an exception if the exit status is
177177- not zero, otherwise returning the standard output. Commands are run
178178- with `set -euo pipefail` set:
179179-180180- - If several commands are separated by `;` and one fails, the
181181- command as a whole will fail.
182182-183183- - For pipelines, the last non-zero exit status will be returned
184184- (if there is one, zero will be returned otherwise).
185185-186186- - Dereferencing unset variables fail the command.
187187-188188- - It will wait for stdout to be closed. See `execute` for the
189189- implications.
195195+ not zero, otherwise returning the standard output. Similar to `execute`,
196196+ except that the timeout is `None` by default. See `execute` for details on
197197+ command execution.
190198191199`fail`
192200···196204`wait_until_succeeds`
197205198206: Repeat a shell command with 1-second intervals until it succeeds.
207207+ Has a default timeout of 900 seconds which can be modified, e.g.
208208+ `wait_until_succeeds(cmd, timeout=10)`. See `execute` for details on
209209+ command execution.
199210200211`wait_until_fails`
201212202202-: Repeat a shell command with 1-second intervals until it fails.
213213+: Like `wait_until_succeeds`, but repeating the command until it fails.
203214204215`wait_for_unit`
205216
···274274 <listitem>
275275 <para>
276276 Execute a shell command, returning a list
277277- <literal>(status, stdout)</literal>. If the command
278278- detaches, it must close stdout, as
279279- <literal>execute</literal> will wait for this to consume all
280280- output reliably. This can be achieved by redirecting stdout
281281- to stderr <literal>>&2</literal>, to
282282- <literal>/dev/console</literal>,
283283- <literal>/dev/null</literal> or a file. Examples of
284284- detaching commands are <literal>sleep 365d &</literal>,
285285- where the shell forks a new process that can write to stdout
286286- and <literal>xclip -i</literal>, where the
287287- <literal>xclip</literal> command itself forks without
288288- closing stdout. Takes an optional parameter
289289- <literal>check_return</literal> that defaults to
290290- <literal>True</literal>. Setting this parameter to
291291- <literal>False</literal> will not check for the return code
292292- and return -1 instead. This can be used for commands that
293293- shut down the VM and would therefore break the pipe that
294294- would be used for retrieving the return code.
277277+ <literal>(status, stdout)</literal>.
295278 </para>
296296- </listitem>
297297- </varlistentry>
298298- <varlistentry>
299299- <term>
300300- <literal>succeed</literal>
301301- </term>
302302- <listitem>
303279 <para>
304304- Execute a shell command, raising an exception if the exit
305305- status is not zero, otherwise returning the standard output.
306280 Commands are run with <literal>set -euo pipefail</literal>
307281 set:
308282 </para>
···317291 <listitem>
318292 <para>
319293 For pipelines, the last non-zero exit status will be
320320- returned (if there is one, zero will be returned
321321- otherwise).
294294+ returned (if there is one; otherwise zero will be
295295+ returned).
322296 </para>
323297 </listitem>
324298 <listitem>
325299 <para>
326326- Dereferencing unset variables fail the command.
300300+ Dereferencing unset variables fails the command.
327301 </para>
328302 </listitem>
329303 <listitem>
330304 <para>
331331- It will wait for stdout to be closed. See
332332- <literal>execute</literal> for the implications.
305305+ It will wait for stdout to be closed.
333306 </para>
334307 </listitem>
335308 </itemizedlist>
309309+ <para>
310310+ If the command detaches, it must close stdout, as
311311+ <literal>execute</literal> will wait for this to consume all
312312+ output reliably. This can be achieved by redirecting stdout
313313+ to stderr <literal>>&2</literal>, to
314314+ <literal>/dev/console</literal>,
315315+ <literal>/dev/null</literal> or a file. Examples of
316316+ detaching commands are <literal>sleep 365d &</literal>,
317317+ where the shell forks a new process that can write to stdout
318318+ and <literal>xclip -i</literal>, where the
319319+ <literal>xclip</literal> command itself forks without
320320+ closing stdout.
321321+ </para>
322322+ <para>
323323+ Takes an optional parameter <literal>check_return</literal>
324324+ that defaults to <literal>True</literal>. Setting this
325325+ parameter to <literal>False</literal> will not check for the
326326+ return code and return -1 instead. This can be used for
327327+ commands that shut down the VM and would therefore break the
328328+ pipe that would be used for retrieving the return code.
329329+ </para>
330330+ <para>
331331+ A timeout for the command can be specified (in seconds)
332332+ using the optional <literal>timeout</literal> parameter,
333333+ e.g., <literal>execute(cmd, timeout=10)</literal> or
334334+ <literal>execute(cmd, timeout=None)</literal>. The default
335335+ is 900 seconds.
336336+ </para>
337337+ </listitem>
338338+ </varlistentry>
339339+ <varlistentry>
340340+ <term>
341341+ <literal>succeed</literal>
342342+ </term>
343343+ <listitem>
344344+ <para>
345345+ Execute a shell command, raising an exception if the exit
346346+ status is not zero, otherwise returning the standard output.
347347+ Similar to <literal>execute</literal>, except that the
348348+ timeout is <literal>None</literal> by default. See
349349+ <literal>execute</literal> for details on command execution.
350350+ </para>
336351 </listitem>
337352 </varlistentry>
338353 <varlistentry>
···353368 <listitem>
354369 <para>
355370 Repeat a shell command with 1-second intervals until it
356356- succeeds.
371371+ succeeds. Has a default timeout of 900 seconds which can be
372372+ modified, e.g.
373373+ <literal>wait_until_succeeds(cmd, timeout=10)</literal>. See
374374+ <literal>execute</literal> for details on command execution.
357375 </para>
358376 </listitem>
359377 </varlistentry>
···363381 </term>
364382 <listitem>
365383 <para>
366366- Repeat a shell command with 1-second intervals until it
367367- fails.
384384+ Like <literal>wait_until_succeeds</literal>, but repeating
385385+ the command until it fails.
368386 </para>
369387 </listitem>
370388 </varlistentry>
+9-2
nixos/lib/test-driver/test_driver/machine.py
···526526 self.run_callbacks()
527527 self.connect()
528528529529+ # Always run command with shell opts
530530+ command = f"set -euo pipefail; {command}"
531531+532532+ timeout_str = ""
529533 if timeout is not None:
530530- command = "timeout {} sh -c {}".format(timeout, shlex.quote(command))
534534+ timeout_str = f"timeout {timeout}"
535535+536536+ out_command = (
537537+ f"{timeout_str} sh -c {shlex.quote(command)} | (base64 --wrap 0; echo)\n"
538538+ )
531539532532- out_command = f"( set -euo pipefail; {command} ) | (base64 --wrap 0; echo)\n"
533540 assert self.shell
534541 self.shell.send(out_command.encode())
535542