-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
426 lines (365 loc) · 15.3 KB
/
lib.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
// Basic functions that work together, for cURL and to display help messages
$base_url = "https://bamboo.mroomstech.com/artifact/DEV-MOOB";
$psphr1 = null;
$tmp_dir = "tmp/";
function get_latest_build_url($branch = "master"): ?string {
global $base_url;
$url = $base_url;
switch ($branch) {
case "3.11":
$url = $url . "30/JOB1/build-latest/Behat-Failed-Dump/junit_files.zip";
break;
case "3.11+1":
$url = $url . "31/JOB1/build-latest/Behat-Failed-Dump/junit_files.zip";
break;
case "3.11+2":
$url = $url . "32/JOB1/build-latest/Behat-Failed-Dump/junit_files.zip";
break;
case "master":
$url = $url . "/JOB1/build-latest/Behat-Failed-Dump/junit_files.zip";
break;
default:
$url = null;
break;
}
return $url;
}
function get_latest_build_num($user, $branch = "master"): string {
global $tmp_dir;
switch ($branch) {
case "3.11":
$pattern = "/<title>Development - Moodle Behat - release-3.11 (\d+):/";
$download_url = "https://bamboo.mroomstech.com/browse/DEV-MOOB30/latest";
break;
case "3.11+1":
$pattern = "/<title>Development - Moodle Behat - release-3.11\+1 (\d+):/";
$download_url = "https://bamboo.mroomstech.com/browse/DEV-MOOB31/latest";
break;
case "3.11+2":
$pattern = "/<title>Development - Moodle Behat - release-3.11\+2 (\d+):/";
$download_url = "https://bamboo.mroomstech.com/browse/DEV-MOOB32/latest";
break;
case "master":
$pattern = "/<title>Development - Moodle Behat (\d+):/";
$download_url = "https://bamboo.mroomstech.com/browse/DEV-MOOB/latest";
break;
default:
$pattern = null;
$download_url = null;
break;
}
if (!is_null($download_url)) {
download_file($user, $download_url, $tmp_dir . "latest.html");
}
$latest = fopen($tmp_dir . "latest.html", "r");
if (!$latest) {
echo "Fatal error: The number of the latest build could not be retrieved.\n";
echo "Check your password, or try using the --to option, to set this number manually.\n";
exit(1);
}
if (!is_null($pattern)) {
while (($line = fgets($latest)) !== false) {
preg_match($pattern, $line, $matches);
if (isset($matches[1])) {
$latest_build_num = $matches[1];
break;
}
}
}
fclose($latest);
if (!isset($latest_build_num)) {
echo "Fatal error: The number of the latest build could not be retrieved.\n";
echo "Try using the --to option, to set this number manually.\n";
exit(1);
}
return $latest_build_num;
}
function get_value_from_opt($opt, $args): string {
global $optargs;
$idx = array_search($opt, $args);
if (!isset($args[$idx + 1]) || substr($args[$idx + 1], 0, 1) == '-') {
echo "Please check that each of the optional arguments in use has a value.\n";
echo "The available optional arguments are:\n";
print_r($optargs);
print_help();
exit(1);
} else {
$value = $args[$idx + 1];
}
return $value;
}
function create_urls($user, $branch = "master", $from = null, $to = null) : array {
$urls = array();
if (is_null($from) && is_null($to)) {
$max_num = get_latest_build_num($user, $branch);
$min_num = $max_num;
$urls += [$max_num => get_latest_build_url($branch)];
} elseif (!is_null($from) xor !is_null($to)) {
$max_num = get_latest_build_num($user, $branch);
$min_num = $max_num >= 100 ? $max_num - 99 : 1;
if (!is_null($from)) {
$min_num = $from;
} else {
$max_num = $to;
}
} else {
$min_num = $from;
$max_num = $to;
}
for ($idx = $min_num; $idx <= $max_num; $idx++) {
$urls += [$idx => create_url_with_num($idx, $branch)];
}
return $urls;
// Moodle Behat
// https://bamboo.mroomstech.com/artifact/DEV-MOOB/JOB1/build-936/Behat-Failed-Dump/junit_files.zip
// https://bamboo.mroomstech.com/artifact/DEV-MOOB/JOB1/build-927/Behat-Failed-Dump/junit_files.zip
// https://bamboo.mroomstech.com/artifact/DEV-MOOB/JOB1/build-855/Behat-Failed-Dump/junit_files.zip (No fails here, 404)
// branch 3.11
// https://bamboo.mroomstech.com/artifact/DEV-MOOB30/JOB1/build-12/Behat-Failed-Dump/junit_files.zip
// https://bamboo.mroomstech.com/artifact/DEV-MOOB30/JOB1/build-3/Behat-Failed-Dump/junit_files.zip
// https://bamboo.mroomstech.com/artifact/DEV-MOOB30/JOB1/build-latest/Behat-Failed-Dump/junit_files.zip
// branch 3.11+1
// https://bamboo.mroomstech.com/artifact/DEV-MOOB31/JOB1/build-8/Behat-Failed-Dump/junit_files.zip
// https://bamboo.mroomstech.com/artifact/DEV-MOOB31/JOB1/build-17/Behat-Failed-Dump/junit_files.zip
// https://bamboo.mroomstech.com/artifact/DEV-MOOB31/JOB1/build-latest/Behat-Failed-Dump/junit_files.zip (latest finished tests)
// https://bamboo.mroomstech.com/artifact/DEV-MOOB/JOB1/build-latest/Behat-Failed-Dump/junit_files.zip
// branch 3.11+2
// https://bamboo.mroomstech.com/artifact/DEV-MOOB32/JOB1/build-1/Behat-Failed-Dump/junit_files.zip
// https://bamboo.mroomstech.com/artifact/DEV-MOOB32/JOB1/build-latest/Behat-Failed-Dump/junit_files.zip
// failure example: junit_files/enrol_meta_tests_behat_enrol_meta_feature_82.xml
}
function create_urls_and_dates($user, $branch = "master", $from = null, $to = null) : void {
global $tmp_dir;
$url = "https://bamboo.mroomstech.com/browse/DEV-MOOB";
switch ($branch) {
case "3.11":
$url = $url . "30-";
break;
case "3.11+1":
$url = $url . "31-";
break;
case "3.11+2":
$url = $url . "32-";
break;
case "master":
$url = $url . "-";
break;
default:
$url = null;
break;
}
if (is_null($from) && is_null($to)) {
$to = get_latest_build_num($user, $branch);
$from = $to;
} elseif (!is_null($from) xor !is_null($to)) {
$max_num = get_latest_build_num($user, $branch);
$min_num = $max_num >= 100 ? $max_num - 99 : 1;
if (!is_null($from)) {
$to = $max_num;
} else {
$from = $min_num;
}
}
if ($from != $to) {
$urls_file = $tmp_dir . "urls_" . string_to_legal_string($branch) . "_" . $from . "_" . $to . ".csv";
} else {
$urls_file = $tmp_dir . "urls_" . string_to_legal_string($branch) . "_" . $to . ".csv";
}
$fh1 = fopen($urls_file, "w");
$line = '';
$pattern = "/<time datetime=\"(.*?)\"/";
for ($idx = $from; $idx <= $to; $idx++) {
$tmp_file = $tmp_dir . "build_" . $idx . ".html";
echo "Downloading temp file: $tmp_file... ";
if (download_file($user, $url . $idx, $tmp_file)) {
echo "Done\n\n";
} else {
echo "The build #$idx does not exist, skipping\n";
}
$tmp_fh = fopen($tmp_file, "r");
while (($tmp_line = fgets($tmp_fh)) !== false) {
preg_match("/<span>did not complete/", $tmp_line, $not_complete);
if (!empty($not_complete)) {
break;
}
preg_match($pattern, $tmp_line, $matches);
if (isset($matches[1])) {
$line .= '"' . $url . $idx . '"' . ",";
$line .= '"' . $matches[1] . '"' . ",";
break;
}
}
fclose($tmp_fh);
unlink($tmp_file);
}
echo "Generating file $urls_file\n\n";
fwrite($fh1, $line);
fclose($fh1);
}
function create_url_with_num($num, $branch = "master"): string {
global $base_url;
$url = $base_url;
switch ($branch) {
case "3.11":
$url = $url . "30/JOB1/build-" . $num . "/Behat-Failed-Dump/junit_files.zip";
break;
case "3.11+1":
$url = $url . "31/JOB1/build-" . $num . "/Behat-Failed-Dump/junit_files.zip";
break;
case "3.11+2":
$url = $url . "32/JOB1/build-" . $num . "/Behat-Failed-Dump/junit_files.zip";
break;
case "master":
$url = $url . "/JOB1/build-" . $num . "/Behat-Failed-Dump/junit_files.zip";
break;
default:
$url = null;
break;
}
return $url;
}
function download_test_results($user, $branch = "master", $from = null, $to = null, $dir_suffix = null) {
global $psphr1, $tmp_dir;
if (is_null($psphr1)) {
authenticate_user($user);
}
echo "\nInitiating file download, please be patient\n";
echo "If you get an error, please check your VPN connection, your password, and login using the GUI in Bamboo\n\n";
$urls = create_urls($user, $branch, $from, $to);
create_urls_and_dates($user, $branch, $from, $to);
$idxs = array_keys($urls);
$min_num = $idxs[0];
$max_num = end($idxs);
$branch_dir = $tmp_dir . string_to_legal_string($branch);
$branch_dir = is_null($dir_suffix) ? $branch_dir . "/" : $branch_dir . $dir_suffix . "/";
if (!is_dir($branch_dir)) {
mkdir($branch_dir, 0775, true);
}
for ($idx = $min_num; $idx <= $max_num; $idx++) {
$url = $urls[strval($idx)];
$file = $branch_dir . $idx . ".zip";
echo "Downloading file: $file... ";
if (file_exists($file)) {
echo "This file already exists, delete it to download it again\n\n";
} elseif (download_file($user, $url, $file)) {
echo "Done\n\n";
}
}
}
function download_file($user, $url, $output_file): bool {
global $psphr1;
if (is_null($psphr1)) {
authenticate_user($user);
}
if (file_exists($output_file)) {
unlink($output_file);
}
$ch = curl_init($url); // cURL handle.
$fh = fopen($output_file, "w"); // File handle.
$usp = $user . ":" . base64_decode($psphr1);
$timeout = 60;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // The same as -L.
curl_setopt($ch, CURLOPT_FAILONERROR, true); // The same as --fail.
curl_setopt($ch, CURLOPT_USERPWD, $usp); // The same as --user in cURL.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FILE, $fh);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_exec($ch);
if(curl_error($ch)) {
echo "There was a cURL error:\n";
echo curl_error($ch) . "\n";
echo "The file $output_file was not downloaded\n\n";
unlink($output_file);
return false;
}
curl_close($ch);
fclose($fh);
return true;
}
function authenticate_user($user) {
global $psphr1;
// Wait for password input
echo "Enter passphrase for Bamboo user '$user'\n";
$psphr1 = shell_exec("/usr/bin/env sh -c 'stty -echo; read -r psphr; stty echo; echo \$psphr'");
$psphr1 = base64_encode(trim($psphr1));
}
/**
* Converts characters in a string, that may be illegal (on some systems) into legal characters
*
* @param $string
* @return string
*/
function string_to_legal_string($string): string {
$legal_string = preg_replace('/\./', 'd', $string);
$legal_string = preg_replace('/\+/', 'p', $legal_string);
return $legal_string;
}
function print_description() {
global $sn;
$description = <<<EOT
Description:
This script facilitates the realization of time series analysis and cross
sectional analysis of the results of Behat tests. It operates over the
standard XML output files that come as one of the artifacts from executing
a Behat test suite, in order to produce console output or CSV output files
which can then be used to perform said analyses of the results of Behat
tests.
If you are unsure about whether or not an XML file can be processed by this
script, open that file, it should follow this structure:
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="Name of the feature" tests="1">
<testcase name="Name of the scenario" time="3.042" status="passed"/>
</testsuite>
</testsuites>
As can be seen, the <testsuite> element is used to represent features, and
the <testcase> element is for scenarios.
The filename of the XML file can be useful for this script, it should
follow the structure: relative_path_to_feature_file_feature_linenum.xml
where linenum represents the line number of the test inside the Behat file,
for example the file dir1_subdir1_tests_behat_file1_feature_41.xml means
that the XML file contains the result of executing the Behat test in the
location dir1/subdir1/tests/behat/file1.feature:41
If you work with a Continuous Integration and Deployment tool such as
Bamboo, you can use this script to download the XML files for analysis
directly from Bamboo, by using the --user option (to set the Bamboo user).
For this, make sure to be logged into Bamboo before using this script (if
your password doesn't work here, you may need to log in again using
Bamboo's GUI).
In order to perform time series analysis, you can use this script to
download older Bamboo Behat build artifacts, by using the --from and --to
options. Keep in mind that Bamboo has a build history expiration policy,
so it commonly stores the last 100 builds only (or another number).
When using only --from, the value of --to is set to the latest build by
default. When using only --to, the value of --from is set to the first
available build by default. When using none of those two, the latest build
of the selected branch is taken.
This script retrieves the XML files from the master branch by default.
If you want to use another branch, use the --branch option, for example
'--branch 3.11+1' to use the 3.11+1 branch.
You can also use this script without downloading any files (in case you
already have those files). For this, simply do not use the --user option.
If your files are named according to branches, from, and to numbers, you
can use the options --branch, --from, and --to, accordingly.
The --csv flag produces a CSV file with the results. The --show-all flag
shows all tests, both failing and passing tests (by default, only failing
tests are shown).
EOT;
echo $description;
echo "Usage:\n To download and operate over the XMLs from Bamboo between the builds\n DEV-MOOB-927 and DEV-MOOB-936 (for example)\n";
echo "\tphp " . $sn . " --user bamboo_username --from 927 --to 936\n\n";
echo " To operate over the XML files inside a directory\n";
echo "\tphp " . $sn . " dir1\n\n";
echo " To operate over the latest Bamboo build from the 3.11+1 branch\n";
echo "\tphp " . $sn . " --user bamboo_username --branch 3.11+1\n\n";
}
function print_help () {
global $sn;
echo "Usage:\n To download and operate over the XMLs from Bamboo between the builds\n DEV-MOOB-927 and DEV-MOOB-936 (for example)\n";
echo "\tphp " . $sn . " --user bamboo_username --from 927 --to 936\n\n";
echo " To operate over the XML files inside a directory\n";
echo "\tphp " . $sn . " dir1\n\n";
echo " To operate over the latest Bamboo build from the 3.11+1 branch\n";
echo "\tphp " . $sn . " --user bamboo_username --branch 3.11+1\n\n";
}