igr-runner.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <string>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <spawn.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7. extern char **environ;
  8. // Integration test suite runner.
  9. int main(int argc, char **argv)
  10. {
  11. const char * const test_dirs[] = { "basic", "environ", "environ2", "ps-environ", "chain-to", "force-stop",
  12. "restart", "check-basic", "check-cycle", "check-lint", "reload1", "reload2", "no-command-error",
  13. "add-rm-dep", "var-subst", "svc-start-fail", "dep-not-found" };
  14. constexpr int num_tests = sizeof(test_dirs) / sizeof(test_dirs[0]);
  15. int passed = 0;
  16. int skipped = 0;
  17. int failed = 0;
  18. bool aborted_run = false;
  19. std::cout << "============== INTEGRATION TESTS =====================" << std::endl;
  20. for (int i = 0; i < num_tests; i++) {
  21. const char * test_dir = test_dirs[i];
  22. std::string prog_path = "./run-test.sh";
  23. char * const p_argv[2] = { const_cast<char *>(prog_path.c_str()), nullptr };
  24. std::cout << test_dir << "... ";
  25. // "Use posix_spawn", they said. "It will be easy", they said.
  26. if (chdir(test_dir) != 0) {
  27. std::cerr << "Couldn't chdir: " << test_dir << ": " << strerror(errno) << std::endl;
  28. continue;
  29. }
  30. posix_spawn_file_actions_t p_actions;
  31. posix_spawnattr_t p_attr;
  32. if (posix_spawn_file_actions_init(&p_actions) != 0) {
  33. // out of memory?
  34. std::cerr << "Error launching process: " << test_dir << "/run-test.sh: " << strerror(errno) << std::endl;
  35. aborted_run = true;
  36. break;
  37. }
  38. if (posix_spawnattr_init(&p_attr) != 0) {
  39. // out of memory?
  40. std::cerr << "Error launching process: " << test_dir << "/run-test.sh: " << strerror(errno) << std::endl;
  41. aborted_run = true;
  42. break;
  43. }
  44. pid_t subproc_pid;
  45. if (posix_spawn(&subproc_pid, prog_path.c_str(), &p_actions, &p_attr, p_argv, environ) != 0) {
  46. // fail out
  47. std::cerr << "Failed to run run-test.sh in " << test_dir << std::endl;
  48. continue;
  49. }
  50. int wstatus;
  51. if (waitpid(subproc_pid, &wstatus, 0) == -1) {
  52. std::cout << "(unknown)" << std::endl;
  53. std::cerr << "waitpid() failed" << std::endl;
  54. aborted_run = true;
  55. break;
  56. }
  57. if (WIFEXITED(wstatus)) {
  58. if (WEXITSTATUS(wstatus) == 0) {
  59. std::cout << "PASSED" << std::endl;
  60. passed++;
  61. }
  62. else if (WEXITSTATUS(wstatus) == 1) {
  63. std::cout << "FAILED" << std::endl;
  64. failed++;
  65. }
  66. else if (WEXITSTATUS(wstatus) == 2) {
  67. std::cout << "SKIPPED" << std::endl;
  68. skipped++;
  69. }
  70. else {
  71. std::cout << "???" << std::endl;
  72. }
  73. }
  74. else {
  75. std::cout << "*** terminated abnormally ***" << std::endl;
  76. aborted_run = true;
  77. break;
  78. }
  79. posix_spawnattr_destroy(&p_attr);
  80. posix_spawn_file_actions_destroy(&p_actions);
  81. chdir("..");
  82. }
  83. std::cout << "======================================================" << std::endl;
  84. if (! aborted_run) {
  85. std::cout << "Test run finished.\n"
  86. "Passed: " << passed << "\n"
  87. "Failed: " << failed;
  88. if (failed != 0) {
  89. std::cout << " XXX";
  90. }
  91. std::cout << "\n"
  92. "Skipped: " << skipped << std::endl;
  93. }
  94. else {
  95. std::cout << "Test run aborted." << std::endl;
  96. }
  97. return failed == 0 ? 0 : 1;
  98. }