1275170Sngie# Copyright (c) 2012 Edward Tomasz Napiera��a <trasz@FreeBSD.org>
2275170Sngie# All rights reserved.
3275170Sngie#
4275170Sngie# Redistribution and use in source and binary forms, with or without
5275170Sngie# modification, are permitted provided that the following conditions
6275170Sngie# are met:
7275170Sngie# 1. Redistributions of source code must retain the above copyright
8275170Sngie#    notice, this list of conditions and the following disclaimer.
9275170Sngie# 2. Redistributions in binary form must reproduce the above copyright
10275170Sngie#    notice, this list of conditions and the following disclaimer in the
11275170Sngie#    documentation and/or other materials provided with the distribution.
12275170Sngie#
13275170Sngie# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14275170Sngie# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15275170Sngie# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16275170Sngie# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17275170Sngie# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18275170Sngie# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19275170Sngie# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20275170Sngie# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21275170Sngie# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22275170Sngie# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23275170Sngie# SUCH DAMAGE.
24275170Sngie#
25275170Sngie# $FreeBSD: releng/11.0/sbin/mdconfig/tests/mdconfig_test.sh 275170 2014-11-27 06:04:00Z ngie $
26275170Sngie#
27275170Sngie
28275170Sngiecheck_diskinfo()
29275170Sngie{
30275170Sngie	local md=$1
31275170Sngie	local mediasize_in_bytes=$2
32275170Sngie	local mediasize_in_sectors=$3
33275170Sngie	local sectorsize=${4:-512}
34275170Sngie	local stripesize=${5:-0}
35275170Sngie	local stripeoffset=${6:-0}
36275170Sngie
37275170Sngie	atf_check -s exit:0 \
38275170Sngie	    -o match:"/dev/$md *$sectorsize *$mediasize_in_bytes *$mediasize_in_sectors *$stripesize *$stripeoffset" \
39275170Sngie	    -x "diskinfo /dev/$md | expand"
40275170Sngie}
41275170Sngie
42275170Sngiecleanup_common()
43275170Sngie{
44275170Sngie	if [ -f mdconfig.out ]; then
45275170Sngie		mdconfig -d -u $(sed -e 's/md//' mdconfig.out)
46275170Sngie	fi
47275170Sngie}
48275170Sngie
49275170Sngieatf_test_case attach_vnode_non_explicit_type cleanup
50275170Sngieattach_vnode_non_explicit_type_head()
51275170Sngie{
52275170Sngie	atf_set "descr" "Tests out -a / -f without -t"
53275170Sngie}
54275170Sngieattach_vnode_non_explicit_type_body()
55275170Sngie{
56275170Sngie	local md
57275170Sngie	local size_in_mb=1024
58275170Sngie
59275170Sngie	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
60275170Sngie	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig -af xxx'
61275170Sngie	md=$(cat mdconfig.out)
62275170Sngie	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
63275170Sngie	check_diskinfo "$md" "1073741824" "2097152"
64275170Sngie	# This awk strips the file path.
65275170Sngie	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
66275170Sngie	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
67275170Sngie}
68275170Sngieattach_vnode_non_explicit_type_cleanup()
69275170Sngie{
70275170Sngie	cleanup_common
71275170Sngie}
72275170Sngie
73275170Sngieatf_test_case attach_vnode_implicit_a_f cleanup
74275170Sngieattach_vnode_implicit_a_f_head()
75275170Sngie{
76275170Sngie	atf_set "descr" "Tests out implied -a / -f without -t"
77275170Sngie}
78275170Sngieattach_vnode_implicit_a_f_body()
79275170Sngie{
80275170Sngie	local md
81275170Sngie	local size_in_mb=1024
82275170Sngie
83275170Sngie	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
84275170Sngie	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig xxx'
85275170Sngie	md=$(cat mdconfig.out)
86275170Sngie	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
87275170Sngie	check_diskinfo "$md" "1073741824" "2097152"
88275170Sngie	# This awk strips the file path.
89275170Sngie	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
90275170Sngie	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
91275170Sngie}
92275170Sngieattach_vnode_implicit_a_f_cleanup()
93275170Sngie{
94275170Sngie	cleanup_common
95275170Sngie}
96275170Sngie
97275170Sngieatf_test_case attach_vnode_explicit_type cleanup
98275170Sngieattach_vnode_explicit_type_head()
99275170Sngie{
100275170Sngie	atf_set "descr" "Tests out implied -a / -f with -t vnode"
101275170Sngie}
102275170Sngieattach_vnode_explicit_type_body()
103275170Sngie{
104275170Sngie	local md
105275170Sngie	local size_in_mb=1024
106275170Sngie
107275170Sngie	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
108275170Sngie	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig -af xxx -t vnode'
109275170Sngie	md=$(cat mdconfig.out)
110275170Sngie	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
111275170Sngie	check_diskinfo "$md" "1073741824" "2097152"
112275170Sngie	# This awk strips the file path.
113275170Sngie	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
114275170Sngie	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
115275170Sngie}
116275170Sngieattach_vnode_explicit_type_cleanup()
117275170Sngie{
118275170Sngie	[ -f mdconfig.out ] && mdconfig -d -u $(sed -e 's/md//' mdconfig.out)
119275170Sngie	rm -f mdconfig.out xxx
120275170Sngie}
121275170Sngie
122275170Sngieatf_test_case attach_vnode_smaller_than_file cleanup
123275170Sngieattach_vnode_smaller_than_file_head()
124275170Sngie{
125275170Sngie	atf_set "descr" "Tests mdconfig -s with size less than the file size"
126275170Sngie}
127275170Sngieattach_vnode_smaller_than_file_body()
128275170Sngie{
129275170Sngie	local md
130275170Sngie	local size_in_mb=128
131275170Sngie
132275170Sngie	atf_check -s exit:0 -x "truncate -s 1024m xxx"
133275170Sngie	atf_check -s exit:0 -o save:mdconfig.out \
134275170Sngie	    -x "mdconfig -af xxx -s ${size_in_mb}m"
135275170Sngie	md=$(cat mdconfig.out)
136275170Sngie	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
137275170Sngie	check_diskinfo "$md" "134217728" "262144"
138275170Sngie	# This awk strips the file path.
139275170Sngie	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
140275170Sngie	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
141275170Sngie}
142275170Sngieattach_vnode_smaller_than_file_cleanup()
143275170Sngie{
144275170Sngie	cleanup_common
145275170Sngie}
146275170Sngie
147275170Sngieatf_test_case attach_vnode_larger_than_file cleanup
148275170Sngieattach_vnode_larger_than_file_head()
149275170Sngie{
150275170Sngie	atf_set "descr" "Tests mdconfig -s with size greater than the file size"
151275170Sngie}
152275170Sngieattach_vnode_larger_than_file_body()
153275170Sngie{
154275170Sngie	local md
155275170Sngie	local size_in_gb=128
156275170Sngie
157275170Sngie	atf_check -s exit:0 -x "truncate -s 1024m xxx"
158275170Sngie	atf_check -s exit:0 -o save:mdconfig.out \
159275170Sngie	    -x "mdconfig -af xxx -s ${size_in_gb}g"
160275170Sngie	md=$(cat mdconfig.out)
161275170Sngie	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
162275170Sngie	check_diskinfo "$md" "137438953472" "268435456"
163275170Sngie	# This awk strips the file path.
164275170Sngie	atf_check -s exit:0 -o match:"^$md vnode ${size_in_gb}G$" \
165275170Sngie	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
166275170Sngie}
167275170Sngieattach_vnode_larger_than_file_cleanup()
168275170Sngie{
169275170Sngie	cleanup_common
170275170Sngie}
171275170Sngie
172275170Sngieatf_test_case attach_vnode_sector_size cleanup
173275170Sngieattach_vnode_sector_size_head()
174275170Sngie{
175275170Sngie	atf_set "descr" "Tests mdconfig -s with size greater than the file size"
176275170Sngie}
177275170Sngieattach_vnode_sector_size_body()
178275170Sngie{
179275170Sngie	local md
180275170Sngie	local size_in_mb=1024
181275170Sngie
182275170Sngie	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
183275170Sngie	atf_check -s exit:0 -o save:mdconfig.out \
184275170Sngie	    -x "mdconfig -af xxx -S 2048"
185275170Sngie	md=$(cat mdconfig.out)
186275170Sngie	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
187275170Sngie	check_diskinfo "$md" "1073741824" "524288" "2048"
188275170Sngie	# This awk strips the file path.
189275170Sngie	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
190275170Sngie	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
191275170Sngie}
192275170Sngieattach_vnode_sector_size_cleanup()
193275170Sngie{
194275170Sngie	cleanup_common
195275170Sngie}
196275170Sngie
197275170Sngieatf_test_case attach_malloc cleanup
198275170Sngieattach_malloc_head()
199275170Sngie{
200275170Sngie	atf_set "descr" "Tests mdconfig with -t malloc"
201275170Sngie}
202275170Sngieattach_malloc_body()
203275170Sngie{
204275170Sngie	local md
205275170Sngie	local size_in_mb=1024
206275170Sngie
207275170Sngie	atf_check -s exit:0 -o save:mdconfig.out \
208275170Sngie	    -x 'mdconfig -a -t malloc -s 1g'
209275170Sngie	md=$(cat mdconfig.out)
210275170Sngie	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
211275170Sngie	check_diskinfo "$md" "1073741824" "2097152"
212275170Sngie	# This awk strips the file path.
213275170Sngie	atf_check -s exit:0 -o match:"^$md malloc ${size_in_mb}M$" \
214275170Sngie	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
215275170Sngie}
216275170Sngieattach_malloc_cleanup()
217275170Sngie{
218275170Sngie	cleanup_common
219275170Sngie}
220275170Sngie
221275170Sngieatf_test_case attach_swap cleanup
222275170Sngieattach_swap_head()
223275170Sngie{
224275170Sngie	atf_set "descr" "Tests mdconfig with -t swap"
225275170Sngie}
226275170Sngieattach_swap_body()
227275170Sngie{
228275170Sngie	local md
229275170Sngie	local size_in_mb=1024
230275170Sngie
231275170Sngie	atf_check -s exit:0 -o save:mdconfig.out \
232275170Sngie	    -x 'mdconfig -a -t swap -s 1g'
233275170Sngie	md=$(cat mdconfig.out)
234275170Sngie	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
235275170Sngie	check_diskinfo "$md" "1073741824" "2097152"
236275170Sngie	# This awk strips the file path.
237275170Sngie	atf_check -s exit:0 -o match:"^$md swap ${size_in_mb}M$" \
238275170Sngie	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
239275170Sngie}
240275170Sngieattach_swap_cleanup()
241275170Sngie{
242275170Sngie	cleanup_common
243275170Sngie}
244275170Sngie
245275170Sngieatf_test_case attach_with_specific_unit_number cleanup
246275170Sngieattach_with_specific_unit_number_head()
247275170Sngie{
248275170Sngie	atf_set "descr" "Tests mdconfig with a unit specified by -u"
249275170Sngie}
250275170Sngieattach_with_specific_unit_number_body()
251275170Sngie{
252275170Sngie	local md_unit=99
253275170Sngie	local size_in_mb=10
254275170Sngie
255275170Sngie	local md="md${md_unit}"
256275170Sngie
257275170Sngie	echo "$md" > mdconfig.out
258275170Sngie
259275170Sngie	atf_check -s exit:0 -o empty \
260275170Sngie	    -x "mdconfig -a -t malloc -s ${size_in_mb}m -u $md_unit"
261275170Sngie	check_diskinfo "$md" "10485760" "20480"
262275170Sngie	# This awk strips the file path.
263275170Sngie	atf_check -s exit:0 -o match:"^$md malloc "$size_in_mb"M$" \
264275170Sngie	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
265275170Sngie}
266275170Sngieattach_with_specific_unit_number_cleanup()
267275170Sngie{
268275170Sngie	cleanup_common
269275170Sngie}
270275170Sngie
271275170Sngieatf_init_test_cases()
272275170Sngie{
273275170Sngie	atf_add_test_case attach_vnode_non_explicit_type
274275170Sngie	atf_add_test_case attach_vnode_explicit_type
275275170Sngie	atf_add_test_case attach_vnode_smaller_than_file
276275170Sngie	atf_add_test_case attach_vnode_larger_than_file
277275170Sngie	atf_add_test_case attach_vnode_sector_size
278275170Sngie	atf_add_test_case attach_malloc
279275170Sngie	atf_add_test_case attach_swap
280275170Sngie	atf_add_test_case attach_with_specific_unit_number
281275170Sngie}
282