Now we're ready to execute some of the tests inside a container. We've used a Docker Compose file to describe the test environment for the Notes application, using the same architecture as in the production environment. The test scripts and configuration has been injected into the containers. The question is, how do we automate test execution?
The technique we'll use is to run a shell script, and use docker exec -it to execute commands to run the test scripts. This is somewhat automated, and with some more work it can be fully automated.
In test-compose, let's make a shell script called run.sh (on Windows, run.ps1):
docker-compose stop
docker-compose build
docker-compose up --force-recreate -d
docker ps
docker network ls
sleep 20
docker exec -it --workdir /notesapp/test -e DEBUG= notes-test npm install
docker exec -it --workdir /notesapp/test -e DEBUG= notes-test npm run test-notes-memory
docker exec -it --workdir /notesapp/test -e DEBUG= notes-test npm run test-notes-fs
docker exec -it --workdir /notesapp/test -e DEBUG= notes-test npm run test-notes-levelup
docker exec -it --workdir /notesapp/test -e DEBUG= notes-test npm run test-notes-sqlite3
docker exec -it --workdir /notesapp/test -e DEBUG= notes-test npm run test-notes-sequelize-sqlite
docker exec -it --workdir /notesapp/test -e DEBUG= notes-test npm run test-notes-sequelize-mysql
docker-compose stop
That makes the first real step to building the containers, followed by bringing them up. The script sleeps for a few seconds to give the containers time to fully instantiate themselves.
The subsequent commands all follow a particular pattern that is important to understand. The commands are executed in the /notesapp/test directory thanks to the --workdir option. Remember that directory is injected into the container by the Docker Compose file.
Using -e DEBUG= we've disabled the DEBUG options. If those options are set, we'd have excess unwanted output in the test results, so using this option ensures that debugging output doesn't occur.
Now that you understand the options, you can see that the subsequent commands are all executed in the test directory using the package.json in that directory. It starts by running npm install, and then running each of the scenarios in the test matrix.
To run the tests, simply type:
$ sh -x run.sh
That's good, we've got most of our test matrix automated and pretty well squared away. There is a glaring hole in the test matrix and plugging that hole will let us see how to set up MongoDB under Docker.