This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
oliverdavies.uk-old-sculpin/source/_posts/2017-05-05-fixing-drupal-simpletest-issues-with-docker.md

105 lines
4.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Fixing Drupal SimpleTest issues inside Docker Containers
tags:
- docker
- drupal
- drupal-planet
- simpletest
- testing
---
{% block excerpt %}
Ive been a Drupal VM user for a long time, but lately Ive been using a combination Drupal VM and Docker for my local development environment. There were a couple of issues preventing me from completely switching to Docker - one of which being that when I tried running of my Simpletest tests, a lot of them would fail where they would pass when run within Drupal VM.
Heres an excerpt from my `docker-compose.yml` file:
{% endblock %}
{% block content %}
**TL;DR** You need to include the name of your web server container as the `--url` option to `run-scripts.php`.
Ive been a [Drupal VM][1] user for a long time, but lately Ive been using a combination Drupal VM and [Docker][0] for my local development environment. There were a couple of issues preventing me from completely switching to Docker - one of which being that when I tried running of my Simpletest tests, a lot of them would fail where they would pass when run within Drupal VM.
Heres an excerpt from my `docker-compose.yml` file:
```language-yaml
services:
php:
image: wodby/drupal-php:5.6
volumes:
- ./repo:/var/www/html
nginx:
image: wodby/drupal-nginx:7-1.10
environment:
NGINX_BACKEND_HOST: php
NGINX_SERVER_ROOT: /var/www/html/web
ports:
- "80:80"
volumes_from:
- php
...
```
Nginx and PHP-FPM are running in separate containers, the volumes are shared across both and the Nginx backend is set to use the `php` container.
This is the command that I was using to run the tests:
```language-bash
$ docker-compose run --rm \
-w /var/www/html/web \
php \
php scripts/run-tests.sh \
--php /usr/local/bin/php \
--class OverrideNodeOptionsTestCase
```
This creates a new instance of the `php` container, sets the working directory to my Drupal root and runs Drupals `run-tests.sh` script with some arguments. In this case, I'm running the `OverrideNodeOptionsTestCase` class for the override_node_options tests. Once complete, the container is deleted because of the `--rm` option.
This resulted in 60 of the 112 tests failing, whereas they all passed when run within a Drupal VM instance.
```language-markup
Test summary
------------
Override node options 62 passes, 60 fails, 29 exceptions, and 17 debug messages
Test run duration: 2 min 25 sec
```
Running the tests again with the`--verbose` option, I saw this message appear in the output below some of the failing tests:
> simplexml_import_dom(): Invalid Nodetype to import
After checking that I had all of the required PHP extensions installed, I ran `docker-compose exec php bash` to connect to the `php` container and ran `curl http://localhost` to check the output. Rather than seeing the HTML for the site, I got this error message:
> curl: (7) Failed to connect to localhost port 80: Connection refused
Whereas `curl http://nginx` returns the HTML for the page, so included it with the `--url` option to `run-tests.sh`, and this resulted in my tests all passing.
```language-bash
$ docker-compose run --rm \
-w /var/www/html/web \
php \
php scripts/run-tests.sh \
--php /usr/local/bin/php \
--url http://nginx \
--class OverrideNodeOptionsTestCase
```
```language-markup
Test summary
------------
Override node options 121 passes, 0 fails, 0 exceptions, and 34 debug messages
Test run duration: 2 min 31 sec
```
**Note:** In this example I have separate `nginx` and `php` containers, but I've tried and had the same issue when running Nginx and PHP-FPM in the same container - e.g. called `app` - and still needed to add `--url http://app` in order for the tests to run successfully.
I dont know if this issue is macOS specfic (I know that [Drupal CI][2] is based on Docker, and I dont know if its an issue) but Im going to test also on my Ubuntu Desktop environment and investigate further and also compare the test run times for Docker in macOS, Docker in Ubuntu and within Drupal VM. Im also going to test this with PHPUnit tests with Drupal 8.
{% endblock %}
[0]: https://www.docker.com
[1]: https://www.drupalvm.com
[2]: https://www.drupal.org/drupalorg/docs/drupal-ci