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