xref: /freebsd/tests/sys/geom/class/nop/nop_test.sh (revision b70d2a2aa5003027b422e62435ab5bb9390d543c)
1# Copyright (c) 2016 Alan Somers
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
27MD_DEVS="md.devs"
28PLAINFILES=plainfiles
29
30atf_test_case diskinfo cleanup
31diskinfo_head()
32{
33	atf_set "descr" "gnop should preserve diskinfo's basic properties"
34	atf_set "require.user" "root"
35	atf_set "timeout" 15
36}
37diskinfo_body()
38{
39	us=$(alloc_md)
40	atf_check gnop create /dev/${us}
41	md_secsize=$(diskinfo ${us} | cut -wf 2)
42	md_mediasize=$(diskinfo ${us} | cut -wf 3)
43	md_stripesize=$(diskinfo ${us} | cut -wf 5)
44	nop_secsize=$(diskinfo ${us}.nop | cut -wf 2)
45	nop_mediasize=$(diskinfo ${us}.nop | cut -wf 3)
46	nop_stripesize=$(diskinfo ${us}.nop | cut -wf 5)
47	atf_check_equal "$md_secsize" "$nop_secsize"
48	atf_check_equal "$md_mediasize" "$nop_mediasize"
49	atf_check_equal "$md_stripesize" "$nop_stripesize"
50}
51diskinfo_cleanup()
52{
53	common_cleanup
54}
55
56atf_test_case io cleanup
57io_head()
58{
59	atf_set "descr" "I/O works on gnop devices"
60	atf_set "require.user" "root"
61	atf_set "timeout" 15
62}
63io_body()
64{
65	us=$(alloc_md)
66	atf_check gnop create /dev/${us}
67
68	echo src >> $PLAINFILES
69	echo dst >> $PLAINFILES
70	dd if=/dev/random of=src bs=1m count=1 >/dev/null 2>&1
71	dd if=src of=/dev/${us}.nop bs=1m count=1 > /dev/null 2>&1
72	dd if=/dev/${us}.nop of=dst bs=1m count=1 > /dev/null 2>&1
73
74	atf_check_equal `md5 -q src` `md5 -q dst`
75}
76io_cleanup()
77{
78	common_cleanup
79}
80
81atf_test_case size cleanup
82size_head()
83{
84	atf_set "descr" "Test gnop's -s option"
85	atf_set "require.user" "root"
86	atf_set "timeout" 15
87}
88size_body()
89{
90	us=$(alloc_md)
91	for mediasize in 65536 524288 1048576; do
92		atf_check gnop create -s ${mediasize} /dev/${us}
93		gnop_mediasize=`diskinfo /dev/${us}.nop | cut -wf 3`
94		atf_check_equal "${mediasize}" "${gnop_mediasize}"
95		atf_check gnop destroy /dev/${us}.nop
96	done
97	# We shouldn't be able to extend the provider's size
98	atf_check -s not-exit:0 -e ignore gnop create -s 2097152 /dev/${us}
99}
100size_cleanup()
101{
102	common_cleanup
103}
104
105atf_test_case stripesize cleanup
106stripesize_head()
107{
108	atf_set "descr" "Test gnop's -p and -P options"
109	atf_set "require.user" "root"
110	atf_set "timeout" 15
111}
112stripesize_body()
113{
114	us=$(alloc_md)
115	for ss in 512 1024 2048 4096 8192; do
116		for sofs in `seq 0 512 ${ss}`; do
117			[ "$sofs" -eq "$ss" ] && continue
118			atf_check gnop create -p ${ss} -P ${sofs} /dev/${us}
119			gnop_ss=`diskinfo /dev/${us}.nop | cut -wf 5`
120			gnop_sofs=`diskinfo /dev/${us}.nop | cut -wf 6`
121			atf_check_equal "${ss}" "${gnop_ss}"
122			atf_check_equal "${sofs}" "${gnop_sofs}"
123			atf_check gnop destroy /dev/${us}.nop
124		done
125	done
126}
127stripesize_cleanup()
128{
129	common_cleanup
130}
131
132atf_init_test_cases()
133{
134	atf_add_test_case io
135	atf_add_test_case diskinfo
136	atf_add_test_case stripesize
137	atf_add_test_case size
138}
139
140alloc_md()
141{
142	local md
143
144	md=$(mdconfig -a -t swap -s 1M) || atf_fail "mdconfig -a failed"
145	echo ${md} >> $MD_DEVS
146	echo ${md}
147}
148
149common_cleanup()
150{
151	if [ -f "$MD_DEVS" ]; then
152		while read test_md; do
153			gnop destroy -f ${test_md}.nop 2>/dev/null
154			mdconfig -d -u $test_md 2>/dev/null
155		done < $MD_DEVS
156		rm $MD_DEVS
157	fi
158
159	if [ -f "$PLAINFILES" ]; then
160		while read f; do
161			rm -f ${f}
162		done < ${PLAINFILES}
163		rm ${PLAINFILES}
164	fi
165	true
166}
167