xref: /freebsd/lib/libbe/tests/be_create.sh (revision 069ac184)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2019 Rob Wing
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27
28# The code for the following tests was copied from the
29# bectl tests found in src/sbin/bectl/tests, modified as needed.
30
31ZPOOL_NAME_FILE=zpool_name
32get_zpool_name()
33{
34	cat $ZPOOL_NAME_FILE
35}
36make_zpool_name()
37{
38	mktemp -u libbe_test_XXXXXX > $ZPOOL_NAME_FILE
39	get_zpool_name
40}
41
42# Establishes a libbe zpool that can be used for some light testing; contains
43# a 'default' BE and not much else.
44libbe_create_setup()
45{
46	zpool=$1
47	disk=$2
48	mnt=$3
49
50	# Sanity check to make sure `make_zpool_name` succeeded
51	atf_check test -n "$zpool"
52
53	kldload -n -q zfs || atf_skip "ZFS module not loaded on the current system"
54	atf_check mkdir -p ${mnt}
55	atf_check truncate -s 1G ${disk}
56	atf_check zpool create -o altroot=${mnt} ${zpool} ${disk}
57	atf_check zfs create -o mountpoint=none ${zpool}/ROOT
58	atf_check zfs create -o mountpoint=/ -o canmount=noauto \
59	    ${zpool}/ROOT/default
60        atf_check zfs create -o mountpoint=/usr -o canmount=noauto \
61	    ${zpool}/ROOT/default/usr
62        atf_check zfs create -o mountpoint=/usr/obj -o canmount=noauto \
63	    ${zpool}/ROOT/default/usr/obj
64}
65
66libbe_cleanup()
67{
68	zpool=$1
69	cwd=$(atf_get_srcdir)
70
71	if [ -z "$zpool" ]; then
72		echo "Skipping cleanup; zpool not set up"
73	elif zpool get health ${zpool} >/dev/null 2>&1; then
74		zpool destroy -f ${zpool}
75	fi
76
77	if [ -f "${cwd}/disk.img" ]; then
78		rm ${cwd}/disk.img
79	fi
80}
81
82atf_test_case libbe_create cleanup
83libbe_create_head()
84{
85	atf_set "descr" "check _be_create from libbe"
86	atf_set "require.user" root
87}
88libbe_create_body()
89{
90	if [ "$(atf_config_get ci false)" = "true" ] && \
91		[ "$(uname -p)" = "i386" ]; then
92		atf_skip "https://bugs.freebsd.org/249055"
93	fi
94
95	if [ "$(atf_config_get ci false)" = "true" ] && \
96		[ "$(uname -p)" = "armv7" ]; then
97		atf_skip "https://bugs.freebsd.org/249229"
98	fi
99
100	cwd=$(atf_get_srcdir)
101	zpool=$(make_zpool_name)
102	disk=${cwd}/disk.img
103	mount=${cwd}/mnt
104	prog=${cwd}/./target_prog
105
106	# preliminary setup/checks
107	atf_require_prog $prog
108	libbe_create_setup ${zpool} ${disk} ${mount}
109
110	# a recursive and non-recursive snapshot to test against
111	atf_check zfs snapshot ${zpool}/ROOT/default@non-recursive
112	atf_check zfs snapshot -r ${zpool}/ROOT/default@recursive
113
114	# create a dataset after snapshots were taken
115        atf_check zfs create -o mountpoint=/usr/src -o canmount=noauto \
116	    ${zpool}/ROOT/default/usr/src
117
118	# test boot environment creation with depth of 0 (i.e. a non-recursive boot environment).
119	atf_check $prog "${zpool}/ROOT" \
120		nonrecursive \
121		"${zpool}/ROOT/default@non-recursive" \
122		0
123	# the dataset should exist
124	atf_check -o not-empty \
125		zfs list "${zpool}/ROOT/nonrecursive"
126	# the child dataset should not exist.
127	atf_check -e not-empty -s not-exit:0 \
128		zfs list "${zpool}/ROOT/nonrecursive/usr"
129
130	# test boot environment creation with unlimited depth (i.e. a recursive boot environment).
131	atf_check $prog "${zpool}/ROOT" \
132		recursive \
133		"${zpool}/ROOT/default@recursive" \
134		-1
135	# the dataset should exist
136	atf_check -o not-empty \
137		zfs list "${zpool}/ROOT/recursive"
138	# the child dataset should exist
139	atf_check -o not-empty \
140		  zfs list "${zpool}/ROOT/recursive/usr"
141	# the child dataset should exist
142	atf_check -o not-empty \
143		  zfs list "${zpool}/ROOT/recursive/usr/obj"
144	# the child dataset should not exist.
145	atf_check -e not-empty -s not-exit:0 \
146		zfs list "${zpool}/ROOT/recursive/usr/src"
147
148	# test boot environment creation with a depth of 1
149	atf_check $prog "${zpool}/ROOT" \
150		depth \
151		"${zpool}/ROOT/default@recursive" \
152		1
153	# the child dataset should exist
154	atf_check -o not-empty \
155		zfs list "${zpool}/ROOT/depth/usr"
156	# the child dataset should not exist.
157	atf_check -e not-empty -s not-exit:0 \
158		zfs list "${zpool}/ROOT/depth/usr/obj"
159	# the child dataset should not exist.
160	atf_check -e not-empty -s not-exit:0 \
161		zfs list "${zpool}/ROOT/depth/usr/src"
162
163
164	# create a recursive boot environment named 'relative-snap'.
165	# This test is to ensure that a relative snapshot label can be used,
166	# (i.e. the format: 'bootenvironment@snapshot')
167	atf_check $prog "${zpool}/ROOT" \
168		relative-snap \
169		default@recursive \
170		-1
171	# the dataset should exist
172	atf_check -o not-empty \
173		zfs list "${zpool}/ROOT/relative-snap"
174	# the child dataset should exist
175	atf_check -o not-empty \
176		  zfs list "${zpool}/ROOT/relative-snap/usr"
177}
178
179libbe_create_cleanup()
180{
181	libbe_cleanup $(get_zpool_name)
182}
183
184atf_init_test_cases()
185{
186	atf_add_test_case libbe_create
187}
188