3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GitLabのCIからDockerInDocker経由でTestcontainersを使う

Last updated at Posted at 2023-02-04

この記事の目的

業務でGitLabのCI/CDを使うケースが多いので、JUnitテストでDatabaseを使うユースケースを試してみました。

技術スタック

  • GitLab
  • GitLabRunner(Docker Executor)
  • SpringBoot(あまりSpringは関係ない)
  • MySQL
  • Testcontainers for Java

Testcontainers for Javaは、JUnitからDockerでDatabaseを建てられるので、便利です。
GitLabのCIとの統合については、以下のページを参考にしました。
https://www.testcontainers.org/supported_docker_environment/continuous_integration/gitlab_ci/

コード全体

今回のコード全体は以下の場所においてあります。
https://github.com/akiraabe/my-first-gitlab/tree/testcontainers

  • Testcontainersとは関係ない話しですが、ローカル環境での、アプリケーション起動の際には、事前に(手動で)docker composeでMySQLを立ち上げます。
  • JUnit実行時には、特にDockerを意識する必要はなく、Testcontainersが自動的にMySQLを立ち上げてくれます
    使い方は、README.mdを参照ください。

要所の説明

全体を説明すると長くなるので要点だけ説明を加えておきます。
まずは、gitlabのCIの定義です。
ここは、Testcontainersのドキュメントに従い、servicesとvariablesを定義しています。

gitlab-ci.yml
# DinD service is required for Testcontainers
services:
  - name: docker:dind
    # explicitly disable tls to avoid docker startup interruption
    command: ["--tls=false"]

variables:
  # Instruct Testcontainers to use the daemon of DinD, use port 2735 for non-tls connections.
  DOCKER_HOST: "tcp://docker:2375"
  # Instruct Docker not to start over TLS.
  DOCKER_TLS_CERTDIR: ""
  # Improve performance with overlayfs.
  DOCKER_DRIVER: overlay2

image: amazoncorretto:11

stages:
  - test

unit-test:
  stage: test
  tags: [docker]
  script:
    # Maven Wrapper を使用する 
    - ./mvnw clean test
  artifacts:
    reports:
      junit:
        - target/surefire-reports/TEST-*.xml
        - target/failsafe-reports/TEST-*.xml

job-checkstyle:
  stage: test
  script:
    - ./mvnw checkstyle:checkstyle
  artifacts:
    when: always
    paths:
      - target/site/*
  tags: [docker]
  allow_failure: true
  except:
    - tags

JUnitが使う application.properties です。
https://www.testcontainers.org/modules/databases/mysql/
辺りを参考にしました。

application.propeties
# src/test/resources/application.properties
spring.datasource.url=jdbc:tc:mysql:5.7.38://hostname:port/test?TC_MY_CNF=mysql_conf
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
spring.datasource.username=test
spring.datasource.password=
spring.sql.init.encoding=utf-8

spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true

GitLabのPipeline

GitLabにpushするとPipelineが動きます。(今回は、CheckStyleとJUnitの2Job)
Screenshot 2023-02-04 at 13.39.29.png

テスト結果はTestsのタブから見れます。
Screenshot 2023-02-04 at 13.42.16.png

雑感

一昔前は、CIでDatabaseテストを実行するのはかなり大変で、JUnitの@CategoryでCIの対象外するというケースも多かったと思います。(Database周りはモックにするなど…)
現在は、コンテナを使って手軽にCIでDatabaseテストが出来るので非常に便利ですね。
Databaseを実際に使わないと発見しづらい不具合もあると思いますので、品質向上に役立てていきたいです。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?