xref: /freebsd/tests/sys/geom/class/nop/nop_test.sh (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
2#
3# Copyright (c) 2016 Alan Somers
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD$
27
28MD_DEVS="md.devs"
29PLAINFILES=plainfiles
30
31atf_test_case preserve_props cleanup
32preserve_props_head()
33{
34	atf_set "descr" "gnop should preserve basic GEOM properties"
35	atf_set "require.user" "root"
36	atf_set "timeout" 15
37}
38preserve_props_body()
39{
40	load_gnop
41	us=$(alloc_md)
42	atf_check gnop create /dev/${us}
43	md_secsize=$(diskinfo ${us} | cut -wf 2)
44	md_mediasize=$(diskinfo ${us} | cut -wf 3)
45	md_stripesize=$(diskinfo ${us} | cut -wf 5)
46	nop_secsize=$(diskinfo ${us}.nop | cut -wf 2)
47	nop_mediasize=$(diskinfo ${us}.nop | cut -wf 3)
48	nop_stripesize=$(diskinfo ${us}.nop | cut -wf 5)
49	atf_check_equal "$md_secsize" "$nop_secsize"
50	atf_check_equal "$md_mediasize" "$nop_mediasize"
51	atf_check_equal "$md_stripesize" "$nop_stripesize"
52}
53preserve_props_cleanup()
54{
55	common_cleanup
56}
57
58atf_test_case preserve_disk_props cleanup
59preserve_disk_props_head()
60{
61	atf_set "descr" "gnop should preserve properties for disks"
62	atf_set "require.user" "root"
63	atf_set "require.config" "disks"
64	atf_set "timeout" 15
65}
66preserve_disk_props_body()
67{
68	load_gnop
69	disks=`atf_config_get disks`
70	disk=${disks%% *}
71	if [ -z "$disk" ]; then
72		atf_skip "Must define disks (see tests(7))"
73	fi
74	atf_check gnop create ${disk}
75
76	disk_ident=$(diskinfo -s ${disk})
77	disk_physpath=$(diskinfo -p ${disk})
78	disk_descr=$(diskinfo -v ${disk} | awk '/Disk descr/ {print $1}')
79	disk_trim=$(diskinfo -v ${disk} | awk '/TRIM.UNMAP/ {print $1}')
80	disk_rotrate=$(diskinfo -v ${disk} | awk '/Rotation rate/ {print $1}')
81	disk_zonemode=$(diskinfo -v ${disk} | awk '/Zone Mode/ {print $1}')
82	nop_ident=$(diskinfo -s ${disk}.nop)
83	nop_physpath=$(diskinfo -p ${disk}.nop)
84	nop_descr=$(diskinfo -v ${disk}.nop | awk '/Disk descr/ {print $1}')
85	nop_trim=$(diskinfo -v ${disk}.nop | awk '/TRIM.UNMAP/ {print $1}')
86	nop_rotrate=$(diskinfo -v ${disk}.nop | awk '/Rotation/ {print $1}')
87	nop_zonemode=$(diskinfo -v ${disk}.nop | awk '/Zone Mode/ {print $1}')
88	atf_check_equal "$disk_ident" "$nop_ident"
89	atf_check_equal "$disk_physpath" "$nop_physpath"
90	atf_check_equal "$disk_descr" "$nop_descr"
91	atf_check_equal "$disk_trim" "$nop_trim"
92	atf_check_equal "$disk_rotrate" "$nop_rotrate"
93	atf_check_equal "$disk_zonemode" "$nop_zonemode"
94}
95preserve_disk_props_cleanup()
96{
97	disk_cleanup
98	common_cleanup
99}
100
101atf_test_case io cleanup
102io_head()
103{
104	atf_set "descr" "I/O works on gnop devices"
105	atf_set "require.user" "root"
106	atf_set "timeout" 15
107}
108io_body()
109{
110	load_gnop
111	us=$(alloc_md)
112	atf_check gnop create /dev/${us}
113
114	echo src >> $PLAINFILES
115	echo dst >> $PLAINFILES
116	dd if=/dev/random of=src bs=1m count=1 >/dev/null 2>&1
117	dd if=src of=/dev/${us}.nop bs=1m count=1 > /dev/null 2>&1
118	dd if=/dev/${us}.nop of=dst bs=1m count=1 > /dev/null 2>&1
119
120	atf_check_equal `md5 -q src` `md5 -q dst`
121}
122io_cleanup()
123{
124	common_cleanup
125}
126
127atf_test_case physpath cleanup
128physpath_head()
129{
130	atf_set "descr" "Test gnop's -z option"
131	atf_set "require.user" "root"
132	atf_set "timeout" 15
133}
134physpath_body()
135{
136	load_gnop
137	us=$(alloc_md)
138	physpath="some/physical/path"
139	atf_check gnop create -z $physpath /dev/${us}
140	gnop_physpath=$(diskinfo -p ${us}.nop)
141	atf_check_equal "$physpath" "$gnop_physpath"
142}
143physpath_cleanup()
144{
145	common_cleanup
146}
147
148atf_test_case physpath_blank cleanup
149physpath_blank_head()
150{
151	atf_set "descr" "gnop can set physical path to the empty string"
152	atf_set "require.user" "root"
153	atf_set "require.config" "disks"
154	atf_set "timeout" 15
155}
156physpath_blank_body()
157{
158	load_gnop
159	disks=`atf_config_get disks`
160	disk=${disks%% *}
161	if [ -z "$disk" ]; then
162		atf_skip "Must define disks (see tests(7))"
163	fi
164
165	atf_check gnop create -z "" ${disk}
166	gnop_physpath=$(diskinfo -p ${disk}.nop)
167	atf_check_equal "" "$gnop_physpath"
168}
169physpath_blank_cleanup()
170{
171	disk_cleanup
172	common_cleanup
173}
174
175atf_test_case size cleanup
176size_head()
177{
178	atf_set "descr" "Test gnop's -s option"
179	atf_set "require.user" "root"
180	atf_set "timeout" 15
181}
182size_body()
183{
184	load_gnop
185	us=$(alloc_md)
186	for mediasize in 65536 524288 1048576; do
187		atf_check gnop create -s ${mediasize} /dev/${us}
188		gnop_mediasize=`diskinfo /dev/${us}.nop | cut -wf 3`
189		atf_check_equal "${mediasize}" "${gnop_mediasize}"
190		atf_check gnop destroy /dev/${us}.nop
191	done
192	# We shouldn't be able to extend the provider's size
193	atf_check -s not-exit:0 -e ignore gnop create -s 2097152 /dev/${us}
194}
195size_cleanup()
196{
197	common_cleanup
198}
199
200atf_test_case stripesize cleanup
201stripesize_head()
202{
203	atf_set "descr" "Test gnop's -p and -P options"
204	atf_set "require.user" "root"
205	atf_set "timeout" 120
206}
207stripesize_body()
208{
209	load_gnop
210	us=$(alloc_md)
211	for ss in 512 1024 2048 4096 8192; do
212		for sofs in `seq 0 512 ${ss}`; do
213			[ "$sofs" -eq "$ss" ] && continue
214			atf_check gnop create -p ${ss} -P ${sofs} /dev/${us}
215			gnop_ss=`diskinfo /dev/${us}.nop | cut -wf 5`
216			gnop_sofs=`diskinfo /dev/${us}.nop | cut -wf 6`
217			atf_check_equal "${ss}" "${gnop_ss}"
218			atf_check_equal "${sofs}" "${gnop_sofs}"
219			atf_check gnop destroy /dev/${us}.nop
220		done
221	done
222}
223stripesize_cleanup()
224{
225	common_cleanup
226}
227
228atf_init_test_cases()
229{
230	atf_add_test_case io
231	atf_add_test_case physpath
232	atf_add_test_case physpath_blank
233	atf_add_test_case preserve_props
234	atf_add_test_case preserve_disk_props
235	atf_add_test_case stripesize
236	atf_add_test_case size
237}
238
239alloc_md()
240{
241	local md
242
243	md=$(mdconfig -a -t swap -s 1M) || atf_fail "mdconfig -a failed"
244	echo ${md} >> $MD_DEVS
245	echo ${md}
246}
247
248common_cleanup()
249{
250	if [ -f "$MD_DEVS" ]; then
251		while read test_md; do
252			gnop destroy -f ${test_md}.nop 2>/dev/null
253			mdconfig -d -u $test_md 2>/dev/null
254		done < $MD_DEVS
255		rm $MD_DEVS
256	fi
257
258	if [ -f "$PLAINFILES" ]; then
259		while read f; do
260			rm -f ${f}
261		done < ${PLAINFILES}
262		rm ${PLAINFILES}
263	fi
264	true
265}
266
267disk_cleanup()
268{
269	disks=`atf_config_get disks`
270	disk=${disks%% *}
271	if [ -n "$disk" ]; then
272		gnop destroy -f ${disk}.nop 2>/dev/null
273	fi
274}
275
276load_gnop()
277{
278	if ! kldstat -q -m g_nop; then
279		geom nop load || atf_skip "could not load module for geom nop"
280	fi
281}
282