chilio / laravel-dusk-ci

Docker Test suite for Laravel Dusk in gitlab CI
MIT License
160 stars 51 forks source link

Permission denied for cache files #52

Closed thburghout closed 5 years ago

thburghout commented 5 years ago

Hi,

First, thank you for the great work. Setting up Dusk with this docker container was a breeze.

However, I'm running into some issues with file permissions. The following (last part of) the build shows an error occuring in one of the tests. This error occurs because the webserver is unable to create a cache file.

$ chmod -R 775 storage
$ chmod 775 bootstrap/cache
$ chown -R www-data:www-data ./
$ ls -al storage/framework/cache
total 24
drwxrwxr-x. 2 www-data www-data 4096 Sep 29 20:53 .
drwxrwxr-x. 5 www-data www-data 4096 Sep 29 20:53 ..
-rwxrwxr-x. 1 www-data www-data   14 Sep 29 20:53 .gitignore
$ start-nginx-ci-project
 * Starting nginx nginx
   ...done.
$ php artisan dusk || true
Warning: TTY mode requires /dev/tty to be read/writable.
PHPUnit 7.3.5 by Sebastian Bergmann and contributors.

...............F.......                                           23 / 23 (100%)

Time: 1.8 minutes, Memory: 32.00MB

There was 1 failure:

1) Tests\Browser\MapSettingsTest::test_can_store_regions
Did not see expected text [Regions saved.] within element [body].
Failed asserting that false is true.

/builds/project/vendor/laravel/dusk/src/Concerns/MakesAssertions.php:173
/builds/project/vendor/laravel/dusk/src/Concerns/MakesAssertions.php:144
/builds/project/tests/Browser/MapSettingsTest.php:76
/builds/project/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:67
/builds/project/tests/Browser/MapSettingsTest.php:77

FAILURES!
Tests: 23, Assertions: 74, Failures: 1.
$ ls -al storage/framework/cache
total 40
drwxrwxr-x. 4 www-data www-data 4096 Sep 29 20:54 .
drwxrwxr-x. 5 www-data www-data 4096 Sep 29 20:53 ..
drwxr-xr-x. 3 root     root     4096 Sep 29 20:54 5a
drwxr-xr-x. 3 www-data www-data 4096 Sep 29 20:54 85
-rwxrwxr-x. 1 www-data www-data   14 Sep 29 20:53 .gitignore

Given the following gitlab yml:

browser test:
  stage: integration_test
  services:
  - mysql:5.7
  image: chilio/laravel-dusk-ci:php-7.2
  artifacts:
    paths:
    - ./storage/logs
    - ./tests/Browser/screenshots
    - ./tests/Browser/console
    expire_in: 1 days
    when: always
  before_script:
  - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
  - apt-get purge php-xdebug -y
  - cp .env.dusk .env
  - chmod -R 775 storage
  - chmod 775 bootstrap/cache
  - chown -R www-data:www-data ./
  - ls -al storage/framework/cache
  - start-nginx-ci-project
  script:
  - php artisan dusk || true
  - ls -al storage/framework/cache

I'm confused, because the build output shows files created as root. I was under the impression that the php process would be running as www-data.

Any input is appreciated.

chilio commented 5 years ago

@thburghout have you tried 'configure-laravel' without modifying file permissions yourself?

thburghout commented 5 years ago

I see, there seems to be something else going on.

Tests which previously succeeded now fail because of "invalid domain" errors when setting cookies.

1) Tests\Browser\AdminProductTest::test_user_sees_no_products_until_added
Facebook\WebDriver\Exception\UnrecognizedExceptionException: invalid argument: invalid 'domain'
  (Session info: headless chrome=77.0.3865.90)
  (Driver info: chromedriver=77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-refs/branch-heads/3865@{#442}),platform=Linux 4.19.23-coreos-r1 x86_64)

/builds/project/vendor/facebook/webdriver/lib/Exception/WebDriverException.php:158
/builds/project/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:326
/builds/project/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:547
/builds/project/vendor/facebook/webdriver/lib/Remote/RemoteExecuteMethod.php:40
/builds/project/vendor/facebook/webdriver/lib/WebDriverOptions.php:55
/builds/project/vendor/laravel/dusk/src/Concerns/InteractsWithCookies.php:70
...

Which is weird, as those tests would previously succeed. I'm going to investigate this further.

chilio commented 5 years ago

@thburghout please check your .env settings and are you sure you are running all your tests locally in gitlab-runner not hitting remote systems?

thburghout commented 5 years ago

I did not change the env settings w.r.t. the previous runs. My .env file looks like this;

APP_ENV=testing
APP_DEBUG=true
APP_KEY=base64:iPNqnlA8ga5QaFJZ5lr+xMIoyGFHBCbLvypGZkNmWUk=
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
QUEUE_DRIVER=sync
BROADCAST_DRIVER=pusher

SESSION_DRIVER=file
SESSION_SECURE_COOKIE=false

MIX_PUSHER_KEY=xxxxxxxxxx
PUSHER_SECRET=xxxxxxxxxxxx
PUSHER_APP_ID=999999

MAIL_DRIVER=log
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM=x@xx.xx
MAIL_SENDER="xx xx xx"

MOLLIE_TEST_MODE=true
MOLLIE_KEY_TEST=test_xxxxxxxxx

GOOGLE_MAPS_API_KEY=xxxxxxxx

TELEGRAM_BOT_TOKEN=999999999:aaaaaaaaa-aaaaaaaaaa-aaaaaaaaaaaaaa
TELEGRAM_BOT_NAME=xxxxxx

DEBUGBAR_ENABLED=true

Printing the browser's url before setting the cookie yields http://localhost/_dusk/login/1 in the following snippet;

            $second->loginAs($admin);

            $url = $second->driver->getCurrentURL();
            Log::info($url);
            echo $url . "\n";

            $second->addCookie('device_token', $token)
                ->visit('/')
chilio commented 5 years ago

@thburghout did you check withCookie? What Laravel version are you on? Please post your new errors...

thburghout commented 5 years ago

Still running into the same issue as https://github.com/chilio/laravel-dusk-ci/issues/52#issuecomment-536753911

I'm using Laravel v5.7.3 and Dusk v4.0.3.

Please also note this output during execution;

System Chrome version: 77
System Chromedriver version: 77.0
Laravel Chromedriver version: 2.35
Chromedriver shipped with your Laravel installation is NOT compatible with current system chrome version!
Don't worry, we are fixing this right now, and you will be able, to test your app anyway.
Read more, about running tests with system inbuilt chromedriver in laravel-dusk-ci docs.
Starting inbuilt Chromedriver (77.0).
Waiting for inbuilt Chromedriver to launch on port 9515...
Starting ChromeDriver 77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-refs/branch-heads/3865@{#442}) on port 9515

Why does it only behave this way when using configure-laravel? Is the chosen Chromedriver version correct?

chilio commented 5 years ago

@thburghout yes the version is correct. Due to problems with chrome <-> chromedriver matching different updates, this package has it's own ability to actually run correct chromedriver on 9515 port.

The procedure is only included in configure-laravel command, therefore it is important to run this command. This approach is transparent and should not interfere with any of your workflows. After running this command you may issue any other commands specific to your project.

I assume the problem arises when you are trying to set cookie inside dusk test procedure, which I think could not work this way. Please check https://stackoverflow.com/questions/37441736/how-to-set-cookie-before-test-in-laravel

thburghout commented 5 years ago

The link you're providing only considers phpunit and its fake HTTP requests. The same goes for your withCookie suggestion; this is part of local testing, not browser testing with chromedriver.

Today I've further investigated the issue. Unfortunately using configure-laravel causes issues with my dusk + facebook/webdriver version; which does not seem to be aware of cookie domains. I would like to say that the note Don't worry, we are fixing this right now, and you will be able, to test your app anyway. is therefore not correct.

On the other hand, I've successfully updated my Dusk + webdriver to the latest version. Which is a good thing anyway. This resolved the Cookie domain issue with this docker image. However, now I'm back at my original issue regarding file permissions of log files.

So the issue remains exactly the same as it is in my initial comment. The screenshot below shows the browser output during this tests. Logging still shows directories created as root user.

Can you tell me more about which user runs the php-fpm process? It should be www-data right?

failure-Tests_Browser_MapSettingsTest_test_can_store_regions-0

thburghout commented 5 years ago

It was the php artisan dusk process, running as root, invoking procedures which utilize the cache. The cache was set to file, I needed to change this to array in .env.