testing_api_twister_helpers.c (5322B)
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_OS_start_process (GNUNET_OS_INHERIT_STD_ALL, 101 NULL, NULL, NULL, 102 "taler-twister-service", 103 "taler-twister-service", 104 "-c", config_filename, 105 NULL); 106 if (NULL == proc) 107 TWISTER_FAIL (); 108 109 client_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL, 110 NULL, NULL, NULL, 111 "taler-twister", 112 "taler-twister", 113 "-c", config_filename, 114 "-a", NULL); 115 if (NULL == client_proc) 116 { 117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 118 "Could not start the taler-twister client\n"); 119 GNUNET_break (0 == 120 GNUNET_OS_process_kill (proc, 121 SIGTERM)); 122 GNUNET_OS_process_wait (proc); 123 GNUNET_OS_process_destroy (proc); 124 TWISTER_FAIL (); 125 } 126 127 128 if (GNUNET_SYSERR == 129 GNUNET_OS_process_wait_status (client_proc, 130 &type, 131 &code)) 132 { 133 GNUNET_OS_process_destroy (client_proc); 134 GNUNET_break (0 == 135 GNUNET_OS_process_kill (proc, 136 SIGTERM)); 137 GNUNET_OS_process_wait (proc); 138 GNUNET_OS_process_destroy (proc); 139 TWISTER_FAIL (); 140 } 141 if ( (type == GNUNET_OS_PROCESS_EXITED) && 142 (0 != code) ) 143 { 144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 145 "Failed to check twister works.\n"); 146 GNUNET_OS_process_destroy (client_proc); 147 GNUNET_break (0 == 148 GNUNET_OS_process_kill (proc, 149 SIGTERM)); 150 GNUNET_OS_process_wait (proc); 151 GNUNET_OS_process_destroy (proc); 152 TWISTER_FAIL (); 153 } 154 if ( (type != GNUNET_OS_PROCESS_EXITED) || 155 (0 != code) ) 156 { 157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 158 "Unexpected error running `taler-twister'!\n"); 159 GNUNET_OS_process_destroy (client_proc); 160 GNUNET_break (0 == 161 GNUNET_OS_process_kill (proc, 162 SIGTERM)); 163 GNUNET_OS_process_wait (proc); 164 GNUNET_OS_process_destroy (proc); 165 TWISTER_FAIL (); 166 } 167 GNUNET_OS_process_destroy (client_proc); 168 169 return proc; 170 }