Search code examples
chef-infrachef-recipetest-kitchenserverspec

Serverspec package check always returns true


I'm trying to write serverspec tests that check if a recipe is setting up mariadb-server, by using chef, kitchen, and vagrant, with a debian jessie box.

The recipe is simple:

# cookbooks/mariadb/recipes/server.rb
package 'mariadb-server' do
  action :install
end

The spec for it I wrote is:

# cookbooks/mariadb/test/integration/default/serverspec/server_spec.rb
require 'spec_helper'

describe 'mariadb::server' do
  context package('mariadb-server') do
    it 'is installed' do
      expect be_installed
    end
  end
end

However, when running kitchen verify, this always returns true, regardless of the package state. If I ssh into the vagrant box and remove the package, then run kitchen verify, I'm also getting a posive result.

Even if I change the package to some random string, e.g. context package('this-is-not-a-package') do the test result is true.

What am I doing wrong here?


Solution

  • In a more general sense, this was not following RSpec 3 matcher syntax.

    You may consider doing it like:

    describe 'mariadb::server' do
      describe package('mariadb-server') do
        it { expect(subject).to be_installed }
      end
    end
    

    This looks cleaner and will output cleaner because serverspec uses the document formatter for the RSpec output.

    To further give you an idea of what is going on under the hood so you understand what is going on here and not assume this is all magic, here is a general template of how to do these kinds of checks:

    describe method(argument) do
      it { expect(subject).to be_boolean-matcher.with_chain(argument_two) }
    end
    

    and for your situation (you did not use the version chain but I added it for extra info)

    • method: package
    • argument: mariadb-server
    • subject: will resolve to package(mariadb-server)
    • boolean-matcher: installed
    • chain: version
    • argument_two: 1.2.3