xref: /freebsd/sbin/bectl/tests/bectl_test.sh (revision 1edb7116)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
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
28ZPOOL_NAME_FILE=zpool_name
29get_zpool_name()
30{
31	cat $ZPOOL_NAME_FILE
32}
33make_zpool_name()
34{
35	mktemp -u bectl_test_XXXXXX > $ZPOOL_NAME_FILE
36	get_zpool_name
37}
38
39# Establishes a bectl_create zpool that can be used for some light testing; contains
40# a 'default' BE and not much else.
41bectl_create_setup()
42{
43	zpool=$1
44	disk=$2
45	mnt=$3
46
47	# Sanity check to make sure `make_zpool_name` succeeded
48	atf_check test -n "$zpool"
49
50	kldload -n -q zfs || atf_skip "ZFS module not loaded on the current system"
51	if ! getconf MIN_HOLE_SIZE "$(pwd)"; then
52		echo "getconf MIN_HOLE_SIZE $(pwd) failed; sparse files " \
53		    "probably not supported by file system"
54		mount
55		atf_skip "Test's work directory does not support sparse files;" \
56		    "try with a different TMPDIR?"
57	fi
58	atf_check mkdir -p ${mnt}
59	atf_check truncate -s 1G ${disk}
60	atf_check zpool create -R ${mnt} ${zpool} ${disk}
61	atf_check zfs create -o mountpoint=none ${zpool}/ROOT
62	atf_check zfs create -o mountpoint=/ -o canmount=noauto \
63	    ${zpool}/ROOT/default
64}
65bectl_create_deep_setup()
66{
67	zpool=$1
68	disk=$2
69	mnt=$3
70
71	# Sanity check to make sure `make_zpool_name` succeeded
72	atf_check test -n "$zpool"
73
74	bectl_create_setup ${zpool} ${disk} ${mnt}
75	atf_check mkdir -p ${root}
76	atf_check -o ignore bectl -r ${zpool}/ROOT mount default ${root}
77	atf_check mkdir -p ${root}/usr
78	atf_check zfs create -o mountpoint=/usr -o canmount=noauto \
79	    ${zpool}/ROOT/default/usr
80	atf_check -o ignore bectl -r ${zpool}/ROOT umount default
81}
82
83bectl_cleanup()
84{
85	zpool=$1
86	if [ -z "$zpool" ]; then
87		echo "Skipping cleanup; zpool not set up"
88	elif zpool get health ${zpool} >/dev/null 2>&1; then
89		zpool destroy -f ${zpool}
90	fi
91}
92
93atf_test_case bectl_create cleanup
94bectl_create_head()
95{
96	atf_set "descr" "Check the various forms of bectl create"
97	atf_set "require.user" root
98}
99bectl_create_body()
100{
101	if [ "$(atf_config_get ci false)" = "true" ] && \
102		[ "$(uname -p)" = "i386" ]; then
103		atf_skip "https://bugs.freebsd.org/249055"
104	fi
105
106	if [ "$(atf_config_get ci false)" = "true" ] && \
107		[ "$(uname -p)" = "armv7" ]; then
108		atf_skip "https://bugs.freebsd.org/249229"
109	fi
110
111	cwd=$(realpath .)
112	zpool=$(make_zpool_name)
113	disk=${cwd}/disk.img
114	mount=${cwd}/mnt
115
116	bectl_create_setup ${zpool} ${disk} ${mount}
117
118	# Create a child dataset that will be used to test creation
119	# of recursive and non-recursive boot environments.
120	atf_check zfs create -o mountpoint=/usr -o canmount=noauto \
121	    ${zpool}/ROOT/default/usr
122
123	# BE datasets with spaces are not bootable, PR 254441.
124	atf_check -e not-empty -s not-exit:0 \
125		bectl -r ${zpool}/ROOT create "foo bar"
126
127	# Test standard creation, creation of a snapshot, and creation from a
128	# snapshot.
129	atf_check bectl -r ${zpool}/ROOT create -e default default2
130	atf_check bectl -r ${zpool}/ROOT create default2@test_snap
131	atf_check bectl -r ${zpool}/ROOT create -e default2@test_snap default3
132
133	# Test standard creation, creation of a snapshot, and creation from a
134	# snapshot for recursive boot environments.
135	atf_check bectl -r ${zpool}/ROOT create -r -e default recursive
136	atf_check bectl -r ${zpool}/ROOT create -r recursive@test_snap
137	atf_check bectl -r ${zpool}/ROOT create -r -e recursive@test_snap recursive-snap
138
139	# Test that non-recursive boot environments have no child datasets.
140	atf_check -e not-empty -s not-exit:0 \
141		zfs list "${zpool}/ROOT/default2/usr"
142	atf_check -e not-empty -s not-exit:0 \
143		zfs list "${zpool}/ROOT/default3/usr"
144
145	# Test that recursive boot environments have child datasets.
146	atf_check -o not-empty \
147		zfs list "${zpool}/ROOT/recursive/usr"
148	atf_check -o not-empty \
149		zfs list "${zpool}/ROOT/recursive-snap/usr"
150}
151bectl_create_cleanup()
152{
153	bectl_cleanup $(get_zpool_name)
154}
155
156atf_test_case bectl_destroy cleanup
157bectl_destroy_head()
158{
159	atf_set "descr" "Check bectl destroy"
160	atf_set "require.user" root
161}
162bectl_destroy_body()
163{
164	if [ "$(atf_config_get ci false)" = "true" ] && \
165		[ "$(uname -p)" = "i386" ]; then
166		atf_skip "https://bugs.freebsd.org/249055"
167	fi
168
169	if [ "$(atf_config_get ci false)" = "true" ] && \
170		[ "$(uname -p)" = "armv7" ]; then
171		atf_skip "https://bugs.freebsd.org/249229"
172	fi
173
174	cwd=$(realpath .)
175	zpool=$(make_zpool_name)
176	disk=${cwd}/disk.img
177	mount=${cwd}/mnt
178	root=${mount}/root
179
180	bectl_create_setup ${zpool} ${disk} ${mount}
181	atf_check bectl -r ${zpool}/ROOT create -e default default2
182	atf_check -o not-empty zfs get mountpoint ${zpool}/ROOT/default2
183	atf_check -e ignore bectl -r ${zpool}/ROOT destroy default2
184	atf_check -e not-empty -s not-exit:0 zfs get mountpoint ${zpool}/ROOT/default2
185
186	# Test origin snapshot deletion when the snapshot to be destroyed
187	# belongs to a mounted dataset, see PR 236043.
188	atf_check mkdir -p ${root}
189	atf_check -o not-empty bectl -r ${zpool}/ROOT mount default ${root}
190	atf_check bectl -r ${zpool}/ROOT create -e default default3
191	atf_check bectl -r ${zpool}/ROOT destroy -o default3
192	atf_check bectl -r ${zpool}/ROOT unmount default
193
194	# create two be from the same parent and destroy the parent
195	atf_check bectl -r ${zpool}/ROOT create -e default default2
196	atf_check bectl -r ${zpool}/ROOT create -e default default3
197	atf_check bectl -r ${zpool}/ROOT destroy default
198	atf_check bectl -r ${zpool}/ROOT destroy default2
199	atf_check bectl -r ${zpool}/ROOT rename default3 default
200
201	# Create a BE, have it be the parent for another and repeat, then start
202	# deleting environments.  Arbitrarily chose default3 as the first.
203	# Sleeps are required to prevent conflicting snapshots- libbe will
204	# use the time with a serial at the end as needed to prevent collisions,
205	# but as BEs get promoted the snapshot names will convert and conflict
206	# anyways.  libbe should perhaps consider adding something extra to the
207	# default name to prevent collisions like this, but the default name
208	# includes down to the second and creating BEs this rapidly is perhaps
209	# uncommon enough.
210	atf_check bectl -r ${zpool}/ROOT create -e default default2
211	sleep 1
212	atf_check bectl -r ${zpool}/ROOT create -e default2 default3
213	sleep 1
214	atf_check bectl -r ${zpool}/ROOT create -e default3 default4
215	atf_check bectl -r ${zpool}/ROOT destroy default3
216	atf_check bectl -r ${zpool}/ROOT destroy default2
217	atf_check bectl -r ${zpool}/ROOT destroy default4
218
219	# Create two BEs, then create an unrelated snapshot on the originating
220	# BE and destroy it.  We shouldn't have promoted the second BE, and it's
221	# only possible to tell if we promoted it by making sure we didn't
222	# demote the first BE at some point -- if we did, it's origin will no
223	# longer be empty.
224	atf_check bectl -r ${zpool}/ROOT create -e default default2
225	atf_check bectl -r ${zpool}/ROOT create default@test
226
227	atf_check bectl -r ${zpool}/ROOT destroy default@test
228	atf_check -o inline:"-\n" zfs get -Ho value origin ${zpool}/ROOT/default
229	atf_check bectl -r ${zpool}/ROOT destroy default2
230
231	# As observed by beadm, if we explicitly try to destroy a snapshot that
232	# leads to clones, we shouldn't have allowed it.
233	atf_check bectl -r ${zpool}/ROOT create default@test
234	atf_check bectl -r ${zpool}/ROOT create -e default@test default2
235
236	atf_check -e  not-empty -s not-exit:0 bectl -r ${zpool}/ROOT destroy \
237	    default@test
238}
239bectl_destroy_cleanup()
240{
241	bectl_cleanup $(get_zpool_name)
242}
243
244atf_test_case bectl_export_import cleanup
245bectl_export_import_head()
246{
247	atf_set "descr" "Check bectl export and import"
248	atf_set "require.user" root
249}
250bectl_export_import_body()
251{
252	if [ "$(atf_config_get ci false)" = "true" ] && \
253		[ "$(uname -p)" = "i386" ]; then
254		atf_skip "https://bugs.freebsd.org/249055"
255	fi
256
257	if [ "$(atf_config_get ci false)" = "true" ] && \
258		[ "$(uname -p)" = "armv7" ]; then
259		atf_skip "https://bugs.freebsd.org/249229"
260	fi
261
262	cwd=$(realpath .)
263	zpool=$(make_zpool_name)
264	disk=${cwd}/disk.img
265	mount=${cwd}/mnt
266
267	bectl_create_setup ${zpool} ${disk} ${mount}
268	atf_check -o save:exported bectl -r ${zpool}/ROOT export default
269	atf_check -x "bectl -r ${zpool}/ROOT import default2 < exported"
270	atf_check -o not-empty zfs get mountpoint ${zpool}/ROOT/default2
271	atf_check -e ignore bectl -r ${zpool}/ROOT destroy default2
272	atf_check -e not-empty -s not-exit:0 zfs get mountpoint \
273	    ${zpool}/ROOT/default2
274}
275bectl_export_import_cleanup()
276{
277	bectl_cleanup $(get_zpool_name)
278}
279
280atf_test_case bectl_list cleanup
281bectl_list_head()
282{
283	atf_set "descr" "Check bectl list"
284	atf_set "require.user" root
285}
286bectl_list_body()
287{
288	if [ "$(atf_config_get ci false)" = "true" ] && \
289		[ "$(uname -p)" = "i386" ]; then
290		atf_skip "https://bugs.freebsd.org/249055"
291	fi
292
293	if [ "$(atf_config_get ci false)" = "true" ] && \
294		[ "$(uname -p)" = "armv7" ]; then
295		atf_skip "https://bugs.freebsd.org/249229"
296	fi
297
298	cwd=$(realpath .)
299	zpool=$(make_zpool_name)
300	disk=${cwd}/disk.img
301	mount=${cwd}/mnt
302
303	bectl_create_setup ${zpool} ${disk} ${mount}
304	# Test the list functionality, including that BEs come and go away
305	# as they're created and destroyed.  Creation and destruction tests
306	# use the 'zfs' utility to verify that they're actually created, so
307	# these are just light tests that 'list' is picking them up.
308	atf_check -o save:list.out bectl -r ${zpool}/ROOT list
309	atf_check -o not-empty grep 'default' list.out
310	atf_check bectl -r ${zpool}/ROOT create -e default default2
311	atf_check -o save:list.out bectl -r ${zpool}/ROOT list
312	atf_check -o not-empty grep 'default2' list.out
313	atf_check -e ignore bectl -r ${zpool}/ROOT destroy default2
314	atf_check -o save:list.out bectl -r ${zpool}/ROOT list
315	atf_check -s not-exit:0 grep 'default2' list.out
316	# XXX TODO: Formatting checks
317}
318bectl_list_cleanup()
319{
320	bectl_cleanup $(get_zpool_name)
321}
322
323atf_test_case bectl_mount cleanup
324bectl_mount_head()
325{
326	atf_set "descr" "Check bectl mount/unmount"
327	atf_set "require.user" root
328}
329bectl_mount_body()
330{
331	if [ "$(atf_config_get ci false)" = "true" ] && \
332		[ "$(uname -p)" = "i386" ]; then
333		atf_skip "https://bugs.freebsd.org/249055"
334	fi
335
336	if [ "$(atf_config_get ci false)" = "true" ] && \
337		[ "$(uname -p)" = "armv7" ]; then
338		atf_skip "https://bugs.freebsd.org/249229"
339	fi
340
341	cwd=$(realpath .)
342	zpool=$(make_zpool_name)
343	disk=${cwd}/disk.img
344	mount=${cwd}/mnt
345	root=${mount}/root
346
347	bectl_create_deep_setup ${zpool} ${disk} ${mount}
348	atf_check mkdir -p ${root}
349	# Test unmount first...
350	atf_check -o not-empty bectl -r ${zpool}/ROOT mount default ${root}
351	atf_check -o not-empty -x "mount | grep '^${zpool}/ROOT/default'"
352	atf_check bectl -r ${zpool}/ROOT unmount default
353	atf_check -s not-exit:0 -x "mount | grep '^${zpool}/ROOT/default'"
354	# Then umount!
355	atf_check -o not-empty bectl -r ${zpool}/ROOT mount default ${root}
356	atf_check -o not-empty -x "mount | grep '^${zpool}/ROOT/default'"
357	atf_check bectl -r ${zpool}/ROOT umount default
358	atf_check -s not-exit:0 -x "mount | grep '^${zpool}/ROOT/default'"
359}
360bectl_mount_cleanup()
361{
362	bectl_cleanup $(get_zpool_name)
363}
364
365atf_test_case bectl_rename cleanup
366bectl_rename_head()
367{
368	atf_set "descr" "Check bectl rename"
369	atf_set "require.user" root
370}
371bectl_rename_body()
372{
373	if [ "$(atf_config_get ci false)" = "true" ] && \
374		[ "$(uname -p)" = "i386" ]; then
375		atf_skip "https://bugs.freebsd.org/249055"
376	fi
377
378	if [ "$(atf_config_get ci false)" = "true" ] && \
379		[ "$(uname -p)" = "armv7" ]; then
380		atf_skip "https://bugs.freebsd.org/249229"
381	fi
382
383	cwd=$(realpath .)
384	zpool=$(make_zpool_name)
385	disk=${cwd}/disk.img
386	mount=${cwd}/mnt
387
388	bectl_create_setup ${zpool} ${disk} ${mount}
389	atf_check bectl -r ${zpool}/ROOT rename default default2
390	atf_check -o not-empty zfs get mountpoint ${zpool}/ROOT/default2
391	atf_check -e not-empty -s not-exit:0 zfs get mountpoint \
392	    ${zpool}/ROOT/default
393}
394bectl_rename_cleanup()
395{
396	bectl_cleanup $(get_zpool_name)
397}
398
399atf_test_case bectl_jail cleanup
400bectl_jail_head()
401{
402	atf_set "descr" "Check bectl rename"
403	atf_set "require.user" root
404	atf_set "require.progs" jail
405}
406bectl_jail_body()
407{
408	if [ "$(atf_config_get ci false)" = "true" ] && \
409		[ "$(uname -p)" = "i386" ]; then
410		atf_skip "https://bugs.freebsd.org/249055"
411	fi
412
413	if [ "$(atf_config_get ci false)" = "true" ] && \
414		[ "$(uname -p)" = "armv7" ]; then
415		atf_skip "https://bugs.freebsd.org/249229"
416	fi
417
418	cwd=$(realpath .)
419	zpool=$(make_zpool_name)
420	disk=${cwd}/disk.img
421	mount=${cwd}/mnt
422	root=${mount}/root
423
424	if [ ! -f /rescue/rescue ]; then
425		atf_skip "This test requires a rescue binary"
426	fi
427	bectl_create_deep_setup ${zpool} ${disk} ${mount}
428	# Prepare our minimal BE... plop a rescue binary into it
429	atf_check mkdir -p ${root}
430	atf_check -o ignore bectl -r ${zpool}/ROOT mount default ${root}
431	atf_check mkdir -p ${root}/rescue
432	atf_check cp /rescue/rescue ${root}/rescue/rescue
433	atf_check bectl -r ${zpool}/ROOT umount default
434
435	# Prepare some more boot environments
436	atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT create -e default target
437	atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT create -e default 1234
438
439	# Attempt to unjail a BE with numeric name; jail_getid at one point
440	# did not validate that the input was a valid jid before returning the
441	# jid.
442	atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b 1234
443	atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail 1234
444
445	# When a jail name is not explicit, it should match the jail id.
446	atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b -o jid=233637 default
447	atf_check -o inline:"233637\n" -s exit:0 -x "jls -j 233637 name"
448	atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail default
449
450	# Basic command-mode tests, with and without jail cleanup
451	atf_check -o inline:"rescue\nusr\n" bectl -r ${zpool}/ROOT \
452	    jail default /rescue/rescue ls -1
453	atf_check -o inline:"rescue\nusr\n" bectl -r ${zpool}/ROOT \
454	    jail -Uo path=${root} default /rescue/rescue ls -1
455	atf_check [ -f ${root}/rescue/rescue ]
456	atf_check bectl -r ${zpool}/ROOT ujail default
457
458	# Batch mode tests
459	atf_check bectl -r ${zpool}/ROOT jail -bo path=${root} default
460	atf_check -o not-empty -x "jls | grep -F \"${root}\""
461	atf_check bectl -r ${zpool}/ROOT ujail default
462	atf_check -s not-exit:0 -x "jls | grep -F \"${root}\""
463	# 'unjail' naming
464	atf_check bectl -r ${zpool}/ROOT jail -b default
465	atf_check bectl -r ${zpool}/ROOT unjail default
466	atf_check -s not-exit:0 -x "jls | grep -F \"${root}\""
467	# 'unjail' by BE name. Force bectl to lookup jail id by the BE name.
468	atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b default
469	atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b -o name=bectl_test target
470	atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail target
471	atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail default
472	# cannot unjail an unjailed BE (by either command name)
473	atf_check -e ignore -s not-exit:0 bectl -r ${zpool}/ROOT ujail default
474	atf_check -e ignore -s not-exit:0 bectl -r ${zpool}/ROOT unjail default
475
476	# set+unset
477	atf_check bectl -r ${zpool}/ROOT jail -b -o path=${root} -u path default
478	# Ensure that it didn't mount at ${root}
479	atf_check -s not-exit:0 -x "mount | grep -F '${root}'"
480	atf_check bectl -r ${zpool}/ROOT ujail default
481}
482
483# If a test has failed, it's possible that the boot environment hasn't
484# been 'unjail'ed. We want to remove the jail before 'bectl_cleanup'
485# attempts to destroy the zpool.
486bectl_jail_cleanup()
487{
488	if [ "$(atf_config_get ci false)" = "true" ] && \
489		[ "$(uname -p)" = "i386" ]; then
490		atf_skip "https://bugs.freebsd.org/249055"
491	fi
492
493	if [ "$(atf_config_get ci false)" = "true" ] && \
494		[ "$(uname -p)" = "armv7" ]; then
495		atf_skip "https://bugs.freebsd.org/249229"
496	fi
497
498	zpool=$(get_zpool_name)
499	for bootenv in "default" "target" "1234"; do
500		# mountpoint of the boot environment
501		mountpoint="$(bectl -r ${zpool}/ROOT list -H | grep ${bootenv} | awk '{print $3}')"
502
503		# see if any jail paths match the boot environment mountpoint
504		jailid="$(jls | grep ${mountpoint} | awk '{print $1}')"
505
506		if [ -z "$jailid" ]; then
507		       continue;
508		fi
509		jail -r ${jailid}
510	done;
511
512	bectl_cleanup ${zpool}
513}
514
515atf_test_case bectl_promotion cleanup
516bectl_promotion_head()
517{
518	atf_set "descr" "Check bectl promotion upon activation"
519	atf_set "require.user" root
520}
521bectl_promotion_body()
522{
523	if [ "$(atf_config_get ci false)" = "true" ] && \
524		[ "$(uname -p)" = "i386" ]; then
525		atf_skip "https://bugs.freebsd.org/249055"
526	fi
527
528	if [ "$(atf_config_get ci false)" = "true" ] && \
529		[ "$(uname -p)" = "armv7" ]; then
530		atf_skip "https://bugs.freebsd.org/249229"
531	fi
532
533	cwd=$(realpath .)
534	zpool=$(make_zpool_name)
535	disk=${cwd}/disk.img
536	mount=${cwd}/mnt
537	root=${mount}/root
538
539	bectl_create_deep_setup ${zpool} ${disk} ${mount}
540	atf_check mkdir -p ${root}
541
542	# Sleeps interspersed to workaround some naming quirks; notably,
543	# bectl will append a serial if two snapshots were created within
544	# the same second, but it can only do that for the one root it's
545	# operating on.  It won't check that other roots don't have a snapshot
546	# with the same name, and the promotion will fail.
547	atf_check bectl -r ${zpool}/ROOT rename default A
548	sleep 1
549	atf_check bectl -r ${zpool}/ROOT create -r -e A B
550	sleep 1
551	atf_check bectl -r ${zpool}/ROOT create -r -e B C
552
553	# C should be a clone of B to start with
554	atf_check -o not-inline:"-" zfs list -Hr -o origin ${zpool}/ROOT/C
555
556	# Activating it should then promote it all the way out of clone-hood.
557	# This entails two promotes internally, as the first would promote it to
558	# a snapshot of A before finally promoting it the second time out of
559	# clone status.
560	atf_check -o not-empty bectl -r ${zpool}/ROOT activate C
561	atf_check -o inline:"-\n-\n" zfs list -Hr -o origin ${zpool}/ROOT/C
562}
563bectl_promotion_cleanup()
564{
565	bectl_cleanup $(get_zpool_name)
566}
567
568atf_test_case bectl_destroy_bootonce cleanup
569bectl_destroy_bootonce_head()
570{
571	atf_set "descr" "Check bectl destroy (bootonce)"
572	atf_set "require.user" root
573}
574bectl_destroy_bootonce_body()
575{
576	if [ "$(atf_config_get ci false)" = "true" ] && \
577		[ "$(uname -p)" = "i386" ]; then
578		atf_skip "https://bugs.freebsd.org/249055"
579	fi
580
581	if [ "$(atf_config_get ci false)" = "true" ] && \
582		[ "$(uname -p)" = "armv7" ]; then
583		atf_skip "https://bugs.freebsd.org/249229"
584	fi
585
586	cwd=$(realpath .)
587	zpool=$(make_zpool_name)
588	disk=${cwd}/disk.img
589	mount=${cwd}/mnt
590	root=${mount}/root
591
592	be=default2
593
594	bectl_create_setup ${zpool} ${disk} ${mount}
595	atf_check -s exit:0 -o empty bectl -r ${zpool}/ROOT create -e default ${be}
596
597	# Create boot environment and bootonce activate it
598	atf_check -s exit:0 -o ignore bectl -r ${zpool}/ROOT activate -t ${be}
599	atf_check -s exit:0 -o inline:"zfs:${zpool}/ROOT/${be}:\n" zfsbootcfg -z ${zpool}
600
601	# Destroy it
602	atf_check -s exit:0 -o ignore bectl -r ${zpool}/ROOT destroy ${be}
603
604	# Should be empty
605	atf_check -s exit:0 -o empty zfsbootcfg -z ${zpool}
606}
607bectl_destroy_bootonce_cleanup()
608{
609	bectl_cleanup $(get_zpool_name)
610}
611
612atf_test_case bectl_rename_bootonce cleanup
613bectl_rename_bootonce_head()
614{
615	atf_set "descr" "Check bectl destroy (bootonce)"
616	atf_set "require.user" root
617}
618bectl_rename_bootonce_body()
619{
620	if [ "$(atf_config_get ci false)" = "true" ] && \
621		[ "$(uname -p)" = "i386" ]; then
622		atf_skip "https://bugs.freebsd.org/249055"
623	fi
624
625	if [ "$(atf_config_get ci false)" = "true" ] && \
626		[ "$(uname -p)" = "armv7" ]; then
627		atf_skip "https://bugs.freebsd.org/249229"
628	fi
629
630	cwd=$(realpath .)
631	zpool=$(make_zpool_name)
632	disk=${cwd}/disk.img
633	mount=${cwd}/mnt
634	root=${mount}/root
635
636	be=default2
637
638	bectl_create_setup ${zpool} ${disk} ${mount}
639	atf_check -s exit:0 -o empty bectl -r ${zpool}/ROOT create -e default ${be}
640
641	# Create boot environment and bootonce activate it
642	atf_check -s exit:0 -o ignore bectl -r ${zpool}/ROOT activate -t ${be}
643	atf_check -s exit:0 -o inline:"zfs:${zpool}/ROOT/${be}:\n" zfsbootcfg -z ${zpool}
644
645	# Rename it
646	atf_check -s exit:0 -o ignore bectl -r ${zpool}/ROOT rename ${be} ${be}_renamed
647
648	# Should be renamed
649	atf_check -s exit:0 -o inline:"zfs:${zpool}/ROOT/${be}_renamed:\n" zfsbootcfg -z ${zpool}
650}
651bectl_rename_bootonce_cleanup()
652{
653	bectl_cleanup $(get_zpool_name)
654}
655
656atf_init_test_cases()
657{
658	atf_add_test_case bectl_create
659	atf_add_test_case bectl_destroy
660	atf_add_test_case bectl_export_import
661	atf_add_test_case bectl_list
662	atf_add_test_case bectl_mount
663	atf_add_test_case bectl_rename
664	atf_add_test_case bectl_jail
665	atf_add_test_case bectl_promotion
666	atf_add_test_case bectl_destroy_bootonce
667	atf_add_test_case bectl_rename_bootonce
668}
669