Build Status Code Climate

Stub your Chef searches with pre-canned responses. Good for use when unit testing or dummying out a Chef run that uses search calls.


Add this line to your application's Gemfile:

gem 'searchef'

And then execute:

$ bundle

Or install it yourself as:

$ gem install searchef



Require the library and include the Searchef::API module for convenience:

require 'searchef'

include Searchef::API

Stubbing Node Searches

# stub search for all nodes with the `web_node` role in their run list
stub_search(:node, "roles:web_node").to_return([

require 'chef/search/query'

# if running in pry, let's set a node name for signing requests
Chef::Config[:node_name] = "durr"

# run the search
query =, "roles:web_node")

# => [[node[], node[]], 0, 2]

You can also stub more complicated nodes with specific data:

# setting a node platform and platform verson
node_stub("", :platform => "centos", :version => "6.3")

# overriding default fauxhai ohai data
node_stub("", :ohai => { :ipaddress => "" })

# set node attribute data, such as the run_list
node_stub("", :attrs => {
  :run_list => [ 'recipe[common::base]', 'role[load_balancer]' ]

# using a block which provides access to the attributes file dsl methods
node_stub("", :ohai => { :ipaddress => "" }) do
  default['mysql']['tunable']['tmp_table_size'] = "64M"

Stubbing Data Bag Searches

Data bags are a hash of data, so you can return data bag items as an array of hashes:

stub_search(:users, 'groups:admin').to_return([
    "id" => "adam",
    "comment" => "Adam Administrator",
    "groups" => [
    "ssh_keys" => [],
    "shell" => "/bin/bash"

If your data_bag_path is setup, you could also fectch the data from a real data bag, using the data_bag_item method:

stub_search(:users, 'groups:admin').to_return([
  data_bag_item("users", "adam")


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request