chilio / laravel-dusk-ci

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

php_network_getaddresses Name or service not know #21

Closed chinleung closed 6 years ago

chinleung commented 6 years ago

I've taken the files from the example and it keeps giving me this:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name o  
  r service not known

Here's the .gitlab-ci

stages:
  - build
  - test

# Variables
variables:
  MYSQL_ROOT_PASSWORD: root
  MYSQL_USER: root
  MYSQL_PASSWORD: secret
  MYSQL_DATABASE: test
  DB_HOST: mysql
  DB_CONNECTION: mysql

build:
  stage: build
  services:
     - mysql:5.7

  image: chilio/laravel-dusk-ci:stable
  script:
    - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
    # - npm install # if you need to install additional modules from your projects package.json
    # - npm run dev # if you need to run dev scripts for example laravel mix 
  cache:
      key: ${CI_BUILD_REF_NAME}
      paths:
        # these are only examples, you should modify them according to your project, 
        # or remove cache routines entirely, if they are causing any problems on your next builds..
        # below are 2 safe ones if you use composer install and npm install in your stage script
        - vendor
        - node_modules
         # - /resources/assets/vendors  # for example if you put your vendor node-libraries there

test:
  stage: test
  cache:
    key: ${CI_BUILD_REF_NAME}
    paths:
      - vendor
      - node_modules
    policy: pull

  services:
    - mysql:5.7

  image: chilio/laravel-dusk-ci:stable
  script:
    - cp .env.example .env
    # - cp phpunit.xml.ci phpunit.xml # if you are using custom config for your phpunit tests in CI
    - configure-laravel
    - start-nginx-ci-project
    - ./vendor/phpunit/phpunit/phpunit -v --coverage-text --colors --stderr
    # - phpunit -v --coverage-text --colors --stderr # if you want to use preinstalled phpunit
    - php artisan dusk --colors --debug

  artifacts:
    paths:
      - ./storage/logs # for debugging
      - ./tests/Browser/screenshots
      - ./tests/Browser/console
    expire_in: 7 days
    when: always

And my .env.example:

APP_NAME="Testing"
APP_ENV=testing
APP_DEBUG=true
APP_URL=http://localhost
APP_KEY=

DB_HOST=mysql
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=secret

CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
MAIL_DRIVER=log
chilio commented 6 years ago

@chinleung what versions of gitlab, docker and gitlab-runner are you using? After which command the error is displayed? It seems like some problem with underlying docker, cause it means that testing image could not connect to mysql docker instance. Is mysql properly downloaded and started in gitlab pipeline logs?

chinleung commented 6 years ago

@chilio In the GitLab admin, it says the runner is on version 10.4.0 but when I execute gitlab-runner --version on the server, I get the following:

Version:      11.0.0
Git revision: 5396d320
Git branch:   11-0-stable
GO version:   go1.8.7
Built:        2018-06-22T11:03:37+00:00
OS/Arch:      linux/amd64

I see the following in the logs during both the build and the test stage:

Using Docker executor with image chilio/laravel-dusk-ci:stable ...
Starting service mysql:5.7 ...
Pulling docker image mysql:5.7 ...
Using docker image mysql:5.7 ID=sha256:66bc0f66b7af6ba3ea96582685d3afcd6dff93c2f8999da0ffadd67b280db548 for mysql service...
Waiting for services to be up and running...

*** WARNING: Service runner-689f83b9-project-23-concurrent-0-mysql-0 probably didn't start properly.

I'm not too familiar with Docker. Do you have any idea how to fix this?

chilio commented 6 years ago

@chinleung Your problem is in this line: *** WARNING: Service runner-689f83b9-project-23-concurrent-0-mysql-0 probably didn't start properly. Which means that docker has problem running mysql instance. Therefore throwed error: SQLSTATE[HY000] [2002] php_network_getaddresses: makes sense.

There could be several problems, but for sure you need to check your docker installation (sometimes it can be something as silly as low space, mem, etc.)

First I would try to run mysql manually and make sure it works... Some more info here...

chinleung commented 6 years ago

@chilio Yeah that's what I thought too. Thanks I'll have a look on why it's not starting properly. I'll post back here once I find the solution for anyone with the same issue.

chilio commented 6 years ago

@chinleung did you succeeded with this issue?

chinleung commented 6 years ago

@chilio No I am still working on it. I've tried multiple things like these guides:

I've also tried to enable the privileged to the docker container but I'm still unable to start the SQL service.

Using Docker executor with image chilio/laravel-dusk-ci:stable ...
Starting service mysql:5.7 ...
Pulling docker image mysql:5.7 ...
Using docker image sha256:66bc0f66b7af6ba3ea96582685d3afcd6dff93c2f8999da0ffadd67b280db548 for mysql:5.7 ...
Waiting for services to be up and running...

*** WARNING: Service runner-237f18d2-project-23-concurrent-0-mysql-0 probably didn't start properly.

Health check error:
ContainerStart: Error response from daemon: Cannot link to a non running container: /runner-237f18d2-project-23-concurrent-0-mysql-0 AS /runner-237f18d2-project-23-concurrent-0-mysql-0-wait-for-service/service

Service container logs:
2018-07-11T19:49:03.214991318Z 
2018-07-11T19:49:03.215062485Z ERROR: mysqld failed while attempting to check config
2018-07-11T19:49:03.215067480Z command was: "mysqld --verbose --help"
2018-07-11T19:49:03.215070774Z 
2018-07-11T19:49:03.215073778Z mysqld: error while loading shared libraries: libpthread.so.0: cannot stat shared object: Permission denied
chilio commented 6 years ago

@chinleung My advice is you should not dig dipper, with gitlab....

I believe this is a problem with your underlying infrastructure (probably docker).

I have experienced this error only once, and it was when my test system was overloaded...

So please make sure you can run docker mysql instance without any issues....

chinleung commented 6 years ago

@chilio I think that's the issue, I'm not able to run mysql properly with docker but I can't figure out why... Any ideas?

chilio commented 6 years ago

@chinleung I would try to reinstall docker on your system, have you tried that? And check if you don't have mysql installed on your host system. Here is more info

chinleung commented 6 years ago

@chilio Oh wait, I can't have MySQL on my host machine?

chilio commented 6 years ago

@chinleung I've never tried it, but I have seen that others had some problems with that... Please check this issue also...

ghost commented 6 years ago

@chinleung By using services: -mysql:5.7. This kind of structure is automatically created

image

So everything is a docker container and you don't have to manage your mysql VM/container or even the service on your host by yourself.

By using

variables:
  MYSQL_ROOT_PASSWORD: root
  MYSQL_USER: root
  MYSQL_PASSWORD: secret
  MYSQL_DATABASE: test
  DB_HOST: mysql
  DB_CONNECTION: mysql

The database is already created with the credentials and @chilio made a little script to make your life easier by using

script:
    - configure-laravel

Your laravel env will be setup and your database will be created and seeded (Idon't know if this word exist)

You can have a look here to see what this command is really doing.

I hope it will help you to debug or at least to have a better understanding of the workflow.

chilio commented 6 years ago

@chinleung any updates on this?

chinleung commented 6 years ago

Oh sorry I didn't see @Raccoon5031 's message. So basically it won't work if I have MySQL on my main host. The thing is, I'm using the main host to serve websites, therefore I need the MySQL service running.

@chilio It's not really a fix to the issue, but I realized, I don't even need to install MySQL in my docker because I don't use it all during the tests.

chilio commented 6 years ago

@chinleung if so you can modify .gitlab-ci.yml to use alias for example:

services:
    - name: mysql:5.7
      alias: mysql-test

and then in variables section:

variables:
  ...
  DB_HOST: mysql-test

This should work without a problem. Please check and let me know if it works....

chinleung commented 6 years ago

I simply removed the mysql from the services completely and it's working. 👍

chilio commented 6 years ago

@chinleung I know, but could you check this solution? So you can have it working where you will use mysql in tests also?

chinleung commented 6 years ago

@chilio Okay sure, I'll test it and get back to you in like ±3 hours. I don't have access to my laptop right now. Do you want me to add the alias to both stages?

chilio commented 6 years ago

@chinleung yes please do, everywhere where you reference mysql in services:...

chinleung commented 6 years ago

@chilio Yes adding an alias seems to be working!

chilio commented 6 years ago

@chinleung I'm glad to hear that...

scofield-ua commented 5 years ago

@chilio received same error today on the shared runner. Maybe alias for MySQL service should be set by default in example .gitlab-ci.yml file?

chilio commented 5 years ago

@scofield-ua thanks for the tip. Readme and .gitlab-ci.yml updated...