xref: /freebsd/tests/ci/tools/freebsdci (revision 92a7f2d577630d670643f855a1d123a2260102af)
1#!/bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2024 The FreeBSD Foundation
6#
7# This software was developed by Cybermancer Infosec <bofh@FreeBSD.org>
8# under sponsorship from the FreeBSD Foundation.
9#
10# PROVIDE: freebsdci
11# REQUIRE: LOGIN FILESYSTEMS
12# KEYWORD: firstboot
13
14# This script is used to run the firstboot CI tests on the first boot of a
15# FreeBSD image. It is automatically disabled after the first boot.
16#
17# The script will run the firstboot CI tests and then shut down the system.
18# The tests are run in the foreground so that the system can shut down
19# immediately after the tests are finished.
20#
21# Default test types are full and smoke.  To run only the smoke tests, set
22# freebsdci_type="smoke" in /etc/rc.conf.local or /etc/rc.conf.
23# To run only the full tests, set freebsdci_type="full" in
24# /etc/rc.conf.local or /etc/rc.conf.
25
26. /etc/rc.subr
27
28name="freebsdci"
29desc="Run FreeBSD CI"
30rcvar=freebsdci_enable
31start_cmd="firstboot_ci_run"
32stop_cmd=":"
33os_arch=$(uname -p)
34parallelism=$(nproc)
35tardev=/dev/vtbd1
36metadir=/meta
37istar=$(file -s ${tardev} | grep "POSIX tar archive" | wc -l)
38
39load_rc_config $name
40: ${freebsdci_enable:="NO"}
41: ${freebsdci_type:="full"}
42: ${freebsdci_test_filters:=""}
43PATH="${PATH}:/usr/local/sbin:/usr/local/bin"
44
45auto_shutdown()
46{
47	# NOTE: Currently RISC-V kernels lack the ability to
48	#      make qemu exit on shutdown. Reboot instead;
49	#      it makes qemu exit too.
50	case "$os_arch" in
51		riscv64)
52			shutdown -r now
53		;;
54		*)
55			shutdown -p now
56		;;
57	esac
58}
59
60smoke_tests()
61{
62	echo
63	echo "--------------------------------------------------------------"
64	echo "BOOT sequence COMPLETED"
65	echo "INITIATING system SHUTDOWN"
66	echo "--------------------------------------------------------------"
67}
68
69full_tests()
70{
71	echo
72	echo "--------------------------------------------------------------"
73	echo "BOOT sequence COMPLETED"
74	echo "TEST sequence STARTED"
75	if [ "${istar}" -eq 1 ]; then
76		rm -fr ${metadir}
77		mkdir -p ${metadir}
78		tar xvf ${tardev} -C ${metadir}
79		cd /usr/tests
80		set +e
81		kyua \
82			-v parallelism=${parallelism} \
83			test ${freebsdci_test_filters}
84		rc=$?
85		set -e
86		if [ ${rc} -ne 0 ] && [ ${rc} -ne 1 ]; then
87			exit ${rc}
88		fi
89		kyua report --verbose --results-filter passed,skipped,xfail,broken,failed --output test-report.txt
90		kyua report-junit --output=test-report.xml
91		mv test-report.* /${metadir}
92		tar cvf ${tardev} -C ${metadir} .
93	else
94		echo "ERROR: no device with POSIX tar archive format found."
95		# Don't shutdown because this is not run in unattended mode
96		exit 1
97	fi
98	echo "TEST sequence COMPLETED"
99	echo "INITIATING system SHUTDOWN"
100	echo "--------------------------------------------------------------"
101}
102
103firstboot_ci_run()
104{
105	if [ "$freebsdci_type" = "smoke" ]; then
106		smoke_tests
107	elif [ "$freebsdci_type" = "full" ]; then
108		full_tests
109	fi
110	auto_shutdown
111}
112
113run_rc_command "$1"
114