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