1310803Sasomers# Copyright (c) 2016 Alan Somers
2310803Sasomers# All rights reserved.
3310803Sasomers#
4310803Sasomers# Redistribution and use in source and binary forms, with or without
5310803Sasomers# modification, are permitted provided that the following conditions
6310803Sasomers# are met:
7310803Sasomers# 1. Redistributions of source code must retain the above copyright
8310803Sasomers#    notice, this list of conditions and the following disclaimer.
9310803Sasomers# 2. Redistributions in binary form must reproduce the above copyright
10310803Sasomers#    notice, this list of conditions and the following disclaimer in the
11310803Sasomers#    documentation and/or other materials provided with the distribution.
12310803Sasomers#
13310803Sasomers# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14310803Sasomers# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15310803Sasomers# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16310803Sasomers# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17310803Sasomers# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18310803Sasomers# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19310803Sasomers# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20310803Sasomers# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21310803Sasomers# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22310803Sasomers# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23310803Sasomers# SUCH DAMAGE.
24310803Sasomers#
25310803Sasomers# $FreeBSD: stable/10/tests/sys/geom/class/nop/nop_test.sh 312828 2017-01-26 20:10:31Z asomers $
26310803Sasomers
27310803SasomersMD_DEVS="md.devs"
28310803SasomersPLAINFILES=plainfiles
29310803Sasomers
30310803Sasomersatf_test_case diskinfo cleanup
31310803Sasomersdiskinfo_head()
32310803Sasomers{
33310803Sasomers	atf_set "descr" "gnop should preserve diskinfo's basic properties"
34310803Sasomers	atf_set "require.user" "root"
35310803Sasomers	atf_set "timeout" 15
36310803Sasomers}
37310803Sasomersdiskinfo_body()
38310803Sasomers{
39310803Sasomers	us=$(alloc_md)
40310803Sasomers	atf_check gnop create /dev/${us}
41310803Sasomers	md_secsize=$(diskinfo ${us} | cut -wf 2)
42310803Sasomers	md_mediasize=$(diskinfo ${us} | cut -wf 3)
43310803Sasomers	md_stripesize=$(diskinfo ${us} | cut -wf 5)
44310803Sasomers	nop_secsize=$(diskinfo ${us}.nop | cut -wf 2)
45310803Sasomers	nop_mediasize=$(diskinfo ${us}.nop | cut -wf 3)
46310803Sasomers	nop_stripesize=$(diskinfo ${us}.nop | cut -wf 5)
47310803Sasomers	atf_check_equal "$md_secsize" "$nop_secsize"
48310803Sasomers	atf_check_equal "$md_mediasize" "$nop_mediasize"
49310803Sasomers	atf_check_equal "$md_stripesize" "$nop_stripesize"
50310803Sasomers}
51310803Sasomersdiskinfo_cleanup()
52310803Sasomers{
53310803Sasomers	common_cleanup
54310803Sasomers}
55310803Sasomers
56310803Sasomersatf_test_case io cleanup
57310803Sasomersio_head()
58310803Sasomers{
59310803Sasomers	atf_set "descr" "I/O works on gnop devices"
60310803Sasomers	atf_set "require.user" "root"
61310803Sasomers	atf_set "timeout" 15
62310803Sasomers}
63310803Sasomersio_body()
64310803Sasomers{
65310803Sasomers	us=$(alloc_md)
66310803Sasomers	atf_check gnop create /dev/${us}
67310803Sasomers
68310803Sasomers	echo src >> $PLAINFILES
69310803Sasomers	echo dst >> $PLAINFILES
70310803Sasomers	dd if=/dev/random of=src bs=1m count=1 >/dev/null 2>&1
71310803Sasomers	dd if=src of=/dev/${us}.nop bs=1m count=1 > /dev/null 2>&1
72310803Sasomers	dd if=/dev/${us}.nop of=dst bs=1m count=1 > /dev/null 2>&1
73310803Sasomers
74310803Sasomers	atf_check_equal `md5 -q src` `md5 -q dst`
75310803Sasomers}
76310803Sasomersio_cleanup()
77310803Sasomers{
78310803Sasomers	common_cleanup
79310803Sasomers}
80310803Sasomers
81310803Sasomersatf_test_case size cleanup
82310803Sasomerssize_head()
83310803Sasomers{
84310803Sasomers	atf_set "descr" "Test gnop's -s option"
85310803Sasomers	atf_set "require.user" "root"
86310803Sasomers	atf_set "timeout" 15
87310803Sasomers}
88310803Sasomerssize_body()
89310803Sasomers{
90310803Sasomers	us=$(alloc_md)
91310803Sasomers	for mediasize in 65536 524288 1048576; do
92310803Sasomers		atf_check gnop create -s ${mediasize} /dev/${us}
93310803Sasomers		gnop_mediasize=`diskinfo /dev/${us}.nop | cut -wf 3`
94310803Sasomers		atf_check_equal "${mediasize}" "${gnop_mediasize}"
95310803Sasomers		atf_check gnop destroy /dev/${us}.nop
96310803Sasomers	done
97310803Sasomers	# We shouldn't be able to extend the provider's size
98310803Sasomers	atf_check -s not-exit:0 -e ignore gnop create -s 2097152 /dev/${us}
99310803Sasomers}
100310803Sasomerssize_cleanup()
101310803Sasomers{
102310803Sasomers	common_cleanup
103310803Sasomers}
104310803Sasomers
105310803Sasomersatf_test_case stripesize cleanup
106310803Sasomersstripesize_head()
107310803Sasomers{
108310803Sasomers	atf_set "descr" "Test gnop's -p and -P options"
109310803Sasomers	atf_set "require.user" "root"
110310803Sasomers	atf_set "timeout" 15
111310803Sasomers}
112310803Sasomersstripesize_body()
113310803Sasomers{
114310803Sasomers	us=$(alloc_md)
115310803Sasomers	for ss in 512 1024 2048 4096 8192; do
116310803Sasomers		for sofs in `seq 0 512 ${ss}`; do
117310803Sasomers			[ "$sofs" -eq "$ss" ] && continue
118310803Sasomers			atf_check gnop create -p ${ss} -P ${sofs} /dev/${us}
119310803Sasomers			gnop_ss=`diskinfo /dev/${us}.nop | cut -wf 5`
120310803Sasomers			gnop_sofs=`diskinfo /dev/${us}.nop | cut -wf 6`
121310803Sasomers			atf_check_equal "${ss}" "${gnop_ss}"
122310803Sasomers			atf_check_equal "${sofs}" "${gnop_sofs}"
123310803Sasomers			atf_check gnop destroy /dev/${us}.nop
124310803Sasomers		done
125310803Sasomers	done
126310803Sasomers}
127310803Sasomersstripesize_cleanup()
128310803Sasomers{
129310803Sasomers	common_cleanup
130310803Sasomers}
131310803Sasomers
132310803Sasomersatf_init_test_cases()
133310803Sasomers{
134310803Sasomers	atf_add_test_case io
135310803Sasomers	atf_add_test_case diskinfo
136310803Sasomers	atf_add_test_case stripesize
137310803Sasomers	atf_add_test_case size
138310803Sasomers}
139310803Sasomers
140310803Sasomersalloc_md()
141310803Sasomers{
142310803Sasomers	local md
143310803Sasomers
144310803Sasomers	md=$(mdconfig -a -t swap -s 1M) || atf_fail "mdconfig -a failed"
145310803Sasomers	echo ${md} >> $MD_DEVS
146310803Sasomers	echo ${md}
147310803Sasomers}
148310803Sasomers
149310803Sasomerscommon_cleanup()
150310803Sasomers{
151310803Sasomers	if [ -f "$MD_DEVS" ]; then
152310803Sasomers		while read test_md; do
153310803Sasomers			gnop destroy -f ${test_md}.nop 2>/dev/null
154310803Sasomers			mdconfig -d -u $test_md 2>/dev/null
155310803Sasomers		done < $MD_DEVS
156310803Sasomers		rm $MD_DEVS
157310803Sasomers	fi
158310803Sasomers
159310803Sasomers	if [ -f "$PLAINFILES" ]; then
160310803Sasomers		while read f; do
161310803Sasomers			rm -f ${f}
162310803Sasomers		done < ${PLAINFILES}
163310803Sasomers		rm ${PLAINFILES}
164310803Sasomers	fi
165310803Sasomers	true
166310803Sasomers}
167