-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathTestRedis.php
More file actions
198 lines (152 loc) · 5.74 KB
/
Copy pathTestRedis.php
File metadata and controls
198 lines (152 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php define('PHPREDIS_TESTRUN', true);
require_once __DIR__ . "/TestSuite.php";
require_once __DIR__ . "/RedisTest.php";
require_once __DIR__ . "/RedisArrayTest.php";
require_once __DIR__ . "/RedisClusterTest.php";
require_once __DIR__ . "/RedisSentinelTest.php";
function getClassArray($classes) {
$result = [];
if ( ! is_array($classes))
$classes = [$classes];
foreach ($classes as $class) {
$result = array_merge($result, explode(',', $class));
}
return array_unique(
array_map(function ($v) { return strtolower($v); },
$result
)
);
}
function getTestClass($class) {
$valid_classes = [
'redis' => 'Redis_Test',
'redisarray' => 'Redis_Array_Test',
'rediscluster' => 'Redis_Cluster_Test',
'redissentinel' => 'Redis_Sentinel_Test'
];
/* Return early if the class is one of our built-in ones */
if (isset($valid_classes[$class]))
return $valid_classes[$class];
/* Try to load it */
return TestSuite::loadTestClass($class);
}
function raHosts($host, $ports) {
if ( ! is_array($ports))
$ports = [6379, 6380, 6381, 6382];
return array_map(function ($port) use ($host) {
return sprintf("%s:%d", $host, $port);
}, $ports);
}
function getOriginalCommand(array $args): array {
$fallback = array_merge([PHP_BINARY], $args);
/* On Linux, /proc preserves the executable and any PHP options exactly as
* they were invoked. */
if ( ! is_readable('/proc/self/cmdline'))
return $fallback;
$raw = @file_get_contents('/proc/self/cmdline');
if ($raw === false)
return $fallback;
$command = explode("\0", rtrim($raw, "\0"));
/* The PHP-visible argument vector should be the tail of the process
* command line, beginning with the test script. */
if (count($command) < count($args) ||
array_slice($command, -count($args)) !== $args)
{
return $fallback;
}
return $command;
}
function escapeCommandArgument(string $arg): string {
if (preg_match('/^[a-zA-Z0-9_@%+=:,\.\/-]+$/D', $arg))
return $arg;
return escapeshellarg($arg);
}
function getFailedTestCommand(array $args, array $failed_tests): string {
$command = getOriginalCommand($args);
$script_index = count($command) - count($args);
$result = array_slice($command, 0, $script_index + 1);
/* Keep the original invocation but replace its test filters with the
* complete set of failed tests. */
for ($i = $script_index + 1; $i < count($command); $i++) {
if ($command[$i] === '--test') {
$i++;
continue;
}
if (strncmp($command[$i], '--test=', strlen('--test=')) === 0)
continue;
$result[] = $command[$i];
}
$result[] = '--test';
$result[] = implode(',', $failed_tests);
return implode(' ', array_map('escapeCommandArgument', $result));
}
function printFailedTestCommand(array $args) {
$failed_tests = TestSuite::getFailedTests();
if ( ! $failed_tests)
return;
echo "\nTo rerun only the failed tests:\n";
echo getFailedTestCommand($args, $failed_tests) . "\n";
}
/* Make sure errors go to stdout and are shown */
error_reporting(E_ALL);
ini_set( 'display_errors','1');
/* Grab options */
$opt = getopt('', ['host:', 'port:', 'tls-port:', 'class:', 'test:', 'nocolors', 'user:', 'auth:']);
/* The test class(es) we want to run */
$classes = getClassArray($opt['class'] ?? 'redis');
$colorize = !isset($opt['nocolors']);
/* Get our test filter if provided one */
$filter = $opt['test'] ?? NULL;
/* Grab override host/port if it was passed */
$host = $opt['host'] ?? '127.0.0.1';
$port = $opt['port'] ?? 6379;
$tls_port = $opt['tls-port'] ?? 6378;
/* Get optional username and auth (password) */
$user = $opt['user'] ?? NULL;
$auth = $opt['auth'] ?? NULL;
if ($user && $auth) {
$auth = [$user, $auth];
} else if ($user && ! $auth) {
echo TestSuite::make_warning("User passed without a password!\n");
}
/* Toggle colorization in our TestSuite class */
TestSuite::flagColorization($colorize);
/* Let the user know this can take a bit of time */
echo "Note: these tests might take up to a minute. Don't worry :-)\n";
echo "Using PHP version " . PHP_VERSION . " (" . (PHP_INT_SIZE * 8) . " bits)\n";
foreach ($classes as $class) {
$class = getTestClass($class);
/* Depending on the classes being tested, run our tests on it */
echo "Testing class ";
if ($class == 'Redis_Array_Test') {
echo TestSuite::make_bold("RedisArray") . "\n";
$full_ring = raHosts($host, $port);
$sub_ring = array_slice($full_ring, 0, -1);
echo TestSuite::make_bold("Full Ring: ") . implode(' ', $full_ring) . "\n";
echo TestSuite::make_bold(" New Ring: ") . implode(' ', $sub_ring) . "\n";
foreach([true, false] as $useIndex) {
echo "\n". ($useIndex ? "WITH" : "WITHOUT") . " per-node index:\n";
/* The various RedisArray subtests we can run */
$test_classes = [
'Redis_Array_Test', 'Redis_Rehashing_Test', 'Redis_Auto_Rehashing_Test',
'Redis_Multi_Exec_Test', 'Redis_Distributor_Test'
];
foreach ($test_classes as $test_class) {
/* Run until we encounter a failure */
if (run_ra_tests($test_class, $filter, $host, $full_ring, $sub_ring, $auth) != 0) {
printFailedTestCommand($argv);
exit(1);
}
}
}
} else {
echo TestSuite::make_bold($class) . "\n";
if (TestSuite::run("$class", $filter, $host, $port, $auth, $tls_port)) {
printFailedTestCommand($argv);
exit(1);
}
}
}
/* Success */
exit(0);
?>