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