1 /*
2  * Copyright (C) 2009-2017 Codership Oy <info@codership.com>
3  */
4 
5 #include "check_gcomm.hpp"
6 
7 #include "gu_string_utils.hpp" // strsplit()
8 #include "gu_exception.hpp"
9 #include "gu_logger.hpp"
10 
11 #include <string>
12 #include <vector>
13 #include <algorithm>
14 #include <cstdlib>
15 #include <check.h>
16 #include <string.h>
17 #include <unistd.h>
18 
19 // <using namespace gcomm;
20 
21 using std::string;
22 using std::vector;
23 
24 typedef Suite* (*suite_creator_f)();
25 
26 struct GCommSuite
27 {
28     string name;
29     suite_creator_f suite;
30 };
31 
32 static GCommSuite suites[] = {
33     {"fair_send_queue", fair_send_queue_suite},
34     {"util", util_suite},
35     {"types", types_suite},
36     {"evs2", evs2_suite},
37     {"pc", pc_suite},
38     {"", 0}
39 };
40 
41 #define LOG_FILE "check_gcomm.log"
42 
main(int argc,char * argv[])43 int main(int argc, char* argv[])
44 {
45 
46     SRunner* sr = srunner_create(0);
47     vector<string>* suits = 0;
48     FILE* log_file(0);
49 
50     if (argc > 1 && !strcmp(argv[1],"nofork")) {
51         srunner_set_fork_status(sr, CK_NOFORK);
52     }
53     else if (argc > 1 && strcmp(argv[1], "nolog") == 0)
54     { /* no log redirection */}
55     else { // running in the background, loggin' to file
56         log_file = fopen (LOG_FILE, "w");
57         if (!log_file) return EXIT_FAILURE;
58         gu_conf_set_log_file (log_file);
59 
60         // redirect occasional stderr there as well
61         if (dup2(fileno(log_file), 2) < 0)
62         {
63             perror("dup2() failed: ");
64             return EXIT_FAILURE;
65         }
66     }
67 
68     if (::getenv("CHECK_GCOMM_DEBUG"))
69     {
70         gu_log_max_level = GU_LOG_DEBUG;
71         //gu::Logger::enable_debug(true);
72     }
73 
74     log_info << "check_gcomm, start tests";
75     if (::getenv("CHECK_GCOMM_SUITES"))
76     {
77         suits = new vector<string>(gu::strsplit(::getenv("CHECK_GCOMM_SUITES"), ','));
78     }
79 
80     for (size_t i = 0; suites[i].suite != 0; ++i)
81     {
82         if (suits == 0 ||
83             find(suits->begin(), suits->end(), suites[i].name) != suits->end())
84         {
85             srunner_add_suite(sr, suites[i].suite());
86         }
87     }
88     delete suits;
89     suits = 0;
90 
91     srunner_run_all(sr, CK_NORMAL);
92     log_info << "check_gcomm, run all tests";
93     int n_fail = srunner_ntests_failed(sr);
94     srunner_free(sr);
95     if (log_file) fclose(log_file);
96     if (0 == n_fail && 0 != log_file) ::unlink(LOG_FILE);
97 
98     return n_fail == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
99 }
100