Module: ChefSpec::API::Stubs

Extended by:
RSpec::SharedContext
Defined in:
lib/chefspec/api/stubs.rb

Instance Method Summary collapse

Instance Method Details

#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)

Parameters:

  • command (String, Regexp)

    the command to stub

Returns:

  • (ChefSpec::CommandStub)


26
27
28
# File 'lib/chefspec/api/stubs.rb', line 26

def stub_command(command, &block)
  ChefSpec::Stubs::CommandRegistry.register(ChefSpec::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)

Parameters:

  • bag (String, Symbol)

    the name of the data bag to stub

Returns:

  • (ChefSpec::DataBagStub)


54
55
56
# File 'lib/chefspec/api/stubs.rb', line 54

def stub_data_bag(bag, &block)
  ChefSpec::Stubs::DataBagRegistry.register(ChefSpec::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)

Parameters:

  • bag (String, Symbol)

    the name of the data bag to find the item in

  • id (String)

    the id of the data bag item to stub

Returns:

  • (ChefSpec::DataBagItemStub)


81
82
83
# File 'lib/chefspec/api/stubs.rb', line 81

def stub_data_bag_item(bag, id, &block)
  ChefSpec::Stubs::DataBagItemRegistry.register(ChefSpec::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: '18.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

Parameters:

  • name (String)

    the name of the node

  • options (Hash)

    the list of options for the node

Returns:

  • (Chef::Node)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/chefspec/api/stubs.rb', line 122

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)

Parameters:

  • type (String, Symbol)

    the type to search to stub

  • query (String, Symbol, nil) (defaults to: "*:*")

    the query to stub

Returns:

  • (ChefSpec::SearchStub)


168
169
170
# File 'lib/chefspec/api/stubs.rb', line 168

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