This video comes from The Joy of Automating series, hosted by Franklin Webber.
In this episode we focus on working with legacy code. Legacy code is everywhere. Learning the steps to tackle this kind of work essential. This is a cookbook that deploys Redis on Ubuntu. We write an integration test that exposes some issues in the recipe. Then proceeded to write unit tests to capture the current state. The resources used a lot of notifications to generate a chain of events that are important to test. Near the end we refactor out the version into a node attribute and then start to refactor the resource names to make the specifications less brittle. We finish by starting to apply the same refactoring techniques to our test suite.
In this episode we focus on taking an existing cookbook.
When you are done with the initial implementation we encourage you to challenge yourself by:
Clarity in Resource Names
The execute resources within the recipe have their commands specified in their names. This creates for very brittle tests if you were to change version numbers or the flags of the command. It would be better if the command were stored in their appropriate attribute.
A resource named
make && make install could be named
First update the specifications to more clearly state the intention of the command. Run the tests to see the failure. Update the recipe and see the tests pass.
Continue to refactor the tests
The tests within the cookbook are code you maintain as well. As we refactored the version number into a node attribute it would also be good to extract that version number into a let helper method. This will reduce the number of times we repeat the version number in the specification.
Another important step after defining the version number in a
let helper method would be look at specifying the version value in as a node attribute for the chef-client run. ChefSpec provides the ability to set attributes.
Continue to extract more values into attributes
Providing more values as attributes allows for a more flexible cookbook.
- The location to retrieve where to retrieve the source
- The folder to unzip the source
- The name of the service