2

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.

Friedrich 'Fred' Clausen
  • 3,321
  • 8
  • 39
  • 70

2 Answers2

2

Test-Wrapper Cookbooks

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.

LWRP Resources

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.

ChefSpec

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.

Test Kitchen

You can see some kitchen tests in that same cookbook. The general notion is the same - create a test cookbook inside your library cookbook.

Tejay Cardon
  • 4,193
  • 2
  • 16
  • 31
  • As mentioned by Psyreactor and yourself it seems a matchers.rb is the way to go - I am testing this and will accept an answer once I fully understand how it works. – Friedrich 'Fred' Clausen Oct 02 '14 at 06:11
  • @FredClausen Matchers are not a good way to test your library cookbook. They are utilized by other cookbooks that needing tested, but contain your LWRP. With ChefSpec, those matchers will only help you determine that your test cookbook created a resource from your LWRP, but they do nothing to ensure that your LWRP is actually doing what it is supposed to do. – Tejay Cardon Oct 02 '14 at 16:13
  • Thanks for the info @TejayCardon. I am still unsure as to how to go about testing the library cookbook. Do I need to create, somehow, a transient wrapper cookbook that will instantiate the resource? – Friedrich 'Fred' Clausen Oct 03 '14 at 03:37
  • @FredClausen Exactly. Typically you would put that in `myCookbook/test/cookbooks/myCookbook-test` Your ChefSpec and/or kitchen tests would then use that testing cookbook. Generally one recipe in the test cookbook for each spec in your test suite. Don't forget to add the `test` directory to your .chefignore file so that you don't bloat your cookbooks on the server. – Tejay Cardon Oct 03 '14 at 14:05
  • Perfect! That answers my question. Do you mind adding that to your answer and then I'll accept it? – Friedrich 'Fred' Clausen Oct 06 '14 at 11:20
0

Chefspec allows testing LWRP , but you must define the resources in the file /libraries/matchers.rb . In following link that describes how to create the matchers.rb , and test it on chefspec .

http://givens.io/2014/01/chefspec-custom-matchers-for-lwrps/

Sorry for my english!! Best Regards

Psyreactor
  • 343
  • 2
  • 7