Problem/Motivation
The documentation on how to run phpunit with a different user on Linux is incorrect.
The exported variables are nor preserved when switching to the different user, so the tests do fail:
Exception: You must provide a SIMPLETEST_BASE_URL environment variable to run some PHPUnit based functional tests.
The documented way:
> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data ./vendor/bin/phpunit -c core --testsuite functional
> sudo -u www-data ./vendor/bin/phpunit -c core --testsuite functional-javascript
This fails, as the declared environment variables $SIMPLETEST_DB and $SIMPLETEST_BASE_URL are not preserved when switching to www-data. The following command checks for the existence of the environment variables after switching the user:
> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data printenv | grep 'SIMPLETEST_DB\|SIMPLETEST_BASE_URL'
(Nothing is found)
Proposed resolution
Preserve the environment varibles when switching to the different user:
> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data -E ./vendor/bin/phpunit -c core --testsuite functional
> sudo -u www-data -E ./vendor/bin/phpunit -c core --testsuite functional-javascript
The following command checks for the existence of the environment variables after switching the user and preserving the environmment:
> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data -E printenv | grep 'SIMPLETEST_DB\|SIMPLETEST_BASE_URL'
SIMPLETEST_DB=mysql://root@localhost/dev_d8
SIMPLETEST_BASE_URL=http://d8.dev
or more explicit:
> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data SIMPLETEST_BASE_URL=$SIMPLETEST_BASE_URL SIMPLETEST_DB=$SIMPLETEST_DB ./vendor/bin/phpunit -c core --testsuite functional
> sudo -u www-data SIMPLETEST_BASE_URL=$SIMPLETEST_BASE_URL SIMPLETEST_DB=$SIMPLETEST_DB ./vendor/bin/phpunit -c core --testsuite functional-javascript
The following command checks for the existence of the environment variables after switching the user and explicitly passing the exported environment variables:
> export SIMPLETEST_DB='mysql://root@localhost/dev_d8'
> export SIMPLETEST_BASE_URL='http://d8.dev'
> sudo -u www-data SIMPLETEST_BASE_URL=$SIMPLETEST_BASE_URL SIMPLETEST_DB=$SIMPLETEST_DB printenv | grep 'SIMPLETEST_DB\|SIMPLETEST_BASE_URL'
SIMPLETEST_BASE_URL=http://d8.dev
SIMPLETEST_DB=mysql://root@localhost/dev_d8
Remaining tasks
review
User interface changes
none
API changes
none
Data model changes
none
Comment | File | Size | Author |
---|---|---|---|
#3 | environment_is_not-2799053-3.patch | 605 bytes | sanduhrs |
Comments
Comment #2
sanduhrsComment #3
sanduhrsThe attached patch preserves the full environment.
Comment #4
sanduhrsComment #5
dawehnerInteresting! Personally I'm lazy, so I copy the
phpunit.xml.dist
file tophpunit.xml
and set the environment variables there. I guess though for continues integration systems that's not really a good option.Comment #6
alexpottAs a docs only change this is rc eligible.
Committed and pushed bff6791 to 8.3.x and 1c31625 to 8.2.x. Thanks!
Comment #9
alexpottBTW I do this differently and set up env_keep in my sudoers...
Comment #15
sunThis change has been performed for recent versions already:
http://cgit.drupalcode.org/drupal/tree/core/tests/README.md#n46
There is no need to backport this further.