2 * Copyright (c) 2008 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
22 #include "testutils.h"
24 int tu_debugging_enabled = FALSE;
27 alarm_hdlr(int sig G_GNUC_UNUSED)
29 g_fprintf(stderr, "-- TEST TIMED OUT --\n");
33 /* Call testfn in a forked process, such that any failures will trigger a
34 * test failure, but allow the other tests to proceed.
37 callinfork(TestUtilsTest *test, int ignore_timeouts, gboolean skip_fork)
44 /* kill the test after a bit */
45 signal(SIGALRM, alarm_hdlr);
46 if (!ignore_timeouts) alarm(test->timeout);
50 switch (pid = fork()) {
52 /* kill the test after a bit */
53 signal(SIGALRM, alarm_hdlr);
54 if (!ignore_timeouts) alarm(test->timeout);
64 waitpid(pid, &status, 0);
71 g_fprintf(stderr, " PASS %s\n", test->name);
73 g_fprintf(stderr, " FAIL %s\n", test->name);
83 printf("USAGE: <test-script> [-d] [-h] [testname [testname [..]]]\n"
85 "\t-h: this message\n"
86 "\t-d: print debugging messages\n"
87 "\t-t: ignore timeouts\n"
89 "\t-l: loop the same test repeatedly (use with -n for leak checks)\n"
91 "If no test names are specified, all tests are run. Available tests:\n"
94 printf("\t%s\n", tests->name);
100 ignore_debug_messages(
101 const gchar *log_domain G_GNUC_UNUSED,
102 GLogLevelFlags log_level G_GNUC_UNUSED,
103 const gchar *message G_GNUC_UNUSED,
104 gpointer user_data G_GNUC_UNUSED)
112 TestUtilsTest *tests)
117 int ignore_timeouts = 0;
118 gboolean skip_fork = FALSE;
119 gboolean only_one = FALSE;
120 gboolean loop_forever = FALSE;
122 /* first_parse the command line */
124 if (strcmp(argv[1], "-d") == 0) {
125 tu_debugging_enabled = TRUE;
126 } else if (strcmp(argv[1], "-t") == 0) {
127 ignore_timeouts = TRUE;
128 } else if (strcmp(argv[1], "-n") == 0) {
131 } else if (strcmp(argv[1], "-l") == 0) {
134 } else if (strcmp(argv[1], "-h") == 0) {
140 for (t = tests; t->fn; t++) {
141 if (strcmp(argv[1], t->name) == 0) {
149 g_fprintf(stderr, "Test '%s' not found\n", argv[1]);
160 for (t = tests; t->fn; t++)
167 for (t = tests; t->fn; t++) {
173 g_fprintf(stderr, "Only run one test with '-n'\n");
178 /* Make sure g_critical and g_error will exit */
179 g_log_set_always_fatal(G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
181 /* and silently drop debug messages unless we're debugging */
182 if (!tu_debugging_enabled) {
183 g_log_set_handler(NULL, G_LOG_LEVEL_DEBUG, ignore_debug_messages, NULL);
186 /* Now actually run the tests */
188 for (t = tests; t->fn; t++) {
191 success = callinfork(t, ignore_timeouts, skip_fork) && success;
192 } while (loop_forever);