timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ttail_check.h 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #ifndef _ttail_check_h__
  2. #define _ttail_check_h__
  3. #include <check.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <libgen.h>
  8. #include <fcntl.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include "ttail.h"
  12. #include "ttail_init.h"
  13. ttail_t *ttail;
  14. #define __Fname_sz 5
  15. #define FNAME_NO_EXIST 0
  16. #define FNAME_EXIST 1
  17. /**@brief Start a ttail test
  18. *@param const char* suite_str test suite string
  19. *@param const char* tc_str test case string
  20. */
  21. #define TTAIL_CHECK_START(suite_str, tc_str) \
  22. Suite * ttail_test_suite(void) \
  23. {\
  24. Suite *s;\
  25. TCase *tc;\
  26. s = suite_create(suite_str);\
  27. tc = tcase_create(tc_str);
  28. /**@brief Set the setup & teardown fixture
  29. *@param void (*setup)()
  30. *@param void (*teardown)()
  31. */
  32. #define TTAIL_SET_FIXTURE(setup, teardown) \
  33. tcase_add_checked_fixture(tc, setup, teardown)
  34. /**@brief Add a test function defined using START_TEST(name) { ... } END_TEST
  35. */
  36. #define TTAIL_ADD_TEST(test) tcase_add_test(tc, test)
  37. /**@brief End a ttail test */
  38. #define TTAIL_CHECK_END \
  39. suite_add_tcase(s, tc);\
  40. return s;\
  41. }\
  42. \
  43. int main(int argc, char **argv) {\
  44. int n_fail;\
  45. SRunner *sr;\
  46. n_fail = chdir(dirname(argv[0])); /* move in ./tests dir */ \
  47. if(n_fail < 0)\
  48. {\
  49. perror("Unable to chdir in tests folder");\
  50. }\
  51. sr = srunner_create(ttail_test_suite());\
  52. srunner_set_fork_status(sr, CK_FORK);\
  53. srunner_run_all(sr, CK_VERBOSE);\
  54. n_fail = srunner_ntests_failed(sr);\
  55. srunner_free(sr);\
  56. return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;\
  57. }
  58. #define ck_assert_printf(test, format, ...) {\
  59. char _err_msg[512];\
  60. snprintf(_err_msg, sizeof(char) * 512,format, __VA_ARGS__);\
  61. ck_assert_msg(test, _err_msg);\
  62. }
  63. #define close_stdpipe() {\
  64. close(STDPIPE);\
  65. STDPIPE = -1;\
  66. }
  67. #define send_sample_stdpipe(id) {\
  68. int ret;\
  69. char buff[1024];\
  70. while((ret= read(samples_fd[id], buff, 1024)) > 0)\
  71. {\
  72. ret = write(STDPIPE, buff, strlen(buff));\
  73. if(ret < 0)\
  74. {\
  75. perror("Unable to write through pipe");\
  76. ck_abort_msg("Unable to write through pipe");\
  77. }\
  78. }\
  79. if(ret < 0)\
  80. {\
  81. perror("Unable to read through pipe");\
  82. ck_abort_msg("Unable to read through pipe");\
  83. }\
  84. }
  85. char *fname[__Fname_sz];
  86. /*date formats*/
  87. char *fmt[] = TTAIL_DEFAULT_FORMATS;
  88. /* logfiles samples */
  89. char *samples[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
  90. "./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
  91. off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
  92. int samples_fd[6];
  93. int STDPIPE;
  94. void teardown_ttail(void)
  95. {
  96. ttail_free(ttail);
  97. }
  98. void setup_ttail_empty(void)
  99. {
  100. ttail = ttail_init(1, (char**)&"foo");
  101. }
  102. void setup_fname(void)
  103. {
  104. int i;
  105. FILE *fp;
  106. for(i=0;i<__Fname_sz;i++)
  107. {
  108. fname[i] = NULL;
  109. }
  110. for(i=0; i<__Fname_sz; i++)
  111. {
  112. fname[i] = tempnam(NULL, "ttail_check");
  113. if(i%2)
  114. {
  115. if((fp=fopen(fname[i], "w+")) == NULL)
  116. {
  117. perror("Unable to create file for testing");
  118. ck_abort_msg("Unable to create file for testing");
  119. }
  120. if(fclose(fp))
  121. {
  122. perror("Unable to close file for testing");
  123. ck_abort_msg("Unable to close file for testing");
  124. }
  125. }
  126. }
  127. }
  128. void teardown_fname(void)
  129. {
  130. int i;
  131. for(i=0; i<__Fname_sz; i++)
  132. {
  133. unlink(fname[i]);
  134. if(fname[i] != NULL)
  135. {
  136. free(fname[i]);
  137. }
  138. }
  139. }
  140. void setup_file_line(void)
  141. {
  142. setup_ttail_empty();
  143. ttail_add_logfile(ttail, samples[0]);
  144. }
  145. void teardown_closest(void)
  146. {
  147. ttail_free(ttail);
  148. }
  149. void setup_closest(void)
  150. {
  151. ttail = ttail_init(1, (char**)&"foo");
  152. }
  153. void setup_closest_stdin(void)
  154. {
  155. int ret, pipes[2], i;
  156. ret = pipe(pipes);
  157. if(ret < 0)
  158. {
  159. perror("fail to open pipe");
  160. ck_abort_msg("fail to open pipe");
  161. }
  162. _ttail_set_stdin(pipes[0]);
  163. STDPIPE = pipes[1];
  164. for(i=0; i<sizeof(samples_fd)/sizeof(int); i++)
  165. {
  166. samples_fd[i] = open(samples[i], 0, O_RDONLY);
  167. }
  168. setup_closest();
  169. }
  170. void teardown_closest_stdin(void)
  171. {
  172. if(STDPIPE > 0)
  173. {
  174. close(STDPIPE);
  175. }
  176. teardown_closest();
  177. }
  178. void setup_closest_fileinit(void)
  179. {
  180. size_t i;
  181. setup_closest();
  182. for(i=0; i<6; i++)
  183. {
  184. ttail_add_logfile(ttail, samples[i]);
  185. }
  186. }
  187. void teardown_closest_fileinit(void)
  188. {
  189. _ttail_search_file_free(ttail);
  190. teardown_closest();
  191. }
  192. #endif