LCOV - code coverage report
Current view: top level - lib - testing_api_helpers.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 34 54 63.0 %
Date: 2018-09-21 06:18:36 Functions: 2 2 100.0 %

          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           2 : 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           2 :     = GNUNET_OS_start_process (GNUNET_NO,
      53             :                                GNUNET_OS_INHERIT_STD_ALL,
      54             :                                NULL, NULL, NULL,
      55             :                                "taler-merchant-httpd",
      56             :                                "taler-merchant-httpd",
      57             :                                "-c", config_filename,
      58             :                                NULL);
      59           2 :   if (NULL == merchant_proc)
      60           0 :     MERCHANT_FAIL ();
      61             : 
      62           2 :   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           2 :   fprintf (stderr,
      71             :            "Waiting for `taler-merchant-httpd' to be ready\n");
      72           2 :   iter = 0;
      73             :   do
      74             :     {
      75           2 :       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           2 :       fprintf (stderr, ".\n");
      87           2 :       sleep (1);
      88           2 :       iter++;
      89             :     }
      90           2 :   while (0 != system (wget_cmd));
      91           2 :   GNUNET_free (wget_cmd);
      92           2 :   fprintf (stderr, "\n");
      93             : 
      94           2 :   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           2 : 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           2 :   cfg = GNUNET_CONFIGURATION_create ();
     118           2 :   if (GNUNET_OK !=
     119           2 :       GNUNET_CONFIGURATION_load (cfg,
     120             :                                  config_filename))
     121           0 :     MERCHANT_FAIL ();
     122           2 :   if (GNUNET_OK !=
     123           2 :       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           2 :   GNUNET_CONFIGURATION_destroy (cfg);
     136             : 
     137           2 :   if (GNUNET_OK !=
     138           2 :       GNUNET_NETWORK_test_port_free (IPPROTO_TCP,
     139           2 :                                      (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           2 :   if (NULL == (dbinit_proc = GNUNET_OS_start_process
     149             :     (GNUNET_NO,
     150             :      GNUNET_OS_INHERIT_STD_ALL,
     151             :      NULL, NULL, NULL,
     152             :      "taler-merchant-dbinit",
     153             :      "taler-merchant-dbinit",
     154             :      "-c", config_filename,
     155             :      "-r",
     156             :      NULL)))
     157             :   {
     158           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     159             :                 "Failed to run taler-merchant-dbinit."
     160             :                 " Check your PATH.\n");
     161           0 :     MERCHANT_FAIL ();
     162             :   }
     163             : 
     164           2 :   if (GNUNET_SYSERR ==
     165           2 :       GNUNET_OS_process_wait_status (dbinit_proc,
     166             :                                      &type,
     167             :                                      &code))
     168             :   {
     169           0 :     GNUNET_OS_process_destroy (dbinit_proc);
     170           0 :     MERCHANT_FAIL ();
     171             :   }
     172           4 :   if ( (type == GNUNET_OS_PROCESS_EXITED) &&
     173           2 :        (0 != code) )
     174             :   {
     175           0 :     fprintf (stderr,
     176             :              "Failed to setup database\n");
     177           0 :     MERCHANT_FAIL ();
     178             :   }
     179           4 :   if ( (type != GNUNET_OS_PROCESS_EXITED) ||
     180           2 :        (0 != code) )
     181             :   {
     182           0 :     fprintf (stderr,
     183             :              "Unexpected error running"
     184             :              " `taler-merchant-dbinit'!\n");
     185           0 :     MERCHANT_FAIL ();
     186             :   }
     187           2 :   GNUNET_OS_process_destroy (dbinit_proc);
     188             : 
     189             : 
     190             : 
     191           2 :   GNUNET_asprintf (&base_url,
     192             :                    "http://localhost:%llu/",
     193             :                    port);
     194           2 :   return base_url;
     195             : }

Generated by: LCOV version 1.13