]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/bectl/tests/bectl_test.sh
MFV r348555: 9690 metaslab of vdev with no space maps was flushed during removal
[FreeBSD/FreeBSD.git] / sbin / bectl / tests / bectl_test.sh
1 #
2 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 # $FreeBSD$
28
29 ZPOOL_NAME_FILE=zpool_name
30 get_zpool_name()
31 {
32         cat $ZPOOL_NAME_FILE
33 }
34 make_zpool_name()
35 {
36         mktemp -u bectl_test_XXXXXX > $ZPOOL_NAME_FILE
37         get_zpool_name
38 }
39
40 # Establishes a bectl_create zpool that can be used for some light testing; contains
41 # a 'default' BE and not much else.
42 bectl_create_setup()
43 {
44         zpool=$1
45         disk=$2
46         mnt=$3
47
48         # Sanity check to make sure `make_zpool_name` succeeded
49         atf_check test -n "$zpool"
50
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 \
57             ${zpool}/ROOT/default
58 }
59 bectl_create_deep_setup()
60 {
61         zpool=$1
62         disk=$2
63         mnt=$3
64
65         # Sanity check to make sure `make_zpool_name` succeeded
66         atf_check test -n "$zpool"
67
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
75 }
76
77 bectl_cleanup()
78 {
79         zpool=$1
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}
84         fi
85 }
86
87 atf_test_case bectl_create cleanup
88 bectl_create_head()
89 {
90
91         atf_set "descr" "Check the various forms of bectl create"
92         atf_set "require.user" root
93 }
94 bectl_create_body()
95 {
96         cwd=$(realpath .)
97         zpool=$(make_zpool_name)
98         disk=${cwd}/disk.img
99         mount=${cwd}/mnt
100
101         bectl_create_setup ${zpool} ${disk} ${mount}
102         # Test standard creation, creation of a snapshot, and creation from a
103         # snapshot.
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
107 }
108 bectl_create_cleanup()
109 {
110         bectl_cleanup $(get_zpool_name)
111 }
112
113 atf_test_case bectl_destroy cleanup
114 bectl_destroy_head()
115 {
116
117         atf_set "descr" "Check bectl destroy"
118         atf_set "require.user" root
119 }
120 bectl_destroy_body()
121 {
122         cwd=$(realpath .)
123         zpool=$(make_zpool_name)
124         disk=${cwd}/disk.img
125         mount=${cwd}/mnt
126         root=${mount}/root
127
128         bectl_create_setup ${zpool} ${disk} ${mount}
129         atf_check bectl -r ${zpool}/ROOT create -e default default2
130         atf_check -o not-empty zfs get mountpoint ${zpool}/ROOT/default2
131         atf_check -e ignore bectl -r ${zpool}/ROOT destroy default2
132         atf_check -e not-empty -s not-exit:0 zfs get mountpoint ${zpool}/ROOT/default2
133
134         # Test origin snapshot deletion when the snapshot to be destroyed
135         # belongs to a mounted dataset, see PR 236043.
136         atf_check mkdir -p ${root}
137         atf_check -o not-empty bectl -r ${zpool}/ROOT mount default ${root}
138         atf_check bectl -r ${zpool}/ROOT create -e default default3
139         atf_check bectl -r ${zpool}/ROOT destroy -o default3
140         atf_check bectl -r ${zpool}/ROOT unmount default
141 }
142 bectl_destroy_cleanup()
143 {
144
145         bectl_cleanup $(get_zpool_name)
146 }
147
148 atf_test_case bectl_export_import cleanup
149 bectl_export_import_head()
150 {
151
152         atf_set "descr" "Check bectl export and import"
153         atf_set "require.user" root
154 }
155 bectl_export_import_body()
156 {
157         cwd=$(realpath .)
158         zpool=$(make_zpool_name)
159         disk=${cwd}/disk.img
160         mount=${cwd}/mnt
161
162         bectl_create_setup ${zpool} ${disk} ${mount}
163         atf_check -o save:exported bectl -r ${zpool}/ROOT export default
164         atf_check -x "bectl -r ${zpool}/ROOT import default2 < exported"
165         atf_check -o not-empty zfs get mountpoint ${zpool}/ROOT/default2
166         atf_check -e ignore bectl -r ${zpool}/ROOT destroy default2
167         atf_check -e not-empty -s not-exit:0 zfs get mountpoint \
168             ${zpool}/ROOT/default2
169 }
170 bectl_export_import_cleanup()
171 {
172
173         bectl_cleanup $(get_zpool_name)
174 }
175
176 atf_test_case bectl_list cleanup
177 bectl_list_head()
178 {
179
180         atf_set "descr" "Check bectl list"
181         atf_set "require.user" root
182 }
183 bectl_list_body()
184 {
185         cwd=$(realpath .)
186         zpool=$(make_zpool_name)
187         disk=${cwd}/disk.img
188         mount=${cwd}/mnt
189
190         bectl_create_setup ${zpool} ${disk} ${mount}
191         # Test the list functionality, including that BEs come and go away
192         # as they're created and destroyed.  Creation and destruction tests
193         # use the 'zfs' utility to verify that they're actually created, so
194         # these are just light tests that 'list' is picking them up.
195         atf_check -o save:list.out bectl -r ${zpool}/ROOT list
196         atf_check -o not-empty grep 'default' list.out
197         atf_check bectl -r ${zpool}/ROOT create -e default default2
198         atf_check -o save:list.out bectl -r ${zpool}/ROOT list
199         atf_check -o not-empty grep 'default2' list.out
200         atf_check -e ignore bectl -r ${zpool}/ROOT destroy default2
201         atf_check -o save:list.out bectl -r ${zpool}/ROOT list
202         atf_check -s not-exit:0 grep 'default2' list.out
203         # XXX TODO: Formatting checks
204 }
205 bectl_list_cleanup()
206 {
207
208         bectl_cleanup $(get_zpool_name)
209 }
210
211 atf_test_case bectl_mount cleanup
212 bectl_mount_head()
213 {
214
215         atf_set "descr" "Check bectl mount/unmount"
216         atf_set "require.user" root
217 }
218 bectl_mount_body()
219 {
220         cwd=$(realpath .)
221         zpool=$(make_zpool_name)
222         disk=${cwd}/disk.img
223         mount=${cwd}/mnt
224         root=${mount}/root
225
226         bectl_create_deep_setup ${zpool} ${disk} ${mount}
227         atf_check mkdir -p ${root}
228         # Test unmount first...
229         atf_check -o not-empty bectl -r ${zpool}/ROOT mount default ${root}
230         atf_check -o not-empty -x "mount | grep '^${zpool}/ROOT/default'"
231         atf_check bectl -r ${zpool}/ROOT unmount default
232         atf_check -s not-exit:0 -x "mount | grep '^${zpool}/ROOT/default'"
233         # Then umount!
234         atf_check -o not-empty bectl -r ${zpool}/ROOT mount default ${root}
235         atf_check -o not-empty -x "mount | grep '^${zpool}/ROOT/default'"
236         atf_check bectl -r ${zpool}/ROOT umount default
237         atf_check -s not-exit:0 -x "mount | grep '^${zpool}/ROOT/default'"
238 }
239 bectl_mount_cleanup()
240 {
241
242         bectl_cleanup $(get_zpool_name)
243 }
244
245 atf_test_case bectl_rename cleanup
246 bectl_rename_head()
247 {
248
249         atf_set "descr" "Check bectl rename"
250         atf_set "require.user" root
251 }
252 bectl_rename_body()
253 {
254         cwd=$(realpath .)
255         zpool=$(make_zpool_name)
256         disk=${cwd}/disk.img
257         mount=${cwd}/mnt
258
259         bectl_create_setup ${zpool} ${disk} ${mount}
260         atf_check bectl -r ${zpool}/ROOT rename default default2
261         atf_check -o not-empty zfs get mountpoint ${zpool}/ROOT/default2
262         atf_check -e not-empty -s not-exit:0 zfs get mountpoint \
263             ${zpool}/ROOT/default
264 }
265 bectl_rename_cleanup()
266 {
267
268         bectl_cleanup $(get_zpool_name)
269 }
270
271 atf_test_case bectl_jail cleanup
272 bectl_jail_head()
273 {
274
275         atf_set "descr" "Check bectl rename"
276         atf_set "require.user" root
277 }
278 bectl_jail_body()
279 {
280         cwd=$(realpath .)
281         zpool=$(make_zpool_name)
282         disk=${cwd}/disk.img
283         mount=${cwd}/mnt
284         root=${mount}/root
285
286         if [ ! -f /rescue/rescue ]; then
287                 atf_skip "This test requires a rescue binary"
288         fi
289         bectl_create_deep_setup ${zpool} ${disk} ${mount}
290         # Prepare our minimal BE... plop a rescue binary into it
291         atf_check mkdir -p ${root}
292         atf_check -o ignore bectl -r ${zpool}/ROOT mount default ${root}
293         atf_check mkdir -p ${root}/rescue
294         atf_check cp /rescue/rescue ${root}/rescue/rescue
295         atf_check bectl -r ${zpool}/ROOT umount default
296
297         # Prepare some more boot environments
298         atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT create -e default target
299         atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT create -e default 1234
300
301         # Attempt to unjail a BE with numeric name; jail_getid at one point
302         # did not validate that the input was a valid jid before returning the
303         # jid.
304         atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b 1234
305         atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail 1234
306
307         # When a jail name is not explicit, it should match the jail id.
308         atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b -o jid=233637 default
309         atf_check -o inline:"233637\n" -s exit:0 -x "jls -j 233637 name"
310         atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail default
311
312         # Basic command-mode tests, with and without jail cleanup
313         atf_check -o inline:"rescue\nusr\n" bectl -r ${zpool}/ROOT \
314             jail default /rescue/rescue ls -1
315         atf_check -o inline:"rescue\nusr\n" bectl -r ${zpool}/ROOT \
316             jail -Uo path=${root} default /rescue/rescue ls -1
317         atf_check [ -f ${root}/rescue/rescue ]
318         atf_check bectl -r ${zpool}/ROOT ujail default
319
320         # Batch mode tests
321         atf_check bectl -r ${zpool}/ROOT jail -bo path=${root} default
322         atf_check -o not-empty -x "jls | grep -F \"${root}\""
323         atf_check bectl -r ${zpool}/ROOT ujail default
324         atf_check -s not-exit:0 -x "jls | grep -F \"${root}\""
325         # 'unjail' naming
326         atf_check bectl -r ${zpool}/ROOT jail -b default
327         atf_check bectl -r ${zpool}/ROOT unjail default
328         atf_check -s not-exit:0 -x "jls | grep -F \"${root}\""
329         # 'unjail' by BE name. Force bectl to lookup jail id by the BE name.
330         atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b default
331         atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT jail -b -o name=bectl_test target
332         atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail target
333         atf_check -o empty -s exit:0 bectl -r ${zpool}/ROOT unjail default
334         # cannot unjail an unjailed BE (by either command name)
335         atf_check -e ignore -s not-exit:0 bectl -r ${zpool}/ROOT ujail default
336         atf_check -e ignore -s not-exit:0 bectl -r ${zpool}/ROOT unjail default
337
338         # set+unset
339         atf_check bectl -r ${zpool}/ROOT jail -b -o path=${root} -u path default
340         # Ensure that it didn't mount at ${root}
341         atf_check -s not-exit:0 -x "mount | grep -F '${root}'"
342         atf_check bectl -r ${zpool}/ROOT ujail default
343 }
344
345 # If a test has failed, it's possible that the boot environment hasn't
346 # been 'unjail'ed. We want to remove the jail before 'bectl_cleanup'
347 # attempts to destroy the zpool.
348 bectl_jail_cleanup()
349 {
350         zpool=$(get_zpool_name)
351         for bootenv in "default" "target" "1234"; do
352                 # mountpoint of the boot environment
353                 mountpoint="$(bectl -r ${zpool}/ROOT list -H | grep ${bootenv} | awk '{print $3}')"
354
355                 # see if any jail paths match the boot environment mountpoint
356                 jailid="$(jls | grep ${mountpoint} | awk '{print $1}')"
357
358                 if [ -z "$jailid" ]; then
359                        continue;
360                 fi
361                 jail -r ${jailid}
362         done;
363
364         bectl_cleanup ${zpool}
365 }
366
367 atf_init_test_cases()
368 {
369         atf_add_test_case bectl_create
370         atf_add_test_case bectl_destroy
371         atf_add_test_case bectl_export_import
372         atf_add_test_case bectl_list
373         atf_add_test_case bectl_mount
374         atf_add_test_case bectl_rename
375         atf_add_test_case bectl_jail
376 }