LCOV - code coverage report
Current view: top level - tests - test_externals.c (source / functions) Hit Total Coverage
Test: deployctl-0.3.15.2.96a2d Code Coverage Lines: 148 148 100.0 %
Date: 2018-06-22 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  test_externals.c
       3             :  Created by Danny Goossen, Gioxa Ltd on 26/3/17.
       4             : 
       5             :  MIT License
       6             : 
       7             :  Copyright (c) 2017 deployctl, Gioxa Ltd.
       8             : 
       9             :  Permission is hereby granted, free of charge, to any person obtaining a copy
      10             :  of this software and associated documentation files (the "Software"), to deal
      11             :  in the Software without restriction, including without limitation the rights
      12             :  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      13             :  copies of the Software, and to permit persons to whom the Software is
      14             :  furnished to do so, subject to the following conditions:
      15             : 
      16             :  The above copyright notice and this permission notice shall be included in all
      17             :  copies or substantial portions of the Software.
      18             : 
      19             :  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      20             :  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      21             :  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      22             :  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      23             :  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      24             :  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      25             :  SOFTWARE.
      26             : 
      27             :  */
      28             : 
      29             : 
      30             : #include <check.h>
      31             : 
      32             : #include "../src/deployd.h"
      33             : #include "test_web_api.h"
      34             : 
      35             : /*
      36             : int url_verify(char * commit_sha,char * baseHREF,feedback_t * feedback);
      37             : int verify_http_config(void * opaque);
      38             : int reload_http_config(void * opaque);
      39             : 
      40             : */
      41             : int cmd_exec(void * opaque);
      42             : int cmd_write(void * opaque);
      43             : 
      44             : typedef struct test_stubs_s
      45             : {
      46             :    char * arg0;
      47             :    char * arg1;
      48             :    char * arg2;
      49             :    char * arg3;
      50             :    int exitcode;
      51             : } test_stubs_t;
      52             : 
      53             : 
      54             : // stubfunctions:
      55           2 : int cmd_exec(void * opaque)
      56             : {
      57           2 :    test_stubs_t * teststubs= (test_stubs_t *)((data_exchange_t *)opaque)->trace->tests;
      58             : 
      59           2 :    ck_assert_str_eq(teststubs->arg0, ((data_exchange_t *)opaque)->paramlist[0]);
      60           4 :    ck_assert_str_eq(teststubs->arg1, ((data_exchange_t *)opaque)->paramlist[1]);
      61           4 :    ck_assert_str_eq(teststubs->arg2, ((data_exchange_t *)opaque)->paramlist[2]);
      62           2 :    return(teststubs->exitcode);
      63             : }
      64             : 
      65             : //WIP
      66             : // idea is to do the url_verify
      67             : // start webserver, give it what to anser and on which port for what url
      68             : // call url_verify("1234567890","http://127.0.0.1:12345",feedback_t * feedback)
      69             : // stop webserver
      70             : 
      71             : 
      72           1 : START_TEST(check_url_verify)
      73             : {
      74           1 :       u_int16_t port=0;
      75             :       pid_t child_pid;
      76             : 
      77           1 :       int result=0;
      78             :       webserver_t web;
      79             : 
      80             :       // what content we want the api to serve if he got the request matching request
      81           1 :    char * commit_sha="12345678";
      82           1 :    sprintf(web.reply_content,"%s",commit_sha);
      83             : 
      84             :       // what response headers we want to sent back, in case request matches
      85           1 :       sprintf(web.reply_header,"%s","HTTP/1.1 200 OK\r\nServer: testweb/0.0.1\r\nContent-Length:%d\r\n\r\n%s");
      86             : 
      87             : 
      88             :       // what reply on fail
      89           1 :       const char * m404_mesg="{\"message\":\"404 NOT FOUND\"}";
      90           1 :       sprintf(web.reply_fail,"HTTP/1.1 404 NOT FOUND\r\nServer: testweb/0.0.1\r\nContent-Length:%lu\r\n\r\n%s",strlen(m404_mesg),m404_mesg);
      91             : 
      92             :       // what we expect the api to get as request as condition not to reply fail
      93           1 :       sprintf(web.request,"GET /.commit_sha.txt HTTP/1.1\r\n" \
      94             :               "Host: localhost:%%d\r\n" \
      95             :               "Accept: */*\r\n" \
      96             :               "\r\n" );
      97             : 
      98             :       // start test server
      99           1 :       result=webserver(&child_pid,&port,&web,0);
     100           1 :       ck_assert_int_eq(result, 0);
     101           2 :       ck_assert_int_ne(port, 0);
     102           1 :       debug("result create webserver server: %d, port=%d\n",result,port);
     103             : 
     104             :       // prepare environment for command:
     105             : 
     106             : 
     107           1 :    struct trace_Struct * trace=NULL;
     108           1 :    init_dynamic_trace( &trace,"token","url",1);
     109             :       char baseHREF[1024];
     110             : 
     111           1 :       sprintf(baseHREF, "http://localhost:%d",port);
     112             : 
     113             : 
     114           1 :    int result_test=0;
     115             :         // -----------------------------------------------------------------------------
     116             :       // Test one, all conditions ok, should return 0 and have a valid gitlab_release
     117             :       // -----------------------------------------------------------------------------
     118           1 :    result_test=url_verify( commit_sha,baseHREF,trace);
     119           2 :         ck_assert_int_eq(result_test, 0);
     120             : 
     121             :       // stop
     122           1 :       stop_test_web(child_pid);
     123             : 
     124             :       // -----------------------------------------------------------------------------
     125             :       // change to non valid response
     126             :       // -----------------------------------------------------------------------------
     127             : 
     128           1 :       sprintf(web.reply_content,"");
     129             : 
     130             :       // start test server
     131           1 :       result=webserver(&child_pid,&port,&web,0);
     132           2 :       ck_assert_int_eq(result, 0);
     133           2 :       ck_assert_int_ne(port, 0);
     134           1 :       debug("result create webserver server: %d, port=%d\n",result,port);
     135             : 
     136             :       // set correct port for request
     137           1 :       sprintf(baseHREF, "http://localhost:%d",port);
     138           1 :    result_test=url_verify( commit_sha,baseHREF,trace);
     139           2 :    ck_assert_int_ne(result_test, 0);
     140             : 
     141             :       // -----------------------------------------------------------------------------
     142             :       //simulate connection to non existing server, stop webserver!!
     143             :       // -----------------------------------------------------------------------------
     144           1 :     stop_test_web(child_pid);
     145           1 :    result_test=url_verify( commit_sha,baseHREF,trace);
     146           2 :    ck_assert_int_ne(result_test, 0);
     147             : 
     148           1 :    free_dynamic_trace(&trace);
     149             : 
     150             : }
     151           1 : END_TEST
     152             : 
     153           1 : START_TEST(check_verify_http_config)
     154             : {
     155             : 
     156             :    data_exchange_t data_exchange;
     157             :    parameter_t parameters;
     158           1 :    void * opaque=&data_exchange;
     159             :    char * newarg[4];
     160             : 
     161             :   
     162           1 :    struct trace_Struct * trace=NULL;
     163           1 :    init_dynamic_trace( &trace,"token","url",1);
     164           1 :    data_exchange.trace=trace;
     165             : 
     166           1 :    data_exchange.needenvp=0;
     167           1 :    data_exchange.gid=getgid();
     168           1 :    data_exchange.uid=geteuid() ;
     169             : 
     170           1 :    cJSON * env_json=  cJSON_CreateObject();
     171           1 :    ck_assert_ptr_ne(env_json,NULL);
     172           1 :    data_exchange.env_json=env_json;
     173             : 
     174           1 :    data_exchange.paramlist=(char **)newarg;
     175           1 :    data_exchange.timeout=1;
     176           1 :    data_exchange.parameters=&parameters;
     177             :    test_stubs_t test_stubs;
     178           1 :    trace->tests=&test_stubs;
     179             :    // set results for tests
     180           1 :    test_stubs.exitcode=0;
     181           1 :    test_stubs.arg0="/bin/sh";
     182           1 :    test_stubs.arg1="-c";
     183           1 :    test_stubs.arg2="sudo /usr/sbin/nginx -t -q";
     184           1 :    int result=verify_http_config(opaque);
     185             : 
     186           2 :    ck_assert_int_eq(result, 0);
     187           1 :    free_dynamic_trace(&trace);
     188             : }
     189           1 : END_TEST
     190           1 : START_TEST(check_reload_http_config)
     191             : {
     192             : 
     193             :    data_exchange_t data_exchange;
     194             :    parameter_t parameters;
     195           1 :    void * opaque=&data_exchange;
     196             : 
     197             :    char * newarg[4];
     198           1 :    struct trace_Struct * trace=NULL;
     199           1 :    init_dynamic_trace( &trace,"token","url",1);
     200           1 :    data_exchange.trace=trace;
     201             : 
     202           1 :    data_exchange.needenvp=0;
     203           1 :    data_exchange.gid=getgid();
     204           1 :    data_exchange.uid=geteuid() ;
     205             : 
     206           1 :    cJSON * env_json=  cJSON_CreateObject();
     207           1 :    ck_assert_ptr_ne(env_json,NULL);
     208           1 :    data_exchange.env_json=env_json;
     209             : 
     210           1 :    data_exchange.paramlist=(char **)newarg;
     211           1 :    data_exchange.timeout=1;
     212           1 :    data_exchange.parameters=&parameters;
     213             :    test_stubs_t test_stubs;
     214           1 :    trace->tests=&test_stubs;
     215             :    // set results for tests
     216           1 :    test_stubs.exitcode=0;
     217           1 :    test_stubs.arg0="/bin/sh";
     218           1 :    test_stubs.arg1="-c";
     219           1 :    test_stubs.arg2="sudo /usr/sbin/nginx -s reload";
     220           1 :    test_stubs.arg3=NULL;
     221           1 :    int result=reload_http_config(opaque);
     222             : 
     223           2 :    ck_assert_int_eq(result, 0);
     224           1 :    free_dynamic_trace(&trace);
     225             : }
     226           1 : END_TEST
     227             : 
     228             : 
     229             : 
     230             : 
     231             : /*----------------------------------------------------------------------------------
     232             :  * get basepath of repo by url of repo
     233             :  * returns 0 on success and basepath
     234             :  *-----------------------------------------------------------------------------------*/
     235             : /* on curl repo.json, it returns=>
     236             : { "projects": ["https://gitlab.gioxa.com/deployctl/deployctl", "https://gitlab.gioxa.com/deployctl/cJSON"]
     237             :    "repos":   [{
     238             :              "rpm":   [{
     239             :                       "el7":  ["x86_64", "x86_64"]
     240             :                       }]
     241             :              }],
     242             :    "endpoints":       [{
     243             :                    "/rpm/el7/x86_64": ["rpm", "el7", "x86_64"]
     244             :                    }],
     245             :    "base_path":       "/opt/deploy/domain/repo.deployctl.com",
     246             :    "base_href":       "http://repo.deployctl.com"
     247             : }
     248             : */
     249             : 
     250             : //int url_repo_base_path  (char ** basepath,char * baseHREF,feedback_t * feedback)
     251           1 : START_TEST(check_url_repo_base_path)
     252             : {
     253           1 :    u_int16_t port=0;
     254             :    pid_t child_pid;
     255             : 
     256           1 :    int result=0;
     257             :    webserver_t web;
     258             : 
     259             :    // what content we want the api to serve if he got the request matching request
     260           1 :    const char * base_path_in="/test/url/basepath";
     261             : 
     262             :    // create cJSON reply, we only need to extract base_path
     263           1 :    cJSON * reply_json=cJSON_CreateObject();
     264           1 :    cJSON_AddStringToObject(reply_json, "base_path",base_path_in );
     265           1 :    char * reply_str=cJSON_Print(reply_json);
     266           1 :    ck_assert_ptr_ne(reply_str, NULL);
     267           1 :    sprintf(web.reply_content,"%s",reply_str);
     268           1 :    free(reply_str);
     269           1 :    reply_str=NULL;
     270           1 :    cJSON_Delete(reply_json);
     271           1 :    reply_json=NULL;
     272             : 
     273             :    // what response headers we want to sent back, in case request matches
     274           1 :    sprintf(web.reply_header,"%s","HTTP/1.1 200 OK\r\nServer: testweb/0.0.1\r\nContent-Length:%d\r\n\r\n%s");
     275             : 
     276             :    // what reply on fail
     277           1 :    const char * m404_mesg="{\"message\":\"404 NOT FOUND\"}";
     278           1 :    sprintf(web.reply_fail,"HTTP/1.1 404 NOT FOUND\r\nServer: testweb/0.0.1\r\nContent-Length:%lu\r\n\r\n%s",strlen(m404_mesg),m404_mesg);
     279             : 
     280             :    // what we expect the api to get as request as condition not to reply fail
     281           1 :    sprintf(web.request,"GET /repo.json HTTP/1.1\r\n" \
     282             :            "Host: localhost:%%d\r\n" \
     283             :            "Accept: */*\r\n" \
     284             :            "\r\n" );
     285             : 
     286             :    // start test server
     287           1 :    result=webserver(&child_pid,&port,&web,0);
     288           2 :    ck_assert_int_eq(result, 0);
     289           2 :    ck_assert_int_ne(port, 0);
     290           1 :    debug("result create webserver server: %d, port=%d\n",result,port);
     291             : 
     292             :    // prepare environment for command:
     293             : 
     294           1 :    struct trace_Struct * trace=NULL;
     295           1 :    init_dynamic_trace( &trace,"token","url",1);
     296             : 
     297             :    char baseHREF[1024];
     298             : 
     299           1 :    sprintf(baseHREF, "http://localhost:%d",port);
     300             : 
     301             : 
     302           1 :    int result_test=0;
     303           1 :    char * basepath=NULL;
     304             :    // -----------------------------------------------------------------------------
     305             :    // Test one, all conditions ok, should return 0 and have a valid gitlab_release
     306             :    // -----------------------------------------------------------------------------
     307           1 :    result_test= url_repo_base_path  ( &basepath,baseHREF,trace);
     308           2 :    ck_assert_int_eq(result_test, 0);
     309           2 :    ck_assert_str_eq(basepath, base_path_in);
     310           1 :    free(basepath);
     311             : 
     312             :    // stop
     313           1 :    stop_test_web(child_pid);
     314             : 
     315             :    // -----------------------------------------------------------------------------
     316             :    // change to non valid response
     317             :    // -----------------------------------------------------------------------------
     318             : 
     319           1 :    sprintf(web.reply_content,"");
     320             : 
     321             :    // start test server
     322           1 :    result=webserver(&child_pid,&port,&web,0);
     323           2 :    ck_assert_int_eq(result, 0);
     324           2 :    ck_assert_int_ne(port, 0);
     325           1 :    debug("result create webserver server: %d, port=%d\n",result,port);
     326             : 
     327             :    // set correct port for request
     328           1 :    sprintf(baseHREF, "http://localhost:%d",port);
     329             : 
     330           1 :     result_test= url_repo_base_path  ( &basepath,baseHREF,trace);
     331           2 :    ck_assert_int_ne(result_test, 0);
     332             : 
     333             :    // -----------------------------------------------------------------------------
     334             :    //simulate connection to non existing server, stop webserver!!
     335             :    // -----------------------------------------------------------------------------
     336           1 :    stop_test_web(child_pid);
     337           1 :    result_test= url_repo_base_path  ( &basepath,baseHREF,trace);
     338           2 :    ck_assert_int_ne(result_test, 0);
     339           1 :    free_dynamic_trace(&trace);
     340             : }
     341           1 : END_TEST
     342             : 
     343             : 
     344           5 : Suite * externals_suite(void)
     345             : {
     346             :    Suite *s;
     347             :    TCase *tc_core;
     348             :    //TCase *tc_progress;
     349           5 :    s = suite_create("test_externals");
     350             :    /* Core test case */
     351           5 :    tc_core = tcase_create("Core");
     352           5 :    tcase_add_checked_fixture(tc_core, NULL,NULL);
     353             :    //tcase_add_unchecked_fixture(tc_core, setup, teardown);
     354           5 :    tcase_set_timeout(tc_core,65);
     355           5 :    tcase_add_test(tc_core, check_url_verify);
     356           5 :         tcase_add_test(tc_core, check_verify_http_config);
     357           5 :    tcase_add_test(tc_core, check_reload_http_config);
     358           5 :    tcase_add_test(tc_core, check_url_repo_base_path);
     359             : 
     360           5 :    suite_add_tcase(s, tc_core);
     361           5 :    return s;
     362             : }
     363             : 
     364           5 : int main(void)
     365             : {
     366             :    int number_failed;
     367             :    Suite *s;
     368             :    SRunner *sr;
     369             : 
     370           5 :    s = externals_suite();
     371           5 :    sr = srunner_create(s);
     372           5 :    srunner_run_all(sr, CK_VERBOSE );
     373           1 :    number_failed = srunner_ntests_failed(sr);
     374           1 :    srunner_free(sr);
     375           1 :    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
     376             : }

Generated by: LCOV version 1.10