xref: /freebsd/tests/ci/tools/freebsdci (revision 57f725a6a6c19b12015854589764babe58252155)
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"}
42PATH="${PATH}:/usr/local/sbin:/usr/local/bin"
43
44auto_shutdown()
45{
46	# NOTE: Currently RISC-V kernels lack the ability to
47	#      make qemu exit on shutdown. Reboot instead;
48	#      it makes qemu exit too.
49	case "$os_arch" in
50		riscv64)
51			shutdown -r now
52		;;
53		*)
54			shutdown -p now
55		;;
56	esac
57}
58
59smoke_tests()
60{
61	echo
62	echo "--------------------------------------------------------------"
63	echo "BOOT sequence COMPLETED"
64	echo "INITIATING system SHUTDOWN"
65	echo "--------------------------------------------------------------"
66}
67
68full_tests()
69{
70	echo
71	echo "--------------------------------------------------------------"
72	echo "BOOT sequence COMPLETED"
73	echo "TEST sequence STARTED"
74	if [ "${istar}" -eq 1 ]; then
75		rm -fr ${metadir}
76		mkdir -p ${metadir}
77		tar xvf ${tardev} -C ${metadir}
78		cd /usr/tests
79		set +e
80		kyua -v parallelism=${parallelism} test
81		rc=$?
82		set -e
83		if [ ${rc} -ne 0 ] && [ ${rc} -ne 1 ]; then
84			exit ${rc}
85		fi
86		kyua report --verbose --results-filter passed,skipped,xfail,broken,failed --output test-report.txt
87		kyua report-junit --output=test-report.xml
88		mv test-report.* /${metadir}
89		tar cvf ${tardev} -C ${metadir} .
90	else
91		echo "ERROR: no device with POSIX tar archive format found."
92		# Don't shutdown because this is not run in unattended mode
93		exit 1
94	fi
95	echo "TEST sequence COMPLETED"
96	echo "INITIATING system SHUTDOWN"
97	echo "--------------------------------------------------------------"
98}
99
100firstboot_ci_run()
101{
102	if [ "$freebsdci_type" = "smoke" ]; then
103		smoke_tests
104	elif [ "$freebsdci_type" = "full" ]; then
105		full_tests
106	fi
107	auto_shutdown
108}
109
110run_rc_command "$1"
111