A couple of weeks ago I spent a bit of time writing a Ruby DSL to automate the setup of load balancers, firewall and NAT rules through the VCloud API.
The VCloud API deals primarily in XML so the DSL is just a thin layer which creates the appropriate mark up.
When we started out we configured everything manually through the web console and then exported the XML so the first thing that the DSL needed to do was create XML that matched what we already had.
My previous experience using testing frameworks to do this is that they’ll tell you whether the XML you’ve generated is equivalent to your expected XML but if they differ it isn’t easy to work out what was different.
I therefore decided to use a poor man’s approach where I first copied one rule into an XML file, attempted to replicate that in the DSL, and then used vimdiff to compare the files.
Although I had to manually verify whether or not the code was working I found this approach useful as any differences between the two pieces of XML were very easy to see.
90% of the rules were almost identical so I focused on the 10% that were different and once I’d got those working it was reasonably plain sailing.
My vimdiff command read like this:
ruby generate_networking_xml.rb > bar && vimdiff -c 'set diffopt+=iwhite' bar initialFirewall.xml
After I was reasonably confident that I understood the way that the XML should be generated I created an Rspec test which checked that we could correctly create all of the existing configurations using the DSL.
While discussing this approach with Jen she suggested that an alternative would be to start with a Rspec test with most of the rules hard coded in XML and then replace them one by one with the DSL.
I think that probably does make more sense but I still quite like my hacky approach as well!