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(socketandpidfile) - Spawn the
mysqld_safebinary with the following parameters.--socket=and the value ofsocketmakes MySQL accept connections throught that file--datadir=and the value ofdatadirmakes MySQL store all databases in that directory--skip-networkingdisables the TCP listener, thus minimizes interference with other instances--skip_grant_tablesdisables access control, since we don't need that for testing--pid-file=and the value ofpidfilemakes MySQL store the process ID in that file
- Do what you want with the database
- Open the file named
pidfileand read an integer from the only row - Send a
SIGTERMto 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. :)