xref: /freebsd/lib/libbe/tests/be_create.sh (revision fbf2a778760f24565eab950ced3e2ad1df4cf844)
1#
2# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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# $FreeBSD$
28
29# The code for the following tests was copied from the
30# bectl tests found in src/sbin/bectl/tests, modified as needed.
31
32ZPOOL_NAME_FILE=zpool_name
33get_zpool_name()
34{
35	cat $ZPOOL_NAME_FILE
36}
37make_zpool_name()
38{
39	mktemp -u libbe_test_XXXXXX > $ZPOOL_NAME_FILE
40	get_zpool_name
41}
42
43# Establishes a libbe zpool that can be used for some light testing; contains
44# a 'default' BE and not much else.
45libbe_create_setup()
46{
47	zpool=$1
48	disk=$2
49	mnt=$3
50
51	# Sanity check to make sure `make_zpool_name` succeeded
52	atf_check test -n "$zpool"
53
54	kldload -n -q zfs || atf_skip "ZFS module not loaded on the current system"
55	atf_check mkdir -p ${mnt}
56	atf_check truncate -s 1G ${disk}
57	atf_check zpool create -o altroot=${mnt} ${zpool} ${disk}
58	atf_check zfs create -o mountpoint=none ${zpool}/ROOT
59	atf_check zfs create -o mountpoint=/ -o canmount=noauto \
60	    ${zpool}/ROOT/default
61        atf_check zfs create -o mountpoint=/usr -o canmount=noauto \
62	    ${zpool}/ROOT/default/usr
63        atf_check zfs create -o mountpoint=/usr/obj -o canmount=noauto \
64	    ${zpool}/ROOT/default/usr/obj
65}
66
67libbe_cleanup()
68{
69	zpool=$1
70	cwd=$(atf_get_srcdir)
71
72	if [ -z "$zpool" ]; then
73		echo "Skipping cleanup; zpool not set up"
74	elif zpool get health ${zpool} >/dev/null 2>&1; then
75		zpool destroy -f ${zpool}
76	fi
77
78	if [ -f "${cwd}/disk.img" ]; then
79		rm ${cwd}/disk.img
80	fi
81}
82
83atf_test_case libbe_create cleanup
84libbe_create_head()
85{
86	atf_set "descr" "check _be_create from libbe"
87	atf_set "require.user" root
88}
89libbe_create_body()
90{
91	if [ "$(atf_config_get ci false)" = "true" ] && \
92		[ "$(uname -p)" = "i386" ]; then
93		atf_skip "https://bugs.freebsd.org/249055"
94	fi
95
96	cwd=$(atf_get_srcdir)
97	zpool=$(make_zpool_name)
98	disk=${cwd}/disk.img
99	mount=${cwd}/mnt
100	prog=${cwd}/./target_prog
101
102	# preliminary setup/checks
103	atf_require_prog $prog
104	libbe_create_setup ${zpool} ${disk} ${mount}
105
106	# a recursive and non-recursive snapshot to test against
107	atf_check zfs snapshot ${zpool}/ROOT/default@non-recursive
108	atf_check zfs snapshot -r ${zpool}/ROOT/default@recursive
109
110	# create a dataset after snapshots were taken
111        atf_check zfs create -o mountpoint=/usr/src -o canmount=noauto \
112	    ${zpool}/ROOT/default/usr/src
113
114	# test boot environment creation with depth of 0 (i.e. a non-recursive boot environment).
115	atf_check $prog "${zpool}/ROOT" \
116		nonrecursive \
117		"${zpool}/ROOT/default@non-recursive" \
118		0
119	# the dataset should exist
120	atf_check -o not-empty \
121		zfs list "${zpool}/ROOT/nonrecursive"
122	# the child dataset should not exist.
123	atf_check -e not-empty -s not-exit:0 \
124		zfs list "${zpool}/ROOT/nonrecursive/usr"
125
126	# test boot environment creation with unlimited depth (i.e. a recursive boot environment).
127	atf_check $prog "${zpool}/ROOT" \
128		recursive \
129		"${zpool}/ROOT/default@recursive" \
130		-1
131	# the dataset should exist
132	atf_check -o not-empty \
133		zfs list "${zpool}/ROOT/recursive"
134	# the child dataset should exist
135	atf_check -o not-empty \
136		  zfs list "${zpool}/ROOT/recursive/usr"
137	# the child dataset should exist
138	atf_check -o not-empty \
139		  zfs list "${zpool}/ROOT/recursive/usr/obj"
140	# the child dataset should not exist.
141	atf_check -e not-empty -s not-exit:0 \
142		zfs list "${zpool}/ROOT/recursive/usr/src"
143
144	# test boot environment creation with a depth of 1
145	atf_check $prog "${zpool}/ROOT" \
146		depth \
147		"${zpool}/ROOT/default@recursive" \
148		1
149	# the child dataset should exist
150	atf_check -o not-empty \
151		zfs list "${zpool}/ROOT/depth/usr"
152	# the child dataset should not exist.
153	atf_check -e not-empty -s not-exit:0 \
154		zfs list "${zpool}/ROOT/depth/usr/obj"
155	# the child dataset should not exist.
156	atf_check -e not-empty -s not-exit:0 \
157		zfs list "${zpool}/ROOT/depth/usr/src"
158
159
160	# create a recursive boot environment named 'relative-snap'.
161	# This test is to ensure that a relative snapshot label can be used,
162	# (i.e. the format: 'bootenvironment@snapshot')
163	atf_check $prog "${zpool}/ROOT" \
164		relative-snap \
165		default@recursive \
166		-1
167	# the dataset should exist
168	atf_check -o not-empty \
169		zfs list "${zpool}/ROOT/relative-snap"
170	# the child dataset should exist
171	atf_check -o not-empty \
172		  zfs list "${zpool}/ROOT/relative-snap/usr"
173}
174
175libbe_create_cleanup()
176{
177	libbe_cleanup $(get_zpool_name)
178}
179
180atf_init_test_cases()
181{
182	atf_add_test_case libbe_create
183}
184