ovsdb: Compact databases online automatically and on-demand.
[sliver-openvswitch.git] / tests / ovsdb-server.at
1 AT_BANNER([OVSDB -- ovsdb-server transactions (Unix sockets)])
2
3 m4_define([OVSDB_SERVER_SHUTDOWN], 
4   [cp pid savepid
5    AT_CHECK([ovs-appctl -t $PWD/unixctl -e exit], [0], [ignore], [ignore])
6    OVS_WAIT_WHILE([kill -0 `cat savepid`], [kill `cat savepid`])])
7
8 # OVSDB_CHECK_EXECUTION(TITLE, SCHEMA, TRANSACTIONS, OUTPUT, [KEYWORDS])
9 #
10 # Creates a database with the given SCHEMA, starts an ovsdb-server on
11 # that database, and runs each of the TRANSACTIONS (which should be a
12 # quoted list of quoted strings) against it with ovsdb-client one at a
13 # time.
14 #
15 # Checks that the overall output is OUTPUT, but UUIDs in the output
16 # are replaced by markers of the form <N> where N is a number.  The
17 # first unique UUID is replaced by <0>, the next by <1>, and so on.
18 # If a given UUID appears more than once it is always replaced by the
19 # same marker.
20 #
21 # TITLE is provided to AT_SETUP and KEYWORDS to AT_KEYWORDS.
22 m4_define([OVSDB_CHECK_EXECUTION], 
23   [AT_SETUP([$1])
24    AT_KEYWORDS([ovsdb server positive unix $5])
25    AT_DATA([schema], [$2
26 ])
27    AT_CHECK([ovsdb-tool create db schema], [0], [stdout], [ignore])
28    AT_CHECK([ovsdb-server --detach --pidfile=$PWD/pid --remote=punix:socket --unixctl=$PWD/unixctl db], [0], [ignore], [ignore])
29    m4_foreach([txn], [$3], 
30      [AT_CHECK([ovsdb-client transact unix:socket 'txn'], [0], [stdout], [ignore],
31      [test ! -e pid || kill `cat pid`])
32 cat stdout >> output
33 ])
34    AT_CHECK([perl $srcdir/uuidfilt.pl output], [0], [$4], [ignore],
35             [test ! -e pid || kill `cat pid`])
36    OVSDB_SERVER_SHUTDOWN
37    AT_CLEANUP])
38
39 EXECUTION_EXAMPLES
40 \f
41 AT_SETUP([database multiplexing implementation])
42 AT_KEYWORDS([ovsdb server positive])
43 AT_DATA([schema], [ORDINAL_SCHEMA
44 ])
45 AT_CHECK([ovsdb-tool create db schema], [0], [ignore], [ignore])
46 AT_CHECK([ovsdb-server --detach --pidfile=$PWD/pid --unixctl=$PWD/unixctl --remote=punix:socket db], [0], [ignore], [ignore])
47 AT_CHECK(
48   [[ovsdb-client list-dbs unix:socket]], 
49   [0], [ordinals
50 ], [ignore], [test ! -e pid || kill `cat pid`])
51 AT_CHECK(
52   [[ovsdb-client get-schema unix:socket nonexistent]], 
53   [1], [], [[ovsdb-client: syntax "{"syntax":"[\"nonexistent\"]","details":"get_schema request specifies unknown database nonexistent","error":"unknown database"}": syntax error: Parsing database schema failed: Required 'name' member is missing.
54 ]], [test ! -e pid || kill `cat pid`])
55 OVSDB_SERVER_SHUTDOWN
56 AT_CLEANUP
57
58 AT_SETUP([--remote=db: implementation])
59 AT_KEYWORDS([ovsdb server positive])
60 AT_DATA([schema],
61   [[{"name": "mydb",
62      "tables": {
63        "Manager": {
64          "columns": {
65            "manager": {"type": "string"}}}}}
66 ]])
67 AT_CHECK([ovsdb-tool create db schema], [0], [ignore], [ignore])
68 AT_CHECK(
69   [[ovsdb-tool transact db \
70      '["mydb",
71        {"op": "insert",
72         "table": "Manager",
73         "row": {"manager": "punix:socket"}}]']], [0], [ignore], [ignore])
74 AT_CHECK([ovsdb-server --detach --pidfile=$PWD/pid --remote=db:Manager,manager --unixctl=$PWD/unixctl db], [0], [ignore], [ignore])
75 AT_CHECK(
76   [[ovsdb-client transact unix:socket \
77      '["mydb",
78        {"op": "select",
79         "table": "Manager",
80         "where": [],
81         "columns": ["manager"]}]']], 
82   [0], [stdout], [ignore], [test ! -e pid || kill `cat pid`])
83 AT_CHECK(
84   [perl $srcdir/uuidfilt.pl stdout], 
85   [0], 
86   [[[{"rows":[{"manager":"punix:socket"}]}]
87 ]], 
88   [ignore], 
89   [test ! -e pid || kill `cat pid`])
90 OVSDB_SERVER_SHUTDOWN
91 AT_CLEANUP
92
93 AT_SETUP([compacting online])
94 AT_KEYWORDS([ovsdb server compact])
95 AT_DATA([schema], [ORDINAL_SCHEMA
96 ])
97 touch .db.~lock~
98 AT_CHECK([ovsdb-tool create db schema], [0], [ignore], [ignore])
99 AT_CHECK([ovsdb-server --detach --pidfile=$PWD/pid --unixctl=$PWD/unixctl --remote=punix:socket --log-file=$PWD/ovsdb-server.log db], [0], [ignore], [ignore])
100 AT_CAPTURE_FILE([ovsdb-server.log])
101 dnl Do a bunch of random transactions that put crap in the database log.
102 AT_CHECK(
103   [[for pair in 'zero 0' 'one 1' 'two 2' 'three 3' 'four 4' 'five 5'; do
104       set -- $pair
105       ovsdb-client transact unix:socket '
106         ["ordinals",
107          {"op": "insert",
108           "table": "ordinals",
109           "row": {"name": "'$1'", "number": '$2'}},
110          {"op": "comment",
111           "comment": "add row for '"$pair"'"}]'
112       ovsdb-client transact unix:socket '
113         ["ordinals",
114          {"op": "delete",
115           "table": "ordinals",
116           "where": [["number", "==", '$2']]},
117          {"op": "comment",
118           "comment": "delete row for '"$2"'"}]'
119       ovsdb-client transact unix:socket '
120         ["ordinals",
121          {"op": "insert",
122           "table": "ordinals",
123           "row": {"name": "'$1'", "number": '$2'}},
124          {"op": "comment",
125           "comment": "add back row for '"$pair"'"}]'
126     done]],
127   [0], [stdout], [ignore], [test ! -e pid || kill `cat pid`])
128 dnl Check that all the crap is in fact in the database log.
129 AT_CHECK([[perl $srcdir/uuidfilt.pl db | grep -v ^OVSDB | sed 's/"_date":[0-9]*/"_date":0/']], [0],
130   [[{"name":"ordinals","tables":{"ordinals":{"columns":{"name":{"type":"string"},"number":{"type":"integer"}}}}}
131 {"ordinals":{"<0>":{"name":"zero"}},"_comment":"add row for zero 0","_date":0}
132 {"ordinals":{"<0>":null},"_comment":"delete row for 0","_date":0}
133 {"ordinals":{"<1>":{"name":"zero"}},"_comment":"add back row for zero 0","_date":0}
134 {"ordinals":{"<2>":{"number":1,"name":"one"}},"_comment":"add row for one 1","_date":0}
135 {"ordinals":{"<2>":null},"_comment":"delete row for 1","_date":0}
136 {"ordinals":{"<3>":{"number":1,"name":"one"}},"_comment":"add back row for one 1","_date":0}
137 {"ordinals":{"<4>":{"number":2,"name":"two"}},"_comment":"add row for two 2","_date":0}
138 {"ordinals":{"<4>":null},"_comment":"delete row for 2","_date":0}
139 {"ordinals":{"<5>":{"number":2,"name":"two"}},"_comment":"add back row for two 2","_date":0}
140 {"ordinals":{"<6>":{"number":3,"name":"three"}},"_comment":"add row for three 3","_date":0}
141 {"ordinals":{"<6>":null},"_comment":"delete row for 3","_date":0}
142 {"ordinals":{"<7>":{"number":3,"name":"three"}},"_comment":"add back row for three 3","_date":0}
143 {"ordinals":{"<8>":{"number":4,"name":"four"}},"_comment":"add row for four 4","_date":0}
144 {"ordinals":{"<8>":null},"_comment":"delete row for 4","_date":0}
145 {"ordinals":{"<9>":{"number":4,"name":"four"}},"_comment":"add back row for four 4","_date":0}
146 {"ordinals":{"<10>":{"number":5,"name":"five"}},"_comment":"add row for five 5","_date":0}
147 {"ordinals":{"<10>":null},"_comment":"delete row for 5","_date":0}
148 {"ordinals":{"<11>":{"number":5,"name":"five"}},"_comment":"add back row for five 5","_date":0}
149 ]], [], [test ! -e pid || kill `cat pid`])
150 dnl Dump out and check the actual database contents.
151 AT_CHECK([[ovsdb-client dump unix:socket ordinals]],
152   [0], [stdout], [ignore])
153 AT_CHECK([perl $srcdir/uuidfilt.pl stdout], [0],
154   [_uuid                                name  number
155 ------------------------------------ ----- ------
156 <0> five  5     @&t@
157 <1> four  4     @&t@
158 <2> one   1     @&t@
159 <3> three 3     @&t@
160 <4> two   2     @&t@
161 <5> zero  0     @&t@
162 ], [], [test ! -e pid || kill `cat pid`])
163 dnl Now compact the database in-place.
164 AT_CHECK([[ovs-appctl -t $PWD/unixctl ovsdb-server/compact]],
165   [0], [], [ignore], [test ! -e pid || kill `cat pid`])
166 dnl We can't fully re-check the contents of the database log, because the
167 dnl order of the records is not predictable, but there should only be 4 lines
168 dnl in it now.
169 AT_CAPTURE_FILE([db])
170 AT_CHECK([wc -l < db], [0], [4
171 ], [], [test ! -e pid || kill `cat pid`])
172 dnl And check that the dumped data is the same too:
173 AT_CHECK([ovsdb-client dump unix:socket ordinals], [0], [stdout], [ignore],
174   [test ! -e pid || kill `cat pid`])
175 AT_CHECK([perl $srcdir/uuidfilt.pl stdout], [0],
176   [_uuid                                name  number
177 ------------------------------------ ----- ------
178 <0> five  5     @&t@
179 <1> four  4     @&t@
180 <2> one   1     @&t@
181 <3> three 3     @&t@
182 <4> two   2     @&t@
183 <5> zero  0     @&t@
184 ], [], [test ! -e pid || kill `cat pid`])
185 dnl Now do some more transactions.
186 AT_CHECK(
187   [[ovsdb-client transact unix:socket '
188      ["ordinals",
189       {"op": "delete",
190        "table": "ordinals",
191        "where": [["number", "<", 3]]}]']],
192   [0], [[[{"count":3}]
193 ]], [ignore], [test ! -e pid || kill `cat pid`])
194 dnl There should be 6 lines in the log now.
195 AT_CHECK([wc -l < db], [0], [6
196 ], [], [test ! -e pid || kill `cat pid`])
197 dnl Then check that the dumped data is correct.
198 AT_CHECK([ovsdb-client dump unix:socket ordinals], [0], [stdout], [ignore],
199   [test ! -e pid || kill `cat pid`])
200 AT_CHECK([perl $srcdir/uuidfilt.pl stdout], [0],
201   [_uuid                                name  number
202 ------------------------------------ ----- ------
203 <0> five  5     @&t@
204 <1> four  4     @&t@
205 <2> three 3     @&t@
206 ], [], [test ! -e pid || kill `cat pid`])
207 OVSDB_SERVER_SHUTDOWN
208 AT_CLEANUP
209 \f
210 AT_BANNER([OVSDB -- ovsdb-server transactions (SSL sockets)])
211
212 # OVSDB_CHECK_EXECUTION(TITLE, SCHEMA, TRANSACTIONS, OUTPUT, [KEYWORDS])
213 #
214 # Creates a database with the given SCHEMA, starts an ovsdb-server on
215 # that database, and runs each of the TRANSACTIONS (which should be a
216 # quoted list of quoted strings) against it with ovsdb-client one at a
217 # time.
218 #
219 # Checks that the overall output is OUTPUT, but UUIDs in the output
220 # are replaced by markers of the form <N> where N is a number.  The
221 # first unique UUID is replaced by <0>, the next by <1>, and so on.
222 # If a given UUID appears more than once it is always replaced by the
223 # same marker.
224 #
225 # TITLE is provided to AT_SETUP and KEYWORDS to AT_KEYWORDS.
226 m4_define([OVSDB_CHECK_EXECUTION], 
227   [AT_SETUP([$1])
228    AT_KEYWORDS([ovsdb server positive ssl $5])
229    AT_SKIP_IF([test "$HAVE_OPENSSL" = no])
230    AT_SKIP_IF([test "x$RANDOM" = x])
231    AT_DATA([schema], [$2
232 ])
233    SSL_PORT=`expr 32767 + \( $RANDOM % 32767 \)`
234    PKIDIR=$abs_top_srcdir/tests
235    AT_CHECK([ovsdb-tool create db schema], [0], [stdout], [ignore])
236    AT_CHECK([ovsdb-server --detach --pidfile=$PWD/pid --private-key=$PKIDIR/testpki-privkey2.pem --certificate=$PKIDIR/testpki-cert2.pem --ca-cert=$PKIDIR/testpki-cacert.pem --remote=pssl:$SSL_PORT:127.0.0.1 --unixctl=$PWD/unixctl db], [0], [ignore], [ignore])
237    m4_foreach([txn], [$3], 
238      [AT_CHECK([ovsdb-client --private-key=$PKIDIR/testpki-privkey.pem --certificate=$PKIDIR/testpki-cert.pem --ca-cert=$PKIDIR/testpki-cacert.pem transact ssl:127.0.0.1:$SSL_PORT 'txn'], [0], [stdout], [ignore],
239      [test ! -e pid || kill `cat pid`])
240 cat stdout >> output
241 ])
242    AT_CHECK([perl $srcdir/uuidfilt.pl output], [0], [$4], [ignore],
243             [test ! -e pid || kill `cat pid`])
244    OVSDB_SERVER_SHUTDOWN
245    AT_CLEANUP])
246
247 EXECUTION_EXAMPLES
248 \f
249 AT_BANNER([OVSDB -- transactions on transient ovsdb-server])
250
251 # OVSDB_CHECK_EXECUTION(TITLE, SCHEMA, TRANSACTIONS, OUTPUT, [KEYWORDS])
252 #
253 # Creates a database with the given SCHEMA and runs each of the
254 # TRANSACTIONS (which should be a quoted list of quoted strings)
255 # against it with ovsdb-client one at a time.  Each ovsdb-client
256 # is run against a separately started ovsdb-server that executes
257 # only that single transaction.  (The idea is that this should
258 # help to ferret out any differences between what ovsdb-server has
259 # in memory and what actually gets committed to disk.)
260 #
261 # Checks that the overall output is OUTPUT, but UUIDs in the output
262 # are replaced by markers of the form <N> where N is a number.  The
263 # first unique UUID is replaced by <0>, the next by <1>, and so on.
264 # If a given UUID appears more than once it is always replaced by the
265 # same marker.
266 #
267 # TITLE is provided to AT_SETUP and KEYWORDS to AT_KEYWORDS.
268 m4_define([OVSDB_CHECK_EXECUTION], 
269   [AT_SETUP([$1])
270    AT_KEYWORDS([ovsdb server positive transient $5])
271    AT_DATA([schema], [$2
272 ])
273    AT_CHECK([ovsdb-tool create db schema], [0], [stdout], [ignore])
274    m4_foreach([txn], [$3], 
275      [AT_DATA([txnfile], [ovsdb-client transact unix:socket 'txn'
276 ])
277       AT_CHECK([ovsdb-server --remote=punix:socket --unixctl=$PWD/unixctl db --run="sh txnfile"], [0], [stdout], [ignore])
278       cat stdout >> output
279 ])
280    AT_CHECK([perl $srcdir/uuidfilt.pl output], [0], [$4], [ignore])
281    AT_CLEANUP])
282
283 EXECUTION_EXAMPLES