xref: /freebsd/sbin/mdconfig/tests/mdconfig_test.sh (revision 4b72bab96e8978eaed30fd44f7f51e1b4918d4db)
166ca0717SEnji Cooper# Copyright (c) 2012 Edward Tomasz Napierała <trasz@FreeBSD.org>
266ca0717SEnji Cooper#
366ca0717SEnji Cooper# Redistribution and use in source and binary forms, with or without
466ca0717SEnji Cooper# modification, are permitted provided that the following conditions
566ca0717SEnji Cooper# are met:
666ca0717SEnji Cooper# 1. Redistributions of source code must retain the above copyright
766ca0717SEnji Cooper#    notice, this list of conditions and the following disclaimer.
866ca0717SEnji Cooper# 2. Redistributions in binary form must reproduce the above copyright
966ca0717SEnji Cooper#    notice, this list of conditions and the following disclaimer in the
1066ca0717SEnji Cooper#    documentation and/or other materials provided with the distribution.
1166ca0717SEnji Cooper#
1266ca0717SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1366ca0717SEnji Cooper# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1466ca0717SEnji Cooper# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1566ca0717SEnji Cooper# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1666ca0717SEnji Cooper# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1766ca0717SEnji Cooper# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1866ca0717SEnji Cooper# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1966ca0717SEnji Cooper# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2066ca0717SEnji Cooper# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2166ca0717SEnji Cooper# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2266ca0717SEnji Cooper# SUCH DAMAGE.
2366ca0717SEnji Cooper#
2466ca0717SEnji Cooper#
2566ca0717SEnji Cooper
2666ca0717SEnji Coopercheck_diskinfo()
2766ca0717SEnji Cooper{
2866ca0717SEnji Cooper	local md=$1
2966ca0717SEnji Cooper	local mediasize_in_bytes=$2
3066ca0717SEnji Cooper	local mediasize_in_sectors=$3
3166ca0717SEnji Cooper	local sectorsize=${4:-512}
3266ca0717SEnji Cooper	local stripesize=${5:-0}
3366ca0717SEnji Cooper	local stripeoffset=${6:-0}
3466ca0717SEnji Cooper
3566ca0717SEnji Cooper	atf_check -s exit:0 \
3666ca0717SEnji Cooper	    -o match:"/dev/$md *$sectorsize *$mediasize_in_bytes *$mediasize_in_sectors *$stripesize *$stripeoffset" \
3766ca0717SEnji Cooper	    -x "diskinfo /dev/$md | expand"
3866ca0717SEnji Cooper}
3966ca0717SEnji Cooper
4066ca0717SEnji Coopercleanup_common()
4166ca0717SEnji Cooper{
4266ca0717SEnji Cooper	if [ -f mdconfig.out ]; then
4366ca0717SEnji Cooper		mdconfig -d -u $(sed -e 's/md//' mdconfig.out)
4466ca0717SEnji Cooper	fi
4566ca0717SEnji Cooper}
4666ca0717SEnji Cooper
4766ca0717SEnji Cooperatf_test_case attach_vnode_non_explicit_type cleanup
4866ca0717SEnji Cooperattach_vnode_non_explicit_type_head()
4966ca0717SEnji Cooper{
5066ca0717SEnji Cooper	atf_set "descr" "Tests out -a / -f without -t"
5166ca0717SEnji Cooper}
5266ca0717SEnji Cooperattach_vnode_non_explicit_type_body()
5366ca0717SEnji Cooper{
5466ca0717SEnji Cooper	local md
5566ca0717SEnji Cooper	local size_in_mb=1024
5666ca0717SEnji Cooper
5766ca0717SEnji Cooper	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
5866ca0717SEnji Cooper	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig -af xxx'
5966ca0717SEnji Cooper	md=$(cat mdconfig.out)
6066ca0717SEnji Cooper	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
6166ca0717SEnji Cooper	check_diskinfo "$md" "1073741824" "2097152"
6266ca0717SEnji Cooper	# This awk strips the file path.
6366ca0717SEnji Cooper	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
6466ca0717SEnji Cooper	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
6566ca0717SEnji Cooper}
6666ca0717SEnji Cooperattach_vnode_non_explicit_type_cleanup()
6766ca0717SEnji Cooper{
6866ca0717SEnji Cooper	cleanup_common
6966ca0717SEnji Cooper}
7066ca0717SEnji Cooper
7166ca0717SEnji Cooperatf_test_case attach_vnode_implicit_a_f cleanup
7266ca0717SEnji Cooperattach_vnode_implicit_a_f_head()
7366ca0717SEnji Cooper{
7466ca0717SEnji Cooper	atf_set "descr" "Tests out implied -a / -f without -t"
7566ca0717SEnji Cooper}
7666ca0717SEnji Cooperattach_vnode_implicit_a_f_body()
7766ca0717SEnji Cooper{
7866ca0717SEnji Cooper	local md
7966ca0717SEnji Cooper	local size_in_mb=1024
8066ca0717SEnji Cooper
8166ca0717SEnji Cooper	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
8266ca0717SEnji Cooper	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig xxx'
8366ca0717SEnji Cooper	md=$(cat mdconfig.out)
8466ca0717SEnji Cooper	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
8566ca0717SEnji Cooper	check_diskinfo "$md" "1073741824" "2097152"
8666ca0717SEnji Cooper	# This awk strips the file path.
8766ca0717SEnji Cooper	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
8866ca0717SEnji Cooper	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
8966ca0717SEnji Cooper}
9066ca0717SEnji Cooperattach_vnode_implicit_a_f_cleanup()
9166ca0717SEnji Cooper{
9266ca0717SEnji Cooper	cleanup_common
9366ca0717SEnji Cooper}
9466ca0717SEnji Cooper
9566ca0717SEnji Cooperatf_test_case attach_vnode_explicit_type cleanup
9666ca0717SEnji Cooperattach_vnode_explicit_type_head()
9766ca0717SEnji Cooper{
9866ca0717SEnji Cooper	atf_set "descr" "Tests out implied -a / -f with -t vnode"
9966ca0717SEnji Cooper}
10066ca0717SEnji Cooperattach_vnode_explicit_type_body()
10166ca0717SEnji Cooper{
10266ca0717SEnji Cooper	local md
10366ca0717SEnji Cooper	local size_in_mb=1024
10466ca0717SEnji Cooper
10566ca0717SEnji Cooper	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
10666ca0717SEnji Cooper	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig -af xxx -t vnode'
10766ca0717SEnji Cooper	md=$(cat mdconfig.out)
10866ca0717SEnji Cooper	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
10966ca0717SEnji Cooper	check_diskinfo "$md" "1073741824" "2097152"
11066ca0717SEnji Cooper	# This awk strips the file path.
11166ca0717SEnji Cooper	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
11266ca0717SEnji Cooper	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
11366ca0717SEnji Cooper}
11466ca0717SEnji Cooperattach_vnode_explicit_type_cleanup()
11566ca0717SEnji Cooper{
11666ca0717SEnji Cooper	[ -f mdconfig.out ] && mdconfig -d -u $(sed -e 's/md//' mdconfig.out)
11766ca0717SEnji Cooper	rm -f mdconfig.out xxx
11866ca0717SEnji Cooper}
11966ca0717SEnji Cooper
12066ca0717SEnji Cooperatf_test_case attach_vnode_smaller_than_file cleanup
12166ca0717SEnji Cooperattach_vnode_smaller_than_file_head()
12266ca0717SEnji Cooper{
12366ca0717SEnji Cooper	atf_set "descr" "Tests mdconfig -s with size less than the file size"
12466ca0717SEnji Cooper}
12566ca0717SEnji Cooperattach_vnode_smaller_than_file_body()
12666ca0717SEnji Cooper{
12766ca0717SEnji Cooper	local md
12866ca0717SEnji Cooper	local size_in_mb=128
12966ca0717SEnji Cooper
13066ca0717SEnji Cooper	atf_check -s exit:0 -x "truncate -s 1024m xxx"
13166ca0717SEnji Cooper	atf_check -s exit:0 -o save:mdconfig.out \
13266ca0717SEnji Cooper	    -x "mdconfig -af xxx -s ${size_in_mb}m"
13366ca0717SEnji Cooper	md=$(cat mdconfig.out)
13466ca0717SEnji Cooper	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
13566ca0717SEnji Cooper	check_diskinfo "$md" "134217728" "262144"
13666ca0717SEnji Cooper	# This awk strips the file path.
13766ca0717SEnji Cooper	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
13866ca0717SEnji Cooper	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
13966ca0717SEnji Cooper}
14066ca0717SEnji Cooperattach_vnode_smaller_than_file_cleanup()
14166ca0717SEnji Cooper{
14266ca0717SEnji Cooper	cleanup_common
14366ca0717SEnji Cooper}
14466ca0717SEnji Cooper
14566ca0717SEnji Cooperatf_test_case attach_vnode_larger_than_file cleanup
14666ca0717SEnji Cooperattach_vnode_larger_than_file_head()
14766ca0717SEnji Cooper{
14866ca0717SEnji Cooper	atf_set "descr" "Tests mdconfig -s with size greater than the file size"
14966ca0717SEnji Cooper}
15066ca0717SEnji Cooperattach_vnode_larger_than_file_body()
15166ca0717SEnji Cooper{
15266ca0717SEnji Cooper	local md
15366ca0717SEnji Cooper	local size_in_gb=128
15466ca0717SEnji Cooper
15566ca0717SEnji Cooper	atf_check -s exit:0 -x "truncate -s 1024m xxx"
15666ca0717SEnji Cooper	atf_check -s exit:0 -o save:mdconfig.out \
15766ca0717SEnji Cooper	    -x "mdconfig -af xxx -s ${size_in_gb}g"
15866ca0717SEnji Cooper	md=$(cat mdconfig.out)
15966ca0717SEnji Cooper	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
16066ca0717SEnji Cooper	check_diskinfo "$md" "137438953472" "268435456"
16166ca0717SEnji Cooper	# This awk strips the file path.
16266ca0717SEnji Cooper	atf_check -s exit:0 -o match:"^$md vnode ${size_in_gb}G$" \
16366ca0717SEnji Cooper	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
16466ca0717SEnji Cooper}
16566ca0717SEnji Cooperattach_vnode_larger_than_file_cleanup()
16666ca0717SEnji Cooper{
16766ca0717SEnji Cooper	cleanup_common
16866ca0717SEnji Cooper}
16966ca0717SEnji Cooper
17066ca0717SEnji Cooperatf_test_case attach_vnode_sector_size cleanup
17166ca0717SEnji Cooperattach_vnode_sector_size_head()
17266ca0717SEnji Cooper{
1734420bb6aSMark Johnston	atf_set "descr" "Tests mdconfig -S with a backing file"
17466ca0717SEnji Cooper}
17566ca0717SEnji Cooperattach_vnode_sector_size_body()
17666ca0717SEnji Cooper{
17766ca0717SEnji Cooper	local md
17866ca0717SEnji Cooper	local size_in_mb=1024
17966ca0717SEnji Cooper
18066ca0717SEnji Cooper	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
18166ca0717SEnji Cooper	atf_check -s exit:0 -o save:mdconfig.out \
18266ca0717SEnji Cooper	    -x "mdconfig -af xxx -S 2048"
18366ca0717SEnji Cooper	md=$(cat mdconfig.out)
18466ca0717SEnji Cooper	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
18566ca0717SEnji Cooper	check_diskinfo "$md" "1073741824" "524288" "2048"
18666ca0717SEnji Cooper	# This awk strips the file path.
18766ca0717SEnji Cooper	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
18866ca0717SEnji Cooper	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
18966ca0717SEnji Cooper}
19066ca0717SEnji Cooperattach_vnode_sector_size_cleanup()
19166ca0717SEnji Cooper{
19266ca0717SEnji Cooper	cleanup_common
19366ca0717SEnji Cooper}
19466ca0717SEnji Cooper
19566ca0717SEnji Cooperatf_test_case attach_malloc cleanup
19666ca0717SEnji Cooperattach_malloc_head()
19766ca0717SEnji Cooper{
19866ca0717SEnji Cooper	atf_set "descr" "Tests mdconfig with -t malloc"
19966ca0717SEnji Cooper}
20066ca0717SEnji Cooperattach_malloc_body()
20166ca0717SEnji Cooper{
20266ca0717SEnji Cooper	local md
20366ca0717SEnji Cooper	local size_in_mb=1024
20466ca0717SEnji Cooper
20566ca0717SEnji Cooper	atf_check -s exit:0 -o save:mdconfig.out \
20666ca0717SEnji Cooper	    -x 'mdconfig -a -t malloc -s 1g'
20766ca0717SEnji Cooper	md=$(cat mdconfig.out)
20866ca0717SEnji Cooper	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
20966ca0717SEnji Cooper	check_diskinfo "$md" "1073741824" "2097152"
21066ca0717SEnji Cooper	# This awk strips the file path.
21166ca0717SEnji Cooper	atf_check -s exit:0 -o match:"^$md malloc ${size_in_mb}M$" \
21266ca0717SEnji Cooper	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
21366ca0717SEnji Cooper}
21466ca0717SEnji Cooperattach_malloc_cleanup()
21566ca0717SEnji Cooper{
21666ca0717SEnji Cooper	cleanup_common
21766ca0717SEnji Cooper}
21866ca0717SEnji Cooper
21966ca0717SEnji Cooperatf_test_case attach_swap cleanup
22066ca0717SEnji Cooperattach_swap_head()
22166ca0717SEnji Cooper{
22266ca0717SEnji Cooper	atf_set "descr" "Tests mdconfig with -t swap"
22366ca0717SEnji Cooper}
22466ca0717SEnji Cooperattach_swap_body()
22566ca0717SEnji Cooper{
22666ca0717SEnji Cooper	local md
22766ca0717SEnji Cooper	local size_in_mb=1024
22866ca0717SEnji Cooper
22966ca0717SEnji Cooper	atf_check -s exit:0 -o save:mdconfig.out \
23066ca0717SEnji Cooper	    -x 'mdconfig -a -t swap -s 1g'
23166ca0717SEnji Cooper	md=$(cat mdconfig.out)
23266ca0717SEnji Cooper	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
23366ca0717SEnji Cooper	check_diskinfo "$md" "1073741824" "2097152"
23466ca0717SEnji Cooper	# This awk strips the file path.
23566ca0717SEnji Cooper	atf_check -s exit:0 -o match:"^$md swap ${size_in_mb}M$" \
23666ca0717SEnji Cooper	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
23766ca0717SEnji Cooper}
23866ca0717SEnji Cooperattach_swap_cleanup()
23966ca0717SEnji Cooper{
24066ca0717SEnji Cooper	cleanup_common
24166ca0717SEnji Cooper}
24266ca0717SEnji Cooper
24366ca0717SEnji Cooperatf_test_case attach_with_specific_unit_number cleanup
24466ca0717SEnji Cooperattach_with_specific_unit_number_head()
24566ca0717SEnji Cooper{
24666ca0717SEnji Cooper	atf_set "descr" "Tests mdconfig with a unit specified by -u"
24766ca0717SEnji Cooper}
24866ca0717SEnji Cooperattach_with_specific_unit_number_body()
24966ca0717SEnji Cooper{
25066ca0717SEnji Cooper	local md_unit=99
25166ca0717SEnji Cooper	local size_in_mb=10
25266ca0717SEnji Cooper
25366ca0717SEnji Cooper	local md="md${md_unit}"
25466ca0717SEnji Cooper
25566ca0717SEnji Cooper	echo "$md" > mdconfig.out
25666ca0717SEnji Cooper
25766ca0717SEnji Cooper	atf_check -s exit:0 -o empty \
25866ca0717SEnji Cooper	    -x "mdconfig -a -t malloc -s ${size_in_mb}m -u $md_unit"
25966ca0717SEnji Cooper	check_diskinfo "$md" "10485760" "20480"
26066ca0717SEnji Cooper	# This awk strips the file path.
26166ca0717SEnji Cooper	atf_check -s exit:0 -o match:"^$md malloc "$size_in_mb"M$" \
26266ca0717SEnji Cooper	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
26366ca0717SEnji Cooper}
26466ca0717SEnji Cooperattach_with_specific_unit_number_cleanup()
26566ca0717SEnji Cooper{
26666ca0717SEnji Cooper	cleanup_common
26766ca0717SEnji Cooper}
26866ca0717SEnji Cooper
269ed59446bSMark Johnstonatf_test_case attach_size_rounddown cleanup
270ed59446bSMark Johnstonattach_size_rounddown()
271ed59446bSMark Johnston{
272ed59446bSMark Johnston	atf_set "descr" "Verify that md provider sizes are a multiple of the sector size"
273ed59446bSMark Johnston}
274ed59446bSMark Johnstonattach_size_rounddown_body()
275ed59446bSMark Johnston{
276ed59446bSMark Johnston	local md
277ed59446bSMark Johnston	local ss=8192
278ed59446bSMark Johnston	local ms=$(($ss + 4096))
279ed59446bSMark Johnston	local ms2=$((2 * $ss + 4096))
280ed59446bSMark Johnston
281ed59446bSMark Johnston	# Use a sector size that's a likely multiple of PAGE_SIZE, as md(4)
282ed59446bSMark Johnston	# expects that for swap MDs.
283ed59446bSMark Johnston	atf_check -s exit:0 -o save:mdconfig.out -e empty \
284ed59446bSMark Johnston	    -x "mdconfig -a -t swap -S $ss -s ${ms}b"
285ed59446bSMark Johnston	md=$(cat mdconfig.out)
286ed59446bSMark Johnston	# 12288 bytes should be rounded down to one sector.
287ed59446bSMark Johnston	check_diskinfo "$md" 8192 1 $ss
288ed59446bSMark Johnston
289ed59446bSMark Johnston	# Resize and verify that the new size was also rounded down.
290ed59446bSMark Johnston	atf_check -s exit:0 -o empty -e empty \
291ed59446bSMark Johnston	    -x "mdconfig -r -u ${md#md} -s ${ms2}b"
292ed59446bSMark Johnston	check_diskinfo "$md" 16384 2 $ss
293ed59446bSMark Johnston}
294d1bd097dSAlan Somersattach_size_rounddown_cleanup()
295ed59446bSMark Johnston{
296ed59446bSMark Johnston	cleanup_common
297ed59446bSMark Johnston}
298ed59446bSMark Johnston
299*4b72bab9SAlan Somersatf_test_case query_verbose cleanup
300*4b72bab9SAlan Somersquery_verbose()
301*4b72bab9SAlan Somers{
302*4b72bab9SAlan Somers	atf_set "descr" "mdconfig -lv should print device details"
303*4b72bab9SAlan Somers}
304*4b72bab9SAlan Somersquery_verbose_body()
305*4b72bab9SAlan Somers{
306*4b72bab9SAlan Somers	atf_check -s exit:0 -o save:mdconfig.out \
307*4b72bab9SAlan Somers	    -x 'mdconfig -a -t swap -s 1m -o reserve -o force'
308*4b72bab9SAlan Somers	md=$(cat mdconfig.out)
309*4b72bab9SAlan Somers	atf_check -s exit:0 \
310*4b72bab9SAlan Somers		-o match:"$md[[:space:]]+swap[[:space:]]+1024K[[:space:]]+[-][[:space:]]+[-][[:space:]]+force,reserve" \
311*4b72bab9SAlan Somers	-x "mdconfig -lv -u $md"
312*4b72bab9SAlan Somers}
313*4b72bab9SAlan Somersquery_verbose_cleanup()
314*4b72bab9SAlan Somers{
315*4b72bab9SAlan Somers	cleanup_common
316*4b72bab9SAlan Somers}
317*4b72bab9SAlan Somers
31866ca0717SEnji Cooperatf_init_test_cases()
31966ca0717SEnji Cooper{
32066ca0717SEnji Cooper	atf_add_test_case attach_vnode_non_explicit_type
32166ca0717SEnji Cooper	atf_add_test_case attach_vnode_explicit_type
32266ca0717SEnji Cooper	atf_add_test_case attach_vnode_smaller_than_file
32366ca0717SEnji Cooper	atf_add_test_case attach_vnode_larger_than_file
32466ca0717SEnji Cooper	atf_add_test_case attach_vnode_sector_size
32566ca0717SEnji Cooper	atf_add_test_case attach_malloc
32666ca0717SEnji Cooper	atf_add_test_case attach_swap
32766ca0717SEnji Cooper	atf_add_test_case attach_with_specific_unit_number
328ed59446bSMark Johnston	atf_add_test_case attach_size_rounddown
329*4b72bab9SAlan Somers	atf_add_test_case query_verbose
33066ca0717SEnji Cooper}
331