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