See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * CLI tool with utilities to manage parallel Behat integration in Moodle 19 * 20 * All CLI utilities uses $CFG->behat_dataroot and $CFG->prefix_dataroot as 21 * $CFG->dataroot and $CFG->prefix 22 * 23 * @package tool_behat 24 * @copyright 2012 David MonllaĆ³ 25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 26 */ 27 28 29 if (isset($_SERVER['REMOTE_ADDR'])) { 30 die(); // No access from web!. 31 } 32 33 define('BEHAT_UTIL', true); 34 define('CLI_SCRIPT', true); 35 define('NO_OUTPUT_BUFFERING', true); 36 define('IGNORE_COMPONENT_CACHE', true); 37 define('ABORT_AFTER_CONFIG', true); 38 39 require_once (__DIR__ . '/../../../../lib/clilib.php'); 40 41 // CLI options. 42 list($options, $unrecognized) = cli_get_params( 43 array( 44 'help' => false, 45 'install' => false, 46 'drop' => false, 47 'enable' => false, 48 'disable' => false, 49 'diag' => false, 50 'parallel' => 0, 51 'maxruns' => false, 52 'updatesteps' => false, 53 'fromrun' => 1, 54 'torun' => 0, 55 'optimize-runs' => '', 56 'add-core-features-to-theme' => false, 57 ), 58 array( 59 'h' => 'help', 60 'j' => 'parallel', 61 'm' => 'maxruns', 62 'o' => 'optimize-runs', 63 'a' => 'add-core-features-to-theme', 64 ) 65 ); 66 67 // Checking util.php CLI script usage. 68 $help = " 69 Behat utilities to manage the test environment 70 71 Usage: 72 php util.php [--install|--drop|--enable|--disable|--diag|--updatesteps|--help] [--parallel=value [--maxruns=value]] 73 74 Options: 75 --install Installs the test environment for acceptance tests 76 --drop Drops the database tables and the dataroot contents 77 --enable Enables test environment and updates tests list 78 --disable Disables test environment 79 --diag Get behat test environment status code 80 --updatesteps Update feature step file. 81 82 -j, --parallel Number of parallel behat run operation 83 -m, --maxruns Max parallel processes to be executed at one time. 84 -o, --optimize-runs Split features with specified tags in all parallel runs. 85 -a, --add-core-features-to-theme Add all core features to specified theme's 86 87 -h, --help Print out this help 88 89 Example from Moodle root directory: 90 \$ php admin/tool/behat/cli/util.php --enable --parallel=4 91 92 More info in http://docs.moodle.org/dev/Acceptance_testing#Running_tests 93 "; 94 95 if (!empty($options['help'])) { 96 echo $help; 97 exit(0); 98 } 99 100 $cwd = getcwd(); 101 102 // If Behat parallel site is being initiliased, then define a param to be used to ignore single run install. 103 if (!empty($options['parallel'])) { 104 define('BEHAT_PARALLEL_UTIL', true); 105 } 106 107 require_once(__DIR__ . '/../../../../config.php'); 108 require_once (__DIR__ . '/../../../../lib/behat/lib.php'); 109 require_once (__DIR__ . '/../../../../lib/behat/classes/behat_command.php'); 110 require_once (__DIR__ . '/../../../../lib/behat/classes/behat_config_manager.php'); 111 112 // Remove error handling overrides done in config.php. This is consistent with admin/tool/behat/cli/util_single_run.php. 113 $CFG->debug = (E_ALL | E_STRICT); 114 $CFG->debugdisplay = 1; 115 error_reporting($CFG->debug); 116 ini_set('display_errors', '1'); 117 ini_set('log_errors', '1'); 118 119 // Import the necessary libraries. 120 require_once($CFG->libdir . '/setuplib.php'); 121 require_once($CFG->libdir . '/behat/classes/util.php'); 122 123 // For drop option check if parallel site. 124 if ((empty($options['parallel'])) && ($options['drop']) || $options['updatesteps']) { 125 $options['parallel'] = behat_config_manager::get_behat_run_config_value('parallel'); 126 } 127 128 // If not a parallel site then open single run. 129 if (empty($options['parallel'])) { 130 // Set run config value for single run. 131 behat_config_manager::set_behat_run_config_value('singlerun', 1); 132 133 chdir(__DIR__); 134 // Check if behat is initialised, if not exit. 135 passthru("php util_single_run.php --diag", $status); 136 if ($status) { 137 exit ($status); 138 } 139 $cmd = commands_to_execute($options); 140 $processes = cli_execute_parallel(array($cmd), __DIR__); 141 $status = print_sequential_output($processes, false); 142 chdir($cwd); 143 exit($status); 144 } 145 146 // Default torun is maximum parallel runs. 147 if (empty($options['torun'])) { 148 $options['torun'] = $options['parallel']; 149 } 150 151 $status = false; 152 $cmds = commands_to_execute($options); 153 154 // Start executing commands either sequential/parallel for options provided. 155 if ($options['diag'] || $options['enable'] || $options['disable']) { 156 // Do it sequentially as it's fast and need to be displayed nicely. 157 foreach (array_chunk($cmds, 1, true) as $cmd) { 158 $processes = cli_execute_parallel($cmd, __DIR__); 159 print_sequential_output($processes); 160 } 161 162 } else if ($options['drop']) { 163 $processes = cli_execute_parallel($cmds, __DIR__); 164 $exitcodes = print_combined_drop_output($processes); 165 foreach ($exitcodes as $exitcode) { 166 $status = (bool)$status || (bool)$exitcode; 167 } 168 169 // Remove run config file. 170 $behatrunconfigfile = behat_config_manager::get_behat_run_config_file_path(); 171 if (file_exists($behatrunconfigfile)) { 172 if (!unlink($behatrunconfigfile)) { 173 behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Can not delete behat run config file'); 174 } 175 } 176 177 // Remove test file path. 178 if (file_exists(behat_util::get_test_file_path())) { 179 if (!unlink(behat_util::get_test_file_path())) { 180 behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Can not delete test file enable info'); 181 } 182 } 183 184 } else if ($options['install']) { 185 // This is intensive compared to behat itself so run them in chunk if option maxruns not set. 186 if ($options['maxruns']) { 187 foreach (array_chunk($cmds, $options['maxruns'], true) as $chunk) { 188 $processes = cli_execute_parallel($chunk, __DIR__); 189 $exitcodes = print_combined_install_output($processes); 190 foreach ($exitcodes as $name => $exitcode) { 191 if ($exitcode != 0) { 192 echo "Failed process [[$name]]" . PHP_EOL; 193 echo $processes[$name]->getOutput(); 194 echo PHP_EOL; 195 echo $processes[$name]->getErrorOutput(); 196 echo PHP_EOL . PHP_EOL; 197 } 198 $status = (bool)$status || (bool)$exitcode; 199 } 200 } 201 } else { 202 $processes = cli_execute_parallel($cmds, __DIR__); 203 $exitcodes = print_combined_install_output($processes); 204 foreach ($exitcodes as $name => $exitcode) { 205 if ($exitcode != 0) { 206 echo "Failed process [[$name]]" . PHP_EOL; 207 echo $processes[$name]->getOutput(); 208 echo PHP_EOL; 209 echo $processes[$name]->getErrorOutput(); 210 echo PHP_EOL . PHP_EOL; 211 } 212 $status = (bool)$status || (bool)$exitcode; 213 } 214 } 215 216 } else if ($options['updatesteps']) { 217 // Rewrite config file to ensure we have all the features covered. 218 if (empty($options['parallel'])) { 219 behat_config_manager::update_config_file('', true, '', $options['add-core-features-to-theme'], false, false); 220 } else { 221 // Update config file, ensuring we have up-to-date behat.yml. 222 for ($i = $options['fromrun']; $i <= $options['torun']; $i++) { 223 $CFG->behatrunprocess = $i; 224 225 // Update config file for each run. 226 behat_config_manager::update_config_file('', true, $options['optimize-runs'], $options['add-core-features-to-theme'], 227 $options['parallel'], $i); 228 } 229 unset($CFG->behatrunprocess); 230 } 231 232 // Do it sequentially as it's fast and need to be displayed nicely. 233 foreach (array_chunk($cmds, 1, true) as $cmd) { 234 $processes = cli_execute_parallel($cmd, __DIR__); 235 print_sequential_output($processes); 236 } 237 exit(0); 238 239 } else { 240 // We should never reach here. 241 echo $help; 242 exit(1); 243 } 244 245 // Ensure we have success status to show following information. 246 if ($status) { 247 echo "Unknown failure $status" . PHP_EOL; 248 exit((int)$status); 249 } 250 251 // Show command o/p (only one per time). 252 if ($options['install']) { 253 echo "Acceptance tests site installed for sites:".PHP_EOL; 254 255 // Display all sites which are installed/drop/diabled. 256 for ($i = $options['fromrun']; $i <= $options['torun']; $i++) { 257 if (empty($CFG->behat_parallel_run[$i - 1]['behat_wwwroot'])) { 258 echo $CFG->behat_wwwroot . "/" . BEHAT_PARALLEL_SITE_NAME . $i . PHP_EOL; 259 } else { 260 echo $CFG->behat_parallel_run[$i - 1]['behat_wwwroot'] . PHP_EOL; 261 } 262 263 } 264 } else if ($options['drop']) { 265 echo "Acceptance tests site dropped for " . $options['parallel'] . " parallel sites" . PHP_EOL; 266 267 } else if ($options['enable']) { 268 echo "Acceptance tests environment enabled on $CFG->behat_wwwroot, to run the tests use:" . PHP_EOL; 269 echo behat_command::get_behat_command(true, true); 270 271 // Save fromrun and to run information. 272 if (isset($options['fromrun'])) { 273 behat_config_manager::set_behat_run_config_value('fromrun', $options['fromrun']); 274 } 275 276 if (isset($options['torun'])) { 277 behat_config_manager::set_behat_run_config_value('torun', $options['torun']); 278 } 279 if (isset($options['parallel'])) { 280 behat_config_manager::set_behat_run_config_value('parallel', $options['parallel']); 281 } 282 283 echo PHP_EOL; 284 285 } else if ($options['disable']) { 286 echo "Acceptance tests environment disabled for " . $options['parallel'] . " parallel sites" . PHP_EOL; 287 288 } else if ($options['diag']) { 289 // Valid option, so nothing to do. 290 } else { 291 echo $help; 292 chdir($cwd); 293 exit(1); 294 } 295 296 chdir($cwd); 297 exit(0); 298 299 /** 300 * Create commands to be executed for parallel run. 301 * 302 * @param array $options options provided by user. 303 * @return array commands to be executed. 304 */ 305 function commands_to_execute($options) { 306 $removeoptions = array('maxruns', 'fromrun', 'torun'); 307 $cmds = array(); 308 $extraoptions = $options; 309 $extra = ""; 310 311 // Remove extra options not in util_single_run.php. 312 foreach ($removeoptions as $ro) { 313 $extraoptions[$ro] = null; 314 unset($extraoptions[$ro]); 315 } 316 317 foreach ($extraoptions as $option => $value) { 318 if ($options[$option]) { 319 $extra .= " --$option"; 320 if ($value) { 321 $extra .= "=\"$value\""; 322 } 323 } 324 } 325 326 if (empty($options['parallel'])) { 327 $cmds = "php util_single_run.php " . $extra; 328 } else { 329 // Create commands which has to be executed for parallel site. 330 for ($i = $options['fromrun']; $i <= $options['torun']; $i++) { 331 $prefix = BEHAT_PARALLEL_SITE_NAME . $i; 332 $cmds[$prefix] = "php util_single_run.php " . $extra . " --run=" . $i . " 2>&1"; 333 } 334 } 335 return $cmds; 336 } 337 338 /** 339 * Print drop output merging each run. 340 * 341 * @param array $processes list of processes. 342 * @return array exit codes of each process. 343 */ 344 function print_combined_drop_output($processes) { 345 $exitcodes = array(); 346 $maxdotsonline = 70; 347 $remainingprintlen = $maxdotsonline; 348 $progresscount = 0; 349 echo "Dropping tables:" . PHP_EOL; 350 351 while (count($exitcodes) != count($processes)) { 352 usleep(10000); 353 foreach ($processes as $name => $process) { 354 if ($process->isRunning()) { 355 $op = $process->getIncrementalOutput(); 356 if (trim($op)) { 357 $update = preg_filter('#^\s*([FS\.\-]+)(?:\s+\d+)?\s*$#', '$1', $op); 358 $strlentoprint = strlen($update); 359 360 // If not enough dots printed on line then just print. 361 if ($strlentoprint < $remainingprintlen) { 362 echo $update; 363 $remainingprintlen = $remainingprintlen - $strlentoprint; 364 } else if ($strlentoprint == $remainingprintlen) { 365 $progresscount += $maxdotsonline; 366 echo $update . " " . $progresscount . PHP_EOL; 367 $remainingprintlen = $maxdotsonline; 368 } else { 369 while ($part = substr($update, 0, $remainingprintlen) > 0) { 370 $progresscount += $maxdotsonline; 371 echo $part . " " . $progresscount . PHP_EOL; 372 $update = substr($update, $remainingprintlen); 373 $remainingprintlen = $maxdotsonline; 374 } 375 } 376 } 377 } else { 378 // Process exited. 379 $process->clearOutput(); 380 $exitcodes[$name] = $process->getExitCode(); 381 } 382 } 383 } 384 385 echo PHP_EOL; 386 return $exitcodes; 387 } 388 389 /** 390 * Print install output merging each run. 391 * 392 * @param array $processes list of processes. 393 * @return array exit codes of each process. 394 */ 395 function print_combined_install_output($processes) { 396 $exitcodes = array(); 397 $line = array(); 398 399 // Check what best we can do to accommodate all parallel run o/p on single line. 400 // Windows command line has length of 80 chars, so default we will try fit o/p in 80 chars. 401 if (defined('BEHAT_MAX_CMD_LINE_OUTPUT') && BEHAT_MAX_CMD_LINE_OUTPUT) { 402 $lengthofprocessline = (int)max(10, BEHAT_MAX_CMD_LINE_OUTPUT / count($processes)); 403 } else { 404 $lengthofprocessline = (int)max(10, 80 / count($processes)); 405 } 406 407 echo "Installing behat site for " . count($processes) . " parallel behat run" . PHP_EOL; 408 409 // Show process name in first row. 410 foreach ($processes as $name => $process) { 411 // If we don't have enough space to show full run name then show runX. 412 if ($lengthofprocessline < strlen($name) + 2) { 413 $name = substr($name, -5); 414 } 415 // One extra padding as we are adding | separator for rest of the data. 416 $line[$name] = str_pad('[' . $name . '] ', $lengthofprocessline + 1); 417 } 418 ksort($line); 419 $tableheader = array_keys($line); 420 echo implode("", $line) . PHP_EOL; 421 422 // Now print o/p from each process. 423 while (count($exitcodes) != count($processes)) { 424 usleep(50000); 425 $poutput = array(); 426 // Create child process. 427 foreach ($processes as $name => $process) { 428 if ($process->isRunning()) { 429 $output = $process->getIncrementalOutput(); 430 if (trim($output)) { 431 $poutput[$name] = explode(PHP_EOL, $output); 432 } 433 } else { 434 // Process exited. 435 $exitcodes[$name] = $process->getExitCode(); 436 } 437 } 438 ksort($poutput); 439 440 // Get max depth of o/p before displaying. 441 $maxdepth = 0; 442 foreach ($poutput as $pout) { 443 $pdepth = count($pout); 444 $maxdepth = $pdepth >= $maxdepth ? $pdepth : $maxdepth; 445 } 446 447 // Iterate over each process to get line to print. 448 for ($i = 0; $i <= $maxdepth; $i++) { 449 $pline = ""; 450 foreach ($tableheader as $name) { 451 $po = empty($poutput[$name][$i]) ? "" : substr($poutput[$name][$i], 0, $lengthofprocessline - 1); 452 $po = str_pad($po, $lengthofprocessline); 453 $pline .= "|". $po; 454 } 455 if (trim(str_replace("|", "", $pline))) { 456 echo $pline . PHP_EOL; 457 } 458 } 459 unset($poutput); 460 $poutput = null; 461 462 } 463 echo PHP_EOL; 464 return $exitcodes; 465 } 466 467 /** 468 * Print install output merging showing one run at a time. 469 * If any process fail then exit. 470 * 471 * @param array $processes list of processes. 472 * @param bool $showprefix show prefix. 473 * @return bool exitcode. 474 */ 475 function print_sequential_output($processes, $showprefix = true) { 476 $status = false; 477 foreach ($processes as $name => $process) { 478 $shownname = false; 479 while ($process->isRunning()) { 480 $op = $process->getIncrementalOutput(); 481 if (trim($op)) { 482 // Show name of the run once for sequential. 483 if ($showprefix && !$shownname) { 484 echo '[' . $name . '] '; 485 $shownname = true; 486 } 487 echo $op; 488 } 489 } 490 // If any error then exit. 491 $exitcode = $process->getExitCode(); 492 if ($exitcode != 0) { 493 exit($exitcode); 494 } 495 $status = $status || (bool)$exitcode; 496 } 497 return $status; 498 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body