When I created registr, I wanted a way to test it on the same RDBMS as the
one I use for Redmine, MySQL. For the purposes of testing, I wanted to
start a fresh instance of mysqld
that could be ran without superuser
privileges, without affecting other running MySQL instances, and with
minimal resource consumtion.
Although the test suite was developed in Python, the idea can be used with any language that makes it possible to create temporary directories in a manner that avoids race conditions and spawn processes. The code can be found in the TestRedmineMySQL class, and it follows the steps described below.
- Create a temporary directory (
path
) - Create a directory inside
path
(datadir
) - Generate two filenames inside
path
(socket
andpidfile
) - Spawn the
mysqld_safe
binary with the following parameters.--socket=
and the value ofsocket
makes MySQL accept connections throught that file--datadir=
and the value ofdatadir
makes MySQL store all databases in that directory--skip-networking
disables the TCP listener, thus minimizes interference with other instances--skip_grant_tables
disables access control, since we don't need that for testing--pid-file=
and the value ofpidfile
makes MySQL store the process ID in that file
- Do what you want with the database
- Open the file named
pidfile
and read an integer from the only row - Send a
SIGTERM
to the PID - Wait for the process to finish.
The above way worked fine for me, didn't leave any garbage on the system, and ran as fast as an Oracle product could do. :)