xref: /freebsd/tests/sys/geom/class/gate/ggate_test.sh (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1# $FreeBSD$
2
3PIDFILE=ggated.pid
4PLAINFILES=plainfiles
5PORT=33080
6CONF=gg.exports
7
8atf_test_case ggatec_trim cleanup
9ggatec_trim_head()
10{
11	atf_set "descr" "ggatec survives a trim"
12	atf_set "require.progs" "ggatec"
13	atf_set "require.user" "root"
14	atf_set "timeout" 60
15}
16
17ggatec_trim_body()
18{
19	load_ggate
20
21	us=$(alloc_ggate_dev)
22	work=$(alloc_md)
23	atf_check -e ignore -o ignore dd if=/dev/random of=/dev/$work bs=1m count=1 conv=notrunc
24	echo $CONF >> $PLAINFILES
25	echo "localhost RW /dev/$work" > $CONF
26	atf_check ggated -p $PORT -F $PIDFILE $CONF
27	atf_check ggatec create -p $PORT -u $us localhost /dev/$work
28	ggate_dev=/dev/ggate${us}
29	wait_for_ggate_device ${ggate_dev}
30
31	# ggatec only supports read or write.
32	atf_check -s not-exit:0 -e ignore -o ignore trim -q -f ${ggate_dev}
33}
34
35ggatec_trim_cleanup()
36{
37	common_cleanup
38}
39
40
41atf_test_case ggated cleanup
42ggated_head()
43{
44	atf_set "descr" "ggated can proxy geoms"
45	atf_set "require.progs" "ggatec ggated"
46	atf_set "require.user" "root"
47	atf_set "timeout" 60
48}
49
50ggated_body()
51{
52	if [ "$(atf_config_get ci false)" = "true" ] && \
53		[ "$(uname -p)" = "i386" ]; then
54		atf_skip "https://bugs.freebsd.org/244737"
55	fi
56
57	load_ggate
58
59	us=$(alloc_ggate_dev)
60	work=$(alloc_md)
61	src=$(alloc_md)
62
63	atf_check -e ignore -o ignore \
64	    dd if=/dev/random of=/dev/$work bs=1m count=1 conv=notrunc
65	atf_check -e ignore -o ignore \
66	    dd if=/dev/random of=/dev/$src bs=1m count=1 conv=notrunc
67
68	echo $CONF >> $PLAINFILES
69	echo "127.0.0.1 RW /dev/$work" > $CONF
70
71	atf_check ggated -p $PORT -F $PIDFILE $CONF
72	atf_check ggatec create -p $PORT -u $us 127.0.0.1 /dev/$work
73
74	ggate_dev=/dev/ggate${us}
75
76	wait_for_ggate_device ${ggate_dev}
77
78	atf_check -e ignore -o ignore \
79	    dd if=/dev/${src} of=${ggate_dev} bs=1m count=1 conv=notrunc
80
81	checksum /dev/$src /dev/$work
82}
83
84ggated_cleanup()
85{
86	common_cleanup
87}
88
89atf_test_case ggatel_file cleanup
90ggatel_file_head()
91{
92	atf_set "descr" "ggatel can proxy files"
93	atf_set "require.progs" "ggatel"
94	atf_set "require.user" "root"
95	atf_set "timeout" 15
96}
97
98ggatel_file_body()
99{
100	load_ggate
101
102	us=$(alloc_ggate_dev)
103
104	echo src work >> ${PLAINFILES}
105	dd if=/dev/random of=work bs=1m count=1
106	dd if=/dev/random of=src bs=1m count=1
107
108	atf_check ggatel create -u $us work
109
110	ggate_dev=/dev/ggate${us}
111
112	wait_for_ggate_device ${ggate_dev}
113
114	atf_check -e ignore -o ignore \
115	    dd if=src of=${ggate_dev} bs=1m count=1 conv=notrunc
116
117	checksum src work
118}
119
120ggatel_file_cleanup()
121{
122	common_cleanup
123}
124
125atf_test_case ggatel_md cleanup
126ggatel_md_head()
127{
128	atf_set "descr" "ggatel can proxy files"
129	atf_set "require.progs" "ggatel"
130	atf_set "require.user" "root"
131	atf_set "timeout" 15
132}
133
134ggatel_md_body()
135{
136	load_ggate
137
138	us=$(alloc_ggate_dev)
139	work=$(alloc_md)
140	src=$(alloc_md)
141
142	atf_check -e ignore -o ignore \
143	    dd if=/dev/random of=$work bs=1m count=1 conv=notrunc
144	atf_check -e ignore -o ignore \
145	    dd if=/dev/random of=$src bs=1m count=1 conv=notrunc
146
147	atf_check ggatel create -u $us /dev/$work
148
149	ggate_dev=/dev/ggate${us}
150
151	wait_for_ggate_device ${ggate_dev}
152
153	atf_check -e ignore -o ignore \
154	    dd if=/dev/$src of=${ggate_dev} bs=1m count=1 conv=notrunc
155
156	checksum /dev/$src /dev/$work
157}
158
159ggatel_md_cleanup()
160{
161	common_cleanup
162}
163
164atf_init_test_cases()
165{
166	atf_add_test_case ggated
167	atf_add_test_case ggatel_file
168	atf_add_test_case ggatel_md
169	atf_add_test_case ggatec_trim
170}
171
172alloc_ggate_dev()
173{
174	local us
175
176	us=0
177	while [ -c /dev/ggate${us} ]; do
178		: $(( us += 1 ))
179	done
180	echo ${us} > ggate.devs
181	echo ${us}
182}
183
184alloc_md()
185{
186	local md
187
188	md=$(mdconfig -a -t malloc -s 1M) || \
189		atf_fail "failed to allocate md device"
190	echo ${md} >> md.devs
191	echo ${md}
192}
193
194checksum()
195{
196	local src work
197	src=$1
198	work=$2
199
200	src_checksum=$(md5 -q $src)
201	work_checksum=$(md5 -q $work)
202
203	if [ "$work_checksum" != "$src_checksum" ]; then
204		atf_fail "work md5 checksum didn't match"
205	fi
206
207	ggate_checksum=$(md5 -q /dev/ggate${us})
208	if [ "$ggate_checksum" != "$src_checksum" ]; then
209		atf_fail "ggate md5 checksum didn't match"
210	fi
211}
212
213common_cleanup()
214{
215	if [ -f "ggate.devs" ]; then
216		while read test_ggate; do
217			ggatec destroy -f -u $test_ggate >/dev/null
218		done < ggate.devs
219		rm ggate.devs
220	fi
221
222	if [ -f "$PIDFILE" ]; then
223		pkill -F "$PIDFILE"
224		rm $PIDFILE
225	fi
226
227	if [ -f "PLAINFILES" ]; then
228		while read f; do
229			rm -f ${f}
230		done < ${PLAINFILES}
231		rm ${PLAINFILES}
232	fi
233
234	if [ -f "md.devs" ]; then
235		while read test_md; do
236			# ggatec destroy doesn't release the provider
237			# synchronously, so we may need to retry destroying it.
238			while ! mdconfig -d -u $test_md; do
239				sleep 0.1
240			done
241		done < md.devs
242		rm md.devs
243	fi
244	true
245}
246
247load_ggate()
248{
249	local class=gate
250
251	# If the geom class isn't already loaded, try loading it.
252	if ! kldstat -q -m g_${class}; then
253		if ! geom ${class} load; then
254			atf_skip "could not load module for geom class=${class}"
255		fi
256	fi
257}
258
259# Bug 204616: ggatel(8) creates /dev/ggate* asynchronously if `ggatel create`
260#             isn't called with `-v`.
261wait_for_ggate_device()
262{
263	ggate_device=$1
264
265	while [ ! -c $ggate_device ]; do
266		sleep 0.5
267	done
268}
269