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-05-16 06:16:58 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           1 : 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           1 :     = 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           1 :   if (NULL == merchant_proc)
      60           0 :     MERCHANT_FAIL ();
      61             : 
      62           1 :   GNUNET_asprintf (&wget_cmd,
      63             :                    "wget -q -t 1 -T 1 %s"
      64             :                    " -o /dev/null -O /dev/null",
      65             :                    merchant_url);
      66             : 
      67             :   /* give child time to start and bind against the socket */
      68           1 :   fprintf (stderr,
      69             :            "Waiting for `taler-merchant-httpd' to be ready\n");
      70           1 :   iter = 0;
      71             :   do
      72             :     {
      73           1 :       if (10 == iter)
      74             :       {
      75           0 :         fprintf (stderr,
      76             :                  "Failed to launch"
      77             :                  " `taler-merchant-httpd' (or `wget')\n");
      78           0 :         GNUNET_OS_process_kill (merchant_proc,
      79             :                                 SIGTERM);
      80           0 :         GNUNET_OS_process_wait (merchant_proc);
      81           0 :         GNUNET_OS_process_destroy (merchant_proc);
      82           0 :         MERCHANT_FAIL ();
      83             :       }
      84           1 :       fprintf (stderr, ".\n");
      85           1 :       sleep (1);
      86           1 :       iter++;
      87             :     }
      88           1 :   while (0 != system (wget_cmd));
      89           1 :   GNUNET_free (wget_cmd);
      90           1 :   fprintf (stderr, "\n");
      91             : 
      92           1 :   return merchant_proc;
      93             : }
      94             : 
      95             : 
      96             : /**
      97             :  * Prepare the merchant execution.  Create tables and check if
      98             :  * the port is available.
      99             :  *
     100             :  * @param config_filename configuration filename.
     101             :  *
     102             :  * @return the base url, or NULL upon errors.  Must be freed
     103             :  *         by the caller.
     104             :  */
     105             : char *
     106           1 : TALER_TESTING_prepare_merchant (const char *config_filename)
     107             : {
     108             :   struct GNUNET_CONFIGURATION_Handle *cfg;
     109             :   unsigned long long port;
     110             :   struct GNUNET_OS_Process *dbinit_proc;
     111             :   enum GNUNET_OS_ProcessStatusType type;
     112             :   unsigned long code;
     113             :   char *base_url;
     114             : 
     115           1 :   cfg = GNUNET_CONFIGURATION_create ();
     116           1 :   if (GNUNET_OK !=
     117           1 :       GNUNET_CONFIGURATION_load (cfg,
     118             :                                  config_filename))
     119           0 :     MERCHANT_FAIL ();
     120           1 :   if (GNUNET_OK !=
     121           1 :       GNUNET_CONFIGURATION_get_value_number (cfg,
     122             :                                              "merchant",
     123             :                                              "PORT",
     124             :                                              &port))
     125             :   {
     126           0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     127             :                                "merchant",
     128             :                                "PORT");
     129           0 :     GNUNET_CONFIGURATION_destroy (cfg);
     130           0 :     MERCHANT_FAIL ();
     131             :   }
     132             : 
     133           1 :   GNUNET_CONFIGURATION_destroy (cfg);
     134             : 
     135           1 :   if (GNUNET_OK !=
     136           1 :       GNUNET_NETWORK_test_port_free (IPPROTO_TCP,
     137           1 :                                      (uint16_t) port))
     138             :   {
     139           0 :     fprintf (stderr,
     140             :              "Required port %llu not available, skipping.\n",
     141             :              port);
     142           0 :     MERCHANT_FAIL ();
     143             :   }
     144             : 
     145             :   /* DB preparation */
     146           1 :   if (NULL == (dbinit_proc = GNUNET_OS_start_process
     147             :     (GNUNET_NO,
     148             :      GNUNET_OS_INHERIT_STD_ALL,
     149             :      NULL, NULL, NULL,
     150             :      "taler-merchant-dbinit",
     151             :      "taler-merchant-dbinit",
     152             :      "-c", config_filename,
     153             :      "-r",
     154             :      NULL)))
     155             :   {
     156           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     157             :                 "Failed to run taler-merchant-dbinit."
     158             :                 " 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           2 :   if ( (type == GNUNET_OS_PROCESS_EXITED) &&
     171           1 :        (0 != code) )
     172             :   {
     173           0 :     fprintf (stderr,
     174             :              "Failed to setup database\n");
     175           0 :     MERCHANT_FAIL ();
     176             :   }
     177           2 :   if ( (type != GNUNET_OS_PROCESS_EXITED) ||
     178           1 :        (0 != code) )
     179             :   {
     180           0 :     fprintf (stderr,
     181             :              "Unexpected error running"
     182             :              " `taler-merchant-dbinit'!\n");
     183           0 :     MERCHANT_FAIL ();
     184             :   }
     185           1 :   GNUNET_OS_process_destroy (dbinit_proc);
     186             : 
     187             : 
     188             : 
     189           1 :   GNUNET_asprintf (&base_url,
     190             :                    "http://localhost:%llu/",
     191             :                    port);
     192           1 :   return base_url;
     193             : }

Generated by: LCOV version 1.13