include.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. function get_fpm_path() /* {{{ */
  3. {
  4. $php_path = getenv("TEST_PHP_EXECUTABLE");
  5. for ($i = 0; $i < 2; $i++) {
  6. $slash_pos = strrpos($php_path, "/");
  7. if ($slash_pos) {
  8. $php_path = substr($php_path, 0, $slash_pos);
  9. } else {
  10. return false;
  11. }
  12. }
  13. if ($php_path && is_dir($php_path) && file_exists($php_path."/fpm/php-fpm") && is_executable($php_path."/fpm/php-fpm")) {
  14. /* gotcha */
  15. return $php_path."/fpm/php-fpm";
  16. }
  17. return false;
  18. }
  19. /* }}} */
  20. function run_fpm($config, &$out = false, $extra_args = '') /* {{{ */
  21. {
  22. $cfg = dirname(__FILE__).'/test-fpm-config.tmp';
  23. file_put_contents($cfg, $config);
  24. $desc = [];
  25. if ($out !== false) {
  26. $desc = [1 => array('pipe', 'w')];
  27. }
  28. /* Since it's not possible to spawn a process under linux without using a
  29. * shell in php (why?!?) we need a little shell trickery, so that we can
  30. * actually kill php-fpm */
  31. $fpm = proc_open('killit () { kill $child; }; trap killit TERM; '.get_fpm_path().' -F -O -y '.$cfg.' '.$extra_args.' 2>&1 & child=$!; wait', $desc, $pipes);
  32. register_shutdown_function(
  33. function($fpm) use($cfg) {
  34. @unlink($cfg);
  35. if (is_resource($fpm)) {
  36. @proc_terminate($fpm);
  37. while (proc_get_status($fpm)['running']) {
  38. usleep(10000);
  39. }
  40. }
  41. },
  42. $fpm
  43. );
  44. if ($out !== false) {
  45. $out = $pipes[1];
  46. }
  47. return $fpm;
  48. }
  49. /* }}} */
  50. function run_fpm_till($needle, $config, $max = 10) /* {{{ */
  51. {
  52. $i = 0;
  53. $fpm = run_fpm($config, $tail);
  54. if (is_resource($fpm)) {
  55. while($i < $max) {
  56. $i++;
  57. $line = fgets($tail);
  58. if(preg_match($needle, $line) === 1) {
  59. break;
  60. }
  61. }
  62. if ($i >= $max) {
  63. $line = false;
  64. }
  65. proc_terminate($fpm);
  66. stream_get_contents($tail);
  67. fclose($tail);
  68. proc_close($fpm);
  69. }
  70. return $line;
  71. }
  72. /* }}} */
  73. function fpm_display_log($tail, $n=1, $ignore='systemd') {
  74. while ($n) {
  75. $a = fgets($tail);
  76. if (empty($ignore) || !strpos($a, $ignore)) {
  77. echo $a;
  78. $n--;
  79. }
  80. }
  81. }
  82. function run_request($host, $port, $uri='/ping', $query='', $headers=array()) {
  83. require_once 'fcgi.inc';
  84. $client = new Adoy\FastCGI\Client($host, $port);
  85. $params = array_merge(array(
  86. 'GATEWAY_INTERFACE' => 'FastCGI/1.0',
  87. 'REQUEST_METHOD' => 'GET',
  88. 'SCRIPT_FILENAME' => $uri,
  89. 'SCRIPT_NAME' => $uri,
  90. 'QUERY_STRING' => $query,
  91. 'REQUEST_URI' => $uri . ($query ? '?'.$query : ""),
  92. 'DOCUMENT_URI' => $uri,
  93. 'SERVER_SOFTWARE' => 'php/fcgiclient',
  94. 'REMOTE_ADDR' => '127.0.0.1',
  95. 'REMOTE_PORT' => '9985',
  96. 'SERVER_ADDR' => '127.0.0.1',
  97. 'SERVER_PORT' => '80',
  98. 'SERVER_NAME' => php_uname('n'),
  99. 'SERVER_PROTOCOL' => 'HTTP/1.1',
  100. 'CONTENT_TYPE' => '',
  101. 'CONTENT_LENGTH' => 0
  102. ), $headers);
  103. return $client->request($params, false)."\n";
  104. }