testing_api_twister_helpers.c (5539B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2014-2018 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as 7 published by the Free Software Foundation; either version 3, or 8 (at your option) any later version. 9 10 TALER is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with TALER; see the file COPYING. If not, see 17 <http://www.gnu.org/licenses/> 18 */ 19 20 /** 21 * @file testing_api_twister_helpers.c 22 * @brief helper functions for test library. 23 * @author Christian Grothoff 24 * @author Marcello Stanisci 25 */ 26 #include "taler/platform.h" 27 #include <gnunet/gnunet_util_lib.h> 28 #include <taler/taler_twister_service.h> 29 #include "taler/taler_twister_testing_lib.h" 30 31 32 /** 33 * Prepare twister for execution; mainly checks whether the 34 * HTTP port is available and construct the base URL based on it. 35 * 36 * @param config_filename configuration file name. 37 * @return twister base URL, NULL upon errors. 38 */ 39 char * 40 TALER_TWISTER_prepare_twister (const char *config_filename) 41 { 42 struct GNUNET_CONFIGURATION_Handle *cfg; 43 unsigned long long port; 44 char *base_url; 45 46 cfg = GNUNET_CONFIGURATION_create (TWISTER_project_data ()); 47 if (GNUNET_OK != 48 GNUNET_CONFIGURATION_load (cfg, 49 config_filename)) 50 TWISTER_FAIL (); 51 52 if (GNUNET_OK != 53 GNUNET_CONFIGURATION_get_value_number (cfg, 54 "twister", 55 "HTTP_PORT", 56 &port)) 57 { 58 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 59 "twister", 60 "HTTP_PORT"); 61 GNUNET_CONFIGURATION_destroy (cfg); 62 TWISTER_FAIL (); 63 } 64 65 GNUNET_CONFIGURATION_destroy (cfg); 66 67 if (GNUNET_OK != 68 GNUNET_NETWORK_test_port_free (IPPROTO_TCP, 69 (uint16_t) port)) 70 { 71 fprintf (stderr, 72 "Required port %llu not available, skipping.\n", 73 port); 74 TWISTER_FAIL (); 75 } 76 77 GNUNET_assert (0 < GNUNET_asprintf 78 (&base_url, 79 "http://localhost:%llu/", 80 port)); 81 82 return base_url; 83 } 84 85 86 /** 87 * Run the twister service. 88 * 89 * @param config_filename configuration file name. 90 * @return twister process handle, NULL upon errors. 91 */ 92 struct GNUNET_OS_Process * 93 TALER_TWISTER_run_twister (const char *config_filename) 94 { 95 struct GNUNET_OS_Process *proc; 96 struct GNUNET_OS_Process *client_proc; 97 unsigned long code; 98 enum GNUNET_OS_ProcessStatusType type; 99 100 proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ALL); 101 if (GNUNET_OK != 102 GNUNET_process_run_command_va (proc, 103 "taler-twister-service", 104 "taler-twister-service", 105 "-c", config_filename, 106 NULL)) 107 TWISTER_FAIL (); 108 109 client_proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ALL); 110 if (GNUNET_OK != 111 GNUNET_process_run_command_va (client_proc, 112 "taler-twister", 113 "taler-twister", 114 "-c", config_filename, 115 "-a", NULL)) 116 { 117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 118 "Could not start the taler-twister client\n"); 119 GNUNET_break (GNUNET_OK == 120 GNUNET_process_kill (proc, 121 SIGTERM)); 122 GNUNET_break (GNUNET_OK == 123 GNUNET_process_wait (proc, 124 true, 125 NULL, 126 NULL)); 127 GNUNET_process_destroy (proc); 128 TWISTER_FAIL (); 129 } 130 131 132 if (GNUNET_SYSERR == 133 GNUNET_process_wait (client_proc, 134 false, 135 &type, 136 &code)) 137 { 138 GNUNET_process_destroy (client_proc); 139 GNUNET_break (GNUNET_OK == 140 GNUNET_process_kill (proc, 141 SIGTERM)); 142 GNUNET_process_wait (proc); 143 GNUNET_process_destroy (proc); 144 TWISTER_FAIL (); 145 } 146 if ( (type == GNUNET_OS_PROCESS_EXITED) && 147 (0 != code) ) 148 { 149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 150 "Failed to check twister works.\n"); 151 GNUNET_process_destroy (client_proc); 152 GNUNET_break (GNUNET_OK == 153 GNUNET_process_kill (proc, 154 SIGTERM)); 155 GNUNET_process_wait (proc); 156 GNUNET_process_destroy (proc); 157 TWISTER_FAIL (); 158 } 159 if ( (type != GNUNET_OS_PROCESS_EXITED) || 160 (0 != code) ) 161 { 162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 163 "Unexpected error running `taler-twister'!\n"); 164 GNUNET_process_destroy (client_proc); 165 GNUNET_break (GNUNET_OK == 166 GNUNET_process_kill (proc, 167 SIGTERM)); 168 GNUNET_process_wait (proc, 169 true, 170 NULL, 171 NULL); 172 GNUNET_process_destroy (proc); 173 TWISTER_FAIL (); 174 } 175 GNUNET_process_destroy (client_proc); 176 177 return proc; 178 }