Unit Testing Chef Cookbooks

Posted on January 13, 2013 in Chef, Tutorials | Comments

Okay, now that I'm done ranting about how to Unit test, let's move onto Chef.

I spoke at Chef Summit a few months ago and received a lot of questions about ChefSpec. It's very difficult to demonstrate the value in a Unit test when everyone is thinking at a higher level (acceptance testing).

Let's say I have a simple cookbook that just installs apache:

package value_for_platform(
  %w(centos redhat suse fedora) => {
    'default' => 'httpd'
  },
  %w(ubuntu debian) => {
    'default' => 'apache2'
  }
)
Read More

Unit Testing Correctly

Posted on January 12, 2013 in Rant, Ruby, Tutorial | Comments

Let's talk about testing. Testing is fun, it's awesome, and if you want to be agile, it's a necessity. But chances are, you're doing it wrong.

Before we dive into Chef, let's look at a small Ruby example. Consider a class writes a downloads an HTML page from a website and writes the contents to a file:

require 'net/http'

class Scraper
  attr_reader :webpage

  def initialize(webpage = 'http://sethvargo.com')
    @webpage = URI.parse(webpage)
    write Net::HTTP.get(@webpage)
  end

  def write(contents)
    File.open("#{@webpage.host}.html", 'w') do |file|
      file.write(contents)
    end
  end
end

Scraper.new

This class should download sethvargo.com to a text file in the current working directory (go ahead and test it out if you don't believe me).

Read More

Two Factor SSH Authentication

Posted on January 07, 2013 in SSH, Security, Tutorial | Comments

With many popular websites providing two factor authentication, why shouldn't you add two-factor authentication to SSH? Public-key, Private-key encryption is generally considered to be very secure, but why not take an extra step?

This tutorial will use the open source Google Authenticator project and PAM for setting up two-factor authentication.

Read More

Install Ruby 2.0.rc1

Posted on January 05, 2013 in Preview, Ruby, Tutorial | Comments

Ruby 2.0 is no longer a work in progress! Ruby 2.0 Release Candidate 1 was released today! Here's how to get it:

Install Homebrew:

ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"

Make sure brew is up to date:

brew update

Install rbenv, ruby-build, and openssl:

brew install rbenv ruby-build openssl

Or, if you already have rbenv:

brew upgrade rbenv

Use rbenv to install Ruby 2.0 RC1

rbenv install 2.0.0-rc1
Read More

Moving to Individual Cookbooks

Posted on January 02, 2013 in Berkshelf, Chef, Git, Tutorial | Comments

If you've been working with Chef in your organization for awhile, you've probably accumulated a bunch of cookbooks in a giant repository. With great tools like Berkshelf out there, it's become customary to rely on external community cookbooks, git repositories, and your own Chef Server for cookbooks. Essentially cookbooks have been extracted as a first-class object.

With Berkshelf, you can manage cookbooks as "dependencies", much like bundler does with a Gemfile. But that raises a big question - how do I move to Berkshelf? Joshua Timberman wrote up a nice blog post on moving to Berkshelf, and he listed techniques for moving to a community-managed cookbook.

In this blog post, I'll show how to extract your non-community cookbooks (like your proprietary organization cookbooks) into their own (private) git repositories.

Read More