Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test edit_resource? #812

Closed
rmoriz opened this issue Feb 23, 2017 · 8 comments
Closed

How to test edit_resource? #812

rmoriz opened this issue Feb 23, 2017 · 8 comments

Comments

@rmoriz
Copy link

rmoriz commented Feb 23, 2017

Chef DSL's edit_resource should be test-able as well. see:

What do you think?

@lamont-granquist
Copy link
Contributor

you gotta leave a more useful bug report.

AFAIK it should be testable.

there's bugs around use_inline_resources like #793 which need to get fixed or else a lot of the use cases for edit_resources will be broken, but that has nothing to do with edit_resources directly.

@rmoriz
Copy link
Author

rmoriz commented Mar 17, 2017

Okay, i'll try to provide a specific example:

# cookbook1::default
file '/tmp/test' do
  content 'original content'
end


# cookbook2::default
edit_resource(:file, '/tmp/test') do
  content 'super new content'
end

the original file resource is easy testable, but how can i verify that cookbook2::default does the edit_resource right (e.g. updated content attribute)?

I would think of something like:

  cached(:chef_run) { ChefSpec::ServerRunner.new.converge('cookbook2::default') }
...

  expect(chef_run).to edit_resource(:template, '/etc/file').with(
    content: 'super new content'
  )

… however edit_resource is not a resource so maybe another way is needed:…

  cached(:chef_run) { ChefSpec::ServerRunner.new.converge('cookbook2::default') }
...
 
  resource = chef_run.file('/etc/file')

  expect(resource).to edit_resource('file[/etc/file]').with(
    content: 'super new content'
  )

My goal is to explicity test the "edit_resource" action because usually I'll use it by modifying/wrapping external cookbooks.

(updated)

@lamont-granquist
Copy link
Contributor

its exactly the same way. look for the file resource on the resource collection to have gotten constructed with the 'super new content' content.

@lamont-granquist
Copy link
Contributor

@tas50 i think this one is closable

@tas50 tas50 closed this as completed Apr 18, 2017
@rmoriz
Copy link
Author

rmoriz commented Apr 18, 2017 via email

@lamont-granquist
Copy link
Contributor

you don't test the implementation details.

testing this:

file "/tmp/foo"

edit_resource(:file, "/tmp/foo") do
  content "foo"
end

is the same as testing this:

file "/tmp/foo" do
  content "foo"
end

which in both cases is just this:

  it 'should install zsh package' do
    expect(chef_run).to create_file('/tmp/foo').with(content: "foo")
  end

it doesn't matter if its written using edit_resource or not, what you're setting up expectations on is the final construction of the resource collection. how you get there is irrelevant.

@olebel
Copy link

olebel commented Jun 21, 2017

Recommended approach is to do not test library cookbook resources, excluding any library "include_recipe" appearances.

Common pattern for using "edit_resource" is to edit resources that's defined in library, that we don't test.

Chefspec drop failures, when it meets edit_resource with template/file from library cookbook.

What's recommended way to use Chefspec in such situations?

@rmoriz
Copy link
Author

rmoriz commented Jun 21, 2017

@olebel why not?

@lock lock bot locked as resolved and limited conversation to collaborators Aug 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants