11 March 2014

We have a Grails application that relies on Apache ZooKeeper for some of its data. This application makes use of the Apache Curator library to connect to Apache ZooKeeper. The Apache Curator library also includes a testing server to make it easy to write unit tests for the classes that need to interact with Apache ZooKeeper. Our Apache ZooKeeper implementation makes uses of SASL-based authentication to prevent unwanted access to different parts of the data stored in Apache ZooKeeper. Unfortunately, the ability to set which configuration file to use when connecting to Apache ZooKeeper is set via JVM system properties. This is fine when the application is running, but can make unit testing difficult, as developers have to remember to set the appropriate system properties before executing unit test. Luckily, Grails has a nice events framework that fires as certain stages in execution are hit. I decided that it would be trivial to add an event hook that listens for the start of the test phase and sets the appropriate system properties required to simulate the same access control when interacting with the Apache Curator test server. I made the following modification to the scripts/Events.groovy file in the Grails application:

eventTestPhaseStart = { args ->
    //Add the required ZooKeeper auth system properties so that tests will pass!
    System.setProperty('java.security.auth.login.config', "${System.getProperty('user.dir')}/test/unit/jaas_test.config")
    System.setProperty('zookeeper.authProvider.1', 'org.apache.zookeeper.server.auth.SASLAuthenticationProvider')
    grailsConsole.addStatus "Running tests with the following system properties:"
    grailsConsole.addStatus "${System.getProperties()}"

The above event is fired right before the first test is executed by Grails when using the grails test-app command. It sets the login configuration file to one that is packaged within the project and sets the authProvider to the appropriate type (in our case, SASL). There are many other events that you can listen for in order to perform logic upon firing. You can see more information about Grails events in the documentation or by looking through the Gant scripts in the scripts folder of the Grails installation.

comments powered by Disqus