Skip to content

Commit

Permalink
Embed selenium docker run in behat command.
Browse files Browse the repository at this point in the history
This closes moodlehq#10
  • Loading branch information
kabalin committed Jul 17, 2020
1 parent 46136b0 commit 1bc4917
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 23 deletions.
1 change: 0 additions & 1 deletion .travis.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ before_install:
- cd ../..
- composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^2
- export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH"
- docker run -d -p 127.0.0.1:4444:4444 --net=host --shm-size=2g -v $HOME/build/moodle:$HOME/build/moodle selenium/standalone-firefox:2.53.1

install:
- moodle-plugin-ci install
Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ before_install:
- cp -R tests/Fixture/moodle-local_travis ../moodle-local_travis
- export PATH="$(cd bin; pwd):$(cd vendor/bin; pwd):$PATH"
- export TRAVIS_BUILD_DIR=$(cd ../moodle-local_travis; pwd)
# Start Selenium Standalone with Chrome.
- docker run -d -p 127.0.0.1:4444:4444 --net=host --shm-size=2g -v $HOME/build/moodle:$HOME/build/moodle selenium/standalone-chrome:3

install:
- moodle-plugin-ci install
Expand All @@ -53,6 +51,7 @@ script:
- moodle-plugin-ci grunt || [ "$MOODLE_BRANCH" != 'master' -a "$MOODLE_BRANCH" != 'MOODLE_39_STABLE' ]
- moodle-plugin-ci phpdoc
- moodle-plugin-ci phpunit --coverage-text
- moodle-plugin-ci behat --profile default
- moodle-plugin-ci behat --profile chrome

jobs:
Expand Down
8 changes: 6 additions & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt
- ACTION REQUIRED: project organization renamed to moodlehq. You must update your `.travis.yml` to use `moodlehq/moodle-plugin-ci`
- Updated [.travis.dist.yml] with a new `services` section to ensure databases start.
- Updated [.travis.dist.yml] to remove `openjdk-8-jre-headless` and updated `moodlehq/moodle-local_ci` to fix Mustache linting. See [moodle-local_ci/pull#198](https://github.com/moodlehq/moodle-local_ci/pull/198).
- Replaced Selenium in-built functionality with docker image for Selenium
Standalone server. See [#107](https://github.com/blackboard-open-source/moodle-plugin-ci/pull/107).
- `moodle-plugin-ci behat` is using Selenium docker container for built-in Selenium server.
- ACTION REQUIRED: If you initiated Selenium server in docker container as
part of your test scenario (e.g. separate step in install stage similar to
one outlined in workaround
[blackboard-open-source/issue#110](https://github.com/blackboard-open-source/moodle-plugin-ci/issues/110)),
this is no longer required, you can remove this step.
- Updated version of `moodlehq/moodle-local_codechecker` to v2.9.6
- Updated [.travis.dist.yml] to build Moodle 3.9
- Updated [.travis.dist.yml] to use nvm 14.0.0
Expand Down
2 changes: 1 addition & 1 deletion docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ Behat suite to use (Moodle theme)

#### `--start-servers`

Start PHP server
Start Selenium and PHP servers

* Accept value: no
* Is value required: no
Expand Down
9 changes: 1 addition & 8 deletions docs/TravisFileExplained.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ before_install:
- composer create-project -n --no-dev --prefer-dist moodlehq/plugin-ci ci ^2
# Update the $PATH so scripts from this project can be called easily.
- export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH"
# Start Selenium Standalone server with Chrome/Firefox installed. If you
# prefer to run Behat tests with Chrome profile (see Behat step details below),
# use selenium/standalone-chrome:3 image instead. If you don't run Behat tests,
# this step is not needed.
- docker run -d -p 127.0.0.1:4444:4444 --net=host --shm-size=2g -v $HOME/build/moodle:$HOME/build/moodle selenium/standalone-firefox:2.53.1

# This lists steps that are run for installation and setup.
install:
Expand Down Expand Up @@ -132,8 +127,6 @@ script:
# - The suite option allows you to set the theme to use for behat test. If
# not specified, the default theme is used, EG usage: --suite boost
# - The profile option allows you to set the browser driver to use,
# default is Firefox. If you need Chrome, set '--profile chrome' and make
# sure that you are using correct Selenium server docker image in
# before_install section above.
# default is Firefox. If you need Chrome, set '--profile chrome'.
- moodle-plugin-ci behat
```
58 changes: 49 additions & 9 deletions src/Command/BehatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,30 @@ class BehatCommand extends AbstractMoodleCommand
use ExecuteTrait;

/**
* @var Process[]
* Selenium standalone Firefox image.
*
* @var string
*/
private $servers = [];
private $seleniumFirefoxImage = 'selenium/standalone-firefox:2.53.1';

/**
* Selenium standalone Chrome image.
*
* @var string
*/
private $seleniumChromeImage = 'selenium/standalone-chrome:3';

/**
* Wait this many microseconds for Selenium server to start/stop.
*
* @var int
*/
private $seleniumWaitTime = 5000000;

/**
* @var Process
*/
private $webserver;

protected function configure()
{
Expand All @@ -38,7 +59,7 @@ protected function configure()
$this->setName('behat')
->addOption('profile', 'p', InputOption::VALUE_REQUIRED, 'Behat profile to use', 'default')
->addOption('suite', null, InputOption::VALUE_REQUIRED, 'Behat suite to use (Moodle theme)', 'default')
->addOption('start-servers', null, InputOption::VALUE_NONE, 'Start PHP server')
->addOption('start-servers', null, InputOption::VALUE_NONE, 'Start Selenium and PHP servers')
->addOption('auto-rerun', null, InputOption::VALUE_REQUIRED, 'Number of times to rerun failures', 2)
->addOption('dump', null, InputOption::VALUE_NONE, 'Print contents of Behat failure HTML files')
->setDescription('Run Behat on a plugin');
Expand All @@ -63,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$servers = $input->getOption('start-servers');
}

$servers && $this->startServerProcesses();
$servers && $this->startServerProcesses($input);

$builder = ProcessBuilder::create()
->setPrefix('php')
Expand Down Expand Up @@ -91,21 +112,40 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $process->isSuccessful() ? 0 : 1;
}

private function startServerProcesses()
/**
* @param InputInterface $input
*/
private function startServerProcesses(InputInterface $input)
{
// Test we have docker cli.
$process = $this->execute->run('docker -v');
if (!$process->isSuccessful()) {
throw new \RuntimeException('Docker is not available, can\'t start Selenium server');
}

// Start docker container using desired image.
$image = ($input->getOption('profile') === 'chrome') ? $this->seleniumChromeImage : $this->seleniumFirefoxImage;
$cmd = sprintf('docker run -d --rm -p 127.0.0.1:4444:4444 --name=selenium --net=host --shm-size=2g -v %s:%s %s',
$this->moodle->directory, $this->moodle->directory, $image);
$this->execute->mustRun($cmd);

// Start web server.
$web = new Process('php -S localhost:8000', $this->moodle->directory);
$web->setTimeout(0);
$web->disableOutput();
$web->start();
$this->webserver = $web;

$this->servers = [$web];
// Need to wait for Selenium to start up. Not really sure how long that takes.
usleep($this->seleniumWaitTime);
}

private function stopServerProcesses()
{
foreach ($this->servers as $process) {
$process->stop();
}
// Stop docker. This will also destroy container.
$this->execute->mustRun('docker stop selenium');
// Stop webserver.
$this->webserver->stop();
}

private function dumpFailures(OutputInterface $output)
Expand Down

0 comments on commit 1bc4917

Please sign in to comment.