xref: /freebsd/sbin/mdconfig/tests/mdconfig_test.sh (revision ec273ebf3b6aed5fba8c56b6ece5ad8693a48ea7)
1# Copyright (c) 2012 Edward Tomasz Napierała <trasz@FreeBSD.org>
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23# SUCH DAMAGE.
24#
25# $FreeBSD$
26#
27
28check_diskinfo()
29{
30	local md=$1
31	local mediasize_in_bytes=$2
32	local mediasize_in_sectors=$3
33	local sectorsize=${4:-512}
34	local stripesize=${5:-0}
35	local stripeoffset=${6:-0}
36
37	atf_check -s exit:0 \
38	    -o match:"/dev/$md *$sectorsize *$mediasize_in_bytes *$mediasize_in_sectors *$stripesize *$stripeoffset" \
39	    -x "diskinfo /dev/$md | expand"
40}
41
42cleanup_common()
43{
44	if [ -f mdconfig.out ]; then
45		mdconfig -d -u $(sed -e 's/md//' mdconfig.out)
46	fi
47}
48
49atf_test_case attach_vnode_non_explicit_type cleanup
50attach_vnode_non_explicit_type_head()
51{
52	atf_set "descr" "Tests out -a / -f without -t"
53}
54attach_vnode_non_explicit_type_body()
55{
56	local md
57	local size_in_mb=1024
58
59	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
60	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig -af xxx'
61	md=$(cat mdconfig.out)
62	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
63	check_diskinfo "$md" "1073741824" "2097152"
64	# This awk strips the file path.
65	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
66	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
67}
68attach_vnode_non_explicit_type_cleanup()
69{
70	cleanup_common
71}
72
73atf_test_case attach_vnode_implicit_a_f cleanup
74attach_vnode_implicit_a_f_head()
75{
76	atf_set "descr" "Tests out implied -a / -f without -t"
77}
78attach_vnode_implicit_a_f_body()
79{
80	local md
81	local size_in_mb=1024
82
83	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
84	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig xxx'
85	md=$(cat mdconfig.out)
86	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
87	check_diskinfo "$md" "1073741824" "2097152"
88	# This awk strips the file path.
89	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
90	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
91}
92attach_vnode_implicit_a_f_cleanup()
93{
94	cleanup_common
95}
96
97atf_test_case attach_vnode_explicit_type cleanup
98attach_vnode_explicit_type_head()
99{
100	atf_set "descr" "Tests out implied -a / -f with -t vnode"
101}
102attach_vnode_explicit_type_body()
103{
104	local md
105	local size_in_mb=1024
106
107	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
108	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig -af xxx -t vnode'
109	md=$(cat mdconfig.out)
110	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
111	check_diskinfo "$md" "1073741824" "2097152"
112	# This awk strips the file path.
113	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
114	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
115}
116attach_vnode_explicit_type_cleanup()
117{
118	[ -f mdconfig.out ] && mdconfig -d -u $(sed -e 's/md//' mdconfig.out)
119	rm -f mdconfig.out xxx
120}
121
122atf_test_case attach_vnode_smaller_than_file cleanup
123attach_vnode_smaller_than_file_head()
124{
125	atf_set "descr" "Tests mdconfig -s with size less than the file size"
126}
127attach_vnode_smaller_than_file_body()
128{
129	local md
130	local size_in_mb=128
131
132	atf_check -s exit:0 -x "truncate -s 1024m xxx"
133	atf_check -s exit:0 -o save:mdconfig.out \
134	    -x "mdconfig -af xxx -s ${size_in_mb}m"
135	md=$(cat mdconfig.out)
136	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
137	check_diskinfo "$md" "134217728" "262144"
138	# This awk strips the file path.
139	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
140	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
141}
142attach_vnode_smaller_than_file_cleanup()
143{
144	cleanup_common
145}
146
147atf_test_case attach_vnode_larger_than_file cleanup
148attach_vnode_larger_than_file_head()
149{
150	atf_set "descr" "Tests mdconfig -s with size greater than the file size"
151}
152attach_vnode_larger_than_file_body()
153{
154	local md
155	local size_in_gb=128
156
157	atf_check -s exit:0 -x "truncate -s 1024m xxx"
158	atf_check -s exit:0 -o save:mdconfig.out \
159	    -x "mdconfig -af xxx -s ${size_in_gb}g"
160	md=$(cat mdconfig.out)
161	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
162	check_diskinfo "$md" "137438953472" "268435456"
163	# This awk strips the file path.
164	atf_check -s exit:0 -o match:"^$md vnode ${size_in_gb}G$" \
165	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
166}
167attach_vnode_larger_than_file_cleanup()
168{
169	cleanup_common
170}
171
172atf_test_case attach_vnode_sector_size cleanup
173attach_vnode_sector_size_head()
174{
175	atf_set "descr" "Tests mdconfig -s with size greater than the file size"
176}
177attach_vnode_sector_size_body()
178{
179	local md
180	local size_in_mb=1024
181
182	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
183	atf_check -s exit:0 -o save:mdconfig.out \
184	    -x "mdconfig -af xxx -S 2048"
185	md=$(cat mdconfig.out)
186	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
187	check_diskinfo "$md" "1073741824" "524288" "2048"
188	# This awk strips the file path.
189	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
190	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
191}
192attach_vnode_sector_size_cleanup()
193{
194	cleanup_common
195}
196
197atf_test_case attach_malloc cleanup
198attach_malloc_head()
199{
200	atf_set "descr" "Tests mdconfig with -t malloc"
201}
202attach_malloc_body()
203{
204	local md
205	local size_in_mb=1024
206
207	atf_check -s exit:0 -o save:mdconfig.out \
208	    -x 'mdconfig -a -t malloc -s 1g'
209	md=$(cat mdconfig.out)
210	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
211	check_diskinfo "$md" "1073741824" "2097152"
212	# This awk strips the file path.
213	atf_check -s exit:0 -o match:"^$md malloc ${size_in_mb}M$" \
214	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
215}
216attach_malloc_cleanup()
217{
218	cleanup_common
219}
220
221atf_test_case attach_swap cleanup
222attach_swap_head()
223{
224	atf_set "descr" "Tests mdconfig with -t swap"
225}
226attach_swap_body()
227{
228	local md
229	local size_in_mb=1024
230
231	atf_check -s exit:0 -o save:mdconfig.out \
232	    -x 'mdconfig -a -t swap -s 1g'
233	md=$(cat mdconfig.out)
234	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
235	check_diskinfo "$md" "1073741824" "2097152"
236	# This awk strips the file path.
237	atf_check -s exit:0 -o match:"^$md swap ${size_in_mb}M$" \
238	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
239}
240attach_swap_cleanup()
241{
242	cleanup_common
243}
244
245atf_test_case attach_with_specific_unit_number cleanup
246attach_with_specific_unit_number_head()
247{
248	atf_set "descr" "Tests mdconfig with a unit specified by -u"
249}
250attach_with_specific_unit_number_body()
251{
252	local md_unit=99
253	local size_in_mb=10
254
255	local md="md${md_unit}"
256
257	echo "$md" > mdconfig.out
258
259	atf_check -s exit:0 -o empty \
260	    -x "mdconfig -a -t malloc -s ${size_in_mb}m -u $md_unit"
261	check_diskinfo "$md" "10485760" "20480"
262	# This awk strips the file path.
263	atf_check -s exit:0 -o match:"^$md malloc "$size_in_mb"M$" \
264	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
265}
266attach_with_specific_unit_number_cleanup()
267{
268	cleanup_common
269}
270
271atf_init_test_cases()
272{
273	atf_add_test_case attach_vnode_non_explicit_type
274	atf_add_test_case attach_vnode_explicit_type
275	atf_add_test_case attach_vnode_smaller_than_file
276	atf_add_test_case attach_vnode_larger_than_file
277	atf_add_test_case attach_vnode_sector_size
278	atf_add_test_case attach_malloc
279	atf_add_test_case attach_swap
280	atf_add_test_case attach_with_specific_unit_number
281}
282