Module: ChefSpec::API

Defined in:
lib/chefspec/api.rb,
lib/chefspec/api/link.rb,
lib/chefspec/api/user.rb,
lib/chefspec/api/reboot.rb,
lib/chefspec/api/do_nothing.rb,
lib/chefspec/api/render_file.rb,
lib/chefspec/api/state_attrs.rb,
lib/chefspec/api/notifications.rb,
lib/chefspec/api/subscriptions.rb,
lib/chefspec/api/include_recipe.rb

Instance Method Summary collapse

Instance Method Details

#create_user(resource_name) ⇒ ChefSpec::Matchers::ResourceMatcher

Assert that a user resource exists in the Chef run with the action :create. Given a Chef Recipe that creates “apache2” as a user:

user 'apache2' do
  action :create
end

The Examples section demonstrates the different ways to test a user resource with ChefSpec.

Examples:

Assert that a user was created

expect(chef_run).to create_user('apache2')

Assert that a user was created with predicate matchers

expect(chef_run).to create_user('apache2').with_uid(1234)

Assert that a user was created with attributes

expect(chef_run).to create_user('apache2').with(uid: 1234)

Assert that a user was created using a regex

expect(chef_run).to create_user('apache2').with(uid: /\d+/)

Assert that a user was not created

expect(chef_run).to_not create_user('apache2')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:


37
38
39
# File 'lib/chefspec/api/user.rb', line 37

def create_user(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:user, :create, resource_name)
end

#do_nothingChefSpec::Matchers::DoNothingMatcher

Assert that a resource in the Chef run does not perform any actions. Given a resource with action :nothing:

package 'apache2' do
  action :nothing
end

The Examples section demonstrates the different ways to test that no actions were performed on a resource in a Chef run.

Examples:

Assert the package does not perform any actions

Returns:


18
19
20
# File 'lib/chefspec/api/do_nothing.rb', line 18

def do_nothing
  ChefSpec::Matchers::DoNothingMatcher.new
end

#have_state_attrs(*state_attrs) ⇒ ChefSpec::Matchers::StateAttrsMatcher

Assert that a Chef resource has certain state attributes (since Chef 11.8.0):

state_attrs :time, :temperature

The Examples section demonstrates the different ways to test a resource's state_attrs with ChefSpec.

Examples:

Assert the lwrp resource has two state attributes

expect(lwrp).to have_state_attrs(:time, :temperature)

Parameters:

  • state_attrs (Array)

    the list of state attributes to assert

Returns:

See Also:


22
23
24
# File 'lib/chefspec/api/state_attrs.rb', line 22

def have_state_attrs(*state_attrs)
  ChefSpec::Matchers::StateAttrsMatcher.new(state_attrs)
end

#include_recipe(recipe_name) ⇒ ChefSpec::Matchers::IncludeRecipeMatcher

Assert that a Chef run includes a certain recipe. Given a Chef Recipe that calls include_recipe:

include_recipe 'apache2::default'

The Examples section demonstrates the different ways to test an include_recipe directive with ChefSpec.

Examples:

Assert the apache2::default recipe is included in the Chef run

expect(chef_run).to include_recipe('apache2::default')

Parameters:

  • recipe_name (String)

    the name of the recipe to be included

Returns:


20
21
22
# File 'lib/chefspec/api/include_recipe.rb', line 20

def include_recipe(recipe_name)
  ChefSpec::Matchers::IncludeRecipeMatcher.new(recipe_name)
end

Assert that a symlink links to a specific path. This is really syntactic sugar for the following:

expect(chef_run).to create_link('/tmp/thing').with(to: '/tmp/other_thing')

Examples:

Using link_to with a String path

link = chef_run.link('/tmp/thing')
expect(link).to link_to('/tmp/other_thing')

Using link_to with a regular expression

expect(link).to link_to(/\/tmp/(.+)/)

Parameters:

  • path (String, Regex)

    the path to link to

Returns:


20
21
22
# File 'lib/chefspec/api/link.rb', line 20

def link_to(path)
  ChefSpec::Matchers::LinkToMatcher.new(path)
end

#lock_user(resource_name) ⇒ ChefSpec::Matchers::ResourceMatcher

Assert that a user resource exists in the Chef run with the action :lock. Given a Chef Recipe that locks “apache2” as a user:

user 'apache2' do
  action :lock
end

The Examples section demonstrates the different ways to test a user resource with ChefSpec.

Examples:

Assert that a user was locked

expect(chef_run).to lock_user('apache2')

Assert that a user was locked with predicate matchers

expect(chef_run).to lock_user('apache2').with_uid(1234)

Assert that a user was locked with attributes

expect(chef_run).to lock_user('apache2').with(uid: 1234)

Assert that a user was locked using a regex

expect(chef_run).to lock_user('apache2').with(uid: /\d+/)

Assert that a user was not locked

expect(chef_run).to_not lock_user('apache2')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:


185
186
187
# File 'lib/chefspec/api/user.rb', line 185

def lock_user(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:user, :lock, resource_name)
end

#manage_user(resource_name) ⇒ ChefSpec::Matchers::ResourceMatcher

Assert that a user resource exists in the Chef run with the action :manage. Given a Chef Recipe that manages “apache2” as a user:

user 'apache2' do
  action :manage
end

The Examples section demonstrates the different ways to test a user resource with ChefSpec.

Examples:

Assert that a user was managed

expect(chef_run).to manage_user('apache2')

Assert that a user was managed with predicate matchers

expect(chef_run).to manage_user('apache2').with_uid(1234)

Assert that a user was managed with attributes

expect(chef_run).to manage_user('apache2').with(uid: 1234)

Assert that a user was managed using a regex

expect(chef_run).to manage_user('apache2').with(uid: /\d+/)

Assert that a user was not managed

expect(chef_run).to_not manage_user('apache2')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:


148
149
150
# File 'lib/chefspec/api/user.rb', line 148

def manage_user(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:user, :manage, resource_name)
end

#modify_user(resource_name) ⇒ ChefSpec::Matchers::ResourceMatcher

Assert that a user resource exists in the Chef run with the action :modify. Given a Chef Recipe that modifies “apache2” as a user:

user 'apache2' do
  action :modify
end

The Examples section demonstrates the different ways to test a user resource with ChefSpec.

Examples:

Assert that a user was modified

expect(chef_run).to modify_user('apache2')

Assert that a user was modified with predicate matchers

expect(chef_run).to modify_user('apache2').with_uid(1234)

Assert that a user was modified with attributes

expect(chef_run).to modify_user('apache2').with(uid: 1234)

Assert that a user was modified using a regex

expect(chef_run).to modify_user('apache2').with(uid: /\d+/)

Assert that a user was not modified

expect(chef_run).to_not modify_user('apache2')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:


111
112
113
# File 'lib/chefspec/api/user.rb', line 111

def modify_user(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:user, :modify, resource_name)
end

#notify(signature) ⇒ ChefSpec::Matchers::NotificationsMatcher

Assert that a resource notifies another. Given a Chef Recipe that notifies a template resource to restart apache:

template '/etc/apache2/config' do
  notifies :restart, 'service[apache2]'
end

The Examples section demonstrates the different ways to test an notifications on a resource with ChefSpec.

Examples:

Assert the template notifies apache of something

template = chef_run.template('/etc/apache2.conf')
expect(template).to notify('service[apache2]')

Assert the template notifies apache to restart

expect(template).to notify('service[apache2]').to(:restart)

Assert the template notifies apache to restart immediately

expect(template).to notify('service[apache2]').to(:restart).immediately

Assert the template notifies apache to restart delayed

expect(template).to notify('service[apache2]').to(:restart).delayed

Parameters:

  • signature (String)

    the signature of the notification to match

Returns:


32
33
34
# File 'lib/chefspec/api/notifications.rb', line 32

def notify(signature)
  ChefSpec::Matchers::NotificationsMatcher.new(signature)
end

#now_reboot(resource_name) ⇒ Object


2
3
4
# File 'lib/chefspec/api/reboot.rb', line 2

def now_reboot(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:reboot, :reboot_now, resource_name)
end

#remove_user(resource_name) ⇒ ChefSpec::Matchers::ResourceMatcher

Assert that a user resource exists in the Chef run with the action :remove. Given a Chef Recipe that removes “apache2” as a user:

user 'apache2' do
  action :remove
end

The Examples section demonstrates the different ways to test a user resource with ChefSpec.

Examples:

Assert that a user was remove

expect(chef_run).to remove_user('apache2')

Assert that a user was remove with predicate matchers

expect(chef_run).to remove_user('apache2').with_uid(1234)

Assert that a user was remove with attributes

expect(chef_run).to remove_user('apache2').with(uid: 1234)

Assert that a user was remove using a regex

expect(chef_run).to remove_user('apache2').with(uid: /\d+/)

Assert that a user was not remove

expect(chef_run).to_not remove_user('apache2')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:


74
75
76
# File 'lib/chefspec/api/user.rb', line 74

def remove_user(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:user, :remove, resource_name)
end

#render_file(path) ⇒ ChefSpec::Matchers::RenderFileMatcher

Assert that a file is rendered by the Chef run. This matcher works for template, file, and cookbook_file resources. The content from the resource must be convertable to a string; verifying the content of a binary file is not permissible at this time.

Examples:

Assert a template is rendered

expect(chef_run).to render_file('/etc/foo')

Assert a template is rendered with certain content

expect(template).to render_file('/etc/foo').with_content('This is a file')

Assert a template is rendered with matching content

expect(template).to render_file('/etc/foo').with_content(/^This(.+)$/)

Assert a template is rendered with content matching any RSpec matcher

expect(template).to render_file('/etc/foo').with_content(starts_with('This'))

Assert a partial path to a template is rendered with matching content

expect(template).to render_file(/\/etc\/foo-(\d+)$/).with_content(/^This(.+)$/)

Parameters:

  • path (String)

    the path of the file to render

Returns:


29
30
31
# File 'lib/chefspec/api/render_file.rb', line 29

def render_file(path)
  ChefSpec::Matchers::RenderFileMatcher.new(path)
end

#request_reboot(resource_name) ⇒ Object


6
7
8
# File 'lib/chefspec/api/reboot.rb', line 6

def request_reboot(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:reboot, :request_reboot, resource_name)
end

#subscribe_to(signature) ⇒ ChefSpec::Matchers::NotificationsMatcher

Assert that a resource subscribes to another. Given a Chef Recipe that subscribes a template resource to restart apache:

service 'apache2' do
  subscribes :create, 'template[/etc/apache2/config]'
end

The Examples section demonstrates the different ways to test a subscription on a resource with ChefSpec.

Examples:

Assert a basic subscription

service = chef_run.service('apache2')
expect(service).to subscribe_to('template[/etc/apache2/config]')

Assert a subscription with specified action

expect(service).to subscribe_to('template[/etc/apache2/config]').on(:restart)

Assert a subscription with specified action and timing

expect(service).to subscribe_to('template[/etc/apache2/config]').on(:restart).immediately

Parameters:

  • signature (String)

    the signature of the notification to match

Returns:


29
30
31
# File 'lib/chefspec/api/subscriptions.rb', line 29

def subscribe_to(signature)
  ChefSpec::Matchers::SubscribesMatcher.new(signature)
end

#unlock_user(resource_name) ⇒ ChefSpec::Matchers::ResourceMatcher

Assert that a user resource exists in the Chef run with the action :unlock. Given a Chef Recipe that unlocks “apache2” as a user:

user 'apache2' do
  action :unlock
end

The Examples section demonstrates the different ways to test a user resource with ChefSpec.

Examples:

Assert that a user was unlocked

expect(chef_run).to unlock_user('apache2')

Assert that a user was unlocked with predicate matchers

expect(chef_run).to unlock_user('apache2').with_uid(1234)

Assert that a user was unlocked with attributes

expect(chef_run).to unlock_user('apache2').with(uid: 1234)

Assert that a user was unlocked using a regex

expect(chef_run).to unlock_user('apache2').with(uid: /\d+/)

Assert that a user was not unlocked

expect(chef_run).to_not unlock_user('apache2')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:


222
223
224
# File 'lib/chefspec/api/user.rb', line 222

def unlock_user(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:user, :unlock, resource_name)
end