xref: /freebsd/contrib/kyua/doc/kyuafile.5.in (revision 6befd3511105a17decdafbf9d3f55324c83aaea1)
1.\" Copyright 2012-2024 The Kyua Authors.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions are
6.\" met:
7.\"
8.\" * Redistributions of source code must retain the above copyright
9.\"   notice, this list of conditions and the following disclaimer.
10.\" * Redistributions in binary form must reproduce the above copyright
11.\"   notice, this list of conditions and the following disclaimer in the
12.\"   documentation and/or other materials provided with the distribution.
13.\" * Neither the name of Google Inc. nor the names of its contributors
14.\"   may be used to endorse or promote products derived from this software
15.\"   without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18.\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20.\" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21.\" OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27.\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28.Dd March 23, 2024
29.Dt KYUAFILE 5
30.Os
31.Sh NAME
32.Nm Kyuafile
33.Nd Test suite description files
34.Sh SYNOPSIS
35.Fn atf_test_program "string name" "[string metadata]"
36.Fn current_kyuafile
37.Fn fs.basename "string path"
38.Fn fs.dirname "string path"
39.Fn fs.exists "string path"
40.Fn fs.files "string path"
41.Fn fs.is_absolute "string path"
42.Fn fs.join "string path" "string path"
43.Fn include "string path"
44.Fn plain_test_program "string name" "[string metadata]"
45.Fn syntax "int version"
46.Fn tap_test_program "string name" "[string metadata]"
47.Fn test_suite "string name"
48.Sh DESCRIPTION
49A test suite is a collection of test programs and is represented by a
50hierarchical layout of test binaries on the file system.
51Any subtree of the file system can represent a test suite, provided that it
52includes one or more
53.Nm Ns s ,
54which are the test suite definition files.
55.Pp
56A
57.Nm
58is a Lua script whose purpose is to describe the structure of the test
59suite it belongs to.
60To do so, the script has access to a collection of special functions provided
61by
62.Xr kyua 1
63as described in
64.Sx Helper functions .
65.Ss File versioning
66Every
67.Nm
68file starts with a call to
69.Fn syntax "int version" .
70This call determines the specific schema used by the file so that future
71backwards-incompatible modifications to the file can be introduced.
72.Pp
73Any new
74.Nm
75file should set
76.Fa version
77to
78.Sq 2 .
79.Ss Test suite definition
80If the
81.Nm
82registers any test programs,
83the
84.Nm
85must define the name of the test suite the test programs belong to by using the
86.Fn test_suite
87function at the very beginning of the file.
88.Pp
89The test suite name provided in the
90.Fn test_suite
91call tells
92.Xr kyua 1
93which set of configuration variables from
94.Xr kyua.conf 5
95to pass to the test programs at run time.
96.Ss Test program registration
97A
98.Nm
99can register test programs by means of a variety of
100.Fn *_test_program
101functions, all of which take the name of a test program and a set of
102optional metadata properties that describe such test program.
103.Pp
104The test programs to be registered must live in the current directory; in
105other words, the various
106.Fn *_test_program
107calls cannot reference test programs in other directories.
108The rationale for this is to force all
109.Nm
110files to be self-contained, and to simplify their internal representation.
111.Pp
112.Em ATF test programs
113are those that use the
114.Xr atf 7
115libraries.
116They can be registered with the
117.Fn atf_test_program
118table constructor.
119This function takes the
120.Fa name
121of the test program and a collection of optional metadata settings for all
122the test cases in the test program.
123Any metadata properties defined by the test cases themselves override the
124metadata values defined here.
125The mapping to ATF metadata naming for each property is provided below.
126.Pp
127.Em Plain test programs
128are those that return 0 on success and non-0 on failure; in general, most test
129programs (even those that use fancy unit-testing libraries) behave this way and
130thus also qualify as plain test programs.
131They can be registered with the
132.Fn plain_test_program
133table constructor.
134This function takes the
135.Fa name
136of the test program, an optional
137.Fa test_suite
138name that overrides the global test suite name, and a collection of optional
139metadata settings for the test program.
140.Pp
141.Em TAP test programs
142are those that implement the Test Anything Protocol.
143They can be registered with the
144.Fn tap_test_program
145table constructor.
146This function takes the
147.Fa name
148of the test program and a collection of optional metadata settings for the
149test program.
150.Pp
151The following metadata properties can be passed to any test program definition:
152.Bl -tag -width XX -offset indent
153.It Va allowed_architectures
154Whitespace-separated list of machine architecture names allowed by the test.
155If empty or not defined, the test is allowed to run on any machine
156architecture.
157.Pp
158ATF:
159.Va require.arch
160.It Va allowed_platforms
161Whitespace-separated list of machine platform names allowed by the test.
162If empty or not defined, the test is allowed to run on any machine
163platform.
164.Pp
165ATF:
166.Va require.machine
167.It Va custom.NAME
168Custom variable defined by the test where
169.Sq NAME
170denotes the name of the variable.
171These variables are useful to tag your tests with information specific to
172your project.
173The values of such variables are propagated all the way from the tests to the
174results files and later to any generated reports.
175.Pp
176Note that if the name happens to have dashes or any other special characters
177in it, you will have to use a special Lua syntax to define the property.
178Refer to the
179.Sx EXAMPLES
180section below for clarification.
181.Pp
182ATF:
183.Va X-NAME
184.It Va description
185Textual description of the test.
186.Pp
187ATF:
188.Va descr
189.It Va execenv
190The name of the execution environment to be used for running the test.
191If empty or not defined, the
192.Sq host
193execution environment is meant.
194The possible values are:
195.Bl -tag -width xUnnnnnnn
196.It host
197The default environment which runs the test as a usual child process.
198.It jail
199The
200.Fx
201.Xr jail 8
202environment.
203It creates a temporary jail to run the test and its optional cleanup logic
204within.
205.Pp
206This feature requires
207.Xr kyua 1
208to be running with superuser privileges.
209.Pp
210The difference between
211.Va security.jail.children.max
212and
213.Va security.jail.children.cur
214sysctl of the jail
215.Xr kyua 1
216is running within must have a value high enough for the jail based tests
217planned to be run.
218For instance, the value 1 should be enough for a sequential run of simple
219tests.
220Otherwise, such aspects as parallel test execution and sub-jails spawned
221by specific test cases should be considered.
222.Pp
223The formula of a temporary jail name is
224.Sq kyua
225+
226.Va test program path
227+
228.Sq _
229+
230.Va test case name .
231All non-alphanumeric characters are replaced with
232.Sq _ .
233.Sq kyua_usr_tests_sys_netpfil_pf_pass_block_v4
234is an example for /usr/tests/sys/netpfil/pf/pass_block:v4 test case.
235.El
236.Pp
237ATF:
238.Va execenv
239.It Va execenv_jail_params
240Additional test-specific whitespace-separated parameters of
241.Fx
242.Xr jail 8
243to create a temporary jail within which the test is run.
244It makes sense only if execenv is set to
245.Sq jail .
246.sp
247.Xr kyua 1
248implicitly passes
249.Sq children.max
250parameter to
251.Xr jail 8
252for a temporary jail with the maximum possible value according to
253the jail
254.Xr kyua 1
255itself is running within.
256It allows tests to easily spawn their own sub-jails without additional
257configuration.
258It can be overridden via
259.Va execenv_jail_params
260if needed.
261.Pp
262ATF:
263.Va execenv.jail.params
264.It Va is_exclusive
265If true, indicates that this test program cannot be executed along any other
266programs at the same time.
267Test programs that affect global system state, such as those that modify the
268value of a
269.Xr sysctl 8
270setting, must set themselves as exclusive to prevent failures due to race
271conditions.
272Defaults to false.
273.Pp
274ATF:
275.Va is.exclusive
276.It Va required_configs
277Whitespace-separated list of configuration variables that the test requires
278to be defined before it can run.
279.Pp
280ATF:
281.Va require.config
282.It Va required_disk_space
283Amount of available disk space that the test needs to run successfully.
284.Pp
285ATF:
286.Va require.diskspace
287.It Va required_files
288Whitespace-separated list of paths that the test requires to exist before
289it can run.
290.Pp
291ATF:
292.Va require.files
293.It Va required_memory
294Amount of physical memory that the test needs to run successfully.
295.Pp
296ATF:
297.Va require.memory
298.It Va required_programs
299Whitespace-separated list of basenames or absolute paths pointing to executable
300binaries that the test requires to exist before it can run.
301.Pp
302ATF:
303.Va require.progs
304.It Va required_user
305If empty, the test has no restrictions on the calling user for it to run.
306If set to
307.Sq unprivileged ,
308the test needs to not run as root.
309If set to
310.Sq root ,
311the test must run as root.
312.Pp
313ATF:
314.Va require.user
315.It Va timeout
316Amount of seconds that the test is allowed to execute before being killed.
317.Pp
318ATF:
319.Va timeout
320.El
321.Ss Recursion
322To reference test programs in another subdirectory, a different
323.Nm
324must be created in that directory and it must be included into the original
325.Nm
326by means of the
327.Fn include
328function.
329.Pp
330.Fn include
331may only be called with a relative path and with at most one directory
332component.
333This is by design: Kyua uses the file system structure as the layout of the
334test suite definition.
335Therefore, each subdirectory in a test suite must include its own
336.Nm
337and each
338.Nm
339can only descend into the
340.Nm Ns s
341of immediate subdirectories.
342.Pp
343If you need to source a
344.Nm
345located in disjoint parts of your file system namespace, you will have to
346create a
347.Sq shadow tree
348using symbolic links and possibly helper
349.Nm Ns s
350to plug the various subdirectories together.
351See the
352.Sx EXAMPLES
353section below for details.
354.Pp
355Note that each file is processed in its own Lua environment: there is no
356mechanism to pass state from one file to the other.
357The reason for this is that there is no such thing as a
358.Dq top-level
359.Nm
360in a test suite: the user has to be able to run the test suite from any
361directory in a given hierarchy, and this execution must not depend on files
362that live in parent directories.
363.Ss Top-level Kyuafile
364Every system has a top directory into which test suites get installed.
365The default is
366.Pa __TESTSDIR__ .
367Within this directory live test suites, each of which is in an independent
368subdirectory.
369Each subdirectory can be provided separately by independent third-party
370packages.
371.Pp
372Kyua allows running all the installed test suites at once in order to
373provide comprehensive cross-component reports.
374In order to do this, there is a special file in the top directory that knows
375how to inspect the subdirectories in search for other Kyuafiles and include
376them.
377.Pp
378The
379.Sx FILES
380section includes more details on where this file lives.
381.Ss Helper functions
382The
383.Sq base ,
384.Sq string ,
385and
386.Sq table
387Lua modules are fully available in the context of a
388.Nm .
389.Pp
390The following extra functions are provided by Kyua:
391.Bl -tag -width XX -offset indent
392.It Ft string Fn current_kyuafile
393Returns the absolute path to the current
394.Nm .
395.It Ft string Fn fs.basename "string path"
396Returns the last component of the given path.
397.It Ft string Fn fs.dirname "string path"
398Returns the given path without its last component or a dot if the path has
399a single component.
400.It Ft bool Fn fs.exists "string path"
401Checks if the given path exists.
402If the path is not absolute, it is relative to the directory containing the
403.Nm
404in which the call to this function occurs.
405.It Ft iterator Fn fs.files "string path"
406Opens a directory for scanning of its entries.
407The returned iterator yields an entry on each call, and the entry is simply
408the filename.
409If the path is not absolute, it is relative to the directory containing the
410.Nm
411in which the call to this function occurs.
412.It Ft is_absolute Fn fs.is_absolute "string path"
413Returns true if the given path is absolute; false otherwise.
414.It Ft join Fn fs.join "string path" "string path"
415Concatenates the two paths.
416The second path cannot be absolute.
417.El
418.Sh FILES
419.Bl -tag -width XX
420.It Pa __TESTSDIR__/Kyuafile .
421Top-level
422.Nm
423for the current system.
424.It Pa __EGDIR__/Kyuafile.top .
425Sample file to serve as a top-level
426.Nm .
427.El
428.Sh EXAMPLES
429The following
430.Nm
431is the simplest you can define.
432It provides a test suite definition and registers a couple of different test
433programs using different interfaces:
434.Bd -literal -offset indent
435syntax(2)
436
437test_suite('first')
438
439atf_test_program{name='integration_test'}
440plain_test_program{name='legacy_test'}
441.Ed
442.Pp
443The following example is a bit more elaborate.
444It introduces some metadata properties to the test program definitions and
445recurses into a couple of subdirectories:
446.Bd -literal -offset indent
447syntax(2)
448
449test_suite('second')
450
451plain_test_program{name='legacy_test',
452                   allowed_architectures='amd64 i386',
453                   required_files='/bin/ls',
454                   timeout=30}
455
456tap_test_program{name='privileged_test',
457                 required_user='root'}
458
459include('module-1/Kyuafile')
460include('module-2/Kyuafile')
461.Ed
462.Pp
463The syntax to define custom properties may be not obvious if their names
464have any characters that make the property name not be a valid Lua identifier.
465Dashes are just one example.
466To set such properties, do something like this:
467.Bd -literal -offset indent
468syntax(2)
469
470test_suite('FreeBSD')
471
472plain_test_program{name='the_test',
473                   ['custom.FreeBSD-Bug-Id']='category/12345'}
474.Ed
475.Ss FreeBSD jail execution environment
476The following example configures the test to be run within a temporary jail
477with
478.Xr vnet 9
479support and the permission to create raw sockets:
480.Bd -literal -offset indent
481syntax(2)
482
483test_suite('FreeBSD')
484
485atf_test_program{name='network_test',
486                 execenv='jail',
487                 execenv_jail_params='vnet allow.raw_sockets',
488                 required_user='root'}
489.Ed
490.Pp
491A test case itself may have no requirements in superuser privileges,
492but required_user='root' metadata property reminds that the jail execution
493environment requires
494.Xr kyua 1
495being running with root privileges, and the test is skipped otherwise with
496the respective message.
497The combination of
498.Va execenv
499set to
500.Sq jail
501and
502.Va required_user
503set to
504.Sq unprivileged
505does not work respectively.
506.Ss Connecting disjoint test suites
507Now suppose you had various test suites on your file system and you would
508like to connect them together so that they could be executed and treated as
509a single unit.
510The test suites we would like to connect live under
511.Pa /usr/tests ,
512.Pa /usr/local/tests
513and
514.Pa ~/local/tests .
515.Pp
516We cannot create a
517.Nm
518that references these because the
519.Fn include
520directive does not support absolute paths.
521Instead, what we can do is create a shadow tree using symbolic links:
522.Bd -literal -offset indent
523$ mkdir ~/everything
524$ ln -s /usr/tests ~/everything/system-tests
525$ ln -s /usr/local/tests ~/everything/local-tests
526$ ln -s ~/local/tests ~/everything/home-tests
527.Ed
528.Pp
529And then we create an
530.Pa ~/everything/Kyuafile
531file to drive the execution of the integrated test suite:
532.Bd -literal -offset indent
533syntax(2)
534
535test_suite('test-all-the-things')
536
537include('system-tests/Kyuafile')
538include('local-tests/Kyuafile')
539include('home-tests/Kyuafile')
540.Ed
541.Pp
542Or, simply, you could reuse the sample top-level
543.Nm
544to avoid having to manually craft the list of directories into which to
545recurse:
546.Bd -literal -offset indent
547$ cp __EGDIR__/Kyuafile.top ~/everything/Kyuafile
548.Ed
549.Sh SEE ALSO
550.Xr kyua 1
551