ruby - Test file initialization based off template using ChefSpec -


i've got following template file creation in cookbook:

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

and following assertions in chefspec tests:

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

which results in following failure:

  no file resource named 'my_file' action :create found. 

this due fact not using afile resource template resource. question: how can test file creation off template resource using chefspec?

there 2 ways solve problem.

first, can use create_template matcher. match "template" resources in run context:

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

this matcher chainable, can assert attributes:

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

however, matcher won't actually render template. can't check if you've setup file-specificity correctly.

there's top-level matcher kind of "file" (file, cookbook_file, , template) renders contents in memory:

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

you can find more information render_file in readme.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -