-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-tests-against-multiple-db-versions.php
executable file
·337 lines (281 loc) · 12.1 KB
/
run-tests-against-multiple-db-versions.php
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env php
<?php
function readFromLine( $prompt = '' ) {
echo $prompt;
return trim(rtrim( fgets( STDIN ), PHP_EOL ));
}
function sleepWithEcho(int $seconds) {
echo 'Sleeping....' . PHP_EOL;
sleep($seconds);
echo 'Waking....' . PHP_EOL;
}
function echoWithLineBreaks(string $str): void {
echo PHP_EOL . $str . PHP_EOL;
}
function readableElapsedTime($microtime, $format = null, $round = 3) {
if (is_null($format)) {
$format = '%.3f%s';
}
if ($microtime >= 3600) {
$unit = ' hour(s)';
$time = round(($microtime / 3600), $round);
} elseif ($microtime >= 60) {
$unit = ' minute(s)';
$time = round(($microtime / 60), $round);
} elseif ($microtime >= 1) {
$unit = ' second(s)';
$time = round($microtime, $round);
} else {
$unit = 'ms';
$time = round($microtime*1000);
$format = preg_replace('/(%.[\d]+f)/', '%d', $format);
}
return sprintf($format, $time, $unit);
}
$new_line = PHP_EOL;
echoWithLineBreaks(
'WARNING: This test suite could take up to 4 hours to run depending on the'
. ' performance capability of this computer.' . $new_line
. "\t Podman also needs to be installed in order for the tests to run." . $new_line
);
$console_prompt = "If you have an instance of (MySql / Mariadb) and / or Postgresql"
. " running, please stop them and then press Enter.{$new_line}This"
. " script will be spawning new container instances of MySql & Postgresql"
. " that will be forwarding to ports 3306 and 5432 on this machine:";
$console_response = readFromLine($console_prompt); // hitting enter returns an empty string,
// maybe later other options could be read
// into this variable
$console_prompt2 = "Please enter a password that would be used for the Mysql & Mariadb root accounts:";
$mysql_root_psw = readFromLine(PHP_EOL . $console_prompt2);
$test_results = [];
$mysql_maria_db_sql_dsn = 'mysql:host=127.0.0.1;port=3306';
$mysql_user = 'root';
$pgsql_dsn = 'pgsql:host=127.0.0.1;port=5432;user=postgres;password=doesntmatter;dbname=blog';
$pgsql_user = 'postgres';
$pgsql_pass = 'doesntmatter';
////////////////////////////////////////////////////////////////////////////////
// This array should be updated periodically with new db image versions added
// and old versions that are no longer supported removed.
////////////////////////////////////////////////////////////////////////////////
$container_creation_commands = [
[
'mysql:5.6.51' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:5.6.51",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'postgres:12.19' => [
'run_container' => "podman run -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:12.19",
'dsn' => $pgsql_dsn,
'username' => $pgsql_user,
'password' => $pgsql_pass,
],
],
[
'mysql:5.7.44' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:5.7.44",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'postgres:13.15' => [
'run_container' => "podman run -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:13.15",
'dsn' => $pgsql_dsn,
'username' => $pgsql_user,
'password' => $pgsql_pass,
],
],
[
'mysql:8.0.38' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:8.0.38",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'postgres:14.12' => [
'run_container' => "podman run -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:14.12",
'dsn' => $pgsql_dsn,
'username' => $pgsql_user,
'password' => $pgsql_pass,
],
],
[
'mysql:8.1.0' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:8.1.0",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'mysql:8.2.0' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:8.2.0",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'mysql:8.3.0' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:8.3.0",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'mysql:8.4.1' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mysql:8.4.1",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'postgres:15.7' => [
'run_container' => "podman run -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:15.7",
'dsn' => $pgsql_dsn,
'username' => $pgsql_user,
'password' => $pgsql_pass,
],
],
[
'postgres:16.3' => [
'run_container' => "podman run -dt -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=blog docker.io/library/postgres:16.3",
'dsn' => $pgsql_dsn,
'username' => $pgsql_user,
'password' => $pgsql_pass,
],
],
[
'mariadb:10.4.34' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:10.4.34",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'mariadb:10.5.25' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:10.5.25",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'mariadb:10.6.18' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:10.6.18",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'mariadb:10.11.8' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:10.11.8",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'mariadb:11.0.6' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:11.0.6",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'mariadb:11.1.5' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:11.1.5",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'mariadb:11.2.4' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:11.2.4",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
[
'mariadb:11.4.2' => [
'run_container' => "podman run -dt -p 3306:3306 -e MYSQL_ROOT_PASSWORD={$mysql_root_psw} docker.io/library/mariadb:11.4.2",
'dsn' => $mysql_maria_db_sql_dsn,
'username' => $mysql_user,
'password' => $mysql_root_psw,
],
],
];
echo PHP_EOL . PHP_EOL . 'Starting to create containers and run tests....' . PHP_EOL . PHP_EOL;
$phpunit = __DIR__ . '/vendor/bin/phpunit --coverage-text --stop-on-error';
$start_time = microtime(true);
foreach($container_creation_commands as $current_container_creation_command) {
$db_versions = '';
foreach($current_container_creation_command as $db_version => $command ) {
$db_versions .= $db_version . PHP_EOL;
$retval=null;
$output=null;
echoWithLineBreaks($command['run_container']);
exec($command['run_container'], $output, $retval);
echo PHP_EOL . PHP_EOL;
}
sleepWithEcho(90); // allow databases to load properly
echo PHP_EOL . PHP_EOL;
$output = null;
$phpunit_retval = null;
// Print out current db versions being tested with
echo "Testing against the following databases:" .PHP_EOL . $db_versions . PHP_EOL;
$phpunit_with_env =
"LEANORM_PDO_DSN=\"{$command['dsn']}\" LEANORM_PDO_USERNAME={$command['username']} LEANORM_PDO_PASSWORD={$command['password']} {$phpunit}";
echoWithLineBreaks($phpunit_with_env);
system($phpunit_with_env, $phpunit_retval);
echo "Stopping container instances & deleting their images" .PHP_EOL . PHP_EOL;
echoWithLineBreaks("podman stop -a");
system("podman stop -a"); // stop the containers
// remove their images from the machine to save disk space
echoWithLineBreaks("podman system prune --all --force");
system("podman system prune --all --force");
echoWithLineBreaks("podman rmi --all");
system("podman rmi --all");
echoWithLineBreaks("podman volume rm --all --force");
system("podman volume rm --all --force");
if($phpunit_retval !== 0) {
////////////////////////////////////////////////////////////////////////
// A PHPUnit Test failed.
// if $phpunit_retval !== 0, test failed, stop containers and exit
// see https://github.com/sebastianbergmann/phpunit/blob/10.5/src/TextUI/ShellExitCodeCalculator.php
// for phpunit shell exit codes
////////////////////////////////////////////////////////////////////////
echo PHP_EOL . "Test failed for the following databases:" . PHP_EOL . $db_versions;
echo PHP_EOL . 'Goodbye!'. PHP_EOL;
exit(1);
} eLse {
$test_results[] = $db_versions;
echo PHP_EOL;
} // if($phpunit_retval !== 0)
} // foreach($postgres_and_mysql_container_creation_commands as $postgres_and_mysql_container_creation_command)
$end_time = microtime(true);
$elapsed = $end_time - $start_time;
if (count($test_results) > 0) {
echo PHP_EOL . PHP_EOL
. "Test passed for the following databases:"
. PHP_EOL . PHP_EOL;
foreach ($test_results as $test_result) {
echo $test_result . PHP_EOL;
} // foreach ($test_results as $test_result)
} // if (count($test_results) > 0)
echo PHP_EOL . 'Time taken: ' . readableElapsedTime($elapsed). PHP_EOL. PHP_EOL;
echo PHP_EOL . 'Goodbye!'. PHP_EOL;