Module: ChefSpec::API::TemplateMatchers

Defined in:
lib/chefspec/api/template.rb

Overview

Since:

  • 0.0.1

Instance Method Summary collapse

Instance Method Details

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

Assert that a template resource exists in the Chef run with the action :create. Given a Chef Recipe that creates “/tmp/config” as a template:

template '/tmp/config' do
  action :create
end

To test the content rendered by a template, see RenderFileMatchers.

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

Examples:

Assert that a template was created

expect(chef_run).to create_template('/tmp/config')

Assert that a template was created with predicate matchers

expect(chef_run).to create_template('/tmp/config').with_user('svargo')

Assert that a template was created with attributes

expect(chef_run).to create_template('/tmp/config').with(user: 'svargo')

Assert that a template was created using a regex

expect(chef_run).to create_template('/tmp/config').with(user: /sva(.+)/)

Assert that a template was not created

expect(chef_run).to_not create_template('/tmp/config')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:

Since:

  • 0.0.1


42
43
44
# File 'lib/chefspec/api/template.rb', line 42

def create_template(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:template, :create, resource_name)
end

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

Assert that a template resource exists in the Chef run with the action :create_if_missing. Given a Chef Recipe that creates “/tmp/config” if missing as a template:

template '/tmp/config' do
  action :create_if_missing
end

To test the content rendered by a template, see RenderFileMatchers.

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

Examples:

Assert that a template was created if missing

expect(chef_run).to create_template_if_missing('/tmp/config')

Assert that a template was created if missing with predicate matchers

expect(chef_run).to create_template_if_missing('/tmp/config').with_user('svargo')

Assert that a template was created if missing with attributes

expect(chef_run).to create_template_if_missing('/tmp/config').with(user: 'svargo')

Assert that a template was created if missing using a regex

expect(chef_run).to create_template_if_missing('/tmp/config').with(user: /sva(.+)/)

Assert that a template was not created if missing

expect(chef_run).to_not create_template('/tmp/config')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:

Since:

  • 0.0.1


82
83
84
# File 'lib/chefspec/api/template.rb', line 82

def create_template_if_missing(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:template, :create_if_missing, resource_name)
end

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

Assert that a template resource exists in the Chef run with the action :delete. Given a Chef Recipe that deletes “/tmp/config” as a template:

template '/tmp/config' do
  action :delete
end

To test the content rendered by a template, see RenderFileMatchers.

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

Examples:

Assert that a template was deleted

expect(chef_run).to delete_template('/tmp/config')

Assert that a template was deleted with predicate matchers

expect(chef_run).to delete_template('/tmp/config').with_user('svargo')

Assert that a template was deleted with attributes

expect(chef_run).to delete_template('/tmp/config').with(user: 'svargo')

Assert that a template was deleted using a regex

expect(chef_run).to delete_template('/tmp/config').with(user: /sva(.+)/)

Assert that a template was not deleted

expect(chef_run).to_not delete_template('/tmp/config')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:

Since:

  • 0.0.1


122
123
124
# File 'lib/chefspec/api/template.rb', line 122

def delete_template(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:template, :delete, resource_name)
end

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

Assert that a template resource exists in the Chef run with the action :touch. Given a Chef Recipe that touches “/tmp/config” as a template:

template '/tmp/config' do
  action :touch
end

To test the content rendered by a template, see RenderFileMatchers.

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

Examples:

Assert that a template was touched

expect(chef_run).to touch_template('/tmp/config')

Assert that a template was touched with predicate matchers

expect(chef_run).to touch_template('/tmp/config').with_user('svargo')

Assert that a template was touched with attributes

expect(chef_run).to touch_template('/tmp/config').with(user: 'svargo')

Assert that a template was touched using a regex

expect(chef_run).to touch_template('/tmp/config').with(user: /sva(.+)/)

Assert that a template was not touched

expect(chef_run).to_not touch_template('/tmp/config')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:

Since:

  • 0.0.1


162
163
164
# File 'lib/chefspec/api/template.rb', line 162

def touch_template(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:template, :touch, resource_name)
end