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