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