aboutsummaryrefslogtreecommitdiff
path: root/sbin/mdconfig/tests/mdconfig_test.sh
blob: d12e565db4aa27608c997588e2cf72faf9e8fd7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Copyright (c) 2012 Edward Tomasz Napierała <trasz@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#

check_diskinfo()
{
	local md=$1
	local mediasize_in_bytes=$2
	local mediasize_in_sectors=$3
	local sectorsize=${4:-512}
	local stripesize=${5:-0}
	local stripeoffset=${6:-0}

	atf_check -s exit:0 \
	    -o match:"/dev/$md *$sectorsize *$mediasize_in_bytes *$mediasize_in_sectors *$stripesize *$stripeoffset" \
	    -x "diskinfo /dev/$md | expand"
}

cleanup_common()
{
	if [ -f mdconfig.out ]; then
		mdconfig -d -u $(sed -e 's/md//' mdconfig.out)
	fi
}

atf_test_case attach_vnode_non_explicit_type cleanup
attach_vnode_non_explicit_type_head()
{
	atf_set "descr" "Tests out -a / -f without -t"
}
attach_vnode_non_explicit_type_body()
{
	local md
	local size_in_mb=1024

	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig -af xxx'
	md=$(cat mdconfig.out)
	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
	check_diskinfo "$md" "1073741824" "2097152"
	# This awk strips the file path.
	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
}
attach_vnode_non_explicit_type_cleanup()
{
	cleanup_common
}

atf_test_case attach_vnode_implicit_a_f cleanup
attach_vnode_implicit_a_f_head()
{
	atf_set "descr" "Tests out implied -a / -f without -t"
}
attach_vnode_implicit_a_f_body()
{
	local md
	local size_in_mb=1024

	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig xxx'
	md=$(cat mdconfig.out)
	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
	check_diskinfo "$md" "1073741824" "2097152"
	# This awk strips the file path.
	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
}
attach_vnode_implicit_a_f_cleanup()
{
	cleanup_common
}

atf_test_case attach_vnode_explicit_type cleanup
attach_vnode_explicit_type_head()
{
	atf_set "descr" "Tests out implied -a / -f with -t vnode"
}
attach_vnode_explicit_type_body()
{
	local md
	local size_in_mb=1024

	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
	atf_check -s exit:0 -o save:mdconfig.out -x 'mdconfig -af xxx -t vnode'
	md=$(cat mdconfig.out)
	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
	check_diskinfo "$md" "1073741824" "2097152"
	# This awk strips the file path.
	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
}
attach_vnode_explicit_type_cleanup()
{
	[ -f mdconfig.out ] && mdconfig -d -u $(sed -e 's/md//' mdconfig.out)
	rm -f mdconfig.out xxx
}

atf_test_case attach_vnode_smaller_than_file cleanup
attach_vnode_smaller_than_file_head()
{
	atf_set "descr" "Tests mdconfig -s with size less than the file size"
}
attach_vnode_smaller_than_file_body()
{
	local md
	local size_in_mb=128

	atf_check -s exit:0 -x "truncate -s 1024m xxx"
	atf_check -s exit:0 -o save:mdconfig.out \
	    -x "mdconfig -af xxx -s ${size_in_mb}m"
	md=$(cat mdconfig.out)
	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
	check_diskinfo "$md" "134217728" "262144"
	# This awk strips the file path.
	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
}
attach_vnode_smaller_than_file_cleanup()
{
	cleanup_common
}

atf_test_case attach_vnode_larger_than_file cleanup
attach_vnode_larger_than_file_head()
{
	atf_set "descr" "Tests mdconfig -s with size greater than the file size"
}
attach_vnode_larger_than_file_body()
{
	local md
	local size_in_gb=128

	atf_check -s exit:0 -x "truncate -s 1024m xxx"
	atf_check -s exit:0 -o save:mdconfig.out \
	    -x "mdconfig -af xxx -s ${size_in_gb}g"
	md=$(cat mdconfig.out)
	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
	check_diskinfo "$md" "137438953472" "268435456"
	# This awk strips the file path.
	atf_check -s exit:0 -o match:"^$md vnode ${size_in_gb}G$" \
	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
}
attach_vnode_larger_than_file_cleanup()
{
	cleanup_common
}

atf_test_case attach_vnode_sector_size cleanup
attach_vnode_sector_size_head()
{
	atf_set "descr" "Tests mdconfig -s with size greater than the file size"
}
attach_vnode_sector_size_body()
{
	local md
	local size_in_mb=1024

	atf_check -s exit:0 -x "truncate -s ${size_in_mb}m xxx"
	atf_check -s exit:0 -o save:mdconfig.out \
	    -x "mdconfig -af xxx -S 2048"
	md=$(cat mdconfig.out)
	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
	check_diskinfo "$md" "1073741824" "524288" "2048"
	# This awk strips the file path.
	atf_check -s exit:0 -o match:"^$md vnode ${size_in_mb}M$" \
	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
}
attach_vnode_sector_size_cleanup()
{
	cleanup_common
}

atf_test_case attach_malloc cleanup
attach_malloc_head()
{
	atf_set "descr" "Tests mdconfig with -t malloc"
}
attach_malloc_body()
{
	local md
	local size_in_mb=1024

	atf_check -s exit:0 -o save:mdconfig.out \
	    -x 'mdconfig -a -t malloc -s 1g'
	md=$(cat mdconfig.out)
	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
	check_diskinfo "$md" "1073741824" "2097152"
	# This awk strips the file path.
	atf_check -s exit:0 -o match:"^$md malloc ${size_in_mb}M$" \
	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
}
attach_malloc_cleanup()
{
	cleanup_common
}

atf_test_case attach_swap cleanup
attach_swap_head()
{
	atf_set "descr" "Tests mdconfig with -t swap"
}
attach_swap_body()
{
	local md
	local size_in_mb=1024

	atf_check -s exit:0 -o save:mdconfig.out \
	    -x 'mdconfig -a -t swap -s 1g'
	md=$(cat mdconfig.out)
	atf_check -s exit:0 -o match:'^md[0-9]+$' -x "echo $md"
	check_diskinfo "$md" "1073741824" "2097152"
	# This awk strips the file path.
	atf_check -s exit:0 -o match:"^$md swap ${size_in_mb}M$" \
	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
}
attach_swap_cleanup()
{
	cleanup_common
}

atf_test_case attach_with_specific_unit_number cleanup
attach_with_specific_unit_number_head()
{
	atf_set "descr" "Tests mdconfig with a unit specified by -u"
}
attach_with_specific_unit_number_body()
{
	local md_unit=99
	local size_in_mb=10

	local md="md${md_unit}"

	echo "$md" > mdconfig.out

	atf_check -s exit:0 -o empty \
	    -x "mdconfig -a -t malloc -s ${size_in_mb}m -u $md_unit"
	check_diskinfo "$md" "10485760" "20480"
	# This awk strips the file path.
	atf_check -s exit:0 -o match:"^$md malloc "$size_in_mb"M$" \
	    -x "mdconfig -lv | awk '\$1 == \"$md\" { print \$1, \$2, \$3 }'"
}
attach_with_specific_unit_number_cleanup()
{
	cleanup_common
}

atf_init_test_cases()
{
	atf_add_test_case attach_vnode_non_explicit_type
	atf_add_test_case attach_vnode_explicit_type
	atf_add_test_case attach_vnode_smaller_than_file
	atf_add_test_case attach_vnode_larger_than_file
	atf_add_test_case attach_vnode_sector_size
	atf_add_test_case attach_malloc
	atf_add_test_case attach_swap
	atf_add_test_case attach_with_specific_unit_number
}