LCOV - code coverage report
Current view: top level - testing - testing_api_helpers.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 17 54 31.5 %
Date: 2020-08-15 06:12:35 Functions: 1 2 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 lib/testing_api_helpers.c
      22             :  * @brief helper functions for test library.
      23             :  * @author Christian Grothoff
      24             :  * @author Marcello Stanisci
      25             :  */
      26             : 
      27             : #include "platform.h"
      28             : #include <taler/taler_exchange_service.h>
      29             : #include <taler/taler_testing_lib.h>
      30             : #include "taler_merchant_testing_lib.h"
      31             : 
      32             : 
      33             : /**
      34             :  * Start the merchant backend process.  Assume the port
      35             :  * is available and the database is clean.  Use the "prepare
      36             :  * merchant" function to do such tasks.
      37             :  *
      38             :  * @param config_filename configuration filename.
      39             :  *
      40             :  * @return the process, or NULL if the process could not
      41             :  *         be started.
      42             :  */
      43             : struct GNUNET_OS_Process *
      44           0 : TALER_TESTING_run_merchant (const char *config_filename,
      45             :                             const char *merchant_url)
      46             : {
      47             :   struct GNUNET_OS_Process *merchant_proc;
      48             :   unsigned int iter;
      49             :   char *wget_cmd;
      50             : 
      51             :   merchant_proc
      52           0 :     = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL,
      53             :                                NULL, NULL, NULL,
      54             :                                "taler-merchant-httpd",
      55             :                                "taler-merchant-httpd",
      56             :                                "--log=INFO",
      57             :                                "-c", config_filename,
      58             :                                NULL);
      59           0 :   if (NULL == merchant_proc)
      60           0 :     MERCHANT_FAIL ();
      61             : 
      62           0 :   GNUNET_asprintf (&wget_cmd,
      63             :                    "wget -q -t 1 -T 1"
      64             :                    " --header='Authorization: ApiKey sandbox'"
      65             :                    " %s"
      66             :                    " -o /dev/null -O /dev/null",
      67             :                    merchant_url);
      68             : 
      69             :   /* give child time to start and bind against the socket */
      70           0 :   fprintf (stderr,
      71             :            "Waiting for `taler-merchant-httpd' to be ready\n");
      72           0 :   iter = 0;
      73             :   do
      74             :   {
      75           0 :     if (10 == iter)
      76             :     {
      77           0 :       fprintf (stderr,
      78             :                "Failed to launch"
      79             :                " `taler-merchant-httpd' (or `wget')\n");
      80           0 :       GNUNET_OS_process_kill (merchant_proc,
      81             :                               SIGTERM);
      82           0 :       GNUNET_OS_process_wait (merchant_proc);
      83           0 :       GNUNET_OS_process_destroy (merchant_proc);
      84           0 :       MERCHANT_FAIL ();
      85             :     }
      86           0 :     fprintf (stderr, ".\n");
      87           0 :     sleep (1);
      88           0 :     iter++;
      89             :   }
      90           0 :   while (0 != system (wget_cmd));
      91           0 :   GNUNET_free (wget_cmd);
      92           0 :   fprintf (stderr, "\n");
      93             : 
      94           0 :   return merchant_proc;
      95             : }
      96             : 
      97             : 
      98             : /**
      99             :  * Prepare the merchant execution.  Create tables and check if
     100             :  * the port is available.
     101             :  *
     102             :  * @param config_filename configuration filename.
     103             :  *
     104             :  * @return the base url, or NULL upon errors.  Must be freed
     105             :  *         by the caller.
     106             :  */
     107             : char *
     108           1 : TALER_TESTING_prepare_merchant (const char *config_filename)
     109             : {
     110             :   struct GNUNET_CONFIGURATION_Handle *cfg;
     111             :   unsigned long long port;
     112             :   struct GNUNET_OS_Process *dbinit_proc;
     113             :   enum GNUNET_OS_ProcessStatusType type;
     114             :   unsigned long code;
     115             :   char *base_url;
     116             : 
     117           1 :   cfg = GNUNET_CONFIGURATION_create ();
     118           1 :   if (GNUNET_OK !=
     119           1 :       GNUNET_CONFIGURATION_load (cfg,
     120             :                                  config_filename))
     121           0 :     MERCHANT_FAIL ();
     122           1 :   if (GNUNET_OK !=
     123           1 :       GNUNET_CONFIGURATION_get_value_number (cfg,
     124             :                                              "merchant",
     125             :                                              "PORT",
     126             :                                              &port))
     127             :   {
     128           0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     129             :                                "merchant",
     130             :                                "PORT");
     131           0 :     GNUNET_CONFIGURATION_destroy (cfg);
     132           0 :     MERCHANT_FAIL ();
     133             :   }
     134             : 
     135           1 :   GNUNET_CONFIGURATION_destroy (cfg);
     136             : 
     137           1 :   if (GNUNET_OK !=
     138           1 :       GNUNET_NETWORK_test_port_free (IPPROTO_TCP,
     139           1 :                                      (uint16_t) port))
     140             :   {
     141           0 :     fprintf (stderr,
     142             :              "Required port %llu not available, skipping.\n",
     143             :              port);
     144           0 :     MERCHANT_FAIL ();
     145             :   }
     146             : 
     147             :   /* DB preparation */
     148           1 :   if (NULL == (dbinit_proc = GNUNET_OS_start_process (
     149             :                  GNUNET_OS_INHERIT_STD_ALL,
     150             :                  NULL, NULL, NULL,
     151             :                  "taler-merchant-dbinit",
     152             :                  "taler-merchant-dbinit",
     153             :                  "-c", config_filename,
     154             :                  "-r",
     155             :                  NULL)))
     156             :   {
     157           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     158             :                 "Failed to run taler-merchant-dbinit. Check your PATH.\n");
     159           0 :     MERCHANT_FAIL ();
     160             :   }
     161             : 
     162           1 :   if (GNUNET_SYSERR ==
     163           1 :       GNUNET_OS_process_wait_status (dbinit_proc,
     164             :                                      &type,
     165             :                                      &code))
     166             :   {
     167           0 :     GNUNET_OS_process_destroy (dbinit_proc);
     168           0 :     MERCHANT_FAIL ();
     169             :   }
     170           1 :   if ( (type == GNUNET_OS_PROCESS_EXITED) &&
     171           1 :        (0 != code) )
     172             :   {
     173           1 :     fprintf (stderr,
     174             :              "Failed to setup database\n");
     175           1 :     MERCHANT_FAIL ();
     176             :   }
     177           0 :   if ( (type != GNUNET_OS_PROCESS_EXITED) ||
     178           0 :        (0 != code) )
     179             :   {
     180           0 :     fprintf (stderr,
     181             :              "Unexpected error running"
     182             :              " `taler-merchant-dbinit'!\n");
     183           0 :     MERCHANT_FAIL ();
     184             :   }
     185           0 :   GNUNET_OS_process_destroy (dbinit_proc);
     186             : 
     187             : 
     188           0 :   GNUNET_asprintf (&base_url,
     189             :                    "http://localhost:%llu/",
     190             :                    port);
     191           0 :   return base_url;
     192             : }

Generated by: LCOV version 1.14