未加星标

How to run integration tests at warp speed using Docker and tmpfs

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
Follow @vlad_mihalcea Introduction

Aspreviously explained, you can run database integration tests 20 times faster ! The trick is to map the data directory in memory, and my previous article showed you what changes you need to do when you have a PostgreSQL or mysql instance on your machine.

In this post, I’m going to expand the original idea, and show you how you can achieve the same goal using Docker and tmpfs .

Getting the Docker image

First of all, you need a Docker image for the database you want to run your integration tests on.

To see what Docker images you have on your machine, you need to run the docker images command:

> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oracle/database 12.1.0.2-se2 b5f12a4d9ae0 9 days ago 11.1 GB
bash latest c2a000c8aa3c 11 days ago 12.8 MB
oraclelinux latest 5a42e075a32b 3 weeks ago 225 MB
d4w/nsenter latest 9e4f13a0901e 4 months ago 83.8 kB

Now, let’s get the latest MariaDB Docker image:

> docker pull mariadb
Status: Downloaded newer image for mariadb:latest

If we rerun docker images , we’ll see the MariaDB Docker image listed as well:

> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oracle/database 12.1.0.2-se2 b5f12a4d9ae0 9 days ago 11.1 GB
bash latest c2a000c8aa3c 11 days ago 12.8 MB
mariadb latest 7eca0e0b51c9 2 weeks ago 393 MB
oraclelinux latest 5a42e075a32b 3 weeks ago 225 MB
d4w/nsenter latest 9e4f13a0901e 4 months ago 83.8 kB Running the database in a Docker container

To create a new Docker container, we need use the docker run command:

docker run \
--name mariadb \
-p 3306:3306 \
--tmpfs /var/lib/mysql:rw \
-e MYSQL_ROOT_PASSWORD=admin \
-e MYSQL_USER=hibernate_orm_test \
-e MYSQL_PASSWORD=hibernate_orm_test \
-e MYSQL_DATABASE=hibernate_orm_test \
-d mariadb

On windows, you’ll have to use the ^ multi-line separator instead:

docker run ^
--name mariadb ^
-p 3306:3306 ^
--tmpfs /var/lib/mysql:rw ^
-e MYSQL_ROOT_PASSWORD=admin ^
-e MYSQL_USER=hibernate_orm_test ^
-e MYSQL_PASSWORD=hibernate_orm_test ^
-e MYSQL_DATABASE=hibernate_orm_test ^
-d mariadb

The arguments can be explained as follows:

--name is used to specify the name of the newly created container. We can then reference the container by this name when we want to stop or start it -p 3306:3306 is used to map the Docker container port to the host machine port so we can access the MariaDB database using the 3306 port from within the host machine --tmpfs /var/lib/mysql:rw is the coolest argument since it allows us to map the MariaDB /var/lib/mysql data directory in-memory using tmpfs -e MYSQL_ROOT_PASSWORD=admin defines the root account password for MariaDB -e MYSQL_USER=hibernate_orm_test creates a new user which we’ll use for testing -e MYSQL_PASSWORD=hibernate_orm_test creates a new password for our testing user -e MYSQL_DATABASE=hibernate_orm_test creates a new MariaDB database

After running the aforementioned docker run command, if we list the current Docker containers using docker ps -a , we can see our newly created mariadb Docker container that’s also up and running:

> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c5f5131566b mariadb "docker-entrypoint..." 12 minutes ago Up 12 minutes 0.0.0.0:3306->3306/tcp mariadb
dc280bbfb186 oracle/database:12.1.0.2-se2 "/bin/sh -c $ORACL..." 9 days ago Exited (137) 7 days ago oracle

Using the container id (e.g. 2c5f5131566b ), we can also attach a bash process so that we can inspect the MariaDB Docker container:

> docker exec -i -t 2c5f5131566b /bin/bash
[email protected]:/# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 59G 23G 34G 41% /
tmpfs 2.2G 0 2.2G 0% /dev
tmpfs 2.2G 0 2.2G 0% /sys/fs/cgroup
/dev/sda1 59G 23G 34G 41% /etc/hosts
shm 64M 0 64M 0% /dev/shm
tmpfs 2.2G 115M 2.1G 6% /var/lib/mysql
tmpfs 2.2G 0 2.2G 0% /sys/firmware

As you can see from the df -h output, the var/lib/mysql data directory is mapped on tmpfs. Woohoo!

One more thing to do, let’s grant some admin privileges to our test user. We can do it right from the Docker container bash terminal using the mysql -u root -padmin hibernate_orm_test command:

[email protected]:/# mysql -u root -padmin hibernate_orm_test
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.1.21-MariaDB-1~jessie mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [hibernate_orm_test]>GRANT ALL PRIVILEGES ON *.* TO 'hibernateormtest' WITH GRANT OPTION;

Done!

Testing time

Running all tests (around 400 database integration tests) in the Hibernate documentation folder takes less than 100 seconds:

> gradle test -Pdb=mariadb
:documentation:compileTestJava
:documentation:processTestResources
:documentation:testClasses
:documentation:test
BUILD SUCCESSFUL
Total time: 1 mins 38.748 secs Container lifecycle

When you’re doing using the database container, you can stop it as follows:

> docker stop mariadb

The container is persisted, so you don’t need to rerun all these steps the next time you need it. All you need to do is to start it like this:

> docker start mariadb

If you enjoyed this article, I bet you are going to love my book as well.


How to run integration tests at warp speed using Docker and tmpfs
Conclusion

Mapping a RDBMS data directory on tmpfs is even simpler with Docker, and this is especially relevant for MySQL and MariaDB since the DDL operations take significantly more time than on other database systems (e.g. PostgreSQL, Oracle or SQL Server).

If you liked this article, you might want to subscribe to my newsletter too.

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: DockerSQLPostgreSQLMySQLJavaHibernateWindows2GRYFU
分页:12
转载请注明
本文标题:How to run integration tests at warp speed using Docker and tmpfs
本站链接:http://www.codesec.net/view/532232.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(33)