Test Driven Devops

Illustration: 500 - Internal server errorI like to apply Test Driven Development to my sysadmin work. For example, every time I add a new redirect to a web server configuration I want to make sure I haven’t broken anything else. Further, I want my SSL configurations proactively checked daily for any possible error. I use Ruby RSpec and write tests like these:

describe 'My app' do
  context 'www.myapp.com' do
    it { should be_up }
    it { should have_a_valid_cert }
  end

  it 'serves the "about" page without redirecting' do
    expect('http://www.myapp.com/about').to be_status 200
  end

  it 'only serves via www' do
    expect('http://myapp.com').to redirect_permanently_to 'http://www.myapp.com/'
  end

  it 'forces visitors to use https' do
    expect('myapp.com').to enforce_https_everywhere
  end
end

When I want to make a configuration change, I first write a test for the desired outcome. Naturally, it fails while the old tests pass. I then work on the config change, re-running all the tests as I go, and am finished when they all pass. I also run these automatically from a cron job to get pro-active notification of new problems.

The phrases such as have_a_valid_cert are custom RSpec matchers; they’re added into the RSpec environment by an open source library I’m writing and just made available on Github.  I’ve begun to refactor all the custom matchers out of my internal weblaws.org code into a new rspec-webservice_matchers gem which makes this easy to install.

Update: I’m making an online service based on this code, and am looking for beta testers.

See also

4 comments

  1. John Cooper

    I have been doing a similar thing using cucumber and some simple net/http tests. It works okay but I like the format of what you have done there. Looks a lot simpler. I’ll have a look at it in more detail at the next major change we are looking at.
    Any reason you are switching to python? Will this mean you are not updating this library?
    Thanks for sharing your code.

    • Robb

      I’ll definitely be keeping this up to date; Ruby excels when it comes to test frameworks and Domain Specific Languages. I’d like to see what’s possible with Python because I find code written with it more maintainable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s