Unit testing HWRPs


#1

I have a cookbook that uses LWRPs to configure some weblogic domains. The
LWRP is getting really complex and deserves (I think) to be made into an
HWRP with more complete unit tests, etc.

Does anyone know of best practices for unit testing HRWPs like this?

I found that the dsh cookbook (https://github.com/rcbops-cookbooks/dsh)
does have a spec tested HWRP, which ends up stubbing method calls on the
chef providers used to prevent them from converging on the test machine. Is
this the best approach to take?

Anyone have suggestions or places to start digging?

Thanks,

Ben


#2

Unfortunately the best I’ve found is to use test-kitchen and minitest.

–Noah

On Jan 23, 2014, at 3:59 PM, Benjamin Bytheway bbytheway@gmail.com wrote:

I have a cookbook that uses LWRPs to configure some weblogic domains. The LWRP is getting really complex and deserves (I think) to be made into an HWRP with more complete unit tests, etc.

Does anyone know of best practices for unit testing HRWPs like this?

I found that the dsh cookbook (https://github.com/rcbops-cookbooks/dsh) does have a spec tested HWRP, which ends up stubbing method calls on the chef providers used to prevent them from converging on the test machine. Is this the best approach to take?

Anyone have suggestions or places to start digging?

Thanks,

Ben


#3

Normally I try to keep all of the “logic” out of the provider and stick it
into a library file instead, since those are easier to test.

On Thu, Jan 23, 2014 at 7:02 PM, Noah Kantrowitz noah@coderanger.netwrote:

Unfortunately the best I’ve found is to use test-kitchen and minitest.

–Noah

On Jan 23, 2014, at 3:59 PM, Benjamin Bytheway bbytheway@gmail.com
wrote:

I have a cookbook that uses LWRPs to configure some weblogic domains.
The LWRP is getting really complex and deserves (I think) to be made into
an HWRP with more complete unit tests, etc.

Does anyone know of best practices for unit testing HRWPs like this?

I found that the dsh cookbook (https://github.com/rcbops-cookbooks/dsh)
does have a spec tested HWRP, which ends up stubbing method calls on the
chef providers used to prevent them from converging on the test machine. Is
this the best approach to take?

Anyone have suggestions or places to start digging?

Thanks,

Ben


#4

once its hwrp, its more like standard ruby library. for examples look ate
chef core code base, its have a very nice suite of tests (including the out
of the box chef resources/providers, all of them are hwrp)

On Thu, Jan 23, 2014 at 3:59 PM, Benjamin Bytheway bbytheway@gmail.comwrote:

I have a cookbook that uses LWRPs to configure some weblogic domains. The
LWRP is getting really complex and deserves (I think) to be made into an
HWRP with more complete unit tests, etc.

Does anyone know of best practices for unit testing HRWPs like this?

I found that the dsh cookbook (https://github.com/rcbops-cookbooks/dsh)
does have a spec tested HWRP, which ends up stubbing method calls on the
chef providers used to prevent them from converging on the test machine. Is
this the best approach to take?

Anyone have suggestions or places to start digging?

Thanks,

Ben


#5

When I rewrote the runit_service definition to be a service resource,
I made it “heavyweight” for two reasons:

  1. I wanted to subclass Chef’s service resource, to reuse some of its
    functionality and have runit_service behave like other services.

  2. It was easier to do unit testing of it this way. You can see the
    unit tests here:
    https://github.com/hw-cookbooks/runit/blob/master/test/spec/libraries/provider_runit_service_spec.rb

For testing LWRPs, you can use the step_into feature of ChefSpec.
Documentation is on the ChefSpec site.
http://code.sethvargo.com/chefspec/

Hope this helps.

-Joshua

On Thu, Jan 23, 2014 at 4:59 PM, Benjamin Bytheway bbytheway@gmail.com wrote:

I have a cookbook that uses LWRPs to configure some weblogic domains. The
LWRP is getting really complex and deserves (I think) to be made into an
HWRP with more complete unit tests, etc.

Does anyone know of best practices for unit testing HRWPs like this?

I found that the dsh cookbook (https://github.com/rcbops-cookbooks/dsh) does
have a spec tested HWRP, which ends up stubbing method calls on the chef
providers used to prevent them from converging on the test machine. Is this
the best approach to take?

Anyone have suggestions or places to start digging?

Thanks,

Ben


Joshua Timberman, Chef.


#6

A couple more questions came up today as I continued writing and testing.

Most of the examples I’ve been able to find of HWRP don’t make very
extensive use of already existing chef core resources. I guess LWRPs are
more generally used for this?

Basically the provider I’m writing is assembling mostly basic chef
resources, but in a complex enough way that I have a number of helper
methods, and wanted to break things up into more modular method calls to do
the work and make testing easier.

I found early on that providers include the recipe dsl, so I can use
primitives like “template,” “cookbook_file,” etc. as opposed to
Chef::Resource::Template. This also had the positive side effect of (with
just a little work), letting me use chefspec to both ensure that things
don’t fully converge on my test box and get access to its nice rspec
matchers.

Then I got to thinking that the recipe dsl is adding things to the
resource_collection, even if i manually call run_action on the resource
during my provider action. So it is added to the resource collection after
my HWRP, and touched again even though it doesn’t re-run the action (still
can’t figure out where that is getting skipped, but glad it is).

Is this use of the recipe dsl in providers a dark path I shouldn’t be
treading down? Like I said, I really haven’t found many full providers out
there that make heavy use of core chef resources and even fewer that have
spec suites.

-Ben

On Sun, Jan 26, 2014 at 10:04 AM, Joshua Timberman joshua@getchef.comwrote:

When I rewrote the runit_service definition to be a service resource,
I made it “heavyweight” for two reasons:

  1. I wanted to subclass Chef’s service resource, to reuse some of its
    functionality and have runit_service behave like other services.

  2. It was easier to do unit testing of it this way. You can see the
    unit tests here:

https://github.com/hw-cookbooks/runit/blob/master/test/spec/libraries/provider_runit_service_spec.rb

For testing LWRPs, you can use the step_into feature of ChefSpec.
Documentation is on the ChefSpec site.
http://code.sethvargo.com/chefspec/

Hope this helps.

-Joshua

On Thu, Jan 23, 2014 at 4:59 PM, Benjamin Bytheway bbytheway@gmail.com
wrote:

I have a cookbook that uses LWRPs to configure some weblogic domains.
The
LWRP is getting really complex and deserves (I think) to be made into an
HWRP with more complete unit tests, etc.

Does anyone know of best practices for unit testing HRWPs like this?

I found that the dsh cookbook (https://github.com/rcbops-cookbooks/dsh)
does
have a spec tested HWRP, which ends up stubbing method calls on the chef
providers used to prevent them from converging on the test machine. Is
this
the best approach to take?

Anyone have suggestions or places to start digging?

Thanks,

Ben


Joshua Timberman, Chef.