Mysql database contents available on service creation but empty when tests are ran

Hi All,

I’ve been trying to run some tests which involve a mysql service that runs a database import. The import seems to run correctly but for some reason when running the tests, the database exists but it has no contents (no tables at all). I’m not sure why that is and wondered if anyone had any idea why that may be.
It’s almost like the database is wiped by the time the tests have been run but the actual database is still there, just empty.

I’ve included my gitlab-ci.yml file below but if any further information is needed, please let me know.

Thanks,
Sam

I’m running this on gitlab.com.

image: maven:3-jdk-8

# Mysql server needed to run tests
services:
  - mysql:latest

variables:
  # Configure mysql environment variables (https://hub.docker.com/_/mysql/)
  MYSQL_DATABASE: "db0001"
  MYSQL_ROOT_PASSWORD: "root"

stages:
  - connect
  - test
  - build

test:
  script: "mvn verify"

build:
  script: "mvn install -B"

connect:
  image: mysql
  script:
    - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql "$MYSQL_DATABASE" < database/skeleton.sql
    - echo "select count(*) from $MYSQL_DATABASE.nameservers;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql "$MYSQL_DATABASE"
    - echo "show databases;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql "$MYSQL_DATABASE"

Hi, I just wondered if anyone had any ideas. Looking at this again and have not managed to spot what the problem is. Thanks

Hey there,

I believe the issue is that the global service key you specified is actually doing the same thing as
default: service:.

The service is actually recreated in each job that you have. It doesn’t remain across stages/jobs in a pipeline.

See documentation here referencing individual “jobs”.

For testing, I recommend building a test MySQL container with the data you need, push that image to a registry, and then use that image as a service in your pipelines.

Cheers,
T MOS