Cookbook testing with minitest-handler


David Calavera recently pushed the Minitest Chef Handler project to
Github [1]. I presume that it is chef-solo focused because he works
for Engineyard which is historically a chef-solo shop. I’ve written a
cookbook called minitest-handler [2][3] to utilize this project with a
server. The cookbook installs the minitest gem, downloads the minitest
chef handler, downloads any minitest handler tests it can find from
the cookbooks in the nodes run list and finally activates the handler
using the chef_handler LWRP.

Here is an example test:


class TestApache2 < MiniTest::Chef::TestCase
def test_that_the_package_installed
case node[:platform]
when “ubuntu”,"debian"
assert system(‘apt-cache policy apache2 | grep Installed | grep -v none’)

def test_that_the_service_is_running
assert system(’/etc/init.d/apache2 status’)

def test_that_the_service_is_enabled
assert File.exists?(Dir.glob("/etc/rc5.d/S*apache2").first)

Add recipe[chef_handler] and recipe[minitest-handler] to the top of
your run list and create a sever:

$ knife ec2 server create -I ami-3962a950 -d ubuntu10.04-gems -x
ubuntu -G btm -i btm -r

The tests are run at the end of the Chef run:

[Thu, 01 Mar 2012 08:39:46 +0000] INFO: Running report handlers
Run options: -v --seed 34743

Running tests:

TestApache2#test_that_the_package_installed = Installed: 2.2.20-1ubuntu1.2
0.04 s = .
TestApache2#test_that_the_service_is_enabled = 0.00 s = .
TestApache2#test_that_the_service_is_running = Apache2 is running (pid 9227).
0.10 s = .

Finished tests in 0.144945s, 20.6975 tests/s, 20.6975 assertions/s.

3 tests, 3 assertions, 0 failures, 0 errors, 0 skips
[Thu, 01 Mar 2012 08:39:46 +0000] INFO: Report handlers complete

The big win here is testing against actual systems rather than using
mocks. Because we have access to the node object, we can write tests
against multiple platforms. If one is vigilant about writing tests,
cookbooks can be tested against all supported platforms and full
functionality verified on new platform releases. Add continuous
integration infrastructure with VMs or cloud instances like EC2 and
cookbooks can be automatically tested against all platforms.