1ed6157835e79d8ea16cc6ddd3f2e5067ee44b56
[awesomized/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 world= new Framework();
153
154 if (not world)
155 {
156 return EXIT_FAILURE;
157 }
158
159 libtest::SignalThread signal;
160 if (not signal.setup())
161 {
162 return EXIT_FAILURE;
163 }
164
165 Stats stats;
166
167 get_world(world);
168
169 test_return_t error;
170 void *creators_ptr= world->create(error);
171
172 switch (error)
173 {
174 case TEST_SUCCESS:
175 break;
176
177 case TEST_SKIPPED:
178 Out << "SKIP " << argv[0];
179 delete world;
180 return EXIT_SUCCESS;
181
182 case TEST_FATAL:
183 case TEST_FAILURE:
184 case TEST_MEMORY_ALLOCATION_FAILURE:
185 Error << argv[0] << "create() failed";
186 delete world;
187 return EXIT_FAILURE;
188 }
189
190 char *collection_to_run= NULL;
191 if (argc > 1)
192 {
193 collection_to_run= argv[1];
194 }
195 else if (getenv("TEST_COLLECTION"))
196 {
197 collection_to_run= getenv("TEST_COLLECTION");
198 }
199
200 if (collection_to_run)
201 {
202 Out << "Only testing " << collection_to_run;
203 }
204
205 char *wildcard= NULL;
206 if (argc == 3)
207 {
208 wildcard= argv[2];
209 }
210
211 for (collection_st *next= world->collections; next->name and (not signal.is_shutdown()); next++)
212 {
213 test_return_t collection_rc= TEST_SUCCESS;
214 bool failed= false;
215 bool skipped= false;
216
217 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
218 continue;
219
220 stats.collection_total++;
221
222 collection_rc= world->startup(creators_ptr);
223
224 if (collection_rc == TEST_SUCCESS and next->pre)
225 {
226 collection_rc= world->runner()->pre(next->pre, creators_ptr);
227 }
228
229 switch (collection_rc)
230 {
231 case TEST_SUCCESS:
232 break;
233
234 case TEST_FATAL:
235 case TEST_FAILURE:
236 Out << next->name << " [ failed ]";
237 failed= true;
238 signal.set_shutdown(SHUTDOWN_GRACEFUL);
239 goto cleanup;
240
241 case TEST_SKIPPED:
242 Out << next->name << " [ skipping ]";
243 skipped= true;
244 goto cleanup;
245
246 case TEST_MEMORY_ALLOCATION_FAILURE:
247 test_assert(0, "Allocation failure, or unknown return");
248 }
249
250 Out << "Collection: " << next->name;
251
252 for (test_st *run= next->tests; run->name; run++)
253 {
254 struct timeval start_time, end_time;
255 long int load_time= 0;
256
257 if (wildcard && fnmatch(wildcard, run->name, 0))
258 {
259 continue;
260 }
261
262 test_return_t return_code;
263 if (test_success(return_code= world->item.startup(creators_ptr)))
264 {
265 if (test_success(return_code= world->item.flush(creators_ptr, run)))
266 {
267 // @note pre will fail is SKIPPED is returned
268 if (test_success(return_code= world->item.pre(creators_ptr)))
269 {
270 { // Runner Code
271 gettimeofday(&start_time, NULL);
272 assert(world->runner());
273 assert(run->test_fn);
274 return_code= world->runner()->run(run->test_fn, creators_ptr);
275 gettimeofday(&end_time, NULL);
276 load_time= timedif(end_time, start_time);
277 }
278 }
279
280 // @todo do something if post fails
281 (void)world->item.post(creators_ptr);
282 }
283 else if (return_code == TEST_SKIPPED)
284 { }
285 else if (return_code == TEST_FAILURE)
286 {
287 Error << " item.flush(failure)";
288 signal.set_shutdown(SHUTDOWN_GRACEFUL);
289 }
290 }
291 else if (return_code == TEST_SKIPPED)
292 { }
293 else if (return_code == TEST_FAILURE)
294 {
295 Error << " item.startup(failure)";
296 signal.set_shutdown(SHUTDOWN_GRACEFUL);
297 }
298
299 stats.total++;
300
301 switch (return_code)
302 {
303 case TEST_SUCCESS:
304 Out << "\tTesting " << run->name << "\t\t\t\t\t" << load_time / 1000 << "." << load_time % 1000 << "[ " << test_strerror(return_code) << " ]";
305 stats.success++;
306 break;
307
308 case TEST_FATAL:
309 case TEST_FAILURE:
310 stats.failed++;
311 failed= true;
312 Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
313 break;
314
315 case TEST_SKIPPED:
316 stats.skipped++;
317 skipped= true;
318 Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
319 break;
320
321 case TEST_MEMORY_ALLOCATION_FAILURE:
322 test_assert(0, "Memory Allocation Error");
323 }
324
325 if (test_failed(world->on_error(return_code, creators_ptr)))
326 {
327 Error << "Failed while running on_error()";
328 signal.set_shutdown(SHUTDOWN_GRACEFUL);
329 break;
330 }
331 }
332
333 (void) world->runner()->post(next->post, creators_ptr);
334
335 cleanup:
336 if (failed == false and skipped == false)
337 {
338 stats.collection_success++;
339 }
340
341 if (failed)
342 {
343 stats.collection_failed++;
344 }
345
346 if (skipped)
347 {
348 stats.collection_skipped++;
349 }
350
351 world->shutdown(creators_ptr);
352 Outn();
353 }
354
355 if (not signal.is_shutdown())
356 {
357 signal.set_shutdown(SHUTDOWN_GRACEFUL);
358 }
359
360 int exit_code= EXIT_SUCCESS;
361 shutdown_t status= signal.get_shutdown();
362 if (status == SHUTDOWN_FORCED)
363 {
364 Out << "Tests were aborted.";
365 exit_code= EXIT_FAILURE;
366 }
367 else if (stats.collection_failed)
368 {
369 Out << "Some test failed.";
370 exit_code= EXIT_FAILURE;
371 }
372 else if (stats.collection_skipped and stats.collection_failed and stats.collection_success)
373 {
374 Out << "Some tests were skipped.";
375 }
376 else if (stats.collection_success and stats.collection_failed == 0)
377 {
378 Out << "All tests completed successfully.";
379 }
380
381 stats_print(&stats);
382
383 delete world;
384
385 Outn(); // Generate a blank to break up the messages if make check/test has been run
386
387 return exit_code;
388 }