[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]

Introduction

Note that I was addicted to trying to do automated testing of Rails apps using CircleCI. Especially the connection with the mysql container didn't work. To keep the contents of various setting files and countermeasures against errors as a memorandum.

environment

CircleCI  2.1
Bundler  2.1.4
Rails 6.0.3.4
ruby 2.6.5
MySQL 5.7

The development environment is built with Docker-compose. The relationship between the service name and the container name is as follows.

Service name Container name
web webapp_web_1
db webapp_db_1
app webapp_app_1

setting file

The app name is webapp.

database.yml and db.env

Database configuration file. Since this setting could not be used well on the CircleCI side, I decided to create a database setting file for CircleCI this time by referring to the following article. Therefore, the settings here have nothing to do with CircleCI. [CircleCI 2.0 settings memo](https://qiita.com/toyoken/items/16f3b6df06bbe393e644#mysql%E3%81%A8%E3%81%AE%E7%96%8E%E9%80%9A%E8% A8% AD% E5% AE% 9A% E3% 81% AE% E5% 89% 8D% E3% 81% AB "CircleCI 2.0 Settings Memo")

config/database.yml


default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch('MYSQL_USER') { 'root' } %>
  password: <%= ENV.fetch('MYSQL_PASSWORD') { 'rootpass' } %>
  port: 3306
  host: db
  
development:
  <<: *default
  database: webapp_development
    
test:
  <<: *default
  database: webapp_test

environments/db.env


MYSQL_ROOT_USER=root
MYSQL_ROOT_PASSWORD=rootpass
MYSQL_USER=kagamiya
MYSQL_PASSWORD=kagamiya

You may not need to set the port. host: db is the service name for Docker-compose. I don't think you need {'root'} from ENV.fetch.

database.yml.ci Database configuration file for CI. Use this for automated testing on CircleCI.

yaml:config/database.yml.ci


test:
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: 'root'
  port: 3306
  host: '127.0.0.1'
  database: webapp_test

config.yml The essential CircleCI configuration file. Click here for reference. Automate testing with CircleCI

.circleci/config.yml


version: 2.1 #Version specification

executors:  
  default:  
    working_directory: ~/webapp  
    docker:  
      - image: circleci/ruby:2.6.5-node
        environment:  
          BUNDLER_VERSION: 2.1.4
          RAILS_ENV: test
          DB_HOST: 127.0.0.1
          DB_USERNAME: 'root'
          DB_PASSWORD: ''
      - image: circleci/mysql:5.7 
        environment:
          MYSQL_ROOT_HOST: '%'
          MYSQL_ALLOW_EMPTY_PASSWORD: 'true'

commands:  
  setup:  
    steps:  
      - checkout  
      - run:  
          name: Update bundler #Update bundler to version 2
          command: gem update bundler  

      - run:  
          name: Which bundler? #Version confirmation
          command: bundle -v  

      - restore_cache: #Read cache
          keys:  
            - gem-cache-v1-{{ checksum "Gemfile.lock" }}  
            - gem-cache-v1-  

      - run:  
          name: Bundle Install  
          command: bundle check --path vendor/bundle || bundle install --deployment  

      - save_cache: #Save cache
          key: gem-cache-v1-{{ checksum "Gemfile.lock" }}  
          paths:  
            - vendor/bundle  
      
      #The following is required when using Webpacker with Rails 6
      - restore_cache:  
          keys:  
            - yarn-cache-v1-{{ checksum "yarn.lock" }}  
            - yarn-cache-v1-  

      - run:  
          name: Yarn Install  
          command: yarn install --cache-folder ~/.cache/yarn  

      - save_cache:  
          key: yarn-cache-v1-{{ checksum "yarn.lock" }}  
          paths:  
            - ~/.cache/yarn  

jobs:  
  test:  
    executor: default  
    environment:  
      RAILS_ENV: test  
    steps:  
      - checkout  
      - setup  
      - run:  
          name: Wait for DB  
          command: dockerize -wait tcp://127.0.0.1:3306 -timeout 90s  
      
      - run:
          name: Use specific database.yml #Use database settings for CircleCI
          command: mv config/database.yml.ci config/database.yml

      - run:  
          name: Database setup  
          command: |
            bin/rails db:create
            bin/rails db:schema:load --trace  

      - run: #Run regular tests and system tests
          name: Rails Test  
          command: |  
            bin/rails test  
            bin/rails test:system  

workflows:  
  build_and_test:  
    jobs:  
      - test  

(When using MySQL 8 series, it seems that it is necessary to add a description, so I decided to use 5.7. Also, the ruby image could not use the yarn command without a node. )

I was particularly addicted to the settings around the database. Because I didn't really understand the MySQL specs (or even now) ... Initially, following the reference article,

python


DB_USER: 'kagamiya'
DB_PASSWORD: 'kagamiya'

MYSQL_USER: 'kagamiya'
MYSQL_PASSWORD: 'kagamiya'

I was saying. However, a user named kagamiya is not registered in CircleCI's MySQL image in the first place. (Of course, it is in the db container of the development environment) Therefore, root is inevitably used, so I did the following.

python


DB_USER: 'root'
DB_PASSWORD: 'rootpass'

MYSQL_USER: 'root'
MYSQL_PASSWORD: 'rootpass'

But it didn't work. That's because the root user's default password isn't set (probably). That's why emptying the password doesn't work.

python


DB_USER: 'root'
DB_PASSWORD: ''

MYSQL_USER: 'root'
MYSQL_PASSWORD: ''

So it seems that the user must connect as root and the password as MYSQL_ALLOW_EMPTY_PASSWORD:'true' (allow empty?).

python


      - image: circleci/ruby:2.6.5-node
        environment:  
          BUNDLER_VERSION: 2.1.4
          RAILS_ENV: test
          DB_HOST: 127.0.0.1
          DB_USERNAME: 'root'
          DB_PASSWORD: ''
      - image: circleci/mysql:5.7 
        environment:
          MYSQL_ALLOW_EMPTY_PASSWORD: 'true'

I threw an error again here.

Access denied for user 'root'@'127.0.0.1'

I had a hard time understanding here, but it seems that there is no user'root'@ '12 7.0.0.1'. Looking at the mysql user list, I see'root' @'%' and'root' @'localhost', but not'root' @ '12 7.0.0.1'. Whether to use localhost or 127.0.0.1 for the mysql hostname (DB_HOST) seems to be another issue. However, since connecting is the highest priority now, we will specify% as the root host. (% Means "all hosts", but I'm not sure)

python


MYSQL_ROOT_HOST: '%'
MYSQL_ALLOW_EMPTY_PASSWORD: 'true'

Whatever the reason, I was finally able to connect.

Next, the CircleCI image uses its own database settings, so overwrite database.yml with database.yml.ci. Add processing before setting up the database.

python


      - run:
          name: Use specific database.yml #Use database settings for CircleCI
          command: mv config/database.yml.ci config/database.yml

In my environment, I was angry that the specified database (webapp_test) did not exist, so I created the database.

python


      - run:  
          name: Database setup  
          command: |
            bin/rails db:create
            bin/rails db:schema:load --trace  

(This may cause an error in the next execution ...?)

Summary

I don't fully understand the MySQL specifications and the contents of config. Many people have uploaded config samples, but it seems that there are few that explain in detail. Therefore, if an error occurs in a part that you do not understand, there is a high possibility that you will be addicted to it again ...

Recommended Posts

[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
I was addicted to the Spring-Batch test
I was addicted to the record of the associated model
I was addicted to the setting of laradock + VSCode + xdebug
I was addicted to the roll method
I was addicted to the API version min23 setting of registerTorchCallback
A memo that was soberly addicted to the request of multipart / form-data
[Rails] I was addicted to the nginx settings when using Action Cable.
I was addicted to a simple test of Jedis (Java-> Redis library)
Recorded because I was addicted to the standard input of the Scanner class
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
I was addicted to the NoSuchMethodError in Cloud Endpoints
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
A memorandum because I was addicted to the setting of the Android project of IntelliJ IDEA
What I was addicted to when introducing the JNI library
I was addicted to setting default_url_options with Rails devise introduction
I was addicted to looping the Update statement on MyBatis
A story I was addicted to in Rails validation settings
What I was addicted to with the Redmine REST API
The story I was addicted to when setting up STS
I was addicted to starting sbt
I checked the automatic unit test creation tool (end of 2019 version)
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
What I was addicted to when implementing google authentication with rails
About the matter that I was addicted to how to use hashmap
I was able to deploy the Docker + laravel + MySQL app to Heroku!
What I was addicted to when updating the PHP version of the development environment (Docker) from 7.2.11 to 7.4.x
I tried to introduce CircleCI 2.0 to Rails app
The process of introducing Vuetify to Rails
Memorandum: What I was addicted to when I hit the accounting freee API
A story I was addicted to when testing the API using MockMVC
My.cnf configuration problem that I was addicted to when I was touching MySQL 8.0 like 5.7
Problems I was addicted to when building the digdag environment with docker
I was addicted to scrollview because I couldn't tap the variable size UIView
I was addicted to unit testing with the buffer operator in RxJava
[Rails] The cause of not being able to post was in form_with
After all I wanted to preview the contents of mysql with Docker ...
I was addicted to using RXTX on Sierra
I want to output the day of the week
[Rails] Button to return to the top of the page
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I want to var_dump the contents of the intent
I was addicted to installing Ruby/Tk on MacOS
The code I used to connect Rails 3 to PostgreSQL 10
I was addicted to doing onActivityResult () with DialogFragment
I was addicted to not being able to connect to AWS-S3 from the Docker container
I was a little addicted to the S3 Checksum comparison, so I made a note.
I was swallowed by the darkness of the romaji, trying to convert my name to romaji
Rails The concept of view componentization of Rails that I want to convey to those who want to quit
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
A memo that I was addicted to when making batch processing with Spring Boot
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
[Rails] I want to display the link destination of link_to in a separate tab
What I was addicted to when trying to properly openAPI/Swagger documentation with Rails + Grape + Grape Swagger
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I want to know the answer of the rock-paper-scissors app
[Rails] I don't know how to use the model ...
I want to display the name of the poster of the comment