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