2 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 # Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
29 ZPOOL_NAME_FILE=zpool_name
36 mktemp -u bectl_test_XXXXXX > $ZPOOL_NAME_FILE
40 # Establishes a bectl_create zpool that can be used for some light testing; contains
41 # a 'default' BE and not much else.
48 # Sanity check to make sure `make_zpool_name` succeeded
49 atf_check test -n "$zpool"
51 kldload -n -q zfs || atf_skip "ZFS module not loaded on the current system"
52 atf_check mkdir -p ${mnt}
53 atf_check truncate -s 1G ${disk}
54 atf_check zpool create -o altroot=${mnt} ${zpool} ${disk}
55 atf_check zfs create -o mountpoint=none ${zpool}/ROOT
56 atf_check zfs create -o mountpoint=/ -o canmount=noauto \
59 bectl_create_deep_setup()
65 # Sanity check to make sure `make_zpool_name` succeeded
66 atf_check test -n "$zpool"
68 bectl_create_setup ${zpool} ${disk} ${mnt}
69 atf_check mkdir -p ${root}
70 atf_check -o ignore bectl -r ${zpool}/ROOT mount default ${root}
71 atf_check mkdir -p ${root}/usr
72 atf_check zfs create -o mountpoint=/usr -o canmount=noauto \
73 ${zpool}/ROOT/default/usr
74 atf_check -o ignore bectl -r ${zpool}/ROOT umount default
80 if [ -z "$zpool" ]; then
81 echo "Skipping cleanup; zpool not set up"
82 elif zpool get health ${zpool} >/dev/null 2>&1; then
83 zpool destroy -f ${zpool}
87 atf_test_case bectl_create cleanup
91 atf_set "descr" "Check the various forms of bectl create"
92 atf_set "require.user" root
97 zpool=$(make_zpool_name)
101 bectl_create_setup ${zpool} ${disk} ${mount}
102 # Test standard creation, creation of a snapshot, and creation from a
104 atf_check bectl -r ${zpool}/ROOT create -e default default2
105 atf_check bectl -r ${zpool}/ROOT create default2@test_snap
106 atf_check bectl -r ${zpool}/ROOT create -e default2@test_snap default3
108 bectl_create_cleanup()
110 bectl_cleanup $(get_zpool_name)
113 atf_test_case bectl_destroy cleanup
117 atf_set "descr" "Check bectl destroy"
118 atf_set "require.user" root
123 zpool=$(make_zpool_name)
127 bectl_create_setup ${zpool} ${disk} ${mount}
128 atf_check bectl -r ${zpool}/ROOT create -e default default2
129 atf_check -o not-empty zfs get mountpoint ${zpool}/ROOT/default2
130 atf_check -e ignore bectl -r ${zpool}/ROOT destroy default2
131 atf_check -e not-empty -s not-exit:0 zfs get mountpoint ${zpool}/ROOT/default2
133 bectl_destroy_cleanup()
136 bectl_cleanup $(get_zpool_name)
139 atf_test_case bectl_export_import cleanup
140 bectl_export_import_head()
143 atf_set "descr" "Check bectl export and import"
144 atf_set "require.user" root
146 bectl_export_import_body()
149 zpool=$(make_zpool_name)
153 bectl_create_setup ${zpool} ${disk} ${mount}
154 atf_check -o save:exported bectl -r ${zpool}/ROOT export default
155 atf_check -x "bectl -r ${zpool}/ROOT import default2 < exported"
156 atf_check -o not-empty zfs get mountpoint ${zpool}/ROOT/default2
157 atf_check -e ignore bectl -r ${zpool}/ROOT destroy default2
158 atf_check -e not-empty -s not-exit:0 zfs get mountpoint \
159 ${zpool}/ROOT/default2
161 bectl_export_import_cleanup()
164 bectl_cleanup $(get_zpool_name)
167 atf_test_case bectl_list cleanup
171 atf_set "descr" "Check bectl list"
172 atf_set "require.user" root
177 zpool=$(make_zpool_name)
181 bectl_create_setup ${zpool} ${disk} ${mount}
182 # Test the list functionality, including that BEs come and go away
183 # as they're created and destroyed. Creation and destruction tests
184 # use the 'zfs' utility to verify that they're actually created, so
185 # these are just light tests that 'list' is picking them up.
186 atf_check -o save:list.out bectl -r ${zpool}/ROOT list
187 atf_check -o not-empty grep 'default' list.out
188 atf_check bectl -r ${zpool}/ROOT create -e default default2
189 atf_check -o save:list.out bectl -r ${zpool}/ROOT list
190 atf_check -o not-empty grep 'default2' list.out
191 atf_check -e ignore bectl -r ${zpool}/ROOT destroy default2
192 atf_check -o save:list.out bectl -r ${zpool}/ROOT list
193 atf_check -s not-exit:0 grep 'default2' list.out
194 # XXX TODO: Formatting checks
199 bectl_cleanup $(get_zpool_name)
202 atf_test_case bectl_mount cleanup
206 atf_set "descr" "Check bectl mount/unmount"
207 atf_set "require.user" root
212 zpool=$(make_zpool_name)
217 bectl_create_deep_setup ${zpool} ${disk} ${mount}
218 atf_check mkdir -p ${root}
219 # Test unmount first...
220 atf_check -o not-empty bectl -r ${zpool}/ROOT mount default ${root}
221 atf_check -o not-empty -x "mount | grep '^${zpool}/ROOT/default'"
222 atf_check bectl -r ${zpool}/ROOT unmount default
223 atf_check -s not-exit:0 -x "mount | grep '^${zpool}/ROOT/default'"
225 atf_check -o not-empty bectl -r ${zpool}/ROOT mount default ${root}
226 atf_check -o not-empty -x "mount | grep '^${zpool}/ROOT/default'"
227 atf_check bectl -r ${zpool}/ROOT umount default
228 atf_check -s not-exit:0 -x "mount | grep '^${zpool}/ROOT/default'"
230 bectl_mount_cleanup()
233 bectl_cleanup $(get_zpool_name)
236 atf_test_case bectl_rename cleanup
240 atf_set "descr" "Check bectl rename"
241 atf_set "require.user" root
246 zpool=$(make_zpool_name)
250 bectl_create_setup ${zpool} ${disk} ${mount}
251 atf_check bectl -r ${zpool}/ROOT rename default default2
252 atf_check -o not-empty zfs get mountpoint ${zpool}/ROOT/default2
253 atf_check -e not-empty -s not-exit:0 zfs get mountpoint \
254 ${zpool}/ROOT/default
256 bectl_rename_cleanup()
259 bectl_cleanup $(get_zpool_name)
262 atf_test_case bectl_jail cleanup
266 atf_set "descr" "Check bectl rename"
267 atf_set "require.user" root
272 zpool=$(make_zpool_name)
277 if [ ! -f /rescue/rescue ]; then
278 atf_skip "This test requires a rescue binary"
280 bectl_create_deep_setup ${zpool} ${disk} ${mount}
281 # Prepare our minimal BE... plop a rescue binary into it
282 atf_check mkdir -p ${root}
283 atf_check -o ignore bectl -r ${zpool}/ROOT mount default ${root}
284 atf_check mkdir -p ${root}/rescue
285 atf_check cp /rescue/rescue ${root}/rescue/rescue
286 atf_check bectl -r ${zpool}/ROOT umount default
288 # Prepare a second boot environment
289 atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT create -e default target
291 # When a jail name is not explicit, it should match the jail id.
292 atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b -o jid=233637 default
293 atf_check -o inline:"233637\n" -s exit:0 -x "jls -j 233637 name"
294 atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail default
296 # Basic command-mode tests, with and without jail cleanup
297 atf_check -o inline:"rescue\nusr\n" bectl -r ${zpool}/ROOT \
298 jail default /rescue/rescue ls -1
299 atf_check -o inline:"rescue\nusr\n" bectl -r ${zpool}/ROOT \
300 jail -Uo path=${root} default /rescue/rescue ls -1
301 atf_check [ -f ${root}/rescue/rescue ]
302 atf_check bectl -r ${zpool}/ROOT ujail default
305 atf_check bectl -r ${zpool}/ROOT jail -bo path=${root} default
306 atf_check -o not-empty -x "jls | grep -F \"${root}\""
307 atf_check bectl -r ${zpool}/ROOT ujail default
308 atf_check -s not-exit:0 -x "jls | grep -F \"${root}\""
310 atf_check bectl -r ${zpool}/ROOT jail -b default
311 atf_check bectl -r ${zpool}/ROOT unjail default
312 atf_check -s not-exit:0 -x "jls | grep -F \"${root}\""
313 # 'unjail' by BE name. Force bectl to lookup jail id by the BE name.
314 atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b default
315 atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b -o name=bectl_test target
316 atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail target
317 atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail default
318 # cannot unjail an unjailed BE (by either command name)
319 atf_check -e ignore -s not-exit:0 bectl -r ${zpool}/ROOT ujail default
320 atf_check -e ignore -s not-exit:0 bectl -r ${zpool}/ROOT unjail default
323 atf_check bectl -r ${zpool}/ROOT jail -b -o path=${root} -u path default
324 # Ensure that it didn't mount at ${root}
325 atf_check -s not-exit:0 -x "mount | grep -F '${root}'"
326 atf_check bectl -r ${zpool}/ROOT ujail default
329 # If a test has failed, it's possible that the boot environment hasn't
330 # been 'unjail'ed. We want to remove the jail before 'bectl_cleanup'
331 # attempts to destroy the zpool.
334 for bootenv in "default" "target"; do
335 # mountpoint of the boot environment
336 mountpoint="$(bectl -r bectl_test/ROOT list -H | grep ${bootenv} | awk '{print $3}')"
338 # see if any jail paths match the boot environment mountpoint
339 jailid="$(jls | grep ${mountpoint} | awk '{print $1}')"
341 if [ -z "$jailid" ]; then
347 bectl_cleanup $(get_zpool_name)
350 atf_init_test_cases()
352 atf_add_test_case bectl_create
353 atf_add_test_case bectl_destroy
354 atf_add_test_case bectl_export_import
355 atf_add_test_case bectl_list
356 atf_add_test_case bectl_mount
357 atf_add_test_case bectl_rename
358 atf_add_test_case bectl_jail