Skip to content

Commit 0ed1248

Browse files
committed
Add custom commands
1 parent 8c59c0b commit 0ed1248

File tree

11 files changed

+236
-0
lines changed

11 files changed

+236
-0
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Notes:
8787
* The behat faildump directory is exposed at http://localhost:8000/_/faildumps/.
8888
* Use `MOODLE_DOCKER_BROWSER` to switch the browser you want to run the test against.
8989
You need to recreate your containers using `bin/moodle-docker-compose` as described below, if you change it.
90+
* Check the [Custom commands](#custom-commands) section for more options.
9091

9192
## Use containers for running phpunit tests
9293

@@ -110,6 +111,7 @@ OK (2 tests, 7 assertions)
110111

111112
Notes:
112113
* If you want to run test with coverage report, use command: `bin/moodle-docker-compose exec webserver phpdbg -qrr vendor/bin/phpunit --coverage-text auth_manual_testcase auth/manual/tests/manual_test.php`
114+
* Check the [Custom commands](#custom-commands) section for more options.
113115

114116
## Use containers for manual testing
115117

@@ -122,6 +124,7 @@ Notes:
122124
* Moodle is configured to listen on `http://localhost:8000/`.
123125
* Mailhog is listening on `http://localhost:8000/_/mail` to view emails which Moodle has sent out.
124126
* The admin `username` you need to use for logging in is `admin` by default. You can customize it by passing `--adminuser='myusername'`
127+
* Check the [Custom commands](#custom-commands) section for more options.
125128

126129
## Use containers for running behat tests for the Moodle App
127130

@@ -158,6 +161,9 @@ Started at 13-07-2020, 18:34
158161
3m3.17s (55.02Mb)
159162
```
160163

164+
Notes:
165+
* Check the [Custom commands](#custom-commands) section for more options.
166+
161167
If you are going with the second option, this *can* be used for local development of the Moodle App, given that the `moodleapp` container serves the app on the local 8100 port. However, this is intended to run Behat tests that require interacting with a local Moodle environment. Normal development should be easier calling `npm start` in the host system.
162168

163169
By all means, if you don't want to have npm installed locally you can go full Docker executing the following commands before starting the containers:
@@ -190,6 +196,76 @@ bin/moodle-docker-compose stop
190196
bin/moodle-docker-compose start
191197
```
192198

199+
## Custom commands
200+
201+
### moodle-docker-bash
202+
This script was created to easily run any command inside any container. First parameter will be the container name and second one will be the command. Example:
203+
```bash
204+
~$ bin/moodle-docker-bash webserver php -v
205+
PHP 7.4.23 (cli) (built: Sep 3 2021 18:14:02) ( NTS )
206+
```
207+
```bash
208+
~$ bin/moodle-docker-bash db psql --version
209+
psql (PostgreSQL) 11.13 (Debian 11.13-1.pgdg90+1)
210+
```
211+
212+
### mbash
213+
As most of the commands using the `moodle-docker-bash` script will be run on the `webserver` container, this is a shortcut of that script that runs the commands only in the `webserver` container. Example:
214+
```bash
215+
~$ bin/mbash php -v
216+
PHP 7.4.23 (cli) (built: Sep 3 2021 18:14:02) ( NTS )
217+
```
218+
219+
### minstall
220+
This script was created to be automatically installed in the webserver container and to easily run any install command. First parameter will be the database to install (moodle, phpunit or behat) and the rest will be all the parameters that want to be used to override the default one. Note that this script needs to be run either withing the container shell or using `moodle-docker-bash`. Examples:
221+
```bash
222+
~$ bin/mbash minstall moodle --fullname="Moodle first instance" --adminpass="admin"
223+
-------------------------------------------------------------------------------
224+
== Setting up database ==
225+
-->System
226+
```
227+
```bash
228+
~$ bin/mbash minstall phpunit
229+
Initialising Moodle PHPUnit test environment...
230+
```
231+
```bash
232+
~$ bin/mbash minstall behat
233+
You are already using the latest available Composer version 2.1.8 (stable channel).
234+
Installing dependencies from lock file (including require-dev)
235+
```
236+
237+
### mtest
238+
This script was created to be automatically installed in the webserver container and to easily run any test command. First parameter will be the tests to be run (phpunit or behat) and the rest will be all the parameters that want to be used to override the default ones. Note that this script needs to be run either withing the container shell or using `moodle-docker-bash`. Examples:
239+
```bash
240+
~$ bin/mbash mtest phpunit --filter auth_manual_testcase
241+
Moodle 3.11.3 (Build: 20210913), 8c02bd32af238dfc83727fb4260b9caf1b622fdb
242+
Php: 7.4.23, pgsql: 11.13 (Debian 11.13-1.pgdg90+1), OS: Linux 5.10.47-linuxkit x86_64
243+
```
244+
```bash
245+
~$ bin/mbash mtest behat --tags=@auth_manual
246+
Running single behat site:
247+
```
248+
249+
### mutil
250+
This script was created to be automatically installed in the webserver container and to easily access the `util.php` files of phpunit and behat. First parameter will be the test environment (phpunit or behat) and the rest will be all the parameters that want to be used to override the default ones. Note that this script needs to be run either withing the container shell or using `moodle-docker-bash`. Examples:
251+
```bash
252+
~$ bin/mbash mutil phpunit --drop
253+
Purging dataroot:
254+
Dropping tables:
255+
```
256+
```bash
257+
~$ bin/mbash mutil behat --drop
258+
Dropping tables:
259+
```
260+
261+
### mfixversion
262+
After increasing the version number in a branch, going back to the master branch might cause version problems. This script was created to easily solve that issue. Note that this script needs to be run either withing the container shell or using `moodle-docker-bash`. Example:
263+
```bash
264+
~$ bin/mbash mfixversion
265+
-------------------------------------------------------------------------------
266+
== Resetting all version numbers ==
267+
```
268+
193269
## Environment variables
194270

195271
You can change the configuration of the docker images by setting various environment variables **before** calling `bin/moodle-docker-compose up`.

assets/web/commands/mcommon

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
command=''
3+
quoteopen='no'
4+
for (( i=2; i<=$#; i++)); do
5+
part=${!i}
6+
if [[ "$quoteopen" == 'yes' && "$part" == "-"* ]]; then
7+
command="$command\""
8+
quoteopen='no'
9+
fi
10+
eqsign="${part//[^=]}"
11+
if [ ${#eqsign} -eq 1 ]; then
12+
part="${part//=/=\"}"
13+
quoteopen='yes'
14+
fi
15+
command="$command $part"
16+
done
17+
18+
if [ "$quoteopen" == 'yes' ]; then
19+
command="$command\""
20+
quoteopen='no'
21+
fi

assets/web/commands/mfixversion

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
eval "cp ../scripts/fixversions.php fixversions.php"
3+
eval "php fixversions.php"
4+
eval "rm fixversions.php"

assets/web/commands/minstall

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
DIR="${BASH_SOURCE%/*}"
3+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
4+
. "$DIR/mcommon"
5+
6+
case "$1" in
7+
moodle)
8+
eval "php admin/cli/install_database.php --agree-license --fullname=\"Moodle\" --shortname=\"moodle\" --summary=\"Moodle site\" --adminpass=\"admin\" --adminemail=\"[email protected]\" ${command}"
9+
;;
10+
phpunit)
11+
eval "php admin/tool/phpunit/cli/init.php ${command}"
12+
;;
13+
behat)
14+
eval "php admin/tool/behat/cli/init.php -a -o ${command}"
15+
;;
16+
*)
17+
SCRIPT_NAME=`basename "$0"`
18+
echo "Usage: $SCRIPT_NAME {moodle|phpunit|behat} [arguments]"
19+
exit 1
20+
esac

assets/web/commands/mtest

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
DIR="${BASH_SOURCE%/*}"
3+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
4+
. "$DIR/mcommon"
5+
6+
case "$1" in
7+
phpunit)
8+
eval "vendor/bin/phpunit ${command}"
9+
;;
10+
behat)
11+
eval "php admin/tool/behat/cli/run.php ${command}"
12+
;;
13+
*)
14+
SCRIPT_NAME=`basename "$0"`
15+
echo "Usage: $SCRIPT_NAME {phpunit|behat} [arguments]"
16+
exit 1
17+
esac

assets/web/commands/mutil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
DIR="${BASH_SOURCE%/*}"
3+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
4+
. "$DIR/mcommon"
5+
6+
case "$1" in
7+
phpunit)
8+
eval "php admin/tool/phpunit/cli/util.php ${command}"
9+
;;
10+
behat)
11+
eval "php admin/tool/behat/cli/util.php ${command}"
12+
;;
13+
*)
14+
SCRIPT_NAME=`basename "$0"`
15+
echo "Usage: $SCRIPT_NAME {phpunit|behat} [arguments]"
16+
exit 1
17+
esac

assets/web/scripts/fixversions.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
define('CLI_SCRIPT', true);
4+
require(getcwd().'/config.php');
5+
require_once($CFG->libdir.'/clilib.php');
6+
require("$CFG->dirroot/version.php");
7+
8+
cli_separator();
9+
cli_heading('Resetting all version numbers');
10+
11+
$manager = core_plugin_manager::instance();
12+
13+
// Purge caches to make sure we have the fresh information about versions.
14+
$manager::reset_caches();
15+
$configcache = cache::make('core', 'config');
16+
$configcache->purge();
17+
$needsupgrade = false;
18+
$wasdowngraded = false;
19+
20+
$plugininfo = $manager->get_plugins();
21+
foreach ($plugininfo as $type => $plugins) {
22+
foreach ($plugins as $name => $plugin) {
23+
if ($plugin->get_status() !== core_plugin_manager::PLUGIN_STATUS_DOWNGRADE) {
24+
$needsupgrade = $needsupgrade || $plugin->get_status() !== core_plugin_manager::PLUGIN_STATUS_UPTODATE;
25+
continue;
26+
}
27+
28+
$frankenstyle = sprintf("%s_%s", $type, $name);
29+
30+
mtrace("Updating {$frankenstyle} from {$plugin->versiondb} to {$plugin->versiondisk}");
31+
$DB->set_field('config_plugins', 'value', $plugin->versiondisk, array('name' => 'version', 'plugin' => $frankenstyle));
32+
$wasdowngraded = true;
33+
}
34+
}
35+
36+
// Check that the main version hasn't changed.
37+
if ((float) $CFG->version > $version) {
38+
set_config('version', $version);
39+
mtrace("Updated main version from {$CFG->version} to {$version}");
40+
$wasdowngraded = true;
41+
} else if ('' . $CFG->version !== '' . $version) {
42+
$needsupgrade = true;
43+
}
44+
45+
if ($wasdowngraded && !$needsupgrade) {
46+
// Update version hash so Moodle doesn't think that we need to run upgrade.
47+
$manager::reset_caches();
48+
set_config('allversionshash', core_component::get_all_versions_hash());
49+
}
50+
51+
// Purge relevant caches again.
52+
$manager::reset_caches();
53+
$configcache->purge();

base.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ version: "2"
22
services:
33
webserver:
44
image: "moodlehq/moodle-php-apache:${MOODLE_DOCKER_PHP_VERSION}"
5+
build:
6+
context: "."
7+
dockerfile: "./dockerfiles/Dockerfilewebserver"
8+
args:
9+
MOODLE_DOCKER_PHP_VERSION: "${MOODLE_DOCKER_PHP_VERSION}"
510
depends_on:
611
- db
712
volumes:

bin/mbash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
DIR="${BASH_SOURCE%/*}"
3+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
4+
eval "$DIR/moodle-docker-compose exec webserver bash -c '${@}'"

bin/moodle-docker-bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
DIR="${BASH_SOURCE%/*}"
3+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
4+
eval "$DIR/moodle-docker-compose exec $1 bash -c '${@:2}'"

dockerfiles/Dockerfilewebserver

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ARG MOODLE_DOCKER_PHP_VERSION=7.4
2+
FROM moodlehq/moodle-php-apache:${MOODLE_DOCKER_PHP_VERSION}
3+
4+
# Custom commands
5+
COPY assets/web/commands/mcommon /usr/local/bin/mcommon
6+
RUN chmod +x /usr/local/bin/mcommon
7+
COPY assets/web/commands/minstall /usr/local/bin/minstall
8+
RUN chmod +x /usr/local/bin/minstall
9+
COPY assets/web/commands/mutil /usr/local/bin/mutil
10+
RUN chmod +x /usr/local/bin/mutil
11+
COPY assets/web/commands/mtest /usr/local/bin/mtest
12+
RUN chmod +x /usr/local/bin/mtest
13+
COPY assets/web/commands/mfixversion /usr/local/bin/mfixversion
14+
RUN chmod +x /usr/local/bin/mfixversion
15+
COPY assets/web/scripts /var/www/scripts

0 commit comments

Comments
 (0)