Merge in version/etc libtest
[m6w6/libmemcached] / libtest / test.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <libtest/common.h>
23
24 #include <cassert>
25 #include <cstdlib>
26 #include <cstring>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32 #include <ctime>
33 #include <fnmatch.h>
34 #include <iostream>
35
36 #include <signal.h>
37
38 #include <libtest/stats.h>
39 #include <libtest/signal.h>
40
41 #ifndef __INTEL_COMPILER
42 #pragma GCC diagnostic ignored "-Wold-style-cast"
43 #endif
44
45 using namespace libtest;
46
47 static in_port_t global_port= 0;
48 static char global_socket[1024];
49
50 in_port_t default_port()
51 {
52 return global_port;
53 }
54
55 void set_default_port(in_port_t port)
56 {
57 global_port= port;
58 }
59
60 const char *default_socket()
61 {
62 assert(global_socket[0]);
63 return global_socket;
64 }
65
66 bool test_is_local()
67 {
68 return (getenv("LIBTEST_LOCAL"));
69 }
70
71 void set_default_socket(const char *socket)
72 {
73 if (socket)
74 {
75 strncpy(global_socket, socket, strlen(socket));
76 }
77 }
78
79 static void stats_print(Stats *stats)
80 {
81 if (stats->collection_failed == 0 and stats->collection_success == 0)
82 {
83 return;
84 }
85
86 Out << "\tTotal Collections\t\t\t\t" << stats->collection_total;
87 Out << "\tFailed Collections\t\t\t\t" << stats->collection_failed;
88 Out << "\tSkipped Collections\t\t\t\t" << stats->collection_skipped;
89 Out << "\tSucceeded Collections\t\t\t\t" << stats->collection_success;
90 Outn();
91 Out << "Total\t\t\t\t" << stats->total;
92 Out << "\tFailed\t\t\t" << stats->failed;
93 Out << "\tSkipped\t\t\t" << stats->skipped;
94 Out << "\tSucceeded\t\t" << stats->success;
95 }
96
97 static long int timedif(struct timeval a, struct timeval b)
98 {
99 long us, s;
100
101 us = (long)(a.tv_usec - b.tv_usec);
102 us /= 1000;
103 s = (long)(a.tv_sec - b.tv_sec);
104 s *= 1000;
105 return s + us;
106 }
107
108 const char *test_strerror(test_return_t code)
109 {
110 switch (code) {
111 case TEST_SUCCESS:
112 return "ok";
113
114 case TEST_FAILURE:
115 return "failed";
116
117 case TEST_MEMORY_ALLOCATION_FAILURE:
118 return "memory allocation";
119
120 case TEST_SKIPPED:
121 return "skipped";
122
123 case TEST_FATAL:
124 break;
125 }
126
127 return "failed";
128 }
129
130 void create_core(void)
131 {
132 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL)
133 {
134 pid_t pid= fork();
135
136 if (pid == 0)
137 {
138 abort();
139 }
140 else
141 {
142 while (waitpid(pid, NULL, 0) != pid) {};
143 }
144 }
145 }
146
147 static Framework *world= NULL;
148 int main(int argc, char *argv[])
149 {
150 srandom((unsigned int)time(NULL));
151
152 if (getenv("srcdir"))
153 {
154 char buffer[1024];
155 snprintf(buffer, sizeof(buffer), "%s/%s", getenv("srcdir"), "tests");
156 chdir(buffer);
157 }
158 else
159 {
160 chdir("tests");
161 }
162
163 world= new Framework();
164
165 if (not world)
166 {
167 return EXIT_FAILURE;
168 }
169
170 libtest::SignalThread signal;
171 if (not signal.setup())
172 {
173 return EXIT_FAILURE;
174 }
175
176 Stats stats;
177
178 get_world(world);
179
180 test_return_t error;
181 void *creators_ptr= world->create(error);
182
183 switch (error)
184 {
185 case TEST_SUCCESS:
186 break;
187
188 case TEST_SKIPPED:
189 Out << "SKIP " << argv[0];
190 delete world;
191 return EXIT_SUCCESS;
192
193 case TEST_FATAL:
194 case TEST_FAILURE:
195 case TEST_MEMORY_ALLOCATION_FAILURE:
196 Error << argv[0] << "create() failed";
197 delete world;
198 return EXIT_FAILURE;
199 }
200
201 char *collection_to_run= NULL;
202 if (argc > 1)
203 {
204 collection_to_run= argv[1];
205 }
206 else if (getenv("TEST_COLLECTION"))
207 {
208 collection_to_run= getenv("TEST_COLLECTION");
209 }
210
211 if (collection_to_run)
212 {
213 Out << "Only testing " << collection_to_run;
214 }
215
216 char *wildcard= NULL;
217 if (argc == 3)
218 {
219 wildcard= argv[2];
220 }
221
222 for (collection_st *next= world->collections; next->name and (not signal.is_shutdown()); next++)
223 {
224 test_return_t collection_rc= TEST_SUCCESS;
225 bool failed= false;
226 bool skipped= false;
227
228 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
229 continue;
230
231 stats.collection_total++;
232
233 collection_rc= world->startup(creators_ptr);
234
235 if (collection_rc == TEST_SUCCESS and next->pre)
236 {
237 collection_rc= world->runner()->pre(next->pre, creators_ptr);
238 }
239
240 switch (collection_rc)
241 {
242 case TEST_SUCCESS:
243 break;
244
245 case TEST_FATAL:
246 case TEST_FAILURE:
247 Out << next->name << " [ failed ]";
248 failed= true;
249 signal.set_shutdown(SHUTDOWN_GRACEFUL);
250 goto cleanup;
251
252 case TEST_SKIPPED:
253 Out << next->name << " [ skipping ]";
254 skipped= true;
255 goto cleanup;
256
257 case TEST_MEMORY_ALLOCATION_FAILURE:
258 test_assert(0, "Allocation failure, or unknown return");
259 }
260
261 Out << "Collection: " << next->name;
262
263 for (test_st *run= next->tests; run->name; run++)
264 {
265 struct timeval start_time, end_time;
266 long int load_time= 0;
267
268 if (wildcard && fnmatch(wildcard, run->name, 0))
269 {
270 continue;
271 }
272
273 test_return_t return_code;
274 if (test_success(return_code= world->item.startup(creators_ptr)))
275 {
276 if (test_success(return_code= world->item.flush(creators_ptr, run)))
277 {
278 // @note pre will fail is SKIPPED is returned
279 if (test_success(return_code= world->item.pre(creators_ptr)))
280 {
281 { // Runner Code
282 gettimeofday(&start_time, NULL);
283 assert(world->runner());
284 assert(run->test_fn);
285 return_code= world->runner()->run(run->test_fn, creators_ptr);
286 gettimeofday(&end_time, NULL);
287 load_time= timedif(end_time, start_time);
288 }
289 }
290
291 // @todo do something if post fails
292 (void)world->item.post(creators_ptr);
293 }
294 else if (return_code == TEST_SKIPPED)
295 { }
296 else if (return_code == TEST_FAILURE)
297 {
298 Error << " item.flush(failure)";
299 signal.set_shutdown(SHUTDOWN_GRACEFUL);
300 }
301 }
302 else if (return_code == TEST_SKIPPED)
303 { }
304 else if (return_code == TEST_FAILURE)
305 {
306 Error << " item.startup(failure)";
307 signal.set_shutdown(SHUTDOWN_GRACEFUL);
308 }
309
310 stats.total++;
311
312 switch (return_code)
313 {
314 case TEST_SUCCESS:
315 Out << "\tTesting " << run->name << "\t\t\t\t\t" << load_time / 1000 << "." << load_time % 1000 << "[ " << test_strerror(return_code) << " ]";
316 stats.success++;
317 break;
318
319 case TEST_FATAL:
320 case TEST_FAILURE:
321 stats.failed++;
322 failed= true;
323 Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
324 break;
325
326 case TEST_SKIPPED:
327 stats.skipped++;
328 skipped= true;
329 Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
330 break;
331
332 case TEST_MEMORY_ALLOCATION_FAILURE:
333 test_assert(0, "Memory Allocation Error");
334 }
335
336 if (test_failed(world->on_error(return_code, creators_ptr)))
337 {
338 Error << "Failed while running on_error()";
339 signal.set_shutdown(SHUTDOWN_GRACEFUL);
340 break;
341 }
342 }
343
344 (void) world->runner()->post(next->post, creators_ptr);
345
346 cleanup:
347 if (failed == false and skipped == false)
348 {
349 stats.collection_success++;
350 }
351
352 if (failed)
353 {
354 stats.collection_failed++;
355 }
356
357 if (skipped)
358 {
359 stats.collection_skipped++;
360 }
361
362 world->shutdown(creators_ptr);
363 Outn();
364 }
365
366 if (not signal.is_shutdown())
367 {
368 signal.set_shutdown(SHUTDOWN_GRACEFUL);
369 }
370
371 int exit_code= EXIT_SUCCESS;
372 shutdown_t status= signal.get_shutdown();
373 if (status == SHUTDOWN_FORCED)
374 {
375 Out << "Tests were aborted.";
376 exit_code= EXIT_FAILURE;
377 }
378 else if (stats.collection_failed)
379 {
380 Out << "Some test failed.";
381 exit_code= EXIT_FAILURE;
382 }
383 else if (stats.collection_skipped and stats.collection_failed and stats.collection_success)
384 {
385 Out << "Some tests were skipped.";
386 }
387 else if (stats.collection_success and stats.collection_failed == 0)
388 {
389 Out << "All tests completed successfully.";
390 }
391
392 stats_print(&stats);
393
394 delete world;
395
396 Outn(); // Generate a blank to break up the messages if make check/test has been run
397
398 return exit_code;
399 }