xref: /freebsd/contrib/kyua/doc/kyuafile.5.in (revision 3049b2f5115b3b4aed202274e866f360fa2560d1)
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_kmods
294Whitespace-separated list of kernel module names that the test requires to
295be loaded before it can run.
296This requirement checking is platform-dependent.
297It is ignored for a non-supported platform.
298Supported platforms:
299.Fx .
300.Pp
301ATF:
302.Va require.kmods
303.It Va required_memory
304Amount of physical memory that the test needs to run successfully.
305.Pp
306ATF:
307.Va require.memory
308.It Va required_programs
309Whitespace-separated list of basenames or absolute paths pointing to executable
310binaries that the test requires to exist before it can run.
311.Pp
312ATF:
313.Va require.progs
314.It Va required_user
315If empty, the test has no restrictions on the calling user for it to run.
316If set to
317.Sq unprivileged ,
318the test needs to not run as root.
319If set to
320.Sq root ,
321the test must run as root.
322.Pp
323ATF:
324.Va require.user
325.It Va timeout
326Amount of seconds that the test is allowed to execute before being killed.
327.Pp
328ATF:
329.Va timeout
330.El
331.Ss Recursion
332To reference test programs in another subdirectory, a different
333.Nm
334must be created in that directory and it must be included into the original
335.Nm
336by means of the
337.Fn include
338function.
339.Pp
340.Fn include
341may only be called with a relative path and with at most one directory
342component.
343This is by design: Kyua uses the file system structure as the layout of the
344test suite definition.
345Therefore, each subdirectory in a test suite must include its own
346.Nm
347and each
348.Nm
349can only descend into the
350.Nm Ns s
351of immediate subdirectories.
352.Pp
353If you need to source a
354.Nm
355located in disjoint parts of your file system namespace, you will have to
356create a
357.Sq shadow tree
358using symbolic links and possibly helper
359.Nm Ns s
360to plug the various subdirectories together.
361See the
362.Sx EXAMPLES
363section below for details.
364.Pp
365Note that each file is processed in its own Lua environment: there is no
366mechanism to pass state from one file to the other.
367The reason for this is that there is no such thing as a
368.Dq top-level
369.Nm
370in a test suite: the user has to be able to run the test suite from any
371directory in a given hierarchy, and this execution must not depend on files
372that live in parent directories.
373.Ss Top-level Kyuafile
374Every system has a top directory into which test suites get installed.
375The default is
376.Pa __TESTSDIR__ .
377Within this directory live test suites, each of which is in an independent
378subdirectory.
379Each subdirectory can be provided separately by independent third-party
380packages.
381.Pp
382Kyua allows running all the installed test suites at once in order to
383provide comprehensive cross-component reports.
384In order to do this, there is a special file in the top directory that knows
385how to inspect the subdirectories in search for other Kyuafiles and include
386them.
387.Pp
388The
389.Sx FILES
390section includes more details on where this file lives.
391.Ss Helper functions
392The
393.Sq base ,
394.Sq string ,
395and
396.Sq table
397Lua modules are fully available in the context of a
398.Nm .
399.Pp
400The following extra functions are provided by Kyua:
401.Bl -tag -width XX -offset indent
402.It Ft string Fn current_kyuafile
403Returns the absolute path to the current
404.Nm .
405.It Ft string Fn fs.basename "string path"
406Returns the last component of the given path.
407.It Ft string Fn fs.dirname "string path"
408Returns the given path without its last component or a dot if the path has
409a single component.
410.It Ft bool Fn fs.exists "string path"
411Checks if the given path exists.
412If the path is not absolute, it is relative to the directory containing the
413.Nm
414in which the call to this function occurs.
415.It Ft iterator Fn fs.files "string path"
416Opens a directory for scanning of its entries.
417The returned iterator yields an entry on each call, and the entry is simply
418the filename.
419If the path is not absolute, it is relative to the directory containing the
420.Nm
421in which the call to this function occurs.
422.It Ft is_absolute Fn fs.is_absolute "string path"
423Returns true if the given path is absolute; false otherwise.
424.It Ft join Fn fs.join "string path" "string path"
425Concatenates the two paths.
426The second path cannot be absolute.
427.El
428.Sh FILES
429.Bl -tag -width XX
430.It Pa __TESTSDIR__/Kyuafile .
431Top-level
432.Nm
433for the current system.
434.It Pa __EGDIR__/Kyuafile.top .
435Sample file to serve as a top-level
436.Nm .
437.El
438.Sh EXAMPLES
439The following
440.Nm
441is the simplest you can define.
442It provides a test suite definition and registers a couple of different test
443programs using different interfaces:
444.Bd -literal -offset indent
445syntax(2)
446
447test_suite('first')
448
449atf_test_program{name='integration_test'}
450plain_test_program{name='legacy_test'}
451.Ed
452.Pp
453The following example is a bit more elaborate.
454It introduces some metadata properties to the test program definitions and
455recurses into a couple of subdirectories:
456.Bd -literal -offset indent
457syntax(2)
458
459test_suite('second')
460
461plain_test_program{name='legacy_test',
462                   allowed_architectures='amd64 i386',
463                   required_files='/bin/ls',
464                   timeout=30}
465
466tap_test_program{name='privileged_test',
467                 required_user='root'}
468
469include('module-1/Kyuafile')
470include('module-2/Kyuafile')
471.Ed
472.Pp
473The syntax to define custom properties may be not obvious if their names
474have any characters that make the property name not be a valid Lua identifier.
475Dashes are just one example.
476To set such properties, do something like this:
477.Bd -literal -offset indent
478syntax(2)
479
480test_suite('FreeBSD')
481
482plain_test_program{name='the_test',
483                   ['custom.FreeBSD-Bug-Id']='category/12345'}
484.Ed
485.Ss FreeBSD jail execution environment
486The following example configures the test to be run within a temporary jail
487with
488.Xr VNET 9
489support and the permission to create raw sockets:
490.Bd -literal -offset indent
491syntax(2)
492
493test_suite('FreeBSD')
494
495atf_test_program{name='network_test',
496                 execenv='jail',
497                 execenv_jail_params='vnet allow.raw_sockets',
498                 required_user='root'}
499.Ed
500.Pp
501A test case itself may have no requirements in superuser privileges,
502but required_user='root' metadata property reminds that the jail execution
503environment requires
504.Xr kyua 1
505being running with root privileges, and the test is skipped otherwise with
506the respective message.
507The combination of
508.Va execenv
509set to
510.Sq jail
511and
512.Va required_user
513set to
514.Sq unprivileged
515does not work respectively.
516.Ss Connecting disjoint test suites
517Now suppose you had various test suites on your file system and you would
518like to connect them together so that they could be executed and treated as
519a single unit.
520The test suites we would like to connect live under
521.Pa /usr/tests ,
522.Pa /usr/local/tests
523and
524.Pa ~/local/tests .
525.Pp
526We cannot create a
527.Nm
528that references these because the
529.Fn include
530directive does not support absolute paths.
531Instead, what we can do is create a shadow tree using symbolic links:
532.Bd -literal -offset indent
533$ mkdir ~/everything
534$ ln -s /usr/tests ~/everything/system-tests
535$ ln -s /usr/local/tests ~/everything/local-tests
536$ ln -s ~/local/tests ~/everything/home-tests
537.Ed
538.Pp
539And then we create an
540.Pa ~/everything/Kyuafile
541file to drive the execution of the integrated test suite:
542.Bd -literal -offset indent
543syntax(2)
544
545test_suite('test-all-the-things')
546
547include('system-tests/Kyuafile')
548include('local-tests/Kyuafile')
549include('home-tests/Kyuafile')
550.Ed
551.Pp
552Or, simply, you could reuse the sample top-level
553.Nm
554to avoid having to manually craft the list of directories into which to
555recurse:
556.Bd -literal -offset indent
557$ cp __EGDIR__/Kyuafile.top ~/everything/Kyuafile
558.Ed
559.Sh SEE ALSO
560.Xr kyua 1
561