Commit 8cf4591a authored by Dan Poltawski's avatar Dan Poltawski Committed by Andrew Nicols

Merge branch 'wip-mdl-54628' of https://github.com/rajeshtaneja/moodle

parents ac08d455 36cc9f8e
This diff is collapsed.
......@@ -36,11 +36,7 @@ define('NO_OUTPUT_BUFFERING', true);
define('IGNORE_COMPONENT_CACHE', true);
define('ABORT_AFTER_CONFIG', true);
require_once(__DIR__ . '/../../../../config.php');
require_once(__DIR__ . '/../../../../lib/clilib.php');
require_once(__DIR__ . '/../../../../lib/behat/lib.php');
require_once(__DIR__ . '/../../../../lib/behat/classes/behat_command.php');
require_once(__DIR__ . '/../../../../lib/behat/classes/behat_config_manager.php');
// CLI options.
list($options, $unrecognized) = cli_get_params(
......@@ -103,14 +99,26 @@ if (!empty($options['help'])) {
$cwd = getcwd();
// If Behat parallel site is being initiliased, then define a param to be used to ignore single run install.
if (!empty($options['parallel'])) {
define('BEHAT_PARALLEL_UTIL', true);
}
require_once(__DIR__ . '/../../../../config.php');
require_once(__DIR__ . '/../../../../lib/behat/lib.php');
require_once(__DIR__ . '/../../../../lib/behat/classes/behat_command.php');
require_once(__DIR__ . '/../../../../lib/behat/classes/behat_config_manager.php');
// For drop option check if parallel site.
if ((empty($options['parallel'])) && ($options['drop']) || $options['updatesteps']) {
// Get parallel run info from first run.
$options['parallel'] = behat_config_manager::get_parallel_test_runs($options['fromrun']);
$options['parallel'] = behat_config_manager::get_behat_run_config_value('parallel');
}
// If not a parallel site then open single run.
if (empty($options['parallel'])) {
// Set run config value for single run.
behat_config_manager::set_behat_run_config_value('singlerun', 1);
chdir(__DIR__);
// Check if behat is initialised, if not exit.
passthru("php util_single_run.php --diag", $status);
......@@ -147,6 +155,21 @@ if ($options['diag'] || $options['enable'] || $options['disable']) {
$status = (bool)$status || (bool)$exitcode;
}
// Remove run config file.
$behatrunconfigfile = behat_config_manager::get_behat_run_config_file_path();
if (file_exists($behatrunconfigfile)) {
if (!unlink($behatrunconfigfile)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Can not delete behat run config file');
}
}
// Remove test file path.
if (file_exists(behat_util::get_test_file_path())) {
if (!unlink(behat_util::get_test_file_path())) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Can not delete test file enable info');
}
}
} else if ($options['install']) {
// This is intensive compared to behat itself so run them in chunk if option maxruns not set.
if ($options['maxruns']) {
......@@ -233,6 +256,19 @@ if ($options['install']) {
} else if ($options['enable']) {
echo "Acceptance tests environment enabled on $CFG->behat_wwwroot, to run the tests use:" . PHP_EOL;
echo behat_command::get_behat_command(true, true);
// Save fromrun and to run information.
if (isset($options['fromrun'])) {
behat_config_manager::set_behat_run_config_value('fromrun', $options['fromrun']);
}
if (isset($options['torun'])) {
behat_config_manager::set_behat_run_config_value('torun', $options['torun']);
}
if (isset($options['parallel'])) {
behat_config_manager::set_behat_run_config_value('parallel', $options['parallel']);
}
echo PHP_EOL;
} else if ($options['disable']) {
......
......@@ -143,7 +143,7 @@ if ($options['run']) {
$run = $options['run'];
// If parallel option is not passed, then try get it form config.
if (!$options['parallel']) {
$parallel = behat_config_manager::get_parallel_test_runs();
$parallel = behat_config_manager::get_behat_run_config_value('parallel');
} else {
$parallel = $options['parallel'];
}
......@@ -176,10 +176,7 @@ if ($options['install']) {
} else if ($options['enable']) {
if (!empty($parallel)) {
// Save parallel site info for enable and install options.
$filepath = behat_config_manager::get_parallel_test_file_path();
if (!file_put_contents($filepath, $parallel)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $filepath . ' can not be created');
}
behat_config_manager::set_behat_run_config_value('behatsiteenabled', 1);
}
// Enable test mode.
......@@ -200,7 +197,7 @@ if ($options['install']) {
}
} else if ($options['disable']) {
behat_util::stop_test_mode();
behat_util::stop_test_mode($run);
// This is only displayed once for parallel install.
if (empty($run)) {
mtrace("Acceptance tests environment disabled");
......
......@@ -42,6 +42,22 @@ class behat_command {
*/
const DOCS_URL = 'http://docs.moodle.org/dev/Acceptance_testing';
/**
* Ensures the behat dir exists in moodledata
*
* @return string Full path
*/
public static function get_parent_behat_dir() {
global $CFG;
// If not set then return empty string.
if (!isset($CFG->behat_dataroot_parent)) {
return "";
}
return $CFG->behat_dataroot_parent;
}
/**
* Ensures the behat dir exists in moodledata
* @param int $runprocess run process for which behat dir is returned.
......@@ -55,12 +71,11 @@ class behat_command {
return "";
}
if (empty($runprocess)) {
$behatdir = $CFG->behat_dataroot . '/behat';
} else if (isset($CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'])) {
// If $CFG->behat_parallel_run starts with index 0 and $runprocess for parallel run starts with 1.
if (!empty($runprocess) && isset($CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'])) {
$behatdir = $CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'] . '/behat';;
} else {
$behatdir = $CFG->behat_dataroot . $runprocess . '/behat';
$behatdir = $CFG->behat_dataroot . '/behat';
}
if (!is_dir($behatdir)) {
......
......@@ -87,7 +87,7 @@ class behat_config_manager {
// Behat must have a separate behat.yml to have access to the whole set of features and steps definitions.
if ($testsrunner === true) {
$configfilepath = behat_command::get_behat_dir() . '/behat.yml';
$configfilepath = behat_command::get_behat_dir($run) . '/behat.yml';
} else {
// Alternative for steps definitions filtering, one for each user.
$configfilepath = self::get_steps_list_config_filepath();
......@@ -117,7 +117,7 @@ class behat_config_manager {
// Get number of parallel runs if not passed.
if (empty($parallelruns) && ($parallelruns !== false)) {
$parallelruns = self::get_parallel_test_runs();
$parallelruns = self::get_behat_run_config_value('parallel');
}
// Behat config file specifing the main context class,
......@@ -212,31 +212,49 @@ class behat_config_manager {
* Returns the path to the parallel run file which specifies if parallel test environment is enabled
* and how many parallel runs to execute.
*
* @param int $runprocess run process for which behat dir is returned.
* @return string
*/
public final static function get_parallel_test_file_path($runprocess = 0) {
return behat_command::get_behat_dir($runprocess) . '/parallel_environment_enabled.txt';
public final static function get_behat_run_config_file_path() {
return behat_command::get_parent_behat_dir() . '/run_environment.json';
}
/**
* Returns number of parallel runs for which site is initialised.
* Get config for parallel run.
*
* @param int $runprocess run process for which behat dir is returned.
* @return int
* @param string $key Key to store
* @return string|int|array value which is stored.
*/
public final static function get_parallel_test_runs($runprocess = 0) {
public final static function get_behat_run_config_value($key) {
$parallelrunconfigfile = self::get_behat_run_config_file_path();
$parallelrun = 0;
// Get parallel run info from first file and last file.
$parallelrunconfigfile = self::get_parallel_test_file_path($runprocess);
if (file_exists($parallelrunconfigfile)) {
if ($parallel = file_get_contents($parallelrunconfigfile)) {
$parallelrun = (int) $parallel;
if ($parallelrunconfigs = @json_decode(file_get_contents($parallelrunconfigfile), true)) {
if (isset($parallelrunconfigs[$key])) {
return $parallelrunconfigs[$key];
}
}
}
return $parallelrun;
return false;
}
/**
* Save/update config for parallel run.
*
* @param string $key Key to store
* @param string|int|array $value to store.
*/
public final static function set_behat_run_config_value($key, $value) {
$parallelrunconfigs = array();
$parallelrunconfigfile = self::get_behat_run_config_file_path();
// Get any existing config first.
if (file_exists($parallelrunconfigfile)) {
$parallelrunconfigs = @json_decode(file_get_contents($parallelrunconfigfile), true);
}
$parallelrunconfigs[$key] = $value;
@file_put_contents($parallelrunconfigfile, json_encode($parallelrunconfigs, JSON_PRETTY_PRINT));
}
/**
......@@ -247,8 +265,8 @@ class behat_config_manager {
public final static function drop_parallel_site_links() {
global $CFG;
// Get parallel test runs from first run.
$parallelrun = self::get_parallel_test_runs(1);
// Get parallel test runs.
$parallelrun = self::get_behat_run_config_value('parallel');
if (empty($parallelrun)) {
return false;
......
......@@ -147,7 +147,7 @@ class behat_config_util {
public function get_number_of_parallel_run() {
// Get number of parallel runs if not passed.
if (empty($this->parallelruns) && ($this->parallelruns !== false)) {
$this->parallelruns = behat_config_manager::get_parallel_test_runs();
$this->parallelruns = behat_config_manager::get_behat_run_config_value('parallel');
}
return $this->parallelruns;
......
......@@ -139,8 +139,23 @@ class behat_util extends testing_util {
}
self::reset_dataroot();
self::drop_dataroot();
self::drop_database(true);
self::drop_dataroot();
}
/**
* Delete files and directories under dataroot.
*/
public static function drop_dataroot() {
global $CFG;
// As behat directory is now created under default $CFG->behat_dataroot_parent, so remove the whole dir.
if ($CFG->behat_dataroot !== $CFG->behat_dataroot_parent) {
remove_dir($CFG->behat_dataroot, false);
} else {
// It should never come here.
throw new moodle_exception("Behat dataroot should not be same as parent behat data root.");
}
}
/**
......@@ -290,6 +305,7 @@ class behat_util extends testing_util {
}
$testenvfile = self::get_test_file_path();
behat_config_manager::set_behat_run_config_value('behatsiteenabled', 0);
if (!self::is_test_mode_enabled()) {
echo "Test environment was already disabled\n";
......@@ -322,8 +338,8 @@ class behat_util extends testing_util {
* Returns the path to the file which specifies if test environment is enabled
* @return string
*/
protected final static function get_test_file_path() {
return behat_command::get_behat_dir() . '/test_environment_enabled.txt';
public final static function get_test_file_path() {
return behat_command::get_parent_behat_dir() . '/test_environment_enabled.txt';
}
/**
......
......@@ -274,26 +274,41 @@ function behat_check_config_vars() {
'Define $CFG->behat_dataroot in config.php');
}
clearstatcache();
if (!file_exists($CFG->behat_dataroot)) {
if (!file_exists($CFG->behat_dataroot_parent)) {
$permissions = isset($CFG->directorypermissions) ? $CFG->directorypermissions : 02777;
umask(0);
if (!mkdir($CFG->behat_dataroot, $permissions, true)) {
if (!mkdir($CFG->behat_dataroot_parent, $permissions, true)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, '$CFG->behat_dataroot directory can not be created');
}
}
$CFG->behat_dataroot = realpath($CFG->behat_dataroot);
if (empty($CFG->behat_dataroot) or !is_dir($CFG->behat_dataroot) or !is_writable($CFG->behat_dataroot)) {
$CFG->behat_dataroot_parent = realpath($CFG->behat_dataroot_parent);
if (empty($CFG->behat_dataroot_parent) or !is_dir($CFG->behat_dataroot_parent) or !is_writable($CFG->behat_dataroot_parent)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_dataroot in config.php must point to an existing writable directory');
}
if (!empty($CFG->dataroot) and $CFG->behat_dataroot == realpath($CFG->dataroot)) {
if (!empty($CFG->dataroot) and $CFG->behat_dataroot_parent == realpath($CFG->dataroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_dataroot in config.php must be different from $CFG->dataroot');
}
if (!empty($CFG->phpunit_dataroot) and $CFG->behat_dataroot == realpath($CFG->phpunit_dataroot)) {
if (!empty($CFG->phpunit_dataroot) and $CFG->behat_dataroot_parent == realpath($CFG->phpunit_dataroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_dataroot in config.php must be different from $CFG->phpunit_dataroot');
}
// This request is coming from admin/tool/behat/cli/util.php which will call util_single.php. So just return from
// here as we don't need to create a dataroot for single run.
if (defined('BEHAT_PARALLEL_UTIL') && BEHAT_PARALLEL_UTIL && empty($CFG->behatrunprocess)) {
return;
}
if (!file_exists($CFG->behat_dataroot)) {
$permissions = isset($CFG->directorypermissions) ? $CFG->directorypermissions : 02777;
umask(0);
if (!mkdir($CFG->behat_dataroot, $permissions, true)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, '$CFG->behat_dataroot directory can not be created');
}
}
$CFG->behat_dataroot = realpath($CFG->behat_dataroot);
}
/**
......@@ -336,6 +351,10 @@ function behat_update_vars_for_process() {
$behatrunprocess = behat_get_run_process();
$CFG->behatrunprocess = $behatrunprocess;
// Data directory will be a directory under parent directory.
$CFG->behat_dataroot_parent = $CFG->behat_dataroot;
$CFG->behat_dataroot .= '/'. BEHAT_PARALLEL_SITE_NAME;
if ($behatrunprocess) {
if (empty($CFG->behat_parallel_run[$behatrunprocess - 1]['behat_wwwroot'])) {
// Set www root for run process.
......@@ -459,7 +478,7 @@ function behat_get_run_process() {
}
// Check if default behat datroot increment was done.
if (empty($behatrunprocess)) {
$behatdataroot = str_replace("\\", "/", $CFG->behat_dataroot);
$behatdataroot = str_replace("\\", "/", $CFG->behat_dataroot . '/' . BEHAT_PARALLEL_SITE_NAME);
$behatrunprocess = preg_filter("#^{$behatdataroot}" . "(.+?)[/|\\\]behat[/|\\\]behat\.yml#", '$1',
$behatconfig);
}
......
......@@ -412,10 +412,12 @@ class phpunit_util extends testing_util {
self::reset_dataroot();
testing_initdataroot($CFG->dataroot, 'phpunit');
self::drop_dataroot();
// drop all tables
// Drop all tables.
self::drop_database($displayprogress);
// Drop dataroot.
self::drop_dataroot();
}
/**
......
......@@ -84,7 +84,8 @@ if (defined('BEHAT_SITE_RUNNING')) {
// Update config variables for parallel behat runs.
behat_update_vars_for_process();
if (behat_is_test_site()) {
// If behat is being installed for parallel run, then we modify params for parallel run only.
if (behat_is_test_site() && !(defined('BEHAT_PARALLEL_UTIL') && empty($CFG->behatrunprocess))) {
clearstatcache();
// Checking the integrity of the provided $CFG->behat_* vars and the
......@@ -116,7 +117,8 @@ if (defined('BEHAT_SITE_RUNNING')) {
if (!defined('BEHAT_UTIL') and !defined('BEHAT_TEST')) {
// Somebody tries to access test site directly, tell them if not enabled.
if (!file_exists($CFG->behat_dataroot . '/behat/test_environment_enabled.txt')) {
$behatdir = preg_replace("#[/|\\\]" . BEHAT_PARALLEL_SITE_NAME . "\d{0,}$#", '', $CFG->behat_dataroot);
if (!file_exists($behatdir . '/test_environment_enabled.txt')) {
behat_error(BEHAT_EXITCODE_CONFIG, 'Behat is configured but not enabled on this test site.');
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment