Merge branch 'master' of git://nbd.git.sourceforge.net/gitroot/nbd/nbd
[nbd.git] / simple_test
1 #!/bin/sh
2 # Yes, that's POSIX sh, not bash!
3
4 tmpnam=`mktemp`
5 conffile=${tmpnam}.conf
6 pidfile=${tmpnam}.pid
7
8 ulimit -c unlimited
9
10 # Create a one-meg device
11 dd if=/dev/zero of=$tmpnam bs=1024 count=4096 >/dev/null 2>&1
12
13 echo $1
14
15 case $1 in
16         */cmd)
17                 # Test with export specified on command line
18                 ./nbd-server -C /dev/null -p ${pidfile} 11111 $tmpnam &
19                 # -p only works if nbd-server wasn't compiled with -DNOFORK or
20                 # -DNODAEMON, which I sometimes do for testing and debugging.
21                 PID=$!
22                 sleep 1
23                 ./nbd-tester-client 127.0.0.1 11111
24                 retval=$?
25         ;;
26         */cfgsize)
27                 # Test oversized requests
28                 ./nbd-server -C /dev/null -p ${pidfile} 11111 $tmpnam &
29                 # -p only works if nbd-server wasn't compiled with -DNOFORK or
30                 # -DNODAEMON, which I sometimes do for testing and debugging.
31                 PID=$!
32                 sleep 1
33                 ./nbd-tester-client 127.0.0.1 11111 -o
34                 retval=$?
35         ;;
36         */cfg1)
37                 # Test with export specified in config file
38                 cat > ${conffile} <<EOF
39 [generic]
40         oldstyle = true
41 [export]
42         exportname = $tmpnam
43         port = 11112
44 EOF
45                 ./nbd-server -C ${conffile} -p ${pidfile} &
46                 PID=$!
47                 sleep 1
48                 ./nbd-tester-client 127.0.0.1 11112
49                 retval=$?
50         ;;
51         */cfgmulti)
52                 # Test with multiple exports specified in config file, and
53                 # testing more options too
54                 cat >${conffile} <<EOF
55 [generic]
56         oldstyle = true
57 [export1]
58         exportname = $tmpnam
59         port = 11113
60         copyonwrite = true
61         listenaddr = 127.0.0.1
62 [export2]
63         exportname = $tmpnam
64         port = 11114
65         readonly = true
66         listenaddr = 127.0.0.1
67 EOF
68                 ./nbd-server -C ${conffile} -p ${pidfile} &
69                 PID=$!
70                 sleep 1
71                 ./nbd-tester-client localhost 11113
72                 retval=$?
73                 if [ $retval -ne 0 ]
74                 then
75                         if [ -f ${pidfile} ]
76                         then
77                                 kill `cat ${pidfile}`
78                                 rm -f ${pidfile}
79                         else
80                                 kill $PID
81                         fi
82                         if [ -z "$2" ]
83                         then
84                                 rm -f $tmpnam ${conffile}
85                         fi
86                         exit $retval
87                 fi
88                 ./nbd-tester-client localhost 11114
89                 retval=$?
90         ;;
91         */cfgnew)
92                 # Test new-style exports
93                 cat >${conffile} <<EOF
94 [generic]
95 [export1]
96         exportname = $tmpnam
97 EOF
98                 ./nbd-server -C ${conffile} -p ${pidfile} &
99                 PID=$!
100                 sleep 1
101                 ./nbd-tester-client localhost -N export1
102                 retval=$?
103         ;;
104         */write)
105                 # Test writing
106                 cat >${conffile} <<EOF
107 [generic]
108 [export1]
109         exportname = $tmpnam
110 EOF
111                 ./nbd-server -C ${conffile} -p ${pidfile} &
112                 PID=$!
113                 sleep 1
114                 ./nbd-tester-client localhost -N export1 -w
115                 retval=$?
116         ;;
117         */flush)
118                 # Test writes with flush
119                 cat >${conffile} <<EOF
120 [generic]
121 [export1]
122         exportname = $tmpnam
123         flush = true
124         fua = true
125         rotational = true
126 EOF
127                 ./nbd-server -C ${conffile} -p ${pidfile} &
128                 PID=$!
129                 sleep 1
130                 ./nbd-tester-client localhost -N export1 -w -f
131                 retval=$?
132         ;;
133         */integrity)
134                 # Integrity test
135                 cat >${conffile} <<EOF
136 [generic]
137 [export1]
138         exportname = $tmpnam
139         flush = true
140         fua = true
141         rotational = true
142 EOF
143                 # we need a bigger disk
144                 dd if=/dev/zero of=$tmpnam bs=1M count=50 >/dev/null 2>&1
145                 ./nbd-server -C ${conffile} -p ${pidfile} &
146                 PID=$!
147                 sleep 1
148                 ./nbd-tester-client localhost -N export1 -i
149                 retval=$?
150         ;;
151         *)
152                 echo "E: unknown test $1"
153                 exit 1
154         ;;
155 esac
156 if [ -f ${pidfile} ]
157 then
158         kill `cat ${pidfile}`
159         rm -f ${pidfile}
160 else
161         kill $PID
162 fi
163 if [ -z "$2" ]
164 then
165         rm -f $tmpnam ${conffile}
166 fi
167 if [ $retval -ne 0 ]
168 then
169         exit $retval
170 fi