run_tests_valgrind.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <err.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. /* Note: we already test safe_mode in run_tests.c */
  17. static const char *can_run_vg(struct manifest *m)
  18. {
  19. char *output = run_command(m, "valgrind -q true");
  20. if (output)
  21. return talloc_asprintf(m, "No valgrind support: %s", output);
  22. return NULL;
  23. }
  24. struct run_tests_result {
  25. struct list_node list;
  26. struct ccan_file *file;
  27. const char *output;
  28. };
  29. static void *do_run_tests_vg(struct manifest *m)
  30. {
  31. struct list_head *list = talloc(m, struct list_head);
  32. struct run_tests_result *res;
  33. struct ccan_file *i;
  34. char *cmdout;
  35. char *olddir;
  36. /* We run tests in the module directory, so any paths
  37. * referenced can all be module-local. */
  38. olddir = talloc_getcwd(m);
  39. if (!olddir)
  40. err(1, "Could not save cwd");
  41. if (chdir(m->dir) != 0)
  42. err(1, "Could not chdir to %s", m->dir);
  43. list_head_init(list);
  44. list_for_each(&m->run_tests, i, list) {
  45. run_tests_vg.total_score++;
  46. /* FIXME: timeout here */
  47. cmdout = run_command(m, "valgrind -q %s", i->compiled);
  48. if (cmdout) {
  49. res = talloc(list, struct run_tests_result);
  50. res->file = i;
  51. res->output = talloc_steal(res, cmdout);
  52. list_add_tail(list, &res->list);
  53. }
  54. }
  55. list_for_each(&m->api_tests, i, list) {
  56. run_tests_vg.total_score++;
  57. /* FIXME: timeout here */
  58. cmdout = run_command(m, "valgrind -q %s", i->compiled);
  59. if (cmdout) {
  60. res = talloc(list, struct run_tests_result);
  61. res->file = i;
  62. res->output = talloc_steal(res, cmdout);
  63. list_add_tail(list, &res->list);
  64. }
  65. }
  66. if (list_empty(list)) {
  67. talloc_free(list);
  68. list = NULL;
  69. }
  70. if (chdir(olddir) != 0)
  71. err(1, "Could not chdir to %s", olddir);
  72. return list;
  73. }
  74. static unsigned int score_run_tests_vg(struct manifest *m, void *check_result)
  75. {
  76. struct list_head *list = check_result;
  77. struct run_tests_result *i;
  78. unsigned int score = run_tests_vg.total_score;
  79. list_for_each(list, i, list)
  80. score--;
  81. return score;
  82. }
  83. static const char *describe_run_tests_vg(struct manifest *m,
  84. void *check_result)
  85. {
  86. struct list_head *list = check_result;
  87. char *descrip = talloc_strdup(check_result, "Running tests under valgrind failed:\n");
  88. struct run_tests_result *i;
  89. list_for_each(list, i, list)
  90. descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
  91. i->file->name, i->output);
  92. return descrip;
  93. }
  94. static void run_under_debugger_vg(struct manifest *m, void *check_result)
  95. {
  96. struct list_head *list = check_result;
  97. struct run_tests_result *first;
  98. char *command;
  99. if (!ask("Should I run the first failing test under the debugger?"))
  100. return;
  101. first = list_top(list, struct run_tests_result, list);
  102. command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
  103. first->file->compiled);
  104. system(command);
  105. }
  106. struct ccanlint run_tests_vg = {
  107. .name = "run and api tests under valgrind",
  108. .score = score_run_tests_vg,
  109. .check = do_run_tests_vg,
  110. .describe = describe_run_tests_vg,
  111. .can_run = can_run_vg,
  112. .handle = run_under_debugger_vg
  113. };
  114. REGISTER_TEST(run_tests_vg, &run_tests, NULL);