I recently implemented LDAP authentication in our Dropwizard application. How to integration test this?
Here’s what I used:
* This docker image: nickstenning/slapd, containing a basic configuration of the OpenLDAP server.
* The fabric8io docker-maven-plugin
* Some maven foo.
The docker image
I required LDAP configured with some known groups and users to use for testing. I put the following Dockerfile in src/test/docker/ldap, along with users.ldif which defined the users I wanted to load.
FROM nickstenning/slapd #Our envt variables ENV LDAP_ROOTPASS topsecret ENV LDAP_ORGANISATION My Org ENV LDAP_DOMAIN domain.com # Users and groups to be loaded COPY users.ldif /users.ldif # Run slapd to load ldif files RUN apt-get update && apt-get install -y ldap-utils && rm -rf /var/lib/apt/lists/* RUN (/etc/service/slapd/run &) && sleep 2 && \ ldapadd -h localhost -p 389 -c -x -D cn=admin,dc=domain,dc=com -w topsecret -v -f /users.ldif && killall slapd
The parent config
In my parent pom, I put the shared configuration for the fabric8io plugin into pluginManagement. This includes any images I want to run for multiple sub modules (I’m using postgres for other integration tests, in combination with the liquibase for setting up a test database), and the executions which apply to all submodules.
<pluginManagement> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.18.1</version> <configuration> <images> <image> <name>postgres:9.5.1</name> <alias>pg</alias> <run> <env> <POSTGRES_PASSWORD>topsecret</POSTGRES_PASSWORD> <POSTGRES_USER>myuser</POSTGRES_USER> </env> <ports> <port>5432:5432</port> </ports> <wait> <tcp> <ports> <port>5432</port> </ports> </tcp> </wait> </run> </image> </images> </configuration> <executions> <execution> <id>build</id> <phase>pre-integration-test</phase> <goals> <goal>build-nofork</goal> </goals> </execution> <execution> <id>run</id> <phase>pre-integration-test</phase> <goals> <goal>start</goal> </goals> </execution> <execution> <id>stop</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement>
Remember that putting a plugin in pluginManagement in the parent does not mean it will be active in the child modules. It simply defines the default config which will be used, should you add the plugin to the plugins section in the child pom. This approach means we can avoid spinning up docker contained for sub modules which don’t need them.
Remember to use the maven-failsafe-plugin to run your integration tests. This ensures that the docker containers will be stopped even if your integration tests fail, so you don’t get containers hanging around disrupting your next test run.
The sub module config
A key optimisation I wanted to make was not to run the maven-docker-plugin if I was skipping tests – I’d assume I’m skipping tests for a fast build, so the last thing I want is to spin up unnecessary containers. To do this, I activated the docker maven plugin within a profile in the appropriate submodules, which is only active if you haven’t supplied the skipTests parameter.
<profiles> <profile> <id>integ-test</id> <activation> <property> <name>!skipTests</name> </property> </activation> <build> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> </plugin> </plugins> </build> </profile> </profiles>
For submodules which only require the docker images defined in the parent pom, the above configuration is sufficient. However, in one submodule I also want to spin up the ldap image. To do that, I wanted to add to the images defined in the parent. This led me to a useful blog on how to Merging Plugin Configuration in Complex Maven Projects. The combine.children=”append”
attribute ensures that the merge behaviour is as required.
<profile> <id>integ-test</id> <activation> <property> <name>!skipTests</name> </property> </activation> <build> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <configuration> <!-- append docker images for this module --> <images combine.children="append"> <image> <name>test-ldap</name> <build> <skip>${env.skip.docker.run}</skip> <dockerFileDir>${project.basedir}/src/test/docker/ldap</dockerFileDir> </build> <run> <skip>${env.skip.docker.run}</skip> <ports> <port>389:389</port> </ports> <wait> <tcp> <ports> <port>389</port> </ports> </tcp> </wait> </run> </image> </images> </configuration> </plugin> </plugins> </build> </profile>
Result: completely portable integration tests.