[chef] Re: Testing HWRPs


Chronological Thread 
  • From: Benjamin Bytheway < >
  • To:
  • Subject: [chef] Re: Testing HWRPs
  • Date: Mon, 10 Mar 2014 16:42:35 -0600

I've been down this rabbit hole quite a bit trying to figure this out as well over the last couple weeks.  I haven't found one happy path, but here are some suggestions:

First place to look is chef itself.  Base chef resources/providers are written as HWRPs and tested as part of chef.

I tried to leverage ChefSpec matchers by writing an HWRP using the recipe DSL (https://gist.github.com/bytheway/8672304):

template "foo" do ...

instead of

Chef:Resource::Template.new("foo")

Not sure how widely known it is, but the Chef::Provider class includes the same DSL as recipe files, so you can write things this way.  The issue I ran into is that in writing with the DSL, you basically have reverted back to the behavior of LWRPs before the use_inline_resources declaration was introduced.  Any resources created by the DSL are added to the run_list after your custom HWRP resource, which might not be the behavior you want. Your HWRP can call run_action directly on the resource as you create them, but they are still added to the run list, and looked at to see if they need to be run again, after your HWRP provider run is complete.

I've really liked the idea of the Medium-Weight RP that the splunk cookbook used (https://github.com/opscode-cookbooks/chef-splunk/blob/master/libraries/splunk_app_provider.rb) as a way to make a more testable LWRP. Currently it is hard to unit test LWRPs because Chef only initializes them as needed at runtime, and even reloads them if they are already loaded, which makes stubbing out methods more difficult. (Though, I have found an good way to stub LWRP methods in ChefSpec: https://gist.github.com/bytheway/9475815)

There aren't many examples, but a big drawback in most of the cookbooks I have found with tested HWRPs is that they end up stubbing and mocking a whole lot of stuff, to the point that you start wondering how brittle they are.

If your custom resource/provider is mostly just wrapping a bunch of already existing chef resources, then I'd suggest going with an LWRP or MWRP. 
If they are more low level, with very minimal resource re-use, like the resources/providers defined by Chef itself, I'd suggest you get out your mocking hat and use Chef source as the guide for how to test.

-Ben



On Mon, Mar 10, 2014 at 3:40 PM, Stewart, Curtis < " target="_blank"> > wrote:
Curious if there’s a best practice for testing HWRPs?

The jenkins cookbook is a great example of using HWRPs.  I’m wondering how to properly write tests for those.  A couple of options came to mind:

1) Write custom chefspec matchers and test with ChefSpec.
2) Test resource/provider methods with RSpec.

Thanks,
Curtis



  • [chef] Testing HWRPs, Stewart, Curtis, 03/10/2014
    • [chef] Re: Testing HWRPs, Benjamin Bytheway, 03/10/2014

Archive powered by MHonArc 2.6.16.

§