Skip to content

Commit ba85708

Browse files
test(*): Add end to end tests
1 parent 1651580 commit ba85708

File tree

19 files changed

+732
-15
lines changed

19 files changed

+732
-15
lines changed

.github/workflows/test-suite.yml

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ jobs:
2424
repository: julienloizelet/ddev-php
2525

2626
- name: Install DDEV
27+
env:
28+
DDEV_VERSION: v1.18.2
2729
run: |
2830
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
2931
sudo apt-get -qq update
3032
sudo apt-get -qq -y install libnss3-tools
3133
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh
32-
bash install_ddev.sh
34+
bash install_ddev.sh ${{env.DDEV_VERSION}}
3335
ddev config global --instrumentation-opt-in=false --omit-containers=dba,ddev-ssh-agent
3436
rm install_ddev.sh
3537
@@ -44,13 +46,16 @@ jobs:
4446
run: |
4547
cp .ddev/config_overrides/config.${{ env.PHP_VERSION_CODE }}.yaml .ddev/config.${{ env.PHP_VERSION_CODE }}.yaml
4648
cp .ddev/additional_docker_compose/docker-compose.crowdsec.yaml .ddev/docker-compose.crowdsec.yaml
49+
cp .ddev/additional_docker_compose/docker-compose.playwright.yaml .ddev/docker-compose.playwright.yaml
4750
ddev start
51+
ddev --version
4852
ddev exec php -v
4953
5054
51-
- name: Set BOUNCER_KEY env
55+
- name: Set BOUNCER_KEY and PROXY_IP env
5256
run: |
53-
echo "BOUNCER_KEY=$(ddev create-bouncer)" >> $GITHUB_ENV
57+
echo "BOUNCER_KEY=$(ddev create-bouncer)" >> $GITHUB_ENV
58+
echo "PROXY_IP=$(ddev find-ip ddev-router)" >> $GITHUB_ENV
5459
5560
5661
- name: Clone PHP lib Crowdsec files
@@ -63,10 +68,29 @@ jobs:
6368
ddev composer update --working-dir ./my-own-modules/crowdsec-php-lib
6469
6570
66-
- name: PHP UNIT
71+
- name: Run PHP UNIT tests
6772
run: |
6873
ddev create-watcher PhpUnitTestMachine PhpUnitTestMachinePassword
6974
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} LAPI_URL=http://crowdsec:8080 MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/IpVerificationTest.php
7075
71-
76+
- name: Prepare END TO END tests
77+
run: |
78+
ddev create-watcher
79+
cd ${{ github.workspace }}/.ddev
80+
ddev crowdsec-prepend-nginx
81+
cd ${{ github.workspace }}
82+
cp .ddev/custom_files/crowdsec-lib-settings.php crowdsec-lib-settings.php
83+
sed -i -e 's/REPLACE_API_KEY/${{ env.BOUNCER_KEY }}/g' crowdsec-lib-settings.php
84+
sed -i -e 's/REPLACE_PROXY_IP/${{ env.PROXY_IP }}/g' crowdsec-lib-settings.php
85+
mv crowdsec-lib-settings.php my-own-modules/crowdsec-php-lib/examples/auto-prepend/settings.php
86+
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
87+
chmod +x test-init.sh
88+
./test-init.sh
89+
chmod +x run-tests.sh
90+
91+
- name: Run End to end test
92+
run: |
93+
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
94+
./run-tests.sh ci "./__tests__/1-live-mode.js"
95+
7296

docs/ddev.md

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,6 @@ Before using the bouncer in a standalone mode (i.e. with an auto-prepend directi
267267
`examples/auto-prepend/settings.example.php` file to a `examples/auto-prepend/settings.php` and edit it depending on
268268
your needs.
269269

270-
And you need also to have functional php website sources under your `php-project-sources` folder. For a quick test,
271-
you could just create a `php-project-sources/index.php` with the following content;
272-
```php
273-
<?php
274-
275-
echo "This is HOMEPAGE";
276-
```
277270

278271
Then, to configure the Nginx service in order that it uses an auto-prepend directive pointing to the
279272
`examples/auto-prepend/scripts/bounce-via-auto-prepend.php` script, please run the
@@ -286,4 +279,46 @@ ddev crowdsec-prepend-nginx
286279

287280

288281
With that done, every access to your ddev url (i.e. `https://phpXX.ddev.site` where `XX` is your php version) will
289-
be bounce
282+
be bounce.
283+
284+
For example, you should try to browse the following url:
285+
286+
```
287+
https://phpXX.ddev.site/my-own-modules/crowdsec-php-lib/examples/auto-prepend/public/protected-page.php
288+
```
289+
290+
#### End to end tests
291+
292+
In auto-prepend mode, you can run some end to end tests.
293+
294+
We are using a Jest/Playwright Node.js stack to launch a suite of end-to-end tests.
295+
296+
Tests code is in the `tests/end-to-end` folder. You should have to `chmod +x` the scripts you will find in
297+
`tests/end-to-end/__scripts__`.
298+
299+
300+
Then you can use the `run-test.sh` script to run the tests:
301+
302+
- the first parameter specifies if you want to run the test on your machine (`host`) or in the
303+
docker containers (`docker`). You can also use `ci` if you want to have the same behavior as in Github action.
304+
- the second parameter list the test files you want to execute. If empty, all the test suite will be launched.
305+
306+
For example:
307+
308+
./run-tests.sh host "./__tests__/1-live-mode.js"
309+
./run-tests.sh docker "./__tests__/1-live-mode.js"
310+
./run-tests.sh host
311+
312+
Before testing with the `docker` or `ci` parameter, you have to install all the required dependencies
313+
in the playwright container with this command :
314+
315+
./test-init.sh
316+
317+
If you want to test with the `host` parameter, you will have to install manually all the required dependencies:
318+
319+
```
320+
yarn --cwd ./tests/end-to-end --force
321+
yarn global add cross-env
322+
```
323+
324+
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1-
<h1>The way is clear!</h1>
2-
<p>In this example page, if you can see this text, the bouncer considers your IP as clean.</p>
1+
<?php
2+
3+
echo '
4+
<!DOCTYPE html>
5+
<html>
6+
<head>
7+
<meta charset="utf-8"/>
8+
<title>Home page</title>
9+
</head>
10+
11+
<body>
12+
<h1>The way is clear!</h1>
13+
<p>In this example page, if you can see this text, the bouncer considers your IP as clean.</p>
14+
</body>
15+
</html>
16+
';

tests/end-to-end/.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

tests/end-to-end/.eslintrc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"parserOptions": {
3+
// Required for certain syntax usages
4+
"ecmaVersion": 2018
5+
},
6+
"extends": [
7+
"eslint:recommended",
8+
"airbnb-base",
9+
"plugin:prettier/recommended"
10+
],
11+
"rules": {
12+
"no-unused-vars": 1,
13+
"no-underscore-dangle": 0,
14+
"import/no-dynamic-require": 0,
15+
"no-console": [1, { "allow": ["warn", "error", "debug"] }]
16+
},
17+
"env": {
18+
"jest": true
19+
}
20+
}

tests/end-to-end/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
.cookies.json
3+
.test-results-*.json
4+
*.lock
5+
*.log
6+
*.jpg

tests/end-to-end/.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "all",
4+
"printWidth": 80,
5+
"tabWidth": 4
6+
}

tests/end-to-end/CustomEnvironment.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const PlaywrightEnvironment =
2+
require("jest-playwright-preset/lib/PlaywrightEnvironment").default;
3+
4+
class CustomEnvironment extends PlaywrightEnvironment {
5+
async handleTestEvent(event) {
6+
if (process.env.FAIL_FAST) {
7+
if (
8+
event.name === "hook_failure" ||
9+
event.name === "test_fn_failure"
10+
) {
11+
this.failedTest = true;
12+
const buffer = await this.global.page.screenshot({
13+
path: "screenshot.jpg",
14+
type: "jpeg",
15+
quality: 20,
16+
});
17+
console.debug("Screenshot:", buffer.toString("base64"));
18+
} else if (this.failedTest && event.name === "test_start") {
19+
// eslint-disable-next-line no-param-reassign
20+
event.test.mode = "skip";
21+
}
22+
}
23+
24+
if (super.handleTestEvent) {
25+
await super.handleTestEvent(event);
26+
}
27+
}
28+
}
29+
30+
module.exports = CustomEnvironment;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
# Run test suite
3+
# Usage: ./run-tests.sh <type> <file-list>
4+
# type : host, docker or ci (default: host)
5+
# file-list : a list of test files (default: empty so it will run all the tests)
6+
# Example: ./run-tests.sh docker "./__tests__/1-live-mode.js"
7+
8+
YELLOW='\033[33m'
9+
RESET='\033[0m'
10+
if ! ddev --version >/dev/null 2>&1; then
11+
printf "${YELLOW}Ddev is required for this script. Please see docs/ddev.md.${RESET}\n"
12+
exit 1
13+
fi
14+
15+
16+
TYPE=${1:-host}
17+
FILE_LIST=${2:-""}
18+
19+
20+
case $TYPE in
21+
"host")
22+
echo "Running with host stack"
23+
;;
24+
25+
"docker")
26+
echo "Running with ddev docker stack"
27+
;;
28+
29+
30+
"ci")
31+
echo "Running in CI context"
32+
;;
33+
34+
*)
35+
echo "Unknown param '${TYPE}'"
36+
echo "Usage: ./run-tests.sh <type> <file-list>"
37+
exit 1
38+
;;
39+
esac
40+
41+
42+
HOSTNAME=$(ddev exec printenv DDEV_HOSTNAME | sed 's/\r//g')
43+
PHPVERSION=$(ddev exec printenv DDEV_PROJECT | sed 's/\r//g')
44+
PHP_URL=https://$HOSTNAME
45+
PROXY_IP=$(ddev find-ip ddev-router)
46+
BOUNCER_KEY=$(ddev exec grep "'api_key'" /var/www/html/my-own-modules/crowdsec-php-lib/examples/auto-prepend/settings.php | sed 's/api_key//g' | sed -e 's|[=>,"'\'']||g' | sed s/'\s'//g)
47+
JEST_PARAMS="--bail=true --runInBand --verbose"
48+
# If FAIL_FAST, will exit on first individual test fail
49+
# @see CustomEnvironment.js
50+
FAIL_FAST=true
51+
52+
53+
case $TYPE in
54+
"host")
55+
cd "../"
56+
DEBUG_STRING="PWDEBUG=1"
57+
YARN_PATH="./"
58+
COMMAND="yarn --cwd ${YARN_PATH} cross-env"
59+
LAPI_URL_FROM_PLAYWRIGHT=http://$HOSTNAME:8080
60+
CURRENT_IP=$(ddev find-ip host)
61+
TIMEOUT=31000
62+
HEADLESS=false
63+
SLOWMO=150
64+
;;
65+
66+
"docker")
67+
DEBUG_STRING=""
68+
YARN_PATH="./var/www/html/my-own-modules/crowdsec-php-lib/tests/end-to-end"
69+
COMMAND="ddev exec -s playwright yarn --cwd ${YARN_PATH} cross-env"
70+
LAPI_URL_FROM_PLAYWRIGHT=http://crowdsec:8080
71+
CURRENT_IP=$(ddev find-ip playwright)
72+
TIMEOUT=31000
73+
HEADLESS=true
74+
SLOWMO=0
75+
;;
76+
77+
"ci")
78+
DEBUG_STRING="DEBUG=pw:api"
79+
YARN_PATH="./var/www/html/my-own-modules/crowdsec-php-lib/tests/end-to-end"
80+
COMMAND="ddev exec -s playwright xvfb-run --auto-servernum -- yarn --cwd ${YARN_PATH} cross-env"
81+
LAPI_URL_FROM_PLAYWRIGHT=http://crowdsec:8080
82+
CURRENT_IP=$(ddev find-ip playwright)
83+
TIMEOUT=60000
84+
HEADLESS=true
85+
SLOWMO=0
86+
;;
87+
88+
*)
89+
echo "Unknown param '${TYPE}'"
90+
echo "Usage: ./run-tests.sh <type> <file-list>"
91+
exit 1
92+
;;
93+
esac
94+
95+
96+
97+
# Run command
98+
99+
$COMMAND \
100+
PHP_URL=$PHP_URL \
101+
$DEBUG_STRING \
102+
BOUNCER_KEY=$BOUNCER_KEY \
103+
PROXY_IP=$PROXY_IP \
104+
LAPI_URL_FROM_PLAYWRIGHT=$LAPI_URL_FROM_PLAYWRIGHT \
105+
CURRENT_IP=$CURRENT_IP \
106+
TIMEOUT=$TIMEOUT \
107+
HEADLESS=$HEADLESS \
108+
FAIL_FAST=$FAIL_FAST \
109+
SLOWMO=$SLOWMO \
110+
yarn --cwd $YARN_PATH test \
111+
$JEST_PARAMS \
112+
--json \
113+
--outputFile=./.test-results-$PHPVERSION.json \
114+
$FILE_LIST
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
# Prepare Playwright container before testing
3+
# Usage : ./test-init.sh
4+
5+
YELLOW='\033[33m'
6+
RESET='\033[0m'
7+
if ! ddev --version >/dev/null 2>&1; then
8+
printf "${YELLOW}Ddev is required for this script. Please see docs/ddev.md.${RESET}\n"
9+
exit 1
10+
fi
11+
12+
ddev exec -s playwright yarn --cwd ./var/www/html/my-own-modules/crowdsec-php-lib/tests/end-to-end --force && \
13+
ddev exec -s playwright yarn global add cross-env

0 commit comments

Comments
 (0)