2

I want to run unit tests on Gitlab CI. I am using nodejs, jest, mysql. I am trying to run mysql on the CI to connect to the same server while running the unit tests. I am inserting some dummy data as well to keep it simple.

When I run the tests on local the test runs with some errors, but the test passes with some post run errors. However, on gitlab CI the test script is not able to connect to the mysql service.

Following is my gitlab-ci.yml

services:
  - mysql:8.0
variables:
  MYSQL_DATABASE: test_database
  MYSQL_ROOT_PASSWORD: root

stages:
  - build
build:
  stage: build
  services:
    - mysql
  only:
    refs:
      - master
  image: klvenky/node-12-alpine-docker:latest
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - .yarn
  script:
    - yarn config set cache-folder $PWD/.yarn
    - export FROM_GIT_URL="git.ssh://git"
    - export TO_GIT_URL="https://gitlab-ci-token:$CI_JOB_TOKEN"
    - sed -i "s#$FROM_GIT_URL#$TO_GIT_URL#" package.json yarn.lock
    - yarn --ignore-optional --pure-lockfile --prefer-offline
    - yarn test

My Repo is hosted on Gitlab here. Please let me know what is going wrong.

Leela Venkatesh K
  • 1,390
  • 1
  • 13
  • 26
  • 1
    Unit tests do not interract with external resources, e.g. databases. Those are integration tests. – Silviu Burcea Sep 24 '20 at 10:50
  • 1
    Does this answer your question? [MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client](https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server) – urfin78 Sep 24 '20 at 10:58
  • thanks @urfin78 My bad. I thought the mysql service was not accessible. – Leela Venkatesh K Sep 25 '20 at 07:47
  • @urfin78 I am coming back after a long time. I have done all the changes required from js end. All the tests pass etc. But still I am seeing the connection failure. Didn't understand what's the problem though. I have updated the database to allow the same as well. – Leela Venkatesh K May 03 '21 at 12:23

1 Answers1

3

The error message regarding mysql in your pipeline states:

ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

The node package mysqljs doesn't support the default authentication method of MySQL 8.

You can find the answer and possible solutions here.

There is also a problem with your services definition in the gitlab-ci file. As mentionend in the comments, you have specified the service twice with the same name. Defining it only once (with the correct command substitution) should work:

services:
  - mysql:8.0
    alias: mysql
    command: [ "--default-authentication-plugin=mysql_native_password" ]

variables:
  MYSQL_DATABASE: test_database
  MYSQL_ROOT_PASSWORD: root

stages:
  - build
build:
  stage: build
  only:
    refs:
      - master
  image: klvenky/node-12-alpine-docker:latest
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - .yarn
  script:
    - yarn config set cache-folder $PWD/.yarn
    - export FROM_GIT_URL="git.ssh://git"
    - export TO_GIT_URL="https://gitlab-ci-token:$CI_JOB_TOKEN"
    - sed -i "s#$FROM_GIT_URL#$TO_GIT_URL#" package.json yarn.lock
    - yarn --ignore-optional --pure-lockfile --prefer-offline
    - yarn test
 
urfin78
  • 451
  • 3
  • 7
  • That question looks familiar, however doesn't answer my question as such. I am little unsure of what's wrong in my config for being able to connect to the database. – Leela Venkatesh K Sep 29 '20 at 13:14
  • I am not running a mysql server separately, instead I am running along with the ci. So something feels missing. I think the issue is with gitlab-ci config. – Leela Venkatesh K May 03 '21 at 12:25
  • 1
    I checked your config again and the problem seems to be, that you specified the same **mysql** service twice. Once at the toplevel _services_ with the correct command substituiton and again at the *build:* job level without any variables. So the toplevel mysql gets overwritten on job level. So either just remove the service definition from the build: job or move all the mysql service config (service, command, variables) to the build job. – urfin78 May 05 '21 at 13:06
  • That did the trick. Thanks a lot. I was under the assumption that the service should be added to the step to make it available in that build step. Thanks for the correction. Final [gitlab-ci file](https://github.com/klvenky/nodejs-mysql-gitlab-ci-tests/blob/main/.gitlab-ci.yml) is here – Leela Venkatesh K May 06 '21 at 09:56