Initialization of a test file based on a template using ChefSpec

I have the following template file creation in my cookbook:

template "my_file" do path "my_path" source "my_file.erb" owner "root" group "root" mode "0644" variables(@template_variables) notifies :restart, resources(service: "my_service") end 

and the following statements in ChefSpec tests:

  chef_run.should create_file "my_file" chef_run.file("my_file").should be_owned_by('root', 'root') 

This results in the following failure:

  No file resource named 'my_file' with action :create found. 

This is because I am not using the file resource, but the template resource. Question: How can I verify the creation of a file from a template resource using ChefSpec?

+4
source share
2 answers

There are two ways to solve your problem.

First, you can use the create_template match. This will only match the resources of the "template" in the execution context:

 expect(chef_run).to create_template('my_file') 

This connector can also be connected, so you can approve the attributes:

 expect(chef_run).to create_template('my_file') .with_path('my_path') .with_owner('root') 

However, this template will not display the template. Thus, you cannot verify that you have configured the file specification correctly.

There may also be a top-level match for any type of β€œfile” ( file , cookbook_file and template ) that actually displays the contents in memory:

 expect(chef_run).to render_file('my_file').with_content(/^match me$/) 

Further information on render_file can be found in README .

+7
source

According to the docs ( https://github.com/acrmp/chefspec ) you can use:

 expect(chef_run).to create_file 'my_file' 

I think something has changed recently (perhaps the chefspec version on rubygems), however, since the tests I passed earlier (using the same syntax that you use) now fail unexpectedly.

+5
source

Source: https://habr.com/ru/post/1498917/


All Articles