jenkins-promote-staging-trunk-libmemcached-16
[awesomized/libmemcached] / libtest / main.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <config.h>
38 #include <libtest/common.h>
39
40 #include <cassert>
41 #include <cstdlib>
42 #include <cstring>
43 #include <ctime>
44 #include <fnmatch.h>
45 #include <iostream>
46 #include <memory>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/types.h>
50 #include <sys/wait.h>
51 #include <unistd.h>
52
53 #include <signal.h>
54
55 #ifndef __INTEL_COMPILER
56 #pragma GCC diagnostic ignored "-Wold-style-cast"
57 #endif
58
59 using namespace libtest;
60
61 static void stats_print(Framework *frame)
62 {
63 if (frame->failed() == 0 and frame->success() == 0)
64 {
65 return;
66 }
67
68 Outn();
69 Out << "Collections\t\t\t\t\t" << frame->total();
70 Out << "\tFailed\t\t\t\t\t" << frame->failed();
71 Out << "\tSkipped\t\t\t\t\t" << frame->skipped();
72 Out << "\tSucceeded\t\t\t\t" << frame->success();
73 Outn();
74 Out << "Tests\t\t\t\t\t" << frame->sum_total();
75 Out << "\tFailed\t\t\t\t" << frame->sum_failed();
76 Out << "\tSkipped\t\t\t\t" << frame->sum_skipped();
77 Out << "\tSucceeded\t\t\t" << frame->sum_success();
78 }
79
80 #include <getopt.h>
81 #include <unistd.h>
82
83 int main(int argc, char *argv[])
84 {
85 bool opt_massive= false;
86 unsigned long int opt_repeat= 1; // Run all tests once
87 bool opt_quiet= false;
88 std::string collection_to_run;
89 std::string wildcard;
90
91 // Options parsing
92 {
93 enum long_option_t {
94 OPT_LIBYATL_VERSION,
95 OPT_LIBYATL_MATCH_COLLECTION,
96 OPT_LIBYATL_MASSIVE,
97 OPT_LIBYATL_QUIET,
98 OPT_LIBYATL_MATCH_WILDCARD,
99 OPT_LIBYATL_REPEAT
100 };
101
102 static struct option long_options[]=
103 {
104 { "version", no_argument, NULL, OPT_LIBYATL_VERSION },
105 { "quiet", no_argument, NULL, OPT_LIBYATL_QUIET },
106 { "repeat", no_argument, NULL, OPT_LIBYATL_REPEAT },
107 { "collection", required_argument, NULL, OPT_LIBYATL_MATCH_COLLECTION },
108 { "wildcard", required_argument, NULL, OPT_LIBYATL_MATCH_WILDCARD },
109 { "massive", no_argument, NULL, OPT_LIBYATL_MASSIVE },
110 { 0, 0, 0, 0 }
111 };
112
113 int option_index= 0;
114 while (1)
115 {
116 int option_rv= getopt_long(argc, argv, "", long_options, &option_index);
117 if (option_rv == -1)
118 {
119 break;
120 }
121
122 switch (option_rv)
123 {
124 case OPT_LIBYATL_VERSION:
125 break;
126
127 case OPT_LIBYATL_QUIET:
128 opt_quiet= true;
129 break;
130
131 case OPT_LIBYATL_REPEAT:
132 opt_repeat= strtoul(optarg, (char **) NULL, 10);
133 break;
134
135 case OPT_LIBYATL_MATCH_COLLECTION:
136 collection_to_run= optarg;
137 break;
138
139 case OPT_LIBYATL_MATCH_WILDCARD:
140 wildcard= optarg;
141 break;
142
143 case OPT_LIBYATL_MASSIVE:
144 opt_massive= true;
145 break;
146
147 case '?':
148 /* getopt_long already printed an error message. */
149 Error << "unknown option to getopt_long()";
150 exit(EXIT_FAILURE);
151
152 default:
153 break;
154 }
155 }
156 }
157
158 srandom((unsigned int)time(NULL));
159
160 if (bool(getenv("YATL_REPEAT")) and (strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10) > 1))
161 {
162 opt_repeat= strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10);
163 }
164
165 if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet)
166 {
167 opt_quiet= true;
168 }
169 else if (getenv("JENKINS_URL"))
170 {
171 if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0))
172 { }
173 else
174 {
175 opt_quiet= true;
176 }
177 }
178
179 if (opt_quiet)
180 {
181 close(STDOUT_FILENO);
182 }
183
184 char buffer[1024];
185 if (getenv("LIBTEST_TMP"))
186 {
187 snprintf(buffer, sizeof(buffer), "%s", getenv("LIBTEST_TMP"));
188 }
189 else
190 {
191 snprintf(buffer, sizeof(buffer), "%s", LIBTEST_TEMP);
192 }
193
194 if (chdir(buffer) == -1)
195 {
196 char getcwd_buffer[1024];
197 char *dir= getcwd(getcwd_buffer, sizeof(getcwd_buffer));
198
199 Error << "Unable to chdir() from " << dir << " to " << buffer << " errno:" << strerror(errno);
200 return EXIT_FAILURE;
201 }
202
203 if (libtest::libtool() == NULL)
204 {
205 Error << "Failed to locate libtool";
206 return EXIT_FAILURE;
207 }
208
209 if (getenv("YATL_COLLECTION_TO_RUN"))
210 {
211 if (strlen(getenv("YATL_COLLECTION_TO_RUN")))
212 {
213 collection_to_run= getenv("YATL_COLLECTION_TO_RUN");
214 }
215 }
216
217 if (collection_to_run.compare("none") == 0)
218 {
219 return EXIT_SUCCESS;
220 }
221
222 if (collection_to_run.empty() == false)
223 {
224 Out << "Only testing " << collection_to_run;
225 }
226
227 int exit_code;
228
229 try
230 {
231 do
232 {
233 exit_code= EXIT_SUCCESS;
234 fatal_assert(sigignore(SIGPIPE) == 0);
235
236 libtest::SignalThread signal;
237 if (signal.setup() == false)
238 {
239 Error << "Failed to setup signals";
240 return EXIT_FAILURE;
241 }
242
243 std::auto_ptr<Framework> frame(new Framework(signal, collection_to_run, wildcard));
244
245 // Run create(), bail on error.
246 {
247 switch (frame->create())
248 {
249 case TEST_SUCCESS:
250 break;
251
252 case TEST_SKIPPED:
253 Out << "SKIP " << argv[0];
254 return EXIT_SUCCESS;
255
256 case TEST_FAILURE:
257 return EXIT_FAILURE;
258 }
259 }
260
261 frame->exec();
262
263 if (signal.is_shutdown() == false)
264 {
265 signal.set_shutdown(SHUTDOWN_GRACEFUL);
266 }
267
268 shutdown_t status= signal.get_shutdown();
269 if (status == SHUTDOWN_FORCED)
270 {
271 Out << "Tests were aborted.";
272 exit_code= EXIT_FAILURE;
273 }
274 else if (frame->failed())
275 {
276 Out << "Some test failed.";
277 exit_code= EXIT_FAILURE;
278 }
279 else if (frame->skipped() and frame->failed() and frame->success())
280 {
281 Out << "Some tests were skipped.";
282 }
283 else if (frame->success() and (frame->failed() == 0))
284 {
285 Out << "All tests completed successfully.";
286 }
287
288 stats_print(frame.get());
289
290 Outn(); // Generate a blank to break up the messages if make check/test has been run
291 } while (exit_code == EXIT_SUCCESS and --opt_repeat);
292 }
293 catch (libtest::fatal& e)
294 {
295 std::cerr << e.what() << std::endl;
296 exit_code= EXIT_FAILURE;
297 }
298 catch (libtest::disconnected& e)
299 {
300 std::cerr << "Unhandled disconnection occurred:" << e.what() << std::endl;
301 exit_code= EXIT_FAILURE;
302 }
303 catch (std::exception& e)
304 {
305 std::cerr << e.what() << std::endl;
306 exit_code= EXIT_FAILURE;
307 }
308 catch (...)
309 {
310 std::cerr << "Unknown exception halted execution." << std::endl;
311 exit_code= EXIT_FAILURE;
312 }
313
314 return exit_code;
315 }