Behaviour driven infrastructure through Cucumber
When you write code, you always use unit testing & integration testing to verify that the application is working as expected, but why don’t we use that when we install a system?
What are you using to verify that your system is correctly configured and behaves the way you want?
Coincidentally, about a week earlier at Devopsdays in Gent, I was talking to Felix Kronlage and Bernd Ahlers from bytemine about doing similar things through testing SSH and mail delivery with cucumber-nagios.
It’s pretty cool people are thinking about doing BDD/TDD with infrastructure, and it’s even cooler that the tools are at the point where doing this is actually possible.
When doing software testing, your testing tool is normally separate from the language and libraries you’re building the software with (but almost always written in the same language). When testing your infrastructure, I think it makes perfect sense to apply this practice.
So to practise Behaviour Driven Infrastructure right now, you can use Cucumber as the testing tool, and Puppet as the programming language.
One advantage of practicising BDD within sysadmin world is that the testing tools aren’t closely coupled to the language our systems are built with - i.e. if you hate Puppet you can use Cfengine, and if Cucumber isn’t cutting it use PyUnit.
But to something tangible!
Building on Martin’s excellent examples, i’ve pushed out a new version of cucumber-nagios that includes some basic SSH interaction steps, so you can start building behavioural tests for your infrastructure:
The above example shows there’s lots of ways to test the same thing (all depending on what you’re trying to achieve), but there is now also suppport for executing shell commands remotely:
I don’t expect you would do a
cat /etc/passwd in a real test,
however the step definition is a good example of how to interact
with an established SSH connection:
You’d use this to write specific tests for checking system behaviour, such as local user logins vs LDAP logins, or the presence of a daemon.
So the resulting process may look something like this:
- Use cucumber-nagios to write a specification of how you expect your infrastructure to behave.
- Hook your new cucumber-nagios checks into Nagios.
- Start writing your manifests/cookbooks.
- Run your configuration management tool on the node you’re configuring.
- Iterate until your monitoring system is silent.
Not only do you have a functional definition of how your machines work that you can use to build your machines, but if your systems deviate from the expected behaviour at any point in the future, you’ll get an alert from your monitoring system.
Maintaining both a configuration management system and a set of integration tests might get annoying after a while, but if you ever decide to migrate to another configuration management system or move your machines into the cloud you’d have a set of tests you could apply immediately.
This could also be useful for moving existing machines into a configuration management system. Write a set of integration tests for your unmanaged machines, run your configuration management system over the existing machines, see if anything is broken.
I’d be interested to hear how this process or similar works for people!