Summary: How can I best test a library cookbook meant for inclusion from another cookbook?
Details: I am writing a cookbook that contains an LWRP and a minimal default recipe that sets some attributes based on where it is run. However the new resource defined by the LWRP is meant to be used from other cookbooks.
So, for example, the library cookbook defines and implements mylib_example in mylib/resources/example.rb and mylib/providers/example.rb. The "client" cookbook would then, for example, use it from client/recipes/default.rb as
include_recipe 'mylib'
mylib_example "widget1" do
magic_number '42'
end
How do I test that resource usage from the library cookbook itself? I'm not fussed about framework, currently expirementing with Test Kitchen and Chefspec.
In either case, you'll need a 'test' cookbook that exercises your LWRP. Generally that would be test-mylwrp_cookbook
or mylwrp_cookbook-test
in the mylwrp_cookbook/test/cookbooks
directory. I typically have one recipe in my test cookbook for each resource in my library cookbook. You then use the test cookbook recipes in ChefSpec or TestKitchen. Also, be sure to add the mylwrp_cookbook/test
directory to your .chefignore file so that you don't bloat the cookbooks that are uploaded to your chef-server by knife/berkshelf.
Whether you use chefspec or kitchen really depends on how your LWRP code works. If your LWRP is strictly declaring other resources within the provider - and hopefully using use_inline_resources
, then I would use ChefSpec. However, if you are making system calls in your LWRP, then you really need to use Test Kitchen. ChefSpec never executes resources, and thus is less useful when you are testing a library that does more than create other resources.
I wrote most of the spec tests on Chef Pushit if you want an example of how I approach it.
Take special note of the use of step_into, and don't forget to include a matchers.rb The matchers.rb file will make chefspec tests much easier for cookbooks which use your library cookbook.
You can see some kitchen tests in that same cookbook. The general notion is the same - create a test cookbook inside your library cookbook.