You are previewing Test-Driven Infrastructure with Chef.

Test-Driven Infrastructure with Chef

Cover of Test-Driven Infrastructure with Chef by Stephen Nelson-Smith Published by O'Reilly Media, Inc.

Chapter 5. Introduction to Cucumber-Chef

Testing classes and methods is trivial. Mature unit testing frameworks exist that make it very easy to write simple code test-first. As the complexity increases and the need to test code that depends on other services arises, the frameworks become more sophisticated, allowing for the creation of mock services and the ability to stub out slow-responding or third-party interfaces.[15] If the test requirement is to exercise the code end-to-end, the complexity grows once more, and it becomes necessary to use specialist testing libraries for testing network services, or JavaScript.

Testing code that builds an entire infrastructure is a different proposition altogether. Not only do we need sophisticated libraries of code to verify the intended behavior of our systems, we need to be able to build and install the systems themselves. Consider the following test:

Scenario: Bluepill restarts Unicorn
  Given I have a newly installed Ubuntu machine managed by Chef
  And I apply the Unicorn role
  And I apply the Bluepill role
  And the Unicorn service is running
  When I kill the Unicorn process
  Then within 2 seconds the Unicorn process should be running again

To test this manually, we would need to find a machine, install Ubuntu on it, bootstrap it with Chef, apply the role, run Chef, log onto the machine, check that Unicorn is running, kill Unicorn, then finally check that it has restarted. This would be tremendously time-consuming, and expensive—so much so that ...

The best content for your career. Discover unlimited learning on demand for around $1/day.