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