Back to index

dbus-test-runner  12.10.0
dbus-test-runner.c
Go to the documentation of this file.
00001 /*
00002 Copyright 2010 Canonical Ltd.
00003 
00004 Authors:
00005     Ted Gould <ted@canonical.com>
00006 
00007 This program is free software: you can redistribute it and/or modify it 
00008 under the terms of the GNU General Public License version 3, as published 
00009 by the Free Software Foundation.
00010 
00011 This program is distributed in the hope that it will be useful, but 
00012 WITHOUT ANY WARRANTY; without even the implied warranties of 
00013 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
00014 PURPOSE.  See the GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License along 
00017 with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 #include <glib.h>
00022 #include <gio/gio.h>
00023 
00024 #include <libdbustest/dbus-test.h>
00025 
00026 static gint max_wait = 30;
00027 static DbusTestProcess * last_task = NULL;
00028 static DbusTestService * service = NULL;
00029 static gboolean timeout = FALSE;
00030 
00031 #define NAME_SET "dbus-test-runner-name-set"
00032 
00033 static gboolean
00034 option_task (const gchar * arg, const gchar * value, gpointer data, GError ** error)
00035 {
00036        if (last_task != NULL) {
00037               g_object_unref(last_task);
00038               last_task = NULL;
00039        }
00040 
00041        last_task = dbus_test_process_new(value);
00042        dbus_test_service_add_task(service, DBUS_TEST_TASK(last_task));
00043        return TRUE;
00044 }
00045 
00046 static gboolean
00047 option_taskname (const gchar * arg, const gchar * value, gpointer data, GError ** error)
00048 {
00049        if (last_task == NULL) {
00050               g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "No task to put the name %s on.", value);
00051               return FALSE;
00052        }
00053 
00054        if (g_object_get_data(G_OBJECT(last_task), NAME_SET)) {
00055               g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Task already has the name %s.  Asked to put %s on it.", dbus_test_task_get_name(DBUS_TEST_TASK(last_task)), value);
00056               return FALSE;
00057        }
00058 
00059        g_object_set_data(G_OBJECT(last_task), NAME_SET, GINT_TO_POINTER(TRUE));
00060        dbus_test_task_set_name(DBUS_TEST_TASK(last_task), value);
00061        return TRUE;
00062 }
00063 
00064 static gboolean
00065 option_noreturn (const gchar * arg, const gchar * value, gpointer data, GError ** error)
00066 {
00067        if (last_task == NULL) {
00068               g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "No task to put adjust return on.");
00069               return FALSE;
00070        }
00071 
00072        if (dbus_test_task_get_return(DBUS_TEST_TASK(last_task)) != DBUS_TEST_TASK_RETURN_NORMAL) {
00073               g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Task return type has already been modified.");
00074               return FALSE;
00075        }
00076 
00077        dbus_test_task_set_return(DBUS_TEST_TASK(last_task), DBUS_TEST_TASK_RETURN_IGNORE);
00078        return TRUE;
00079 }
00080 
00081 static gboolean
00082 option_invert (const gchar * arg, const gchar * value, gpointer data, GError ** error)
00083 {
00084        if (last_task == NULL) {
00085               g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "No task to put adjust return on.");
00086               return FALSE;
00087        }
00088 
00089        if (dbus_test_task_get_return(DBUS_TEST_TASK(last_task)) != DBUS_TEST_TASK_RETURN_NORMAL) {
00090               g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Task return type has already been modified.");
00091               return FALSE;
00092        }
00093 
00094        dbus_test_task_set_return(DBUS_TEST_TASK(last_task), DBUS_TEST_TASK_RETURN_INVERT);
00095        return TRUE;
00096 }
00097 
00098 static gboolean
00099 option_param (const gchar * arg, const gchar * value, gpointer data, GError ** error)
00100 {
00101        if (last_task == NULL) {
00102               g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "No task to put adjust return on.");
00103               return FALSE;
00104        }
00105 
00106        dbus_test_process_append_param(last_task, value);
00107        return TRUE;
00108 }
00109 
00110 static gboolean
00111 option_wait (const gchar * arg, const gchar * value, gpointer data, GError ** error)
00112 {
00113        if (last_task == NULL) {
00114               g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "No task to add a wait on %s for.", value);
00115               return FALSE;
00116        }
00117 
00118        if (dbus_test_task_get_wait_for(DBUS_TEST_TASK(last_task)) != NULL) {
00119               g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Task is already waiting for %s.  Asked to wait for %s", dbus_test_task_get_wait_for(DBUS_TEST_TASK(last_task)), value);
00120               return FALSE;
00121        }
00122 
00123        dbus_test_task_set_wait_for(DBUS_TEST_TASK(last_task), value);
00124        return TRUE;
00125 }
00126 
00127 static gboolean
00128 max_wait_hit (gpointer user_data)
00129 {
00130        g_warning("Timing out at maximum wait of %d seconds.", max_wait);
00131        dbus_test_service_stop(service);
00132        timeout = TRUE;
00133        return FALSE;
00134 }
00135 
00136 static gchar * dbus_configfile = NULL;
00137 static gchar * dbus_daemon = NULL;
00138 static gchar * bustle_cmd = NULL;
00139 static gchar * bustle_datafile = NULL;
00140 
00141 static GOptionEntry general_options[] = {
00142        {"dbus-daemon",  0,     0,                       G_OPTION_ARG_FILENAME,  &dbus_daemon,     "Path to the DBus deamon to use.  Defaults to 'dbus-daemon'.", "executable"},
00143        {"dbus-config",  'd',   0,                       G_OPTION_ARG_FILENAME,  &dbus_configfile, "Configuration file for newly created DBus server.  Defaults to '" DEFAULT_SESSION_CONF "'.", "config_file"},
00144        {"bustle-monitor", 0,   0,                       G_OPTION_ARG_FILENAME,  &bustle_cmd,      "Path to the Bustle DBus Monitor to use.  Defaults to 'bustle-dbus-monitor'.", "executable"},
00145        {"bustle-data",  'b',   0,                       G_OPTION_ARG_FILENAME,  &bustle_datafile, "A file to write out data from the bustle logger to.", "data_file"},
00146        {"max-wait",     'm',   0,                       G_OPTION_ARG_INT,       &max_wait,        "The maximum amount of time the test runner will wait for the test to complete.  Default is 30 seconds.", "seconds"},
00147        {NULL}
00148 };
00149 
00150 static GOptionEntry task_options[] = {
00151        {"task",          't',  G_OPTION_FLAG_FILENAME,   G_OPTION_ARG_CALLBACK,  option_task,     "Defines a new task to run under our private DBus session.", "executable"},
00152        {"task-name",     'n',  0,                        G_OPTION_ARG_CALLBACK,  option_taskname, "A string to label output from the previously defined task.  Defaults to taskN.", "name"},
00153        {"ignore-return", 'r',  G_OPTION_FLAG_NO_ARG,     G_OPTION_ARG_CALLBACK,  option_noreturn, "Do not use the return value of the task to calculate whether the test passes or fails.", NULL},
00154        {"invert-return", 'i',  G_OPTION_FLAG_NO_ARG,     G_OPTION_ARG_CALLBACK,  option_invert,   "Invert the return value of the task before calculating whether the test passes or fails.", NULL},
00155        {"parameter",     'p',  0,                        G_OPTION_ARG_CALLBACK,  option_param,    "Add a parameter to the call of this utility.  May be called as many times as you'd like.", NULL},
00156        {"wait-for",      'f',  0,                        G_OPTION_ARG_CALLBACK,  option_wait,     "A dbus-name that should appear on the bus before this task is started", "dbus-name"},
00157        {NULL}
00158 };
00159 
00160 int
00161 main (int argc, char * argv[])
00162 {
00163        GError * error = NULL;
00164        GOptionContext * context;
00165 
00166        g_type_init();
00167 
00168        service = dbus_test_service_new(NULL);
00169 
00170        context = g_option_context_new("- run multiple tasks under an independent DBus session bus");
00171 
00172        g_option_context_add_main_entries(context, general_options, "dbus-runner");
00173 
00174        GOptionGroup * taskgroup = g_option_group_new("task-control", "Task control options", "Options that are used to control how the task is handled by the test runner.", NULL, NULL);
00175        g_option_group_add_entries(taskgroup, task_options);
00176        g_option_context_add_group(context, taskgroup);
00177 
00178        if (!g_option_context_parse(context, &argc, &argv, &error)) {
00179               g_print("option parsing failed: %s\n", error->message);
00180               g_error_free(error);
00181               return 1;
00182        }
00183 
00184        if (dbus_daemon != NULL) {
00185               dbus_test_service_set_daemon(service, dbus_daemon);
00186        }
00187 
00188        if (dbus_configfile != NULL) {
00189               dbus_test_service_set_conf_file(service, dbus_configfile);
00190        }
00191 
00192        if (bustle_datafile != NULL) {
00193               DbusTestBustle * bustler = dbus_test_bustle_new(bustle_datafile);
00194               /* We want to ensure that bustle captures all the data so start it first */
00195               dbus_test_service_add_task_with_priority(service, DBUS_TEST_TASK(bustler), DBUS_TEST_SERVICE_PRIORITY_FIRST);
00196 
00197               if (bustle_cmd != NULL) {
00198                      dbus_test_bustle_set_executable(bustler, bustle_cmd);
00199               }
00200 
00201               g_object_unref(bustler);
00202        }
00203 
00204        if (max_wait > 0) {
00205               g_timeout_add_seconds(max_wait, max_wait_hit, NULL);
00206        }
00207 
00208        /* These should all be in the service now */
00209        if (last_task != NULL) {
00210               g_object_unref(last_task);
00211               last_task = NULL;
00212        } else {
00213               g_critical("No tasks assigned");
00214               g_object_unref(service);
00215               return -1;
00216        }
00217 
00218        gint service_status = dbus_test_service_run(service);
00219        g_object_unref(service);
00220 
00221        if (timeout) {
00222               return -1;
00223        } else {
00224               return service_status;
00225        }
00226 }