Module: ChefSpec::Macros

Extended by:
Macros
Included in:
Macros
Defined in:
lib/chefspec/macros.rb

Instance Method Summary collapse

Instance Method Details

#described_cookbookString

The name of the currently running cookbook spec. Given the top-level describe block is of the format:

describe 'my_cookbook::my_recipe' do
  # ...
end

The value of described_cookbook is “my_cookbook”.

Examples:

Using described_cookbook in a context block

context "#{described_recipe} installs foo" do
  # ...
end

23
24
25
# File 'lib/chefspec/macros.rb', line 23

def described_cookbook
  described_recipe.split('::').first
end

#described_recipeString

The name of the currently running recipe spec. Given the top-level describe block is of the format:

describe 'my_cookbook::my_recipe' do
  # ...
end

The value of described_recipe is “my_cookbook::my_recipe”.

Examples:

Using described_recipe in the ChefSpec::SoloRunner

let(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe) }

43
44
45
46
47
48
49
50
51
52
# File 'lib/chefspec/macros.rb', line 43

def described_recipe
  scope = self.is_a?(Class) ? self : self.class

  metahash = scope.
  while metahash.has_key?(:parent_example_group)
    metahash = metahash[:parent_example_group]
  end

  metahash[:description].to_s
end

#stub_command(command, &block) ⇒ ChefSpec::CommandStub

Stub a shell command to return a particular value without shelling out to the system.

Examples:

stubbing a command to return true

stub_command('test -f /tmp/bacon').and_return(true)

stubbing a block that is evaluated at runtime

stub_command('test -f /tmp/bacon') { MyClass.check? }

stubbing a command that matches a pattern

stub_command(/test \-f/).and_return(true)

stubbing a command that raises an exception

stub_command('test -f /tmp/bacon').and_raise(SomeException)

76
77
78
# File 'lib/chefspec/macros.rb', line 76

def stub_command(command, &block)
  Stubs::CommandRegistry.register(Stubs::CommandStub.new(command, &block))
end

#stub_data_bag(bag, &block) ⇒ ChefSpec::DataBagStub

Stub a Chef call to load a data_bag.

Examples:

stubbing a data_bag to return an empty Array

stub_data_bag('users').and_return([])

stubbing a data_bag with a block that is evaluated at runtime

stub_data_bag('users') { JSON.parse(File.read('fixtures/data_bag.json')) }

stubbing a data_bag to return an Array of Hashes

stub_data_bag('users').and_return([
  { id: 'bacon', comment: 'delicious' },
  { id: 'ham', comment: 'also good' }
])

stubbing a data_bag to raise an exception

stub_data_bag('users').and_raise(Chef::Exceptions::PrivateKeyMissing)

104
105
106
# File 'lib/chefspec/macros.rb', line 104

def stub_data_bag(bag, &block)
  Stubs::DataBagRegistry.register(Stubs::DataBagStub.new(bag, &block))
end

#stub_data_bag_item(bag, id, &block) ⇒ ChefSpec::DataBagItemStub

Stub a Chef data_bag call.

Examples:

stubbing a data_bag_item to return a Hash of data

stub_data_bag_item('users', 'svargo').and_return({
  id: 'svargo',
  name: 'Seth Vargo'
})

stubbing a data_bag_item with a block that is evaluated at runtime

stub_data_bag_item('users', 'svargo') { JSON.parse(File.read('fixtures/data_bag_item.json')) }

stubbing a data_bag_item to raise an exception

stub_data_bag('users', 'svargo').and_raise(Chef::Exceptions::PrivateKeyMissing)

131
132
133
# File 'lib/chefspec/macros.rb', line 131

def stub_data_bag_item(bag, id, &block)
  Stubs::DataBagItemRegistry.register(Stubs::DataBagItemStub.new(bag, id, &block))
end

#stub_node(*args, &block) ⇒ Chef::Node

Creates a fake Chef::Node for use in testing.

Examples:

mocking a simple node

stub_node('mynode.example')

mocking a specific platform and version

stub_node('mynode.example', platform: 'ubuntu', version: '12.04')

overriding a specific ohai attribute

stub_node('mynode.example', ohai: { ipaddress: '1.2.3.4' })

stubbing a node based on a JSON fixture

stub_node('mynode.example', path: File.join('fixtures', 'nodes', 'default.json'))

setting specific attributes

stub_node('mynode.example') do |node|
  node.default['attribute']['thing'] = 'value'
end

172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/chefspec/macros.rb', line 172

def stub_node(*args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  name    = args.first || 'node.example'

  fauxhai = Fauxhai.mock(options).data
  fauxhai = fauxhai.merge(options[:ohai] || {})
  fauxhai = Mash.new(fauxhai)

  node = Chef::Node.new
  node.name(name)
  node.automatic_attrs = fauxhai
  node.instance_eval(&block) if block_given?
  node
end

#stub_search(type, query = '*:*', &block) ⇒ ChefSpec::SearchStub

Stub a Chef search to return pre-defined data. When providing a value, the value is automatically mashified (to the best of ChefSpec's abilities) to ease in use.

Examples:

stubbing a search to return nothing

stub_search(:node)

stubbing a search with a query

stub_search(:node, 'name:*')

stubbing a search with a query as a regex

stub_search(:node, /name:(.+)/)

stubbing a search with a block that is evaluated at runtime

stub_search(:node) { JSON.parse(File.read('fixtures/nodes.json')) }

stubbing a search to return a fixed value

stub_search(:node).and_return([ { a: 'b' } ])

stubbing a search to raise an exception

stub_search(:node).and_raise(Chef::Exceptions::PrivateKeyMissing)

218
219
220
# File 'lib/chefspec/macros.rb', line 218

def stub_search(type, query = '*:*', &block)
  Stubs::SearchRegistry.register(Stubs::SearchStub.new(type, query, &block))
end