timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ttail_search_check.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <check.h>
  2. #include <stdio.h>
  3. #include <libgen.h>
  4. #include "ttail.h"
  5. #include "ttail_init.h"
  6. #include "ttail_search.h"
  7. ttail_t *ttail;
  8. char *samples[5] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
  9. "./samples/4.log", "./samples/5.log"};
  10. off_t samples_sz[5] = { 442, 0, 893, 2587, 2310 };
  11. FILE *fpl;
  12. void setup_file_line(void)
  13. {
  14. fpl = fopen(samples[0], "r");
  15. if(!fpl)
  16. {
  17. perror("Unable to open file for testing :");
  18. ck_abort();
  19. }
  20. }
  21. void teardown_file_line(void)
  22. {
  23. fclose(fpl);
  24. }
  25. void teardown_closest(void)
  26. {
  27. ttail_free(ttail);
  28. }
  29. void setup_closest(void)
  30. {
  31. ttail = ttail_init(1, (char**)&"foo");
  32. }
  33. void setup_closest_fileinit(void)
  34. {
  35. setup_closest();
  36. size_t i;
  37. for(i=0; i<5; i++)
  38. {
  39. ttail_add_logfile(ttail, samples[i]);
  40. }
  41. }
  42. void teardown_closest_fileinit(void)
  43. {
  44. teardown_closest();
  45. _ttail_search_file_free(ttail);
  46. }
  47. START_TEST (test_search_closest_init)
  48. {
  49. int ret;
  50. ret = _ttail_search_closest_files_init(ttail);
  51. ck_assert_int_eq(ret,0);
  52. ck_assert(ttail->session->file.vpos == 0);
  53. #ifdef HUGEFILE
  54. ck_assert_int_eq(ttail->session->file.sz_div,0);
  55. #endif
  56. }
  57. END_TEST
  58. START_TEST (test_search_closest_init_filesz)
  59. {
  60. size_t i;
  61. _ttail_search_closest_files_init(ttail);
  62. for(i=0;i<5;i++)
  63. {
  64. ck_assert(samples_sz[i] == ttail->session->file.file_sz[i]);
  65. }
  66. }
  67. END_TEST
  68. START_TEST (test_search_closest_init_vfile)
  69. {
  70. size_t i;
  71. off_t full_sz;
  72. _ttail_search_closest_files_init(ttail);
  73. full_sz = 0;
  74. for(i=0;i<5;i++)
  75. {
  76. ck_assert(full_sz == ttail->session->file.vfile[i]);
  77. full_sz += samples_sz[i];
  78. }
  79. ck_assert(full_sz == ttail->session->file.vsz);
  80. }
  81. END_TEST
  82. /*
  83. * _ttail_file_next_line() & _ttail_file_line_start() tests
  84. */
  85. START_TEST (test_file_line_next)
  86. {
  87. long res;
  88. res = _ttail_file_next_line(fpl);
  89. ck_assert(res == 77);
  90. res = _ttail_file_next_line(fpl);
  91. ck_assert(res == 136);
  92. res = _ttail_file_next_line(fpl);
  93. ck_assert(res == 221);
  94. res = _ttail_file_next_line(fpl);
  95. ck_assert(res == 298);
  96. res = _ttail_file_next_line(fpl);
  97. ck_assert(res == 357);
  98. res = _ttail_file_next_line(fpl);
  99. ck_assert(res == 0);
  100. res = _ttail_file_next_line(fpl);
  101. ck_assert(res == -1);
  102. }
  103. END_TEST
  104. START_TEST (test_file_line_start)
  105. {
  106. long res;
  107. fseek(fpl, -1, SEEK_END);
  108. res = _ttail_file_start_line(fpl);
  109. ck_assert(res == 357);
  110. res = _ttail_file_start_line(fpl);
  111. ck_assert(res == 357);
  112. fseek(fpl, -1, SEEK_CUR);
  113. res = _ttail_file_start_line(fpl);
  114. ck_assert(res == 298);
  115. fseek(fpl, -1, SEEK_CUR);
  116. res = _ttail_file_start_line(fpl);
  117. ck_assert(res == 221);
  118. fseek(fpl, -1, SEEK_CUR);
  119. res = _ttail_file_start_line(fpl);
  120. ck_assert(res == 136);
  121. fseek(fpl, -1, SEEK_CUR);
  122. res = _ttail_file_start_line(fpl);
  123. ck_assert(res == 77);
  124. fseek(fpl, -1, SEEK_CUR);
  125. res = _ttail_file_start_line(fpl);
  126. ck_assert(res == 0);
  127. }
  128. END_TEST
  129. START_TEST (test_file_minmax1)
  130. {
  131. int r;
  132. struct tm tm[2];
  133. printf("%d\n", ttail->logfile_sz);
  134. ttail->flag |= TTAIL_FLAG_FORMAT;
  135. //ttail->fmt = "%b%n%d %H:%M";
  136. r = _ttail_search_closest_files_init(ttail);
  137. ck_assert_int_eq(r, 0);
  138. /*
  139. r = _ttail_file_minmax(ttail, 0, tm);
  140. */
  141. }
  142. END_TEST
  143. Suite * ttail_search_suite(void)
  144. {
  145. Suite *s;
  146. TCase *tc_search_closest_fileinit, *tc_file_line,
  147. *tc_file_minmax;
  148. s = suite_create("ttail search checks");
  149. tc_search_closest_fileinit = tcase_create("\
  150. ttail_logline_closest_files_init() checks");
  151. tcase_add_checked_fixture(tc_search_closest_fileinit,
  152. setup_closest_fileinit, teardown_closest_fileinit);
  153. tcase_add_test(tc_search_closest_fileinit, test_search_closest_init);
  154. tcase_add_test(tc_search_closest_fileinit,
  155. test_search_closest_init_filesz);
  156. tcase_add_test(tc_search_closest_fileinit,
  157. test_search_closest_init_vfile);
  158. tc_file_line = tcase_create("ttail_file_*line*() checks");
  159. tcase_add_checked_fixture(tc_file_line,
  160. setup_file_line, teardown_file_line);
  161. tcase_add_test(tc_file_line, test_file_line_next);
  162. tcase_add_test(tc_file_line, test_file_line_start);
  163. tc_file_minmax = tcase_create("ttail_file_minmax() checks");
  164. tcase_add_checked_fixture(tc_file_minmax,
  165. setup_closest_fileinit, teardown_closest_fileinit);
  166. tcase_add_test(tc_file_minmax, test_file_minmax1);
  167. suite_add_tcase(s, tc_search_closest_fileinit);
  168. suite_add_tcase(s, tc_file_line);
  169. suite_add_tcase(s, tc_file_minmax);
  170. return s;
  171. }
  172. int main(int argc, char **argv)
  173. {
  174. int number_failed = 0;
  175. SRunner *sr;
  176. chdir(dirname(argv[0])); /* move in ./tests dir */
  177. sr = srunner_create(ttail_search_suite());
  178. srunner_set_fork_status(sr, CK_FORK);
  179. srunner_run_all(sr,CK_VERBOSE);
  180. number_failed = srunner_ntests_failed(sr);
  181. srunner_free(sr);
  182. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  183. }