Fix test cases that did not work if memcached was not available.
[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 #ifndef __INTEL_COMPILER
39 #pragma GCC diagnostic ignored "-Wold-style-cast"
40 #endif
41
42 using namespace libtest;
43
44 static void stats_print(Stats *stats)
45 {
46 if (stats->collection_failed == 0 and stats->collection_success == 0)
47 {
48 return;
49 }
50
51 Out << "\tTotal Collections\t\t\t\t" << stats->collection_total;
52 Out << "\tFailed Collections\t\t\t\t" << stats->collection_failed;
53 Out << "\tSkipped Collections\t\t\t\t" << stats->collection_skipped;
54 Out << "\tSucceeded Collections\t\t\t\t" << stats->collection_success;
55 Outn();
56 Out << "Total\t\t\t\t" << stats->total;
57 Out << "\tFailed\t\t\t" << stats->failed;
58 Out << "\tSkipped\t\t\t" << stats->skipped;
59 Out << "\tSucceeded\t\t" << stats->success;
60 }
61
62 static long int timedif(struct timeval a, struct timeval b)
63 {
64 long us, s;
65
66 us = (long)(a.tv_usec - b.tv_usec);
67 us /= 1000;
68 s = (long)(a.tv_sec - b.tv_sec);
69 s *= 1000;
70 return s + us;
71 }
72
73 static Framework *world= NULL;
74 int main(int argc, char *argv[])
75 {
76 srandom((unsigned int)time(NULL));
77
78 if (getenv("LIBTEST_QUIET"))
79 {
80 close(STDOUT_FILENO);
81 }
82
83 char buffer[1024];
84 if (getenv("LIBTEST_TMP"))
85 {
86 snprintf(buffer, sizeof(buffer), "%s", getenv("LIBTEST_TMP"));
87 }
88 else
89 {
90 snprintf(buffer, sizeof(buffer), "%s", LIBTEST_TEMP);
91 }
92
93 if (chdir(buffer) == -1)
94 {
95 char getcwd_buffer[1024];
96 char *dir= getcwd(getcwd_buffer, sizeof(getcwd_buffer));
97
98 Error << "Unable to chdir() from " << dir << " to " << buffer << " errno:" << strerror(errno);
99 return EXIT_FAILURE;
100 }
101
102 if (libtest::libtool() == NULL)
103 {
104 Error << "Failed to locate libtool";
105 return EXIT_FAILURE;
106 }
107
108 world= new Framework();
109
110 if (world == NULL)
111 {
112 Error << "Failed to create Framework()";
113 return EXIT_FAILURE;
114 }
115
116 libtest::SignalThread signal;
117 if (not signal.setup())
118 {
119 return EXIT_FAILURE;
120 }
121
122 Stats stats;
123
124 get_world(world);
125
126 test_return_t error;
127 void *creators_ptr= world->create(error);
128
129 switch (error)
130 {
131 case TEST_SUCCESS:
132 break;
133
134 case TEST_SKIPPED:
135 Out << "SKIP " << argv[0];
136 delete world;
137 return EXIT_SUCCESS;
138
139 case TEST_FATAL:
140 case TEST_FAILURE:
141 case TEST_MEMORY_ALLOCATION_FAILURE:
142 delete world;
143 return EXIT_FAILURE;
144 }
145
146 char *collection_to_run= NULL;
147 if (argc > 1)
148 {
149 collection_to_run= argv[1];
150 }
151 else if (getenv("TEST_COLLECTION"))
152 {
153 collection_to_run= getenv("TEST_COLLECTION");
154 }
155
156 if (collection_to_run)
157 {
158 Out << "Only testing " << collection_to_run;
159 }
160
161 char *wildcard= NULL;
162 if (argc == 3)
163 {
164 wildcard= argv[2];
165 }
166
167 for (collection_st *next= world->collections; next->name and (not signal.is_shutdown()); next++)
168 {
169 test_return_t collection_rc= TEST_SUCCESS;
170 bool failed= false;
171 bool skipped= false;
172
173 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
174 continue;
175
176 stats.collection_total++;
177
178 collection_rc= world->startup(creators_ptr);
179
180 if (collection_rc == TEST_SUCCESS and next->pre)
181 {
182 collection_rc= world->runner()->pre(next->pre, creators_ptr);
183 }
184
185 switch (collection_rc)
186 {
187 case TEST_SUCCESS:
188 break;
189
190 case TEST_FATAL:
191 case TEST_FAILURE:
192 Out << next->name << " [ failed ]";
193 failed= true;
194 signal.set_shutdown(SHUTDOWN_GRACEFUL);
195 goto cleanup;
196
197 case TEST_SKIPPED:
198 Out << next->name << " [ skipping ]";
199 skipped= true;
200 goto cleanup;
201
202 case TEST_MEMORY_ALLOCATION_FAILURE:
203 test_assert(0, "Allocation failure, or unknown return");
204 }
205
206 Out << "Collection: " << next->name;
207
208 for (test_st *run= next->tests; run->name; run++)
209 {
210 struct timeval start_time, end_time;
211 long int load_time= 0;
212
213 if (wildcard && fnmatch(wildcard, run->name, 0))
214 {
215 continue;
216 }
217
218 test_return_t return_code;
219 if (test_success(return_code= world->item.startup(creators_ptr)))
220 {
221 if (test_success(return_code= world->item.flush(creators_ptr, run)))
222 {
223 // @note pre will fail is SKIPPED is returned
224 if (test_success(return_code= world->item.pre(creators_ptr)))
225 {
226 { // Runner Code
227 gettimeofday(&start_time, NULL);
228 assert(world->runner());
229 assert(run->test_fn);
230 return_code= world->runner()->run(run->test_fn, creators_ptr);
231 gettimeofday(&end_time, NULL);
232 load_time= timedif(end_time, start_time);
233 }
234 }
235
236 // @todo do something if post fails
237 (void)world->item.post(creators_ptr);
238 }
239 else if (return_code == TEST_SKIPPED)
240 { }
241 else if (return_code == TEST_FAILURE)
242 {
243 Error << " item.flush(failure)";
244 signal.set_shutdown(SHUTDOWN_GRACEFUL);
245 }
246 }
247 else if (return_code == TEST_SKIPPED)
248 { }
249 else if (return_code == TEST_FAILURE)
250 {
251 Error << " item.startup(failure)";
252 signal.set_shutdown(SHUTDOWN_GRACEFUL);
253 }
254
255 stats.total++;
256
257 switch (return_code)
258 {
259 case TEST_SUCCESS:
260 Out << "\tTesting " << run->name << "\t\t\t\t\t" << load_time / 1000 << "." << load_time % 1000 << "[ " << test_strerror(return_code) << " ]";
261 stats.success++;
262 break;
263
264 case TEST_FATAL:
265 case TEST_FAILURE:
266 stats.failed++;
267 failed= true;
268 Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
269 break;
270
271 case TEST_SKIPPED:
272 stats.skipped++;
273 skipped= true;
274 Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
275 break;
276
277 case TEST_MEMORY_ALLOCATION_FAILURE:
278 test_assert(0, "Memory Allocation Error");
279 }
280
281 if (test_failed(world->on_error(return_code, creators_ptr)))
282 {
283 Error << "Failed while running on_error()";
284 signal.set_shutdown(SHUTDOWN_GRACEFUL);
285 break;
286 }
287 }
288
289 (void) world->runner()->post(next->post, creators_ptr);
290
291 cleanup:
292 if (failed == false and skipped == false)
293 {
294 stats.collection_success++;
295 }
296
297 if (failed)
298 {
299 stats.collection_failed++;
300 }
301
302 if (skipped)
303 {
304 stats.collection_skipped++;
305 }
306
307 world->shutdown(creators_ptr);
308 Outn();
309 }
310
311 if (not signal.is_shutdown())
312 {
313 signal.set_shutdown(SHUTDOWN_GRACEFUL);
314 }
315
316 int exit_code= EXIT_SUCCESS;
317 shutdown_t status= signal.get_shutdown();
318 if (status == SHUTDOWN_FORCED)
319 {
320 Out << "Tests were aborted.";
321 exit_code= EXIT_FAILURE;
322 }
323 else if (stats.collection_failed)
324 {
325 Out << "Some test failed.";
326 exit_code= EXIT_FAILURE;
327 }
328 else if (stats.collection_skipped and stats.collection_failed and stats.collection_success)
329 {
330 Out << "Some tests were skipped.";
331 }
332 else if (stats.collection_success and stats.collection_failed == 0)
333 {
334 Out << "All tests completed successfully.";
335 }
336
337 stats_print(&stats);
338
339 delete world;
340
341 Outn(); // Generate a blank to break up the messages if make check/test has been run
342
343 return exit_code;
344 }